try-fetch
v0.0.4
Published
js fetch wrapper
Downloads
16
Maintainers
Readme
TryFetch
Wrapper over fetch
install
npm i try-fetchconst tryFetch = new TryFetch({
baseUrl: 'localhost:3000/api',
})
// or with custom error data
const tryFetch = new TryFetch({
baseUrl: 'localhost:3000/api',
errorCreator: (response) => {
return {
message: 'Url not exists',
status: response.status,
};
},
})query
const users = await tryFetch.query('/users');
// or with fetch options (RequestInit options)
const users = await tryFetch.query('/users', {
headers: {
auth: 'key for auth',
},
})state ('success' | 'error' | 'loading')
if(tryFetch.state === 'loading') {
return <Loading />
}cancel
tryFetch.cancel();refetch
const result = await tryFetch.query('/users');
if(!result.ok) {
setTimeout(() => {
const refetchResult = await result.refetch();
// ...
}, 3000)
}