src/main.js
function (specificOptions, customParams, callback) {

    // Options - Default values
    const options = Object.assign({}, {
      urlPattern: ['/'],
      method: 'GET',
      successStatusCodes: [HTTP_CODE_200],
      failureStatusCodes: [],
      bodyProp: null,
      noparse: false,
      request: {}
    }, defaultOptions, specificOptions);

    // Create the url
    const url = buildUrl(options.urlPattern, customParams);

    // Build the actual request options
    const requestOptions = Object.assign({
      method: options.method,
      url: url,
      headers: [],
      followAllRedirects: true,
      form: null,
      body: null
    }, options.request);

    // Do the request
    request(requestOptions, function (error, response, body) {
      if (error) {
        callback(error, response);
        return;
      }

      if ((Array.isArray(options.successStatusCodes) && !options.successStatusCodes.includes(response.statusCode))
        || (Array.isArray(options.failureStatusCodes) && options.failureStatusCodes.includes(response.statusCode))) {
        callback(`Server returned unexpected status code: ${response.statusCode}`, response);
        return;
      }

      if (options.noparse) {
        // Wrap body in the response object
        if (typeof body === 'string') {
          body = { body: body };
        }

        // Add location
        const location = response.headers.Location || response.headers.location;

        if (location) {
          body.location = location;
        }

        // Add status code
        body.statusCode = response.statusCode;

        callback(null, body);
      } else {
        tryParseJson(body, options.bodyProp, callback);
      }
    });
  }
function (specificOptions, customParams, callback) {

    // Options - Default values
    const options = Object.assign({}, {
      urlPattern: ['/'],
      method: 'GET',
      successStatusCodes: [HTTP_CODE_200],
      failureStatusCodes: [],
      bodyProp: null,
      noparse: false,
      request: {}
    }, defaultOptions, specificOptions);

    // Create the url
    const url = buildUrl(options.urlPattern, customParams);

    // Build the actual request options
    const requestOptions = Object.assign({
      method: options.method,
      url: url,
      headers: [],
      followAllRedirects: true,
      form: null,
      body: null
    }, options.request);

    // Do the request
    request(requestOptions, function (error, response, body) {
      if (error) {
        callback(error, response);
        return;
      }

      if ((Array.isArray(options.successStatusCodes) && !options.successStatusCodes.includes(response.statusCode))
        || (Array.isArray(options.failureStatusCodes) && options.failureStatusCodes.includes(response.statusCode))) {
        callback(`Server returned unexpected status code: ${response.statusCode}`, response);
        return;
      }

      if (options.noparse) {
        // Wrap body in the response object
        if (typeof body === 'string') {
          body = { body: body };
        }

        // Add location
        const location = response.headers.Location || response.headers.location;

        if (location) {
          body.location = location;
        }

        // Add status code
        body.statusCode = response.statusCode;

        callback(null, body);
      } else {
        tryParseJson(body, options.bodyProp, callback);
      }
    });
  }
function ( specificOptions , customParams , callback ) { // Options - Default values const options = Object . assign ( { } , { urlPattern : [ '/' ] , method : 'GET' , successStatusCodes : [ HTTP_CODE_200 ] , failureStatusCodes : [ ] , bodyProp : null , noparse : false , request : { } } , defaultOptions , specificOptions ) ; // Create the url const url = buildUrl ( options . urlPattern , customParams ) ; // Build the actual request options const requestOptions = Object . assign ( { method : options . method , url : url , headers : [ ] , followAllRedirects : true , form : null , body : null } , options . request ) ; // Do the request request ( requestOptions , function ( error , response , body ) { if ( error ) { callback ( error , response ) ; return ; } if ( ( Array . isArray ( options . successStatusCodes ) && ! options . successStatusCodes . includes ( response . statusCode ) ) || ( Array . isArray ( options . failureStatusCodes ) && options . failureStatusCodes . includes ( response . statusCode ) ) ) { callback ( ` ${ response . statusCode } ` , response ) ; return ; } if ( options . noparse ) { // Wrap body in the response object if ( typeof body === 'string' ) { body = { body : body } ; } // Add location const location = response . headers . Location || response . headers . location ; if ( location ) { body . location = location ; } // Add status code body . statusCode = response . statusCode ; callback ( null , body ) ; } else { tryParseJson ( body , options . bodyProp , callback ) ; } } ) ; }
Run the actual HTTP request.

@param {object} specificOptions - options object overriding the default options below.
@param {object} customParams - custom url params to be added to url.
@param {function} callback - the callback function to be called when request is finished.
Run the actual HTTP request .