UNPKG

rest-api-starter

Version:
118 lines (101 loc) 4.59 kB
/** * Created by svinci on 1/13/17. */ 'use strict'; const configuration = require('config'); const agent = require('superagent'); const Promise = require('bluebird'); /** * Promisifed superagent. */ const pagent = require('superagent-promise')(agent, Promise); /** * Transaction store. */ const localStore = require('./local-store'); /** * This function encapsulates the logic that applies to every request made by this client. * @param request Superagent http request. * @param headers Custom headers. * @param requestTimeout Timeout to be set as deadline. */ const endRequest = (request, headers, requestTimeout) => { let r = request; for (const key in headers) { r = r.set(key, headers[key]); } if (requestTimeout) { r = r.timeout({ 'deadline': requestTimeout }); } return r.set(configuration.get('app.http.transactionHeader'), localStore.getValue('tid')).end().then((response) => response.body); }; /** * Rest Client builder. * @param endpoint The protocol, host, port and base path of the service this client will be doing requests to. (E.g. https://some-api.com/people/v1). * @param globalRequestTimeout Timeout to be set as deadline for every request made by this client, unless overridden for a specific request. */ const client = (endpoint, globalRequestTimeout) => { /** * Execute a GET request. In case of success, the resulting promise will resolve with the response body. In case of failure, the promise will reject with the whole http request, http response and error. * @param uri URI to which the request has to be made. * @param headers Custom headers. * @param requestTimeout Timeout to be set as deadline. */ const get = (uri, headers, requestTimeout) => { const request = pagent.get(`${endpoint}${uri}`); return endRequest(request, headers, requestTimeout || globalRequestTimeout); }; /** * Execute a POST request. In case of success, the resulting promise will resolve with the response body. In case of failure, the promise will reject with the whole http request, http response and error. * @param body Request body. * @param uri URI to which the request has to be made. * @param headers Custom headers. * @param requestTimeout Timeout to be set as deadline. */ const post = (body, uri, headers, requestTimeout) => { const request = pagent.post(`${endpoint}${uri}`).send(body); return endRequest(request, headers, requestTimeout || globalRequestTimeout); }; /** * Execute a PUT request. In case of success, the resulting promise will resolve with the response body. In case of failure, the promise will reject with the whole http request, http response and error. * @param body Request body. * @param uri URI to which the request has to be made. * @param headers Custom headers. * @param requestTimeout Timeout to be set as deadline. */ const put = (body, uri, headers, requestTimeout) => { const request = pagent.put(`${endpoint}${uri}`).send(body); return endRequest(request, headers, requestTimeout || globalRequestTimeout); }; /** * Execute a PATCH request. In case of success, the resulting promise will resolve with the response body. In case of failure, the promise will reject with the whole http request, http response and error. * @param body Request body. * @param uri URI to which the request has to be made. * @param headers Custom headers. * @param requestTimeout Timeout to be set as deadline. */ const patch = (body, uri, headers, requestTimeout) => { const request = pagent.patch(`${endpoint}${uri}`).send(body); return endRequest(request, headers, requestTimeout || globalRequestTimeout); }; /** * Execute a DELETE request. In case of success, the resulting promise will resolve with the response body. In case of failure, the promise will reject with the whole http request, http response and error. * @param uri URI to which the request has to be made. * @param headers Custom headers. * @param requestTimeout Timeout to be set as deadline. */ const del = (uri, headers, requestTimeout) => { const request = pagent.del(`${endpoint}${uri}`); return endRequest(request, headers, requestTimeout || globalRequestTimeout); }; return { 'get': get, 'post': post, 'put': put, 'patch': patch, 'del': del }; }; module.exports = client;