feat: 支持使用方自己判断错误是否进行域名降级 (#77)
* feat: 支持使用方自己判断错误是否进行域名降级 * feat: 增加默认函数 * feat: 补充 code2session 重试
This commit is contained in:
6
build/interface.d.ts
vendored
6
build/interface.d.ts
vendored
@@ -1,6 +1,7 @@
|
|||||||
/// <reference types="miniprogram-api-typings" />
|
/// <reference types="miniprogram-api-typings" />
|
||||||
export declare type Request = <TResp>(options: IRequestOption) => Promise<TResp>;
|
/// <reference types="miniprogram-api-typings" />
|
||||||
export declare type IAnyObject = WechatMiniprogram.IAnyObject;
|
export type Request = <TResp>(options: IRequestOption) => Promise<TResp>;
|
||||||
|
export type IAnyObject = WechatMiniprogram.IAnyObject;
|
||||||
export interface IInitOption {
|
export interface IInitOption {
|
||||||
codeToSession: ICodeToSessionOptions;
|
codeToSession: ICodeToSessionOptions;
|
||||||
sessionName: string;
|
sessionName: string;
|
||||||
@@ -26,6 +27,7 @@ export interface IInitOption {
|
|||||||
systemErrorHandler?: Function | null;
|
systemErrorHandler?: Function | null;
|
||||||
backupDomainList?: IAnyObject;
|
backupDomainList?: IAnyObject;
|
||||||
backupDomainEnableCallback?: Function;
|
backupDomainEnableCallback?: Function;
|
||||||
|
domainChangeTrigger?: Function;
|
||||||
}
|
}
|
||||||
export interface ICodeToSessionOptions {
|
export interface ICodeToSessionOptions {
|
||||||
url: string;
|
url: string;
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
4
build/weRequest.min.js
vendored
4
build/weRequest.min.js
vendored
File diff suppressed because one or more lines are too long
12764
package-lock.json
generated
12764
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "we-request",
|
"name": "we-request",
|
||||||
"version": "1.7.3",
|
"version": "1.7.4",
|
||||||
"description": "本工具通过拓展小程序的wx.request,让开发者通过简单的配置,实现自动管理登录态等功能",
|
"description": "本工具通过拓展小程序的wx.request,让开发者通过简单的配置,实现自动管理登录态等功能",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"登录态",
|
"登录态",
|
||||||
@@ -44,7 +44,7 @@
|
|||||||
"ts-loader": "^5.3.1",
|
"ts-loader": "^5.3.1",
|
||||||
"tslint": "^5.12.0",
|
"tslint": "^5.12.0",
|
||||||
"tslint-config-prettier": "^1.17.0",
|
"tslint-config-prettier": "^1.17.0",
|
||||||
"typescript": "^3.7.4",
|
"typescript": "^4.9.5",
|
||||||
"webpack": "^4.28.0",
|
"webpack": "^4.28.0",
|
||||||
"webpack-cli": "^3.3.12"
|
"webpack-cli": "^3.3.12"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -60,6 +60,8 @@ export interface IInitOption {
|
|||||||
backupDomainList?: IAnyObject;
|
backupDomainList?: IAnyObject;
|
||||||
/* 备用域名启用时回调函数 */
|
/* 备用域名启用时回调函数 */
|
||||||
backupDomainEnableCallback?: Function;
|
backupDomainEnableCallback?: Function;
|
||||||
|
/* 是否需要启用备用域名 */
|
||||||
|
domainChangeTrigger?: Function;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ICodeToSessionOptions{
|
export interface ICodeToSessionOptions{
|
||||||
|
|||||||
@@ -159,7 +159,7 @@ function doRequest(obj: IRequestOption) {
|
|||||||
},
|
},
|
||||||
fail(res) {
|
fail(res) {
|
||||||
// 如果主域名不可用,且配置了备份域名,且本次请求未使用备份域名
|
// 如果主域名不可用,且配置了备份域名,且本次请求未使用备份域名
|
||||||
if ((res?.errMsg?.indexOf('CONNECTION_REFUSED') >= 0 || res?.errMsg?.indexOf('ERR_CONNECTION_RESET') >= 0) && url.isInBackupDomainList(obj.url)) {
|
if ((config.domainChangeTrigger && config.domainChangeTrigger(res)) && url.isInBackupDomainList(obj.url)) {
|
||||||
// 开启备份域名
|
// 开启备份域名
|
||||||
enableBackupDomain(obj.url);
|
enableBackupDomain(obj.url);
|
||||||
// 重试一次
|
// 重试一次
|
||||||
@@ -192,7 +192,7 @@ function doUploadFile(obj: IUploadFileOption) {
|
|||||||
},
|
},
|
||||||
fail(res) {
|
fail(res) {
|
||||||
// 如果主域名不可用,且配置了备份域名,且本次请求未使用备份域名
|
// 如果主域名不可用,且配置了备份域名,且本次请求未使用备份域名
|
||||||
if ((res?.errMsg?.indexOf('CONNECTION_REFUSED') >= 0 || res?.errMsg?.indexOf('ERR_CONNECTION_RESET') >= 0) && url.isInBackupDomainList(obj.url)) {
|
if ((config.domainChangeTrigger && config.domainChangeTrigger(res)) && url.isInBackupDomainList(obj.url)) {
|
||||||
// 开启备份域名
|
// 开启备份域名
|
||||||
enableBackupDomain(obj.url);
|
enableBackupDomain(obj.url);
|
||||||
// 重试一次
|
// 重试一次
|
||||||
|
|||||||
@@ -206,7 +206,7 @@ async function code2Session(code: string) {
|
|||||||
},
|
},
|
||||||
fail: (res) => {
|
fail: (res) => {
|
||||||
// 如果主域名不可用,且配置了备份域名,且本次请求未使用备份域名
|
// 如果主域名不可用,且配置了备份域名,且本次请求未使用备份域名
|
||||||
if ((res?.errMsg?.indexOf('CONNECTION_REFUSED') >= 0 || res?.errMsg?.indexOf('ERR_CONNECTION_RESET') >= 0) && url.isInBackupDomainList(obj.url)) {
|
if ((config.domainChangeTrigger && config.domainChangeTrigger(res)) && url.isInBackupDomainList(obj.url)) {
|
||||||
// 开启备份域名
|
// 开启备份域名
|
||||||
requestHandler.enableBackupDomain(obj.url);
|
requestHandler.enableBackupDomain(obj.url);
|
||||||
// 重试一次
|
// 重试一次
|
||||||
|
|||||||
@@ -33,6 +33,14 @@ const defaultConfig: IInitOption = {
|
|||||||
beforeSend: null,
|
beforeSend: null,
|
||||||
// 自定义系统错误处理函数(网络错误)
|
// 自定义系统错误处理函数(网络错误)
|
||||||
systemErrorHandler: null,
|
systemErrorHandler: null,
|
||||||
|
// 默认降级处理函数
|
||||||
|
domainChangeTrigger: (res: WechatMiniprogram.GeneralCallbackResult) => {
|
||||||
|
// -101 和 -102 默认自动降级
|
||||||
|
if ((res?.errMsg?.indexOf('CONNECTION_REFUSED') >= 0 || res?.errMsg?.indexOf('ERR_CONNECTION_RESET') >= 0)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export default defaultConfig;
|
export default defaultConfig;
|
||||||
|
|||||||
Reference in New Issue
Block a user