robust-http-fetch
Advanced tools
Comparing version 1.0.1 to 1.0.3
@@ -149,7 +149,10 @@ /** | ||
} | ||
const fetcher = (window && window.fetch) || require('node-fetch'); | ||
const isBrowser = new Function("try {return this===window;}catch(e){ return false;}"); | ||
const fetcher = (isBrowser && window && window.fetch) || require('node-fetch'); | ||
return () => fetcher(url, init); | ||
} | ||
module.exports = {robustHttpFetch, oneoffFetch}; | ||
module.exports = exports = {robustHttpFetch, oneoffFetch}; | ||
@@ -156,0 +159,0 @@ |
{ | ||
"name": "robust-http-fetch", | ||
"description": "Redo http fetch request when timeout or failed, aim at providing resilience over plain one-off fetch request by doing retry delayed/failed requests", | ||
"version": "1.0.1", | ||
"version": "1.0.3", | ||
"repository": { | ||
@@ -28,3 +28,16 @@ "type": "git", | ||
"jest": "^24.5.0" | ||
} | ||
}, | ||
"keywords": [ | ||
"fetch", | ||
"http-fetch", | ||
"robust-fetch", | ||
"robust-http-fetch", | ||
"resilent-fetch", | ||
"resilent-http-fetch", | ||
"node-fetch", | ||
"browser-fetch", | ||
"window.fetch", | ||
"api-fetch", | ||
"fetch-request" | ||
] | ||
} |
@@ -6,3 +6,3 @@ <h1 align="center">Robust Http Fetch</h1> | ||
</a> | ||
<a href="https://github.com/gaoqing/robust-http-fetch/LICENSE"> | ||
<a href="https://github.com/gaoqing/robust-http-fetch/blob/master/LICENSE"> | ||
<img alt="License: MIT" src="https://img.shields.io/badge/license-MIT-yellow.svg" target="_blank" /> | ||
@@ -17,3 +17,3 @@ </a> | ||
This robust-http-fetch is a light-weight and [100%-test-coverage](https://codecov.io/gh/gaoqing/robust-http-fetch) javascript util for robustly making fetch request. | ||
This robust-http-fetch is a light-weight and [100%-test-coverage](https://codecov.io/gh/gaoqing/robust-http-fetch) javascript util for robustly making http fetch request. | ||
@@ -44,8 +44,10 @@ The underlying fetch will be delegated to either [window.fetch](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch) when use in browser or [node-fetch](https://www.npmjs.com/package/node-fetch) when use in node server side. | ||
const body = {hello: 'world'}; | ||
const resultAsPromise = new Promise(resolve => { | ||
//Here use the Promise resolve callback function as the callback in 3rd parameter, but you can use any function as callback to fit yourself | ||
const resultAsPromise = new Promise((resolve, reject) => { | ||
robustHttpFetch( | ||
apiUrl, // required, request url | ||
apiUrl, // required, request url | ||
{ | ||
timeout: 3000, // required, ie. request will wait 1500ms before firing another request | ||
maxRequests: 3, // required, ie. upto 3 requests to fire in case previous requests delayed or not resolved happily | ||
timeout: 3000, // required, ie. here request will wait 1500ms before firing another request | ||
maxRequests: 3, // required, ie. here upto 3 requests to fire in case previous requests delayed or not resolved happily | ||
method: 'POST', | ||
@@ -55,4 +57,4 @@ body: JSON.stringify(body), | ||
}, | ||
resolve, // required, callback to invoke | ||
console.log //optional | ||
resolve, // required, callback function to be invoked with a Promise object later | ||
console.log // optional function | ||
); | ||
@@ -62,3 +64,5 @@ }); | ||
//Do your stuff with this promise as usual, for example | ||
resultAsPromise.then(res => res.json()).then(data => console.log(data)); | ||
resultAsPromise | ||
.then(res => res.json()) | ||
.then(data => console.log(data)); | ||
``` | ||
@@ -73,6 +77,6 @@ | ||
| :------------------------ |:-------------:|:-------------: | :-------------| | ||
| url | true |string | the resource destination url to make this request to | ||
| init | true |object | it can have properties in ['init'](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters) parameter of window.fetch or ['options'](https://www.npmjs.com/package/node-fetch#options) parameter of node-fetch. However only two MANDATORY settings : ***'timeout'*** to time-box a request and ***'maxRequests'*** to limit the number of total requests to attempt.<br /> other properties refer to ['init'](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters) of window.fetch or ['options'](https://www.npmjs.com/package/node-fetch#options) of node-fetch | ||
| callback | true |function | it will be invoked with a resolved promise(if a request is well finished before attempting all the retry requests) or last request' result(a promise that might be eventually resolved or rejected) | ||
| optLogger | false |function |optional, if any, will get called with a single string parameter to give small hints when making request | ||
| url | true |string | The resource destination url to make this request to | ||
| init | true |object | It can have properties in ['init'](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters) parameter of window.fetch or ['options'](https://www.npmjs.com/package/node-fetch#options) parameter of node-fetch. <br />however two settings are MANDATORY: ***'timeout'*** to time-box a request and ***'maxRequests'*** to limit the total number of requests to attempt.<br /> other properties refer to ['init'](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters) of window.fetch or ['options'](https://www.npmjs.com/package/node-fetch#options) of node-fetch | ||
| callback | true |function | It will be invoked with a resolved promise(if a request is well finished before attempting all the retry requests) <br /> or last request' result(a promise that might be eventually resolved or rejected) | ||
| optLogger | false |function |Optional, if any, will get called with a single string parameter to give small hints when making request | ||
@@ -79,0 +83,0 @@ |
const {robustHttpFetch} = require("../index"); | ||
describe('e2e tests', () => { | ||
beforeEach(()=>jest.setTimeout(10 * 1000)); | ||
beforeEach(()=>jest.setTimeout(15 * 1000)); | ||
@@ -9,3 +9,3 @@ it('GET request, give smaller timeout number so response might timeout then it will make retry', async done => { | ||
const result = new Promise(resolve => { | ||
robustHttpFetch(testUrl, {method: 'GET', timeout: 2000, maxRequests: 5}, resolve, console.log); | ||
robustHttpFetch(testUrl, {method: 'GET', timeout: 3000, maxRequests: 5}, resolve, console.log); | ||
}); | ||
@@ -28,3 +28,3 @@ | ||
headers: {'Content-Type': 'application/json'}, | ||
timeout: 2000, maxRequests: 5 | ||
timeout: 3000, maxRequests: 5 | ||
}, | ||
@@ -31,0 +31,0 @@ resolve, |
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
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
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
26487
415
84
1