tests/lib.js
/**
 * Promisified version of request. Rejects with an Error or resolves with the
 * response (use response.body to access the parsed body).
 * @param {Object} options The request options
 * @param {Number} delay [0] Delay in milliseconds
 * @return {Promise<Object>}
 */
Promisified version of request. Rejects with an Error or resolves with the
response (use response.body to access the parsed body).
@param {Object} options The request options
@param {Number} delay [0] Delay in milliseconds
@return {Promise}
Promisified version of request . Rejects with an Error or resolves with the response ( use response . body to access the parsed body ) . @param { Object } options The request options @param { Number } delay [ 0 ] Delay in milliseconds @return { Promise }
function requestPromise(options, delay = 0) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            request(Object.assign({ strictSSL: false }, options), (error, res) => {
                if (error) {
                    return reject(new RequestError(error.message, res));
                }
                let message = res.statusMessage || "Request failed";
                if (!res || !res.statusCode) {
                    return reject(new RequestError(message, res));
                }
                if (res.statusCode == 404) {
                    return reject(new RequestError("Not Found", res));
                }
                if (res.statusCode >= 400) {
                    let outcome;
                    try {
                        if (res.body.resourceType == "OperationOutcome") {
                            outcome = res.body;
                            message = res.body.issue.map(
                                i => `${i.code} ${i.severity}: ${i.diagnostics}`
                            ).join(";");
                        }
                    } catch(ex) {
                        message = String(res.body || res.statusMessage || "Unknown error!")
                    }
                    return reject(new RequestError(message, res, outcome));
                }
                resolve(res);
            });
        }, delay);
    });
}
function requestPromise ( options , delay = 0 ) { return new Promise ( ( resolve , reject ) => { setTimeout ( ( ) => { request ( Object . assign ( { strictSSL : false } , options ) , ( error , res ) => { if ( error ) { return reject ( new RequestError ( error . message , res ) ) ; } let message = res . statusMessage || "Request failed" ; if ( ! res || ! res . statusCode ) { return reject ( new RequestError ( message , res ) ) ; } if ( res . statusCode == 404 ) { return reject ( new RequestError ( "Not Found" , res ) ) ; } if ( res . statusCode >= 400 ) { let outcome ; try { if ( res . body . resourceType == "OperationOutcome" ) { outcome = res . body ; message = res . body . issue . map ( i => ` ${ i . code } ${ i . severity } ${ i . diagnostics } ` ) . join ( ";" ) ; } } catch ( ex ) { message = String ( res . body || res . statusMessage || "Unknown error!" ) } return reject ( new RequestError ( message , res , outcome ) ) ; } resolve ( res ) ; } ) ; } , delay ) ; } ) ; }
Promisified version of request.
Promisified version of request .
// console.log(res.body, res.statusMessage)