0.3.3 • Published 2 years ago
onefetch v0.3.3
install
npm i onefetchto use
import { addInterceptor, createFetch } from 'onefetch'
//Interceptor
addInterceptor({
request(req) {
return req
},
response(res) {
console.log('extendParams:', res.config.extendParams)
return res
},
error(err) {
return err
},
})
//get a request
createFetch(
'/user/12345',
{
data: {},
method: 'POST',
headers: { token: '' },
responseType: 'json',
extendParams: true, // your custom parameters
// credentials: 'same-origin', // to see fetch credentials
// cache: 'default', // to see fetch cache
// noContentType: false, //boolean remove content-type
},
6000, //timeout
)
.then(res => console.log(res))
.catch(err => console.log(err))js example
import { addInterceptor, createFetch } from 'onefetch'
const BaseUrl = ''
const Timeout = 6000
addInterceptor({
request(req) {
return req
},
response(res) {
return res
},
error(err) {
return err
},
})
//responseType -> text json blob arrayBuffer
//credentials -> to see fetch credentials
//cache -> to see fetch cache
//noContentType -> boolean
export default function request(obj) {
const { url, ...options } = obj
return createFetch(BaseUrl + url, options, Timeout)
}ts example
import { addInterceptor, createFetch, RequestInitExt, FetchResponseType } from 'onefetch'
const BaseUrl = ''
const Timeout = 6000
export interface RequestObj<T> {
data: T
method: string
url: string
headers?: Record<string, string>
responseType?: FetchResponseType //text json blob arrayBuffer
credentials?: RequestCredentials | undefined
cache?: RequestCache
noContentType?: boolean
}
addInterceptor({
request(req: RequestInitExt): RequestInitExt {
return req
},
response(res: any): any {
return res
},
error(err: any) {
return err
},
})
export default function request(obj: RequestObj<any>): Promise<any> {
const { url, ...options } = obj
return createFetch(BaseUrl + url, options, Timeout)
}0.3.3
2 years ago
0.0.10
3 years ago
0.0.11
3 years ago
0.0.12
3 years ago
0.2.10
3 years ago
0.1.0
3 years ago
0.2.1
3 years ago
0.1.2
3 years ago
0.2.0
3 years ago
0.1.1
3 years ago
0.2.7
3 years ago
0.2.6
3 years ago
0.0.8
3 years ago
0.2.9
3 years ago
0.2.8
3 years ago
0.3.2
2 years ago
0.2.3
3 years ago
0.1.4
3 years ago
0.0.5
3 years ago
0.3.1
3 years ago
0.2.2
3 years ago
0.1.3
3 years ago
0.2.5
3 years ago
0.0.7
3 years ago
0.2.4
3 years ago
0.1.5
3 years ago
0.0.6
3 years ago
0.0.4
3 years ago
0.0.3
3 years ago
0.0.2
3 years ago
0.0.1
3 years ago