Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@capriza/http-utils

Package Overview
Dependencies
Maintainers
4
Versions
60
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@capriza/http-utils - npm Package Compare versions

Comparing version 0.2.2 to 0.2.3

36

lib/httpUtils.js

@@ -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);
});
}
};

2

package.json
{
"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",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc