Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jarweb/rufetch
a lightweight fetch lib with middleware inpired by koa
https://github.com/jarweb/rufetch
fetch middleware
Last synced: 3 days ago
JSON representation
a lightweight fetch lib with middleware inpired by koa
- Host: GitHub
- URL: https://github.com/jarweb/rufetch
- Owner: Jarweb
- Created: 2020-04-22T09:12:22.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T04:00:19.000Z (about 2 years ago)
- Last Synced: 2025-01-17T02:56:09.577Z (11 days ago)
- Topics: fetch, middleware
- Language: TypeScript
- Homepage:
- Size: 2.28 MB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 19
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
### intro
a lightweight fetch lib with middleware inspired by koa
### quick start
```
yarn add @jarzzzi/rufetch
```### example
```javascript
const http = new Rufetch({
baseURL: 'https://github.com/api',
})const getUser = (name) => {
return http.get(`/user/${name}`)
}const getReps = (name, page) => {
return http.get(`/user/${name}`, {params: {page}})
}const createReps = (name, data) => {
return http.post(`/user/${name}`, {data})
}
```### middleware
- built in middleware
- cache
- fetch- error middleware
```javascript
http.use(async (ctx: any, next: any) => {
await next()
const {error} = ctx.resif (error?.status >= 400 && error?.status < 500) {
console.error('client error')
}
if (error?.status >= 500) {
console.error('server error')
}
if (error === 'fetch timeout') {
console.error('fetch timeout')
}
})
```
- token middleware
```javascript
http.use(async (ctx: any, next: any) => {
const token = await getToken()
ctx.req.requestInit.setHeaders(token, token)
await next()
})
```### api
- options
- baseURL
- timeout: default 15000
- params: url search params
- data: body alias
- responseType: default json, arrayBuffer, blob, json, text, formData, stream...
- cacheOptions
- cache: boolean
- expiredAge: default 5000
- body
- cache
- credentials
- headers
- integrity
- keepalive
- method
- mode
- redirect
- referrer
- referrerPolicy
- signal
- window
- new (options)
- instance.get
- instance.post
- instance.put
- instance.delete
- instance.head
- instance.use
- add middleware
- ctx
- req
- request
- res
- response