alloy-request 基于`axios`和`ahooks的useRequest`封装的 react 请求库
npm install alloy-requestalloy-request 基于axios和ahooks的useRequest封装的 react 请求库
alloy-request 依赖 axios 和 ahooks的useRequest,使用前请先安装 3.0 版本以上的ahooks和axios
``sh`
npm install alloy-request
yarn add alloy-request
pnpm install alloy-request
| 参数 | 描述 | 类型 | 是否必填 | 默认值 |
| -------- | -------------------- | ------------------------------------------------- | -------- | ------ |
| request | 实例 request 拦截器 | (req: AxiosRequestConfig) => AxiosRequestConfig | 否 | -- |(req: AxiosResponse) => AxiosResponse
| response | 实例 response 拦截器 | | 否 | -- |
`tsx
import { initInstance } from 'alloy-request'
initInstance()
`
`tsx
import { initInstance } from 'alloy-request'
initInstance(
(request) => {
request.baseURL = 'http://127.0.0.1:80'
return request
},
(response) => {
return response.data
}
)
`
| 参数 | 描述 | 类型 | 是否必填 | 默认值 |
| ------ | -------------- | -------- | -------- | ------ |
| url | 请求的地址 | string | 是 | -- |object
| data | 请求的数据 | | 否 | -- |object
| config | 请求的配置信息 | | 否 | -- |
`tsx
import { get } from 'alloy-request';
async function getData() {
try {
const res = await get('api-url', { query: 'keyword' }, {})
console.log(res)
} catch(error){}
}
`
| 参数 | 描述 | 类型 | 是否必填 | 默认值 |
| ------ | -------------- | -------- | -------- | ------ |
| url | 请求的地址 | string | 是 | -- |object
| data | 请求的数据 | | 否 | -- |object
| config | 请求的配置信息 | | 否 | -- |
`tsx
import { post } from 'alloy-request';
async function getData() {
try {
const res = await post('api-url', { name: 'name' }, {})
} catch(error){}
}
`
基于 ahooks useRequest 封装的参数请求 在 hooks 中使用 使用方法参照useRequest
| 参数 | 描述 | 类型 | 是否必填 | 默认值 |
| --- | --- | --- | --- | --- |
| url | 请求的地址 | string | 是 | -- |(...args: TParams) => Promise
| service | useRequest server | | 是 | -- |object
| options | useRequest options | | 否 | -- |
`tsx
import { useAxios } from 'alloy-request';
const { runAsync } = useAxios('/getDemo', () => ({
method: 'post',
payload: { name: '' },
}), {
manual: true,
formatResult: (res) => {
return res
},
onSuccess: (res) => {
console.log(res)
}
})
``