Skip to content

Instantly share code, notes, and snippets.

@togosh
Last active September 23, 2021 07:54
Show Gist options
  • Save togosh/69d983bbbb7e9a4c145246a104e18008 to your computer and use it in GitHub Desktop.
Save togosh/69d983bbbb7e9a4c145246a104e18008 to your computer and use it in GitHub Desktop.
Node Fetch Retry for JSON APIs - https://github.com/jonbern/fetch-retry
// https://codeakk.medium.com/hex-development-data-a1b1822446fa
// https://togosh.medium.com/hex-developer-guide-3b018a943a55
// https://github.com/jonbern/fetch-retry
// https://github.com/node-fetch/node-fetch/blob/main/README.md#custom-highwatermark
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
var fetchRetry = require('fetch-retry')(fetch, {
retryOn: async function(attempt, error, response) {
if (attempt > 3) { return false; }
if (error !== null) {
console.log(`FETCH --- RETRY ${attempt + 1} --- ERROR --- ` + error.toString()); await sleep(1000);
return true;
}
if (response.status >= 400) { // !response.ok
console.log(`FETCH --- RETRY ${attempt + 1} --- STATUS --- ` + response.status); await sleep(1000);
return true;
}
try {
var response2 = await response.clone().buffer();
const json = JSON.parse(response2);
if (json.errors && Object.keys(json.errors).length > 0) {
if (json.errors[0].message) {
console.log(`FETCH --- INTERNAL JSON ERROR --- ${attempt + 1} --- ` + json.errors[0].message); await sleep(1000);
return true;
}
}
return false;
} catch (error) {
console.log(`FETCH --- RETRY ${attempt + 1} --- JSON ---` + error.toString()); await sleep(1000);
return true;
}
}
});
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function testFetchRetry(){
const FETCH_SIZE = 1048576; // 1MB
try {
const resp = await fetchRetry('https://httpstat.us/400',{
method: 'GET',
highWaterMark: FETCH_SIZE,
headers: { 'Content-Type': 'application/json' }
});
const data = await resp.json();
console.log(json)
} catch (err) {
console.log("ERROR: " + err);
}
}
async function test(){
await testFetchRetry();
}
test();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment