1.2.5 • Published 10 months ago

alloy-request v1.2.5

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months 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请求的数据object--
config请求的配置信息object--

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请求的数据object--
config请求的配置信息object--

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 optionsobject--

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.2.0

10 months ago

1.1.0

10 months ago

1.2.5

10 months ago

1.1.5

10 months ago

1.1.2

10 months ago

1.0.6

12 months ago

1.0.5

12 months ago

1.0.1

12 months ago

1.0.2

12 months ago