@capriza/http-utils
Advanced tools
Comparing version 0.2.6 to 0.2.7
var axios = require('axios'); | ||
var BaseUtils = require("./baseUtils"); | ||
var Queue = require("./rateLimitQueue"); | ||
const URL = require('url'); | ||
const responseTypes = ["data", "status", "statusText", "headers", "config", "request"]; | ||
const availableMethods = ["get", "post", "put", "patch", "head"]; | ||
const retryStatusCodes = [502, 503, 504]; | ||
@@ -54,7 +56,15 @@ const networkErrors = ["ECONNABORTED", "ECONNRESET"]; | ||
async _callRequest(method, url, opts = {}, { priority, debug, logger, responseType = 'data', retriesCount } = { }){ | ||
async _callRequest(method, url, opts = {}, { priority, logger, responseType = 'data', retriesCount } = { }) { | ||
retriesCount = retriesCount === undefined ? this.maxRetries : retriesCount; | ||
const requestLog = logger || this.logger; | ||
return new Promise((resolve, reject)=>{ | ||
var doRequest = async ()=>{ | ||
return new Promise((resolve, reject) => { | ||
try { | ||
const fullURL = URL.resolve(url, opts.baseURL || this.baseURL || ""); | ||
const objURL = new URL.URL(fullURL); | ||
var cleanURL = objURL.origin + objURL.pathname + " (without query)" | ||
} catch (ex) { | ||
cleanURL = "[invalid url]"; | ||
} | ||
var doRequest = async () => { | ||
let options = Object.assign(opts, { url, method }); | ||
@@ -66,3 +76,3 @@ return this.http.request(options); | ||
if(!retriesCount || !(retriesCount > 0)) { | ||
return reject(this._handleError(err, method, `${opts.baseURL || this.baseURL || ""}${url}`, requestLog)); | ||
return reject(this._handleError(err, method, cleanURL, requestLog)); | ||
} else { | ||
@@ -74,6 +84,6 @@ let retryReason; | ||
if (!retryReason) { | ||
return reject(this._handleError(err, method, `${opts.baseURL || this.baseURL || ""}${url}`, requestLog)); | ||
return reject(this._handleError(err, method, cleanURL, requestLog)); | ||
} | ||
requestLog.error(`[HttpUtils] Got ${retryReason} on ${opts.baseURL || this.baseURL || ""}${url} Retrying...`); | ||
requestLog.error(`[HttpUtils] Got ${retryReason} on ${cleanURL}. Retrying...`); | ||
await this._delay(); | ||
@@ -84,13 +94,9 @@ return resolve(this._callRequest(method, url, opts, { priority: true, debug, logger, responseType, retriesCount: retriesCount - 1 })); | ||
if (!["get", "post", "put", "patch", "head"].includes(method)){ | ||
if (!availableMethods.includes(method)){ | ||
reject(`HTTP request method not defined. ${method}`); | ||
} | ||
if(debug){ | ||
requestLog.info(`[HttpUtils] HTTP ${method} request: ${opts.baseURL || this.baseURL}${url}`); | ||
requestLog.info(`[HttpUtils] PRIORITY[${priority}] OPTS: ${JSON.stringify(opts)}`); | ||
} | ||
requestLog.debug(`[HttpUtils] PRIORITY[${priority ? priority : 0}] HTTP ${method} request: ${cleanURL}`); | ||
this.requestQueue.add(doRequest, {priority}).then((response)=>{ | ||
this.requestQueue.add(doRequest, {priority}).then((response) => { | ||
if(responseTypes.includes(responseType)) { | ||
@@ -97,0 +103,0 @@ return resolve(response[responseType]); |
@@ -19,3 +19,3 @@ | ||
if (this.queueLength >= this.maxQueuedPromises) { | ||
throw new Error(`Queue limit(${this.maxQueuedPromises}) reached`); | ||
reject(`Queue limit(${this.maxQueuedPromises}) reached`); | ||
} | ||
@@ -31,3 +31,3 @@ this._queue.push({ callback, args, resolve, reject }); | ||
if (this.queueLength >= this.maxQueuedPromises) { | ||
throw new Error(`Queue limit(${this.maxQueuedPromises}) reached`); | ||
reject(`Queue limit(${this.maxQueuedPromises}) reached`); | ||
} | ||
@@ -34,0 +34,0 @@ this._queue.unshift({ callback, args, resolve, reject }); |
{ | ||
"name": "@capriza/http-utils", | ||
"version": "0.2.6", | ||
"version": "0.2.7", | ||
"description": "HTTP Request utils that handles, request-response, errors, concurrency, priority and authentication", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,1 +0,1 @@ | ||
VERSION=0.2.6 | ||
VERSION=0.2.7 |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32681
357