@microbt/environment 1.2.10 → 1.2.11
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.
|
@@ -1,19 +1,4 @@
|
|
|
1
1
|
export declare const SUPPORTED_DOMAINS: string[];
|
|
2
|
-
/**
|
|
3
|
-
* 验证域名是否在支持列表中
|
|
4
|
-
* @param domain 要验证的域名
|
|
5
|
-
* @returns 有效的域名
|
|
6
|
-
* @throws 如果域名不在支持列表中
|
|
7
|
-
*/
|
|
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;
|
|
17
2
|
export declare function environment(): string;
|
|
18
3
|
export declare function region(): string;
|
|
19
4
|
export declare function isAWSDomain(): boolean;
|
|
@@ -13,35 +13,16 @@ var SUPPORTED_DOMAINS = [
|
|
|
13
13
|
"127.0.0.1",
|
|
14
14
|
"localhost"
|
|
15
15
|
];
|
|
16
|
-
function validateDomain(domain) {
|
|
17
|
-
const validDomain = SUPPORTED_DOMAINS.find((d) => domain.endsWith(d));
|
|
18
|
-
if (!validDomain) {
|
|
19
|
-
throw new Error(`Unsupported domain: ${domain}`);
|
|
20
|
-
}
|
|
21
|
-
return validDomain;
|
|
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
|
-
}
|
|
39
16
|
function getCurrentDomain() {
|
|
40
17
|
if (typeof location === "undefined") {
|
|
41
18
|
throw new Error("location is not defined");
|
|
42
19
|
}
|
|
43
20
|
const host = location.host;
|
|
44
|
-
|
|
21
|
+
const domain = SUPPORTED_DOMAINS.find((d) => host.endsWith(d));
|
|
22
|
+
if (!domain) {
|
|
23
|
+
throw new Error(`Unsupported domain: ${host}`);
|
|
24
|
+
}
|
|
25
|
+
return domain;
|
|
45
26
|
}
|
|
46
27
|
function environment() {
|
|
47
28
|
try {
|
|
@@ -77,7 +58,5 @@ export {
|
|
|
77
58
|
SUPPORTED_DOMAINS,
|
|
78
59
|
environment,
|
|
79
60
|
isAWSDomain,
|
|
80
|
-
region
|
|
81
|
-
validateDomain,
|
|
82
|
-
validateDomainWithRegion
|
|
61
|
+
region
|
|
83
62
|
};
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* 根据当前域名生成基于 name 和 path 的 baseUrl
|
|
3
3
|
*
|
|
4
4
|
* @param params 配置参数
|
|
5
5
|
* @param params.name - 必填,API 服务名称(例如:'api', 'iot-api')
|
|
6
6
|
* @param params.path - 可选,API 路径(例如:'/saas')
|
|
7
|
-
* @param params.domain - 可选,指定域名(必须符合SUPPORTED_DOMAINS约束)
|
|
8
7
|
* @returns 生成的 URL。如果生成失败则返回传入的 path 值
|
|
9
8
|
*
|
|
10
9
|
* @example
|
|
@@ -12,7 +11,6 @@
|
|
|
12
11
|
* // 中国区开发环境
|
|
13
12
|
* baseUrl({ name: 'api' }) // => '//api-cn-dev.superacme.com'
|
|
14
13
|
* baseUrl({ name: 'iot-api', path: '/saas' }) // => '//iot-api-cn-dev.superacme.com/saas'
|
|
15
|
-
* baseUrl({ name: 'api', domain: 'superacme.com' }) // => '//api-cn-dev.superacme.com'
|
|
16
14
|
*
|
|
17
15
|
* // 美区 AWS 环境
|
|
18
16
|
* // location.host: 'mall-us-aws.superacme.com'
|
|
@@ -21,7 +19,6 @@
|
|
|
21
19
|
* // Safio365 域名系列
|
|
22
20
|
* // location.host: 'iot-dev.safio365.com'
|
|
23
21
|
* baseUrl({ name: 'iot-api', path: '/saas' }) // => '//iot-api-dev.safio365.com/saas'
|
|
24
|
-
* baseUrl({ name: 'iot-api', domain: 'safio365.com' }) // => '//iot-api-dev.safio365.com/saas'
|
|
25
22
|
*
|
|
26
23
|
* // Sensforge 域名系列
|
|
27
24
|
* // location.host: 'api-dev.sensforge.com'
|
|
@@ -33,5 +30,4 @@
|
|
|
33
30
|
export declare function baseUrl(params: {
|
|
34
31
|
name: string;
|
|
35
32
|
path?: string;
|
|
36
|
-
domain?: string;
|
|
37
33
|
}): string | undefined;
|
|
@@ -1,56 +1,37 @@
|
|
|
1
1
|
// src/gateway/index.ts
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
environment,
|
|
5
|
-
isAWSDomain,
|
|
6
|
-
region,
|
|
7
|
-
validateDomain,
|
|
8
|
-
validateDomainWithRegion
|
|
9
|
-
} from "../environment/index";
|
|
2
|
+
import { environment, isAWSDomain, region, SUPPORTED_DOMAINS } from "../environment/index";
|
|
10
3
|
import { URLStrategyFactory } from "./factory";
|
|
11
4
|
function getCurrentDomain() {
|
|
12
5
|
const host = location.host;
|
|
13
|
-
|
|
6
|
+
const domain = SUPPORTED_DOMAINS.find((domain2) => host.endsWith(domain2));
|
|
7
|
+
if (!domain) {
|
|
8
|
+
throw new Error(`Unsupported domain: ${host}`);
|
|
9
|
+
}
|
|
10
|
+
return domain;
|
|
14
11
|
}
|
|
15
12
|
function baseUrl(params) {
|
|
16
13
|
try {
|
|
17
14
|
if (!params.name) {
|
|
18
15
|
throw new Error("name parameter is required");
|
|
19
16
|
}
|
|
20
|
-
|
|
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
|
-
}
|
|
17
|
+
const domain = getCurrentDomain();
|
|
37
18
|
const strategy = URLStrategyFactory.getStrategy(domain);
|
|
38
19
|
const urlParams = {
|
|
39
20
|
name: params.name,
|
|
40
21
|
path: params.path || ""
|
|
41
22
|
};
|
|
42
23
|
if (strategy.features.supportEnv) {
|
|
43
|
-
const env =
|
|
24
|
+
const env = environment();
|
|
44
25
|
if (env === "unknown") {
|
|
45
26
|
return params.path;
|
|
46
27
|
}
|
|
47
28
|
urlParams.env = env;
|
|
48
29
|
}
|
|
49
30
|
if (strategy.features.supportRegion) {
|
|
50
|
-
urlParams.region =
|
|
31
|
+
urlParams.region = region();
|
|
51
32
|
}
|
|
52
33
|
if (strategy.features.supportAWS) {
|
|
53
|
-
urlParams.isAWS =
|
|
34
|
+
urlParams.isAWS = isAWSDomain();
|
|
54
35
|
}
|
|
55
36
|
return strategy.generateURL(urlParams);
|
|
56
37
|
} catch (error) {
|
package/dist/esm/global.d.ts
CHANGED