Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rus-sharafiev/fetch-api
A simple wrapper around the Fetch API
https://github.com/rus-sharafiev/fetch-api
fetch-api token-refresh
Last synced: about 2 months ago
JSON representation
A simple wrapper around the Fetch API
- Host: GitHub
- URL: https://github.com/rus-sharafiev/fetch-api
- Owner: rus-sharafiev
- License: mit
- Created: 2023-05-25T21:06:20.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-10-27T18:36:35.000Z (about 2 months ago)
- Last Synced: 2024-10-27T20:31:26.027Z (about 2 months ago)
- Topics: fetch-api, token-refresh
- Language: TypeScript
- Homepage:
- Size: 40 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Fetch with access token refresh
[![npm](https://img.shields.io/npm/v/%40russh%2Ffetch-api)](https://npm.im/@russh/fetch-api)
A simple wrapper around the Fetch API with
- Access token refresh on 401 response status
- Shorthands
- RTK baseQuery
- JSON to FormData converter (requires custom middleware on backend)## Install
```
npm i @russh/fetch-api
```## Usage
First create an instance of the FetchApi class with a base URL and optionally a path name to refresh the access token.
```ts
const api = new FetchApi('https://example.com', '/refresh-token')
```
Then use full declaration...
```ts
const result = await api.fetch({ url: '/path', method: 'POST', body: { foo: 'bar' } })
```
...or shorthand to get fetch data```ts
await api.get('/path')
await api.post('/path', { foo: 'bar' })
await api.patch('/path/id', { foo: 'bar' })
await api.delete('/path/id')
```
The instance also contains fetch-api based RTK baseQuery```ts
export const apiName = createApi({
reducerPath: 'apiName',
baseQuery: api.baseQuery,
endpoints: (builder) => ({
...
})
})
```