index.js
function createResponseHandler(callback, okStatusCodes, noBuffer, retryFn) {
    if (typeof callback !== 'function') {
        callback = function () {};
    }

    if (typeof okStatusCodes === 'function') {
        retryFn = okStatusCodes;
        okStatusCodes = null;
        noBuffer = retryFn;
    }
    if (typeof noBuffer === 'function') {
        retryFn = noBuffer;
        noBuffer = false;
    }

    okStatusCodes = okStatusCodes || [200];

    /**
     * Retry the request if a retry function and retry-after headers are present
     * @param   {HTTPResponse} response The response object
     * @returns {void}
     */
    function retry(response) {
        var retryAfter = response.headers['retry-after'];
        if (typeof retryFn === 'function' && retryAfter) {
            retryAfter = parseInt(retryAfter, 10);
            setTimeout(retryFn, retryAfter * 1000);
            return true;
        }
        return false;
    }

    function handleResponse(response, body) {
        var error;

        // the handler expects a parsed response body
        if (noBuffer !== true && typeof body === 'undefined') {
            response.pipe(concat(function (body) {
                handleResponse(response, parseJSONBody(body));
            }));
            return;
        }

        if (okStatusCodes.indexOf(response.statusCode) > -1) {
            if (!retry(response)) {
                if (noBuffer) {
                    callback(null, response);
                } else {
                    callback(null, body, response);
                }
            }
        } else {
            if (response.statusCode === 429) {
                if (retry(response)) {
                    return;
                }
            }

            handleError(body, response, callback);
        }
    }

    return function (error, response) {
        if (error) {
            callback(error, response);
        } else {
            handleResponse(response);
        }
    };
}
function createResponseHandler(callback, okStatusCodes, noBuffer, retryFn) {
    if (typeof callback !== 'function') {
        callback = function () {};
    }

    if (typeof okStatusCodes === 'function') {
        retryFn = okStatusCodes;
        okStatusCodes = null;
        noBuffer = retryFn;
    }
    if (typeof noBuffer === 'function') {
        retryFn = noBuffer;
        noBuffer = false;
    }

    okStatusCodes = okStatusCodes || [200];

    /**
     * Retry the request if a retry function and retry-after headers are present
     * @param   {HTTPResponse} response The response object
     * @returns {void}
     */
    function retry(response) {
        var retryAfter = response.headers['retry-after'];
        if (typeof retryFn === 'function' && retryAfter) {
            retryAfter = parseInt(retryAfter, 10);
            setTimeout(retryFn, retryAfter * 1000);
            return true;
        }
        return false;
    }

    function handleResponse(response, body) {
        var error;

        // the handler expects a parsed response body
        if (noBuffer !== true && typeof body === 'undefined') {
            response.pipe(concat(function (body) {
                handleResponse(response, parseJSONBody(body));
            }));
            return;
        }

        if (okStatusCodes.indexOf(response.statusCode) > -1) {
            if (!retry(response)) {
                if (noBuffer) {
                    callback(null, response);
                } else {
                    callback(null, body, response);
                }
            }
        } else {
            if (response.statusCode === 429) {
                if (retry(response)) {
                    return;
                }
            }

            handleError(body, response, callback);
        }
    }

    return function (error, response) {
        if (error) {
            callback(error, response);
        } else {
            handleResponse(response);
        }
    };
}
function createResponseHandler ( callback , okStatusCodes , noBuffer , retryFn ) { if ( typeof callback !== 'function' ) { callback = function ( ) { } ; } if ( typeof okStatusCodes === 'function' ) { retryFn = okStatusCodes ; okStatusCodes = null ; noBuffer = retryFn ; } if ( typeof noBuffer === 'function' ) { retryFn = noBuffer ; noBuffer = false ; } okStatusCodes = okStatusCodes || [ 200 ] ; /**
     * Retry the request if a retry function and retry-after headers are present
     * @param   {HTTPResponse} response The response object
     * @returns {void}
     */ function retry ( response ) { var retryAfter = response . headers [ 'retry-after' ] ; if ( typeof retryFn === 'function' && retryAfter ) { retryAfter = parseInt ( retryAfter , 10 ) ; setTimeout ( retryFn , retryAfter * 1000 ) ; return true ; } return false ; } function handleResponse ( response , body ) { var error ; // the handler expects a parsed response body if ( noBuffer !== true && typeof body === 'undefined' ) { response . pipe ( concat ( function ( body ) { handleResponse ( response , parseJSONBody ( body ) ) ; } ) ) ; return ; } if ( okStatusCodes . indexOf ( response . statusCode ) > - 1 ) { if ( ! retry ( response ) ) { if ( noBuffer ) { callback ( null , response ) ; } else { callback ( null , body , response ) ; } } } else { if ( response . statusCode === 429 ) { if ( retry ( response ) ) { return ; } } handleError ( body , response , callback ) ; } } return function ( error , response ) { if ( error ) { callback ( error , response ) ; } else { handleResponse ( response ) ; } } ; }
Return an http response handler for API calls
@param   {Function} callback      The callback method to call
@param   {Array}    okStatusCodes (optional) HTTP status codes to use as OK (default: [200])
@param   {Boolean}  noBuffer      (optional) If true, the response will not be buffered and JSON parsed (unless error), default: false
@param   {Function} retryFn       (optional) If defined, function to call when receiving a Retry-After header
@returns {Function}               The response handler
Return an http response handler for API calls