robust-http-fetch
Advanced tools
Comparing version 1.0.6 to 1.0.7
45
index.js
@@ -98,2 +98,24 @@ /** | ||
function oneoffFetch(url, init) { | ||
const {mockTestOnlyFetch} = init; | ||
if (mockTestOnlyFetch) { | ||
return mockTestOnlyFetch; | ||
} | ||
const isBrowser = new Function("try {return window && this===window;}catch(e){ return false;}"); | ||
const fetcher = (isBrowser() && window.fetch) || require('node-fetch'); | ||
return () => fetcher(url, init); | ||
} | ||
function getLogger(optLogger) { | ||
const logger = typeof optLogger === 'function' ? optLogger : (ignored) => { | ||
}; | ||
return (args) => logger(new Date().toISOString() + ': ' + args); | ||
} | ||
function timeoutMessage(seqNum, timeout) { | ||
return `Request#${seqNum} no response in ${timeout}ms, fire another request`; | ||
} | ||
function checkArgs(...args) { | ||
@@ -135,25 +157,9 @@ const argsCheckedInfo = []; | ||
function timeoutMessage(seqNum, timeout) { | ||
return `Request#${seqNum} no response in ${timeout}ms, fire another request`; | ||
} | ||
robustHttpFetch.oneoffFetch = oneoffFetch; | ||
function getLogger(optLogger) { | ||
const logger = typeof optLogger === 'function' ? optLogger : (ignored) => { | ||
}; | ||
return (args) => logger(new Date().toISOString() + ': ' + args); | ||
} | ||
module.exports = exports = robustHttpFetch; | ||
function oneoffFetch(url, init) { | ||
const {mockTestOnlyFetch} = init; | ||
if (mockTestOnlyFetch) { | ||
return mockTestOnlyFetch; | ||
} | ||
const isBrowser = new Function("try {return window && this===window;}catch(e){ return false;}"); | ||
const fetcher = (isBrowser() && window.fetch) || require('node-fetch'); | ||
return () => fetcher(url, init); | ||
} | ||
module.exports = exports = {robustHttpFetch, oneoffFetch}; | ||
@@ -166,4 +172,1 @@ | ||
{ | ||
"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.6", | ||
"description": "Redo the http request when timeout or failed, aim at providing resilience over plain one-off fetch request by doing retry delayed/failed requests", | ||
"version": "1.0.7", | ||
"repository": { | ||
@@ -29,5 +29,12 @@ "type": "git", | ||
}, | ||
"files": [ | ||
"LICENSE", | ||
"README.md", | ||
"index.js", | ||
"package.json" | ||
], | ||
"keywords": [ | ||
"fetch", | ||
"http-fetch", | ||
"http fetch", | ||
"robust-fetch", | ||
@@ -41,4 +48,5 @@ "robust-http-fetch", | ||
"api-fetch", | ||
"fetch-request" | ||
"fetch-request", | ||
"fetch request" | ||
] | ||
} |
<h1 align="center">Robust Http Fetch</h1> | ||
<p align="center"> | ||
<a href="https://www.npmjs.com/package/robust-http-fetch"> | ||
<img src="https://img.shields.io/badge/npm-v1.0.6-blue" /> | ||
<img src="https://img.shields.io/badge/npm-v1.0.7-blue" /> | ||
</a> | ||
@@ -20,5 +20,5 @@ <a href="https://github.com/gaoqing/robust-http-fetch/blob/master/LICENSE"> | ||
It makes request to url endpoint, if response is not arrived in timely manner('init.timeout' settings below) or failed (fragile network etc), it will fire another same request as backup(up to 'init.maxRequests' requests to fire if none of them are happily resolved). It waits upto 'init.timeout' millisecond for response, if more than one requests are in-flight, the earliest resolved one will be resolved with and returned. Details refer to usage section in this page | ||
It makes request to url endpoint, if response is not arrived in timely manner('init.timeout' settings below) or failed (fragile network etc), it will fire another same request as backup(up to 'init.maxRequests' requests to fire if none of them are happily resolved). It waits upto 'init.timeout' milliseconds for response, if more than one requests are in-flight, the earliest resolved one will be resolved with and returned. Details refer to usage section in this page | ||
***Caveats***: only use this utils when your request is [idempotent](https://developer.mozilla.org/en-US/docs/Glossary/Idempotent), for example GET, no matter how many times calling GET, should have same result and data integrity still maintained. | ||
***Caveats***: only use this utils when your request is [idempotent](https://developer.mozilla.org/en-US/docs/Glossary/Idempotent), for example GET, no matter how many times calling GET, should have same result and data integrity still maintained, | ||
as well as DELETE. In case of POST/PUT, make sure your server side(or rely on DB constraints etc) to maintain the integrity, for example backend to perform checking if previous requests have completed then abort duplicated requests etc. | ||
@@ -36,18 +36,19 @@ | ||
Usage is as simple as below, can refer to tests in [End2End tests](https://github.com/gaoqing/robust-http-fetch/blob/master/test/e2e.test.js) or [unit tests](https://github.com/gaoqing/robust-http-fetch/blob/master/test/index.test.js)) | ||
Usage is as simple as below, can also refer to tests in [end2end tests](https://github.com/gaoqing/robust-http-fetch/blob/master/test/e2e.test.js) or [unit tests](https://github.com/gaoqing/robust-http-fetch/blob/master/test/index.test.js)) | ||
```javascript | ||
const { robustHttpFetch } = require('robust-http-fetch'); | ||
const robustHttpFetch = require('robust-http-fetch'); | ||
const apiUrl = "https://postman-echo.com/post"; | ||
const requestUrl = "https://postman-echo.com/post"; | ||
const body = {hello: 'world'}; | ||
//here use the Promise resolve callback function as the callback in 3rd parameter, | ||
//but you can use any callback function which accept a Promise object as its argument. | ||
// below sample use the Promise resolve callback function as the callback to the 3rd parameter, | ||
// but you can use your custom callback function which accept a Promise object as its argument. | ||
const resultAsPromise = new Promise((resolve, reject) => { | ||
robustHttpFetch( | ||
apiUrl, // required, request url | ||
requestUrl, // required | ||
{ | ||
timeout: 3000, // required, ie. here request will wait 3000ms before firing another request | ||
maxRequests: 3, // required, ie. here upto 3 requests to fire in case previous requests delayed or not well resolved | ||
// below properties are optional, usage to refer to window.fetch(init settings)/node-fetch(options settings) | ||
method: 'POST', | ||
@@ -62,3 +63,3 @@ body: JSON.stringify(body), | ||
//Do your stuff with this promise as usual, for example | ||
//do your stuff with this promise as usual, for example | ||
resultAsPromise | ||
@@ -71,3 +72,3 @@ .then(res => res.json()) | ||
```const {robustHttpFetch} = require('robust-http-fetch')``` robustHttpFetch is a javascript function to use, which accept 4 parameters as following | ||
```const robustHttpFetch = require('robust-http-fetch')```, it is a javascript function to use, which accept 4 parameters as followings | ||
@@ -74,0 +75,0 @@ |
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
86
13767
4
138
2