wx-request-axios
v2.0.4
Published
微信小程序请求封装
Downloads
8
Readme
wx-request-axios
安装
npm i wx-request-axios
简单使用
import { wxRequest } from "wx-request-axios";
wxRequest.request(config);
wxRequest.get(url[, data[, config]]);
wxRequest.post...
使用
import { create } from "wx-request-axios";
// 设置默认参数
const http = create({
baseUrl: "http://www.baidu.com/",
timeout: 2000,
});
拦截器
// 请求拦截器
http.interceptors.request.use((config) => {
return config;
});
// 响应拦截器
http.interceptors.response.use((res) => {
// 自定义错误
if (res.code !== 200) {
return Promise.reject(new Error("Request Error"));
};
return res;
}, (error) => {
const { message, response, config } = error;
return Promise.reject(error);
});
请求
http.request(config)
http.get(url[, data[, config]])
http.post(url[, data[, config]])
http.put(url[, data[, config]])
http.delete(url[, config])
http.head(url[, config])
http.options(url[, config])
http.connect(url[, config])
Typescript
扩充 config 字段示例
import "wx-request-axios";
declare module "wx-request-axios" {
export interface RequestOptions {
/** 显示加载 */
loading?: boolean;
}
}