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

@capriza/http-utils

Package Overview
Dependencies
Maintainers
3
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.6 to 0.2.7

32

lib/httpUtils.js
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
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