yet-another-fetch
v0.1.1
Published
An HTTP client with retry functionality, callbacks, and error handling using neverthrow.
Downloads
5
Readme
Yet another fetch
An HTTP client with retry functionality, callbacks, and error handling using neverthrow.
It utilizes the fetch API under the hood and provides an API similar to fetch with additional options.
[!IMPORTANT]
Only for Node.js!
It's assumed that for the frontend, it's better to use specialized solutions like Tanstack Query or Farfetched.
Usage
pnpm i yet-another-fetchimport { createHttpClient, retryOnStatus, retryDelayExp2 } from 'yet-another-fetch';
const client = createHttpClient({
baseUrl: 'https://example.com',
retries: 3,
retryDelay: (attempt) => 2 ** attempt * 1000, // or use retryDelayExp2(1000)
retryOn: retryOnStatus([401, 500]),
interceptRequest(url, config) {
// Object.assign(config.headers, { hello: "world" });
},
inspectResponse(response) {
// app.requestContext.set('setCookies', response.headers.getSetCookie());
},
inspectError(error) {
// console.error(error);
},
});
const { message } = await client
.get<{ message: string }>('/data')
.andThen((res) => res.json())
.unwrapOr({ message: 'hello' });Do I Need This?
Probably not. Start with more popular alternatives like openapi-fetch and fetch-retry:
import createFetchClient from 'openapi-fetch';
import fetchRetry from 'fetch-retry';
import type { paths } from './my-openapi-3-schema'; // generated by openapi-typescript
const client = createFetchClient<paths>({
baseUrl,
fetch: fetchRetry(fetch, {
retries: 1,
retryDelay: (attempt, error, response) => {
return 2 ** attempt * 1000;
},
}),
});
client.use({
async onRequest({ request, options }) {},
async onResponse({ request, response, options }) {},
async onError({ error }) {},
});
const { data, error } = await client.GET('/data', {});