@4a/http
v0.6.0
Published
简单易用的http接口扩展工具
Downloads
27
Readme
HTTP
peerDependencies
- axios@^1.2.0
Install
npm i @4a/http
pnpm add @4a/http
Usage
import { createHttp } from '@4a/http'
type AnyFunction<T> = (...args: any[]) => T
type API = Record<string, AnyFunction<AxiosRequestConfig>>
createHttp(api: API, opt?: AxiosRequestConfig)
// api.ts
// Example
export const api = {
ok(params: Item): AxiosRequestConfig {
return {
params,
method: 'GET',
url: '/ok',
}
}
}
const http = createHttp(api)
const http = createHttp(api, { timeout: 5000 })
http.ok({ a: 1 })
More
export function createFactory(config: any) {
return createHttp(api(config), {
headers: {
'Content-Type': 'application/json',
},
transformRequest(data: any) {
return JSON.stringify(signed(data, config.secret))
}
})
}