UNPKG

doformify-http-request

Version:

Doformify http request

36 lines (29 loc) 1.02 kB
'use strict'; const axios = require('axios'); function HttpRequest() { this.options = undefined; this.headers = undefined; this.defaultOptions = {timeout: 3000}; this.defaultHeaders = { "X-Source": "doformify-http-request lib.", "Content-Type": "application/json", "Authorization": `Basic ${process.env.api_http_security_token}` }; } HttpRequest.prototype.withOptions = function (opts) { this.options = opts; return this; } HttpRequest.prototype.withHeaders = function (headers) { this.headers = headers; return this; } HttpRequest.prototype.execute = function () { if (this.options) { return axios.create(Object.assign(this.defaultOptions, this.options, { headers: Object.assign(this.defaultHeaders, this.headers || {}) })).request(this.options); } throw new Error("An error occurred while executing the request, the options should not be null."); } module.exports = HttpRequest;