feat(备用域名): 新增backupDomain配置支持备份域名 (#71)
This commit is contained in:
2
build/interface.d.ts
vendored
2
build/interface.d.ts
vendored
@@ -24,6 +24,8 @@ export interface IInitOption {
|
||||
errorHandler?: Function | null;
|
||||
beforeSend?: Function | null;
|
||||
systemErrorHandler?: Function | null;
|
||||
backupDomain?: string;
|
||||
backupDomainEnableCallback?: Function;
|
||||
}
|
||||
export interface ICodeToSessionOptions {
|
||||
url: string;
|
||||
|
||||
2
build/util/url.d.ts
vendored
2
build/util/url.d.ts
vendored
@@ -1,5 +1,7 @@
|
||||
declare function setParams(url: string | undefined, params: object): string;
|
||||
declare function replaceDomain(url?: string, domain?: string): string;
|
||||
declare const _default: {
|
||||
setParams: typeof setParams;
|
||||
replaceDomain: typeof replaceDomain;
|
||||
};
|
||||
export default _default;
|
||||
|
||||
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
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "we-request",
|
||||
"version": "1.6.8",
|
||||
"version": "1.7.0",
|
||||
"description": "本工具通过拓展小程序的wx.request,让开发者通过简单的配置,实现自动管理登录态等功能",
|
||||
"keywords": [
|
||||
"登录态",
|
||||
|
||||
@@ -56,6 +56,10 @@ export interface IInitOption {
|
||||
beforeSend?: Function | null;
|
||||
/* 自定义系统错误处理函数 */
|
||||
systemErrorHandler?: Function | null;
|
||||
/* 备用域名 */
|
||||
backupDomain?: string;
|
||||
/* 备用域名启用时回调函数 */
|
||||
backupDomainEnableCallback?: Function;
|
||||
}
|
||||
|
||||
export interface ICodeToSessionOptions{
|
||||
|
||||
@@ -87,6 +87,11 @@ function initializeRequestObj(obj: IRequestOption) {
|
||||
obj.url = url.setParams(obj.url, gd);
|
||||
}
|
||||
|
||||
// 备用域名逻辑
|
||||
if (status.isEnableBackupDomain && config.backupDomain) {
|
||||
obj.url = url.replaceDomain(obj.url, config.backupDomain);
|
||||
}
|
||||
|
||||
durationReporter.start(obj);
|
||||
|
||||
return obj;
|
||||
@@ -125,6 +130,11 @@ function initializeUploadFileObj(obj: IUploadFileOption) {
|
||||
obj.url = url.setParams(obj.url, gd);
|
||||
}
|
||||
|
||||
// 备用域名逻辑
|
||||
if (status.isEnableBackupDomain && config.backupDomain) {
|
||||
obj.url = url.replaceDomain(obj.url, config.backupDomain);
|
||||
}
|
||||
|
||||
durationReporter.start(obj);
|
||||
|
||||
return obj;
|
||||
@@ -152,6 +162,13 @@ function doRequest(obj: IRequestOption) {
|
||||
return resolve(res);
|
||||
},
|
||||
fail(res) {
|
||||
// 如果主域名不可用,且配置了备份域名,且本次请求未使用备份域名
|
||||
if (res?.errMsg?.indexOf('CONNECTION_REFUSED') >= 0 && config.backupDomain && obj.url.indexOf(config.backupDomain) < 0) {
|
||||
// 开启备份域名
|
||||
enableBackupDomain();
|
||||
// 重试一次
|
||||
return doRequest(obj).then((res)=> resolve(res));
|
||||
}
|
||||
return reject({ type: 'system-error', res });
|
||||
},
|
||||
complete() {
|
||||
@@ -178,6 +195,13 @@ function doUploadFile(obj: IUploadFileOption) {
|
||||
return resolve(res);
|
||||
},
|
||||
fail(res) {
|
||||
// 如果主域名不可用,且配置了备份域名,且本次请求未使用备份域名
|
||||
if (res?.errMsg?.indexOf('CONNECTION_REFUSED') >= 0 && config.backupDomain && obj.url.indexOf(config.backupDomain) < 0) {
|
||||
// 开启备份域名
|
||||
enableBackupDomain();
|
||||
// 重试一次
|
||||
return doUploadFile(obj).then((res)=> resolve(res));
|
||||
}
|
||||
return reject({ type: 'system-error', res });
|
||||
},
|
||||
complete() {
|
||||
@@ -246,6 +270,15 @@ function uploadFile(obj: IUploadFileOption): any {
|
||||
})
|
||||
}
|
||||
|
||||
function enableBackupDomain() {
|
||||
if (!status.isEnableBackupDomain) {
|
||||
status.isEnableBackupDomain = true;
|
||||
if (typeof config.backupDomainEnableCallback === 'function') {
|
||||
config.backupDomainEnableCallback();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
format,
|
||||
request,
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
export default {
|
||||
session: '' as string,
|
||||
// session过期的时间点
|
||||
sessionExpire: Infinity as number
|
||||
sessionExpire: Infinity as number,
|
||||
// 是否启用备用域名
|
||||
isEnableBackupDomain: false
|
||||
} as any
|
||||
|
||||
@@ -25,6 +25,13 @@ function setParams(url: string = "", params: object) {
|
||||
}
|
||||
}
|
||||
|
||||
function replaceDomain(url: string = "", domain: string = "") {
|
||||
// 保证domain只包含域名,没有 http(s) 前缀 和 / 后缀
|
||||
domain = domain.replace(/^http(s)?:\/\//, '').replace(/\/$/, '');
|
||||
return url.replace(/^http(s)?:\/\/(.*?)\//, `https://${domain}/`);
|
||||
}
|
||||
|
||||
export default {
|
||||
setParams
|
||||
setParams,
|
||||
replaceDomain,
|
||||
};
|
||||
|
||||
@@ -12,4 +12,16 @@ describe("setParams", () => {
|
||||
sample = url.setParams(sample, { test2: 2 });
|
||||
expect(sample).toBe("https://www.example.com?test=1&test2=2");
|
||||
});
|
||||
|
||||
test("replace domain with host", () => {
|
||||
let sample = "https://example.com/dosomething";
|
||||
sample = url.replaceDomain(sample, "example2.com");
|
||||
expect(sample).toBe("https://example2.com/dosomething");
|
||||
})
|
||||
|
||||
test("replace domain with origin", () => {
|
||||
let sample = "https://example.com/dosomething";
|
||||
sample = url.replaceDomain(sample, "https://example2.com/");
|
||||
expect(sample).toBe("https://example2.com/dosomething");
|
||||
})
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user