1.0.6 • Published 2 years ago

solo-request v1.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

solo-request

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

安装

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

npm install solo-request
yarn add solo-request
pnpm install solo-request

初始化实例 API

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

demo

import { initInstance } from 'solo-request'

initInstance()
import { initInstance } from 'solo-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 'solo-request';

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

POST API

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

demo

import { psot } from 'solo-request';

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

useAxios API

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

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

demo

import { psot } from 'solo-request';

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

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago