src/index.js
function getDataFromWeb(url, options, apiKey, cb) {
  let useCallback = 'function' === typeof cb;
  const reqOptions = { headers: {} };
  if (apiKey) {
    reqOptions.headers['X-Api-Key'] = apiKey;
  }
  if (options && options.noCache === true) {
    reqOptions.headers['X-No-Cache'] = 'true';
  }
  return fetch(url, reqOptions).then(res => Promise.all([res, res.json()])).then(([res, body]) => {
    if (body.status === 'error') throw new NewsAPIError(body);
    // 'showHeaders' option can be used for clients to debug response headers
    // response will be in form of { headers, body }
    if (options && options.showHeaders) {
      if (useCallback) return cb(null, { headers: res.headers, body });
      return { headers: res.headers, body };
    }
    if (useCallback) return cb(null, body);
    return body;
  }).catch(err => {
    if (useCallback) return cb(err);
    throw err;
  });
}
function getDataFromWeb(url, options, apiKey, cb) {
  let useCallback = 'function' === typeof cb;
  const reqOptions = { headers: {} };
  if (apiKey) {
    reqOptions.headers['X-Api-Key'] = apiKey;
  }
  if (options && options.noCache === true) {
    reqOptions.headers['X-No-Cache'] = 'true';
  }
  return fetch(url, reqOptions).then(res => Promise.all([res, res.json()])).then(([res, body]) => {
    if (body.status === 'error') throw new NewsAPIError(body);
    // 'showHeaders' option can be used for clients to debug response headers
    // response will be in form of { headers, body }
    if (options && options.showHeaders) {
      if (useCallback) return cb(null, { headers: res.headers, body });
      return { headers: res.headers, body };
    }
    if (useCallback) return cb(null, body);
    return body;
  }).catch(err => {
    if (useCallback) return cb(err);
    throw err;
  });
}
function getDataFromWeb ( url , options , apiKey , cb ) { let useCallback = 'function' === typeof cb ; const reqOptions = { headers : { } } ; if ( apiKey ) { reqOptions . headers [ 'X-Api-Key' ] = apiKey ; } if ( options && options . noCache === true ) { reqOptions . headers [ 'X-No-Cache' ] = 'true' ; } return fetch ( url , reqOptions ) . then ( res => Promise . all ( [ res , res . json ( ) ] ) ) . then ( ( [ res , body ] ) => { if ( body . status === 'error' ) throw new NewsAPIError ( body ) ; // 'showHeaders' option can be used for clients to debug response headers // response will be in form of { headers, body } if ( options && options . showHeaders ) { if ( useCallback ) return cb ( null , { headers : res . headers , body } ) ; return { headers : res . headers , body } ; } if ( useCallback ) return cb ( null , body ) ; return body ; } ) . catch ( err => { if ( useCallback ) return cb ( err ) ; throw err ; } ) ; }
Takes a URL string and returns a Promise containing
a buffer with the data from the web.
@param  {String} url      A URL String
@param  {String} apiKey   (Optional) A key to be used for authentication
@return {Promise<Buffer>} A Promise containing a Buffer
Takes a URL string and returns a Promise containing a buffer with the data from the web .