主要目的是能够实现拦截器,以及对请求的统一处理,比如请求头的添加,请求参数的添加,请求的统一处理等等。
现在已经实现请求拦截、响应拦截、错误拦截
npm
npm install fetra --save
yarn
yarn add fetra
pnpm
pnpm add fetra
import {createFetra} from 'fetra'
const fetra = createFetra()
fetra("/api/user").then(res => {
console.log(res)
})
import {createFetra, RequestInterceptor} from 'fetra'
const fetra = createFetra()
const reqInterceptor: RequestInterceptor = async (input: RequestInfo | URL, init: RequestInit, fetcher: Fetcher) => {
// do something
return [input, {...init, headers: {...init.headers, 'Content-Type': 'application/json'}}]
}
fetra.request.add(reqInterceptor)
import {createFetra, ResponseInterceptor} from 'fetra'
const fetra = createFetra()
const respInterceptor: ResponseInterceptor = async (response: Response, input: RequestInfo | URL, init: RequestInit, fetcher: Fetcher) => {
// do something
return response
}
fetra.response.add(respInterceptor)
import {createFetra, ErrorInterceptor} from 'fetra'
const fetra = createFetra()
const errorInterceptor: ErrorInterceptor = async (error: unknown, input: RequestInfo | URL, init: RequestInit, response: Response | null, fetcher: Fetcher) => {
// do something
return response
}
import {createFetra, ErrorInterceptor, wrapRestful} from 'fetra'
const fetra = wrapRestful(createFetra())
//新增了get,post,put,delete,patch方法
fetch.get("/api/user").then(res => {
console.log(res)
})