@capriza/http-utils
Advanced tools
Comparing version 0.2.2 to 0.2.3
@@ -5,2 +5,3 @@ var axios = require('axios'); | ||
const responseTypes = ["data", "status", "statusText", "headers", "config", "request"]; | ||
const retryStatusCodes = [502, 503, 504]; | ||
@@ -10,2 +11,5 @@ module.exports = class HttpUtils extends BaseUtils { | ||
super(options); | ||
this.retryInterval = (!isNaN(options.retryInterval) && options.retryInterval) || 5000; | ||
this.maxRetries = (!isNaN(options.maxRetries) && options.maxRetries) || 0; | ||
this.retryStatusCodes = (Array.isArray(options.retryStatusCodes) && options.retryStatusCodes) || retryStatusCodes; | ||
@@ -42,3 +46,4 @@ this.requestQueue = new Queue(options.limit, options.interval, options.maxConcurrent); | ||
async _callRequest(method, url, opts = {}, { priority, debug, logger, responseType = 'data' } = {}){ | ||
async _callRequest(method, url, opts = {}, { priority, debug, logger, responseType = 'data', retriesCount } = { }){ | ||
retriesCount = retriesCount === undefined ? this.maxRetries : retriesCount; | ||
const requestLog = logger || this.logger; | ||
@@ -51,14 +56,27 @@ return new Promise((resolve, reject)=>{ | ||
}); | ||
return this.http.request(options); | ||
try { | ||
return await this.http.request(options); | ||
} catch (ex) { | ||
if(!this.maxRetries || !ex || !ex.response || !this.retryStatusCodes.includes(ex.response.status)) throw ex; | ||
requestLog.error(`[HttpUtils] Got status ${ex.response.status} on ${opts.baseURL || this.baseURL || ""}${url}${retriesCount === 0 ? `. Reached max retries: ${this.maxRetries}` : " Retrying..."}`); | ||
if(!(retriesCount > 0)) throw ex; | ||
await this._delay(); | ||
return this._callRequest(method, url, opts, { priority: true, debug, logger, responseType, retriesCount: retriesCount - 1 }); | ||
} | ||
}; | ||
var onError = (err)=>{ | ||
if (err.response){ | ||
requestLog.error(`[HttpUtils] HTTP ${method} response error: ${err.response.status} ${err.response.statusText} on ${opts.baseURL || this.baseURL}${url}`); | ||
if (err.response) { | ||
requestLog.error(`[HttpUtils] HTTP ${method} response error: ${err.response.status} ${err.response.statusText} on ${opts.baseURL || this.baseURL || ""}${url}`); | ||
reject(err.response); | ||
} else if (err.status) { | ||
requestLog.error(`[HttpUtils] HTTP ${method} response error: ${err.status} ${err.statusText} on ${opts.baseURL || this.baseURL || ""}${url}`); | ||
reject(err); | ||
} else if (err.code){ | ||
requestLog.error(`[HttpUtils] HTTP ${method} error: ${err.code} on ${opts.baseURL || this.baseURL}${url}`); | ||
requestLog.error(`[HttpUtils] HTTP ${method} error: ${err.code} on ${opts.baseURL || this.baseURL || ""}${url}`); | ||
reject(err); | ||
} else { | ||
requestLog.error(`[HttpUtils] HTTP ${method} error: ${err} on ${opts.baseURL || this.baseURL}${url}`); | ||
requestLog.error(`[HttpUtils] HTTP ${method} error: ${err} on ${opts.baseURL || this.baseURL || ""}${url}`); | ||
reject(err); | ||
@@ -89,2 +107,8 @@ } | ||
} | ||
async _delay() { | ||
return new Promise(res => { | ||
setTimeout(res, this.retryInterval); | ||
}); | ||
} | ||
}; |
{ | ||
"name": "@capriza/http-utils", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"description": "HTTP Request utils that handles, request-response, errors, concurrency, priority and authentication", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
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
28272
341