https://github.com/victor141516/fetch-goodies
Fetch but with goodies.
https://github.com/victor141516/fetch-goodies
Last synced: 3 months ago
JSON representation
Fetch but with goodies.
- Host: GitHub
- URL: https://github.com/victor141516/fetch-goodies
- Owner: victor141516
- Created: 2024-07-31T22:41:30.000Z (11 months ago)
- Default Branch: master
- Last Pushed: 2024-08-01T09:50:07.000Z (11 months ago)
- Last Synced: 2025-03-01T21:03:07.662Z (4 months ago)
- Language: TypeScript
- Homepage:
- Size: 54.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# fetch-goodies
Fetch but with goodies.
## Install
```sh
npm i fetch-goodies
```## Usage
Basic usage:
```ts
import { goodies } from 'fetch-goodies'
const fetchWithGoodies = goodies(fetch)
const response = await fetchWithGoodies('https://example.com')
```Shorthand for verbs:
```ts
await fetchWithGoodies.get('https://example.com')
await fetchWithGoodies.post('https://example.com')
await fetchWithGoodies.put('https://example.com')
```Typed JSON response with:
```ts
const response = await fetchWithGoodies.post<{ key: 'value' }>(
'https://example.com'
)
console.log('Looks good?', response.json()!.key === 'value')
```Common prefix for all requests:
```ts
const fetchWithGoodies = goodies(fetch, {
urlPrefix: 'https://example.com/api',
})
const response = await fetchWithGoodies('/post')
```Powerful retry options:
```ts
fetchWithGoodies('https://example.com', {
retry: {
count: 5,
delay: (attempt) => 2 ** attempt * 1000,
on: ({ error }) => error?.message.includes('Ah Shit, Here We Go Again'),
},
})
```Global retry options:
```ts
const fetchWithGoodies = goodies(fetch, {
retry: { count: 5 },
forceParseJson: true,
httpCodesConsideredSuccessful: [200, 201],
urlPrefix: 'https://example.com',
})
```Can use a polyfilled fetch:
```ts
const fetchWithGoodies = goodies(polyfilledFetch)
```