@microbt/environment 1.2.9 → 1.2.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -6,6 +6,14 @@ export declare const SUPPORTED_DOMAINS: string[];
|
|
|
6
6
|
* @throws 如果域名不在支持列表中
|
|
7
7
|
*/
|
|
8
8
|
export declare function validateDomain(domain: string): string;
|
|
9
|
+
/**
|
|
10
|
+
* 验证传入的域名是否有效
|
|
11
|
+
* 对于支持 region 的域名,必须是包含环境信息的完整域名(如二级域名)
|
|
12
|
+
* 对于不支持 region 的域名,根域名也是有效的
|
|
13
|
+
* @param domain 要验证的域名
|
|
14
|
+
* @returns 是否为有效域名
|
|
15
|
+
*/
|
|
16
|
+
export declare function validateDomainWithRegion(domain: string): boolean;
|
|
9
17
|
export declare function environment(): string;
|
|
10
18
|
export declare function region(): string;
|
|
11
19
|
export declare function isAWSDomain(): boolean;
|
|
@@ -20,6 +20,22 @@ function validateDomain(domain) {
|
|
|
20
20
|
}
|
|
21
21
|
return validDomain;
|
|
22
22
|
}
|
|
23
|
+
function validateDomainWithRegion(domain) {
|
|
24
|
+
try {
|
|
25
|
+
const validBaseDomain = validateDomain(domain);
|
|
26
|
+
const strategy = EnvironmentStrategyFactory.getStrategy(validBaseDomain);
|
|
27
|
+
if (!strategy.features.supportRegion) {
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
if (domain === validBaseDomain) {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
const extractedRegion = strategy.getRegion(domain);
|
|
34
|
+
return extractedRegion !== "unknown";
|
|
35
|
+
} catch (e) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
23
39
|
function getCurrentDomain() {
|
|
24
40
|
if (typeof location === "undefined") {
|
|
25
41
|
throw new Error("location is not defined");
|
|
@@ -62,5 +78,6 @@ export {
|
|
|
62
78
|
environment,
|
|
63
79
|
isAWSDomain,
|
|
64
80
|
region,
|
|
65
|
-
validateDomain
|
|
81
|
+
validateDomain,
|
|
82
|
+
validateDomainWithRegion
|
|
66
83
|
};
|
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
// src/gateway/index.ts
|
|
2
|
-
import {
|
|
2
|
+
import { EnvironmentStrategyFactory } from "../environment/factory";
|
|
3
|
+
import {
|
|
4
|
+
environment,
|
|
5
|
+
isAWSDomain,
|
|
6
|
+
region,
|
|
7
|
+
validateDomain,
|
|
8
|
+
validateDomainWithRegion
|
|
9
|
+
} from "../environment/index";
|
|
3
10
|
import { URLStrategyFactory } from "./factory";
|
|
4
11
|
function getCurrentDomain() {
|
|
5
12
|
const host = location.host;
|
|
@@ -10,24 +17,40 @@ function baseUrl(params) {
|
|
|
10
17
|
if (!params.name) {
|
|
11
18
|
throw new Error("name parameter is required");
|
|
12
19
|
}
|
|
13
|
-
|
|
20
|
+
let domain;
|
|
21
|
+
if (params.domain) {
|
|
22
|
+
if (!validateDomainWithRegion(params.domain)) {
|
|
23
|
+
const baseDomain = validateDomain(params.domain);
|
|
24
|
+
const strategy2 = EnvironmentStrategyFactory.getStrategy(baseDomain);
|
|
25
|
+
if (strategy2.features.supportRegion) {
|
|
26
|
+
throw new Error(
|
|
27
|
+
`Domain '${params.domain}' requires region information. Cannot determine region from root domain. Please provide a subdomain like 'api-cn-dev.${params.domain}' instead.`
|
|
28
|
+
);
|
|
29
|
+
} else {
|
|
30
|
+
throw new Error(`Invalid domain '${params.domain}'.`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
domain = validateDomain(params.domain);
|
|
34
|
+
} else {
|
|
35
|
+
domain = getCurrentDomain();
|
|
36
|
+
}
|
|
14
37
|
const strategy = URLStrategyFactory.getStrategy(domain);
|
|
15
38
|
const urlParams = {
|
|
16
39
|
name: params.name,
|
|
17
40
|
path: params.path || ""
|
|
18
41
|
};
|
|
19
42
|
if (strategy.features.supportEnv) {
|
|
20
|
-
const env = environment();
|
|
43
|
+
const env = params.domain ? EnvironmentStrategyFactory.getStrategy(domain).getEnvironment(params.domain) : environment();
|
|
21
44
|
if (env === "unknown") {
|
|
22
45
|
return params.path;
|
|
23
46
|
}
|
|
24
47
|
urlParams.env = env;
|
|
25
48
|
}
|
|
26
49
|
if (strategy.features.supportRegion) {
|
|
27
|
-
urlParams.region = region();
|
|
50
|
+
urlParams.region = params.domain ? EnvironmentStrategyFactory.getStrategy(domain).getRegion(params.domain) : region();
|
|
28
51
|
}
|
|
29
52
|
if (strategy.features.supportAWS) {
|
|
30
|
-
urlParams.isAWS = isAWSDomain();
|
|
53
|
+
urlParams.isAWS = params.domain ? EnvironmentStrategyFactory.getStrategy(domain).isAWS(params.domain) : isAWSDomain();
|
|
31
54
|
}
|
|
32
55
|
return strategy.generateURL(urlParams);
|
|
33
56
|
} catch (error) {
|