0.3.3 • Published 11 months ago
onefetch v0.3.3
install
npm i onefetch
to 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
11 months ago
0.0.10
1 year ago
0.0.11
1 year ago
0.0.12
1 year ago
0.2.10
1 year ago
0.1.0
1 year ago
0.2.1
1 year ago
0.1.2
1 year ago
0.2.0
1 year ago
0.1.1
1 year ago
0.2.7
1 year ago
0.2.6
1 year ago
0.0.8
1 year ago
0.2.9
1 year ago
0.2.8
1 year ago
0.3.2
1 year ago
0.2.3
1 year ago
0.1.4
1 year ago
0.0.5
1 year ago
0.3.1
1 year ago
0.2.2
1 year ago
0.1.3
1 year ago
0.2.5
1 year ago
0.0.7
1 year ago
0.2.4
1 year ago
0.1.5
1 year ago
0.0.6
1 year ago
0.0.4
1 year ago
0.0.3
1 year ago
0.0.2
1 year ago
0.0.1
1 year ago