applets-request-mode-list
小程序接口请求库
Introduction
基于Promise
,声明式创建接口请求对象集合,支持微信小程序、支付宝小程序、百度小程序、抖音/头条小程序
Features
- 支持 Promise API
- Interceptor request and response
- Transform request and response data
- Transform Config
- Cancel requests
- Automatic transforms for JSON data
- 在
TIMEOUT
和NETWORK_ERROR
错误下,默认会自动重试 2 次,每隔 2000ms 重试一次
Platform
- Wechat
- Alipay
- Swan
- Bytedance
Installing
Using npm:
npm install applets-request-mode-list
Using yarn:
yarn add applets-request-mode-list
Example
假如需要创建一个获取博客文章的 GET 请求:
首先,声明接口:
const apiList = {
getArticle: {
apiUrl: "/article/get",
method: "GET",
interval: 3000,
retryTimes: 2,
desc: "获取单篇文章",
},
};
然后,创建请求对象:
import ApiHttp from "applets-request-mode-list";
const apiHttp = new ApiHttp({
baseUrl: "https: //xxx.com",
apiList: apiList,
});
apiHttp.apis
.getArticle({
data: {
articleId: 1,
},
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
})
.then(function () {
});
声明接口
const apiList = {
functionName: {
apiUrl: string;
method: "POST" | "GET" | "PUT" | "post" | "get" | "put";
interval?: number;
retryTimes?: number;
desc?: string;
fnName?: string;
paramsTyping?: string;
dataTyping?: string;
resTyping?: string;
}
}
Api
ApiHttp
type: class
Methods And Properties
constructor(config, [appletsRequestConfig])
构造函数
config
Type: Object
Required
config.baseUrl
Type: string
config.apiLIst
Type: Object
接口声明
appletsRequestConfig
Type: Object<IAppletsRequestConfig>
Optional
constructor
Example
const apiList = {
getArticle: {
apiUrl: "/article/get",
method: "GET",
interval: 3000,
retryTimes: 2,
desc: "获取单篇文章",
},
};
const config = {
baseUrl: "https://xxx.com",
apiList: apiList,
};
const appletsRequestConfig = {
headers: {
"Content-Type": "application/json; charset=utf-8",
},
};
const apiHttp = new ApiHttp(config, appletsRequestConfig);
baseUrl
(Property)
Type: string
example: https://xxx.com
apis
(Property)
接口列表集合
Type: (reqConfig?: IAppletsRequestConfig): Promise<Data>
Return: Promise
reqConfig
Type: Object<IAppletsRequestConfig>
Optional
apis
Example
apiHttp.apis
.getArticle({
data: {
token: "token",
},
params: {
articleId: 1,
},
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
})
.then(function () {
});
appletsRequest
(Property)
Type: AppletsRequestInstance
一个类axios
的请求库
addRequestInterceptor(fulfilled, [rejected])
(Method)
添加request
拦截器,request
拦截器执行逻辑是先添加的后执行,是一个栈结构。支持异步操作。
fulfilled
-
Type: function
-
Return: any
功能和Promise.resolve
一样
reject
功能和Promise.reject
一样
addResponseInterceptor(fulfilled, [rejected])
(Method)
添加response
拦截器,response
拦截器执行逻辑是先添加的先执行,是一个队列结构。支持异步操作。
参数和addRequestInterceptor
一样
createRetryError(originalErr, [appletsRequestConfig])
(Method)
创建立即重试的错误信息,在Interceptor
中有效
createRetryError
Options
originalErr
原始错误对象
appletsRequestConfig
需要更新的请求数据
createRetryError(originalErr, [appletsRequestConfig])
Example
apiHttp.addResponseInterceptor((res) => {
return createRetryError(res, {
data: {
articleId: 2,
},
});
});
createCancelToken()
创建取消请求对象,用于特定情况取消请求
createCancelToken()
Example
const cancelToken = apiHttp.createCancelToken();
apiHttp.apis
.getArticle({
cancelToken,
data: {
token: "token",
},
params: {
articleId: 1,
},
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
})
.then(function () {
});
setTimeout(() => {
cancelToken.cancel("cancel reason");
}, 100);
addApiList(apiList)
往原有的接口列表中新添加接口
addApiList(apiList)
Example
const newApiList = {
getArticles: {
apiUrl: "/articles/get",
method: "GET",
interval: 3000,
retryTimes: 2,
desc: "获取文章列表",
},
};
apiHttp.addApiList(newApiList);
apiHttp.apis
.getArticles()
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
})
.then(function () {
});