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;
|
errorHandler?: Function | null;
|
||||||
beforeSend?: Function | null;
|
beforeSend?: Function | null;
|
||||||
systemErrorHandler?: Function | null;
|
systemErrorHandler?: Function | null;
|
||||||
|
backupDomain?: string;
|
||||||
|
backupDomainEnableCallback?: Function;
|
||||||
}
|
}
|
||||||
export interface ICodeToSessionOptions {
|
export interface ICodeToSessionOptions {
|
||||||
url: string;
|
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 setParams(url: string | undefined, params: object): string;
|
||||||
|
declare function replaceDomain(url?: string, domain?: string): string;
|
||||||
declare const _default: {
|
declare const _default: {
|
||||||
setParams: typeof setParams;
|
setParams: typeof setParams;
|
||||||
|
replaceDomain: typeof replaceDomain;
|
||||||
};
|
};
|
||||||
export default _default;
|
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",
|
"name": "we-request",
|
||||||
"version": "1.6.8",
|
"version": "1.7.0",
|
||||||
"description": "本工具通过拓展小程序的wx.request,让开发者通过简单的配置,实现自动管理登录态等功能",
|
"description": "本工具通过拓展小程序的wx.request,让开发者通过简单的配置,实现自动管理登录态等功能",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"登录态",
|
"登录态",
|
||||||
|
|||||||
@@ -56,6 +56,10 @@ export interface IInitOption {
|
|||||||
beforeSend?: Function | null;
|
beforeSend?: Function | null;
|
||||||
/* 自定义系统错误处理函数 */
|
/* 自定义系统错误处理函数 */
|
||||||
systemErrorHandler?: Function | null;
|
systemErrorHandler?: Function | null;
|
||||||
|
/* 备用域名 */
|
||||||
|
backupDomain?: string;
|
||||||
|
/* 备用域名启用时回调函数 */
|
||||||
|
backupDomainEnableCallback?: Function;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ICodeToSessionOptions{
|
export interface ICodeToSessionOptions{
|
||||||
|
|||||||
@@ -87,6 +87,11 @@ function initializeRequestObj(obj: IRequestOption) {
|
|||||||
obj.url = url.setParams(obj.url, gd);
|
obj.url = url.setParams(obj.url, gd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 备用域名逻辑
|
||||||
|
if (status.isEnableBackupDomain && config.backupDomain) {
|
||||||
|
obj.url = url.replaceDomain(obj.url, config.backupDomain);
|
||||||
|
}
|
||||||
|
|
||||||
durationReporter.start(obj);
|
durationReporter.start(obj);
|
||||||
|
|
||||||
return obj;
|
return obj;
|
||||||
@@ -125,6 +130,11 @@ function initializeUploadFileObj(obj: IUploadFileOption) {
|
|||||||
obj.url = url.setParams(obj.url, gd);
|
obj.url = url.setParams(obj.url, gd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 备用域名逻辑
|
||||||
|
if (status.isEnableBackupDomain && config.backupDomain) {
|
||||||
|
obj.url = url.replaceDomain(obj.url, config.backupDomain);
|
||||||
|
}
|
||||||
|
|
||||||
durationReporter.start(obj);
|
durationReporter.start(obj);
|
||||||
|
|
||||||
return obj;
|
return obj;
|
||||||
@@ -152,6 +162,13 @@ function doRequest(obj: IRequestOption) {
|
|||||||
return resolve(res);
|
return resolve(res);
|
||||||
},
|
},
|
||||||
fail(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 });
|
return reject({ type: 'system-error', res });
|
||||||
},
|
},
|
||||||
complete() {
|
complete() {
|
||||||
@@ -178,6 +195,13 @@ function doUploadFile(obj: IUploadFileOption) {
|
|||||||
return resolve(res);
|
return resolve(res);
|
||||||
},
|
},
|
||||||
fail(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 });
|
return reject({ type: 'system-error', res });
|
||||||
},
|
},
|
||||||
complete() {
|
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 {
|
export default {
|
||||||
format,
|
format,
|
||||||
request,
|
request,
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
export default {
|
export default {
|
||||||
session: '' as string,
|
session: '' as string,
|
||||||
// session过期的时间点
|
// session过期的时间点
|
||||||
sessionExpire: Infinity as number
|
sessionExpire: Infinity as number,
|
||||||
|
// 是否启用备用域名
|
||||||
|
isEnableBackupDomain: false
|
||||||
} as any
|
} 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 {
|
export default {
|
||||||
setParams
|
setParams,
|
||||||
|
replaceDomain,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -12,4 +12,16 @@ describe("setParams", () => {
|
|||||||
sample = url.setParams(sample, { test2: 2 });
|
sample = url.setParams(sample, { test2: 2 });
|
||||||
expect(sample).toBe("https://www.example.com?test=1&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