@cooperwfloyd/fancyfetch v1.4.2
š fancyfetch
fancyfetch is a simple, lightweight and isomorphic extension of the fetch API to allow for graceful error handling, response validation, automatic retries, and the ability to use any fetch API package.
š Basic usage
Import using the default export from @cooperwfloyd/fancyfetch. fancyfetch can be used client-side in the browser or on the server just like the standard fetch API ā fancyfetch will automatically locate and use Node's global fetch API or your browser's native fetch API.
import fancyfetch from '@cooperwfloyd/fancyfetch';
const data = await fancyfetch('https://www.example.com');š„³ Fancy usage
The third argument is available for all of fancyfetch's extensions, including:
š The ability to use a custom fetch API instead of the global default
import fancyfetch from '@cooperwfloyd/fancyfetch';
import fetch from 'node-fetch';
const data = await fancyfetch(
'https://www.example.com/a-large-server-side-request',
{highWaterMark: 2048 * 2048},
{fetch}
);š The ability to gracefully auto-retry requests if they fail or if their responses don't return true in a custom validation callback
import fancyfetch from '@cooperwfloyd/fancyfetch';
const data = await fancyfetch(
'https://www.example.com/json',
{
headers: {
'Content-Type': 'application/json',
},
},
{
maxAttempts: 10,
retryDelay: 1000,
validateResponse: async (response) => {
try {
const json = await response?.json();
return !!json?.data;
} catch {
return false;
}
},
onRetrySuccess: () => console.log('Successful retry'),
onRetryError: () => console.error('Failed retry'),
onError: () => console.error('No successful attempts'),
}
);āļø Reference
resource(required): fetch.resourceoptions: fetch.optionsextrasfetch: function- The
fetchAPI (ex.fetch,node-fetch,isomorphic-fetch) that requests should use (default, in order of specificity:fetch,global.fetch,window.fetch)
- The
log: boolean- Dictates whether or not
fancyfetch's console statements should be fired (default:true)
- Dictates whether or not
validateResponse: function- This callback function allows for checking the response to determine it's validity. It sends the response as an argument and should return a truthy or falsy value since
fancyfetchwill use a boolean to determine the response's validity.
- This callback function allows for checking the response to determine it's validity. It sends the response as an argument and should return a truthy or falsy value since
maxAttempts: number- Specifies the maximum number of times that the request should be attempted (default:
1). ThevalidateResponsecallback should be used whenevermaxAttemptsis greater than one sincefancyfetchwill not know when to break out of the recursive retry loop without it.
- Specifies the maximum number of times that the request should be attempted (default:
retryDelay: number- Specifies the number of milliseconds that
fancyfetchshould wait before retrying a failed request.
- Specifies the number of milliseconds that
11 months ago
11 months ago
11 months ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago