https://github.com/ivanatias/strong-fetch
A wrapper around the fetch API that provides axios' basic features and automatic AbortController handling
https://github.com/ivanatias/strong-fetch
abortcontroller axios fetch typescript utilities
Last synced: about 1 year ago
JSON representation
A wrapper around the fetch API that provides axios' basic features and automatic AbortController handling
- Host: GitHub
- URL: https://github.com/ivanatias/strong-fetch
- Owner: ivanatias
- License: mit
- Created: 2023-03-13T20:09:34.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-08-31T03:57:12.000Z (almost 3 years ago)
- Last Synced: 2025-02-07T16:46:58.791Z (over 1 year ago)
- Topics: abortcontroller, axios, fetch, typescript, utilities
- Language: TypeScript
- Homepage: https://strong-fetch-ia.vercel.app
- Size: 72.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# [Strong-fetch demo](https://strong-fetch-ia.vercel.app/)
`strong-fetch` is a wrapper around the `fetch` API. It provides `axios`' basic features and has built-in (and optional) automatic `AbortController` handling. You only need to provide an unique `signalKey` for each request. Say no more to creating new `AbortController` instances everywhere, `strong-fetch` will do everything for you.
## `strong-fetch` features:
- Automatic (and optional) `AbortController` handling.
- Automatic JSON response data handling.
- Automatically throws on non 2xx responses.
- A `FetchError` object that extends the `Error` object and includes the status of the error response for better exceptions handling.
- Responses have the shape of `StrongFetchResponse`.
- Utilities for manually aborting requests if needed.
`strong-fetch` is TypeScript friendly.
## Response schema:
The response for a request contains the following information:
```typescript
data: {
} // Object containing the response provided by the server.
status: number // HTTP status from the server response.
statusText: string // HTTP status message from the server response.
headers: {
} // HTTP headers the server responded with.
```
## Usage examples:
```typescript
import fetch, { type StrongFetchResponse } from 'strong-fetch'
import type { ProductResponse } from './types'
const searchProducts = (
query: string
): Promise> => {
return fetch(`https://dummyjson.com/products/search?q=${query}`, {
signalKey: 'searchProducts'
})
}
const results = await searchProducts('something')
/*
{
data: ProductResponse
status: number
statusText: string
headers: Record
}
*/
```
```typescript
import fetch from 'strong-fetch'
import type { ProductResponse } from './types'
const searchProducts = (query: string) => {
return fetch(
`https://dummyjson.com/products/search?q=${query}`,
{
signalKey: 'searchProducts'
}
)
}
const results = await searchProducts('something')
/*
{
data: ProductResponse
status: number
statusText: string
headers: Record
}
*/
```
## Run locally:
```
// Clone the repository
git clone https://github.com/ivanatias/strong-fetch.git
// Install the dependencies
npm install
// Start a development server
npm run dev
```