1.0.6 • Published 13 days ago

alloy-request v1.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
13 days ago

alloy-request

alloy-request 基于axiosahooks的useRequest封装的 react 请求库

安装

alloy-request 依赖 axiosahooks的useRequest,使用前请先安装 3.0 版本以上的ahooksaxios

npm install alloy-request
yarn add alloy-request
pnpm install alloy-request

初始化实例 API

参数描述类型是否必填默认值
request实例 request 拦截器(req: AxiosRequestConfig) => AxiosRequestConfig--
response实例 response 拦截器(req: AxiosResponse) => AxiosResponse--

demo

import { initInstance } from 'alloy-request'

initInstance()
import { initInstance } from 'alloy-request'

initInstance(
  (request) => {
    request.baseURL = 'http://127.0.0.1:80'
    return request
  },
  (response) => {
    return response.data
  }
)

GET API

参数描述类型是否必填默认值
url请求的地址string--
data请求的数据Record<string, any>--
config请求的配置信息Record<string, any>--

demo

import { get } from 'alloy-request';

async function getData() {
  try {
    const res = await get('api-url', { query: 'keyword' }, {})
    console.log(res)
  } catch(error){}
}

POST API

参数描述类型是否必填默认值
url请求的地址string--
data请求的数据Record<string, any>--
config请求的配置信息Record<string, any>--

demo

import { post } from 'alloy-request';

async function getData() {
  try {
    const res = await post('api-url', { name: 'name' }, {})
  } catch(error){}
}

useAxios API

基于 ahooks useRequest 封装的参数请求 在 hooks 中使用 使用方法参照useRequest

参数描述类型是否必填默认值
url请求的地址string--
serviceuseRequest server(...args: TParams) => Promise<TData>--
optionsuseRequest optionsRecord<string, any>--

demo

import { useAxios } from 'alloy-request';

const { runAsync } =  useAxios('/getDemo', () => ({
  method: 'post',
  payload: { name: '' },
  }), {
  manual: true,
    formatResult: (res) => {
      return res
    },
    onSuccess: (res) => {
      console.log(res)
    }
 })
1.0.6

13 days ago

1.0.5

13 days ago

1.0.1

13 days ago

1.0.2

13 days ago