@capriza/http-utils
Advanced tools
Comparing version 0.2.8 to 0.2.9
var axios = require('axios'); | ||
var BaseUtils = require("./baseUtils"); | ||
var Queue = require("./rateLimitQueue"); | ||
const CancelToken = axios.CancelToken; | ||
const URL = require('url'); | ||
@@ -17,3 +18,3 @@ const responseTypes = ["data", "status", "statusText", "headers", "config", "request"]; | ||
this.retryOnNetworkErrors = options.retryOnNetworkErrors || false; | ||
this.requestTimeout = parseInt(options.requestTimeout) || 30 * 1000; | ||
this.requestQueue = new Queue(options.limit, options.interval, options.maxConcurrent); | ||
@@ -23,3 +24,3 @@ this.baseURL = options.baseUrl; | ||
baseURL: options.baseUrl, | ||
timeout: options.requestTimeout || 30000, | ||
timeout: this.requestTimeout, | ||
auth : options.username && options.password && { username : options.username, password : options.password }, | ||
@@ -71,4 +72,32 @@ headers: options.headers || { | ||
var doRequest = async () => { | ||
let options = Object.assign(opts, { url, method }); | ||
return this.http.request(options); | ||
let source = CancelToken.source(); | ||
let options = Object.assign(opts, { url, method, cancelToken: source.token }); | ||
return new Promise(async (resolve, reject) => { | ||
let endedRequest = false; | ||
let _end = (func, res) => { | ||
if(endedRequest) return; | ||
endedRequest = true; | ||
func(res); | ||
}; | ||
const timeoutThreshold = this.requestTimeout + 3 * 1000; | ||
let timeout = setTimeout(() => { | ||
this.logger.info("request time exceeded"); | ||
try { | ||
source.cancel('request was canceled due to time out'); | ||
} catch (ex) { | ||
this.logger.error("source.cancel was failed. ex=" + ex); | ||
} | ||
_end(reject, "request was canceled due to timeout. timeout=" + timeoutThreshold); | ||
}, timeoutThreshold); | ||
try { | ||
let res = await this.http.request(options); | ||
_end(resolve, res); | ||
} catch (ex) { | ||
_end(reject, ex); | ||
} finally { | ||
clearTimeout(timeout); | ||
} | ||
}); | ||
}; | ||
@@ -75,0 +104,0 @@ |
{ | ||
"name": "@capriza/http-utils", | ||
"version": "0.2.8", | ||
"version": "0.2.9", | ||
"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.8 | ||
VERSION=0.2.9 |
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
34060
385