@microbt/environment 0.2.0 → 0.3.1

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.
Files changed (34) hide show
  1. package/README.md +68 -9
  2. package/dist/esm/cookie.d.ts +1 -0
  3. package/dist/esm/environment/factory.d.ts +5 -0
  4. package/dist/esm/environment/factory.js +31 -0
  5. package/dist/esm/environment/index.d.ts +4 -0
  6. package/dist/esm/environment/index.js +59 -0
  7. package/dist/esm/environment/strategies/cinmoore.d.ts +7 -0
  8. package/dist/esm/environment/strategies/cinmoore.js +42 -0
  9. package/dist/esm/environment/strategies/local.d.ts +7 -0
  10. package/dist/esm/environment/strategies/local.js +23 -0
  11. package/dist/esm/environment/strategies/safio365.d.ts +7 -0
  12. package/dist/esm/environment/strategies/safio365.js +42 -0
  13. package/dist/esm/environment/strategies/sensforge.d.ts +7 -0
  14. package/dist/esm/environment/strategies/sensforge.js +42 -0
  15. package/dist/esm/environment/strategies/superacme.d.ts +8 -0
  16. package/dist/esm/environment/strategies/superacme.js +45 -0
  17. package/dist/esm/gateway/factory.d.ts +4 -0
  18. package/dist/esm/gateway/factory.js +25 -0
  19. package/dist/esm/gateway/index.d.ts +33 -0
  20. package/dist/esm/gateway/index.js +44 -0
  21. package/dist/esm/gateway/strategies/index.d.ts +3 -0
  22. package/dist/esm/gateway/strategies/index.js +4 -0
  23. package/dist/esm/gateway/strategies/safio365.d.ts +4 -0
  24. package/dist/esm/gateway/strategies/safio365.js +19 -0
  25. package/dist/esm/gateway/strategies/sensforge.d.ts +4 -0
  26. package/dist/esm/gateway/strategies/sensforge.js +19 -0
  27. package/dist/esm/gateway/strategies/superacme.d.ts +4 -0
  28. package/dist/esm/gateway/strategies/superacme.js +27 -0
  29. package/dist/esm/global.d.ts +37 -0
  30. package/dist/esm/index.d.ts +2 -2
  31. package/dist/esm/index.js +2 -2
  32. package/package.json +1 -1
  33. package/dist/esm/environment.d.ts +0 -15
  34. package/dist/esm/gateway.d.ts +0 -1
package/README.md CHANGED
@@ -60,17 +60,28 @@ console.log(regionCode); // 输出: 'cn'
60
60
 
61
61
  ## 网关相关函数
62
62
 
63
- ### `baseUrl()`
63
+ ### `baseUrl(params: { name: string; path?: string })`
64
64
 
65
- 获取 API 的基础 URL。
65
+ 根据当前域名生成对应的 API URL。
66
66
 
67
- **返回值**: `string | undefined` - 根据环境返回相应的 API 基础 URL,如果是本地或未知环境则返回 `undefined`
67
+ **参数**:
68
+
69
+ - `params.name`: `string` - **必填**,API 服务名称(如 'api'、'iot-api')
70
+ - `params.path`: `string` - **可选**,API 路径(如 '/saas')
71
+
72
+ **返回值**: `string` - 生成的 URL。如果生成失败则返回传入的 path 值
68
73
 
69
74
  **示例**:
70
75
 
71
76
  ```typescript
72
- const url = baseUrl();
73
- console.log(url); // 输出: '//api-cn-prod.superacme.com'
77
+ // 中国区开发环境
78
+ // location.host: 'mall-cn-dev.superacme.com'
79
+ baseUrl({ name: 'api' }); // => '//api-cn-dev.superacme.com'
80
+ baseUrl({ name: 'iot-api', path: '/saas' }); // => '//iot-api-cn-dev.superacme.com/saas'
81
+
82
+ // 美区 AWS 生产环境
83
+ // location.host: 'mall-us-aws.superacme.com'
84
+ baseUrl({ name: 'api' }); // => '//api-us-aws.superacme.com'
74
85
  ```
75
86
 
76
87
  ## 混合应用相关函数
@@ -117,15 +128,36 @@ console.log(isInApp); // 输出: true 或 false
117
128
 
118
129
  ### `superAcmeVersion()`
119
130
 
120
- 获取 SuperAcme 应用的版本号。
131
+ 获取 SuperAcme 应用的版本信息。
132
+
133
+ **返回值**: 返回一个包含版本信息的对象:
134
+
135
+ - 如果在 SuperAcme 应用中且能够解析版本号:
121
136
 
122
- **返回值**: `string | undefined` - 返回 SuperAcme 应用的版本号,如果不在应用中则返回 `undefined`
137
+ ```json
138
+ {
139
+ version: string; // 完整版本号,如 "1.2.3"
140
+ major: number; // 主版本号
141
+ minor: number; // 次版本号
142
+ patch: number; // 修订号
143
+ }
144
+ ```
145
+
146
+ - 如果不在 SuperAcme 应用中或无法解析版本号:
147
+
148
+ ```json
149
+ {
150
+ version: 'unknown';
151
+ }
152
+ ```
123
153
 
124
154
  **示例**:
125
155
 
126
156
  ```typescript
127
- const version = superAcmeVersion();
128
- console.log(version); // 输出: '1.2.3'
157
+ const versionInfo = superAcmeVersion();
158
+ console.log(versionInfo);
159
+ // 在 SuperAcme 应用中可能输出:{ version: "1.2.3", major: 1, minor: 2, patch: 3 }
160
+ // 在非 SuperAcme 应用中输出:{ version: "unknown" }
129
161
  ```
130
162
 
131
163
  ## 平台相关函数
@@ -211,6 +243,33 @@ if (userToken) {
211
243
  - 函数会对 Cookie 值进行解码,如果解码失败会捕获错误并返回 `null`。
212
244
  - 确保传入的 `name` 参数是合法的 Cookie 名称。
213
245
 
246
+ ### `getRUID()`
247
+
248
+ 获取当前域名下的 `RUID` 值。
249
+
250
+ **返回值**: `string | null` - 返回当前域名下的 `RUID` 值,如果不存在或解析失败则返回 `null`
251
+
252
+ **功能描述**:
253
+
254
+ - 基于当前域名(`location.hostname`)动态生成 `RUID` 的 Cookie 名称。
255
+ - 调用 `getCookie` 函数获取对应的 Cookie 值。
256
+
257
+ **示例**:
258
+
259
+ ```typescript
260
+ const ruid = getRUID();
261
+ if (ruid) {
262
+ console.log(`RUID: ${ruid}`);
263
+ } else {
264
+ console.log('RUID not found or invalid');
265
+ }
266
+ ```
267
+
268
+ **注意事项**:
269
+
270
+ - 确保 `RUID` 的 Cookie 名称格式为 `RUID_<当前域名>`。
271
+ - 函数依赖 `getCookie`,因此会继承其解码逻辑和错误处理机制。
272
+
214
273
  ## ARMS 模块
215
274
 
216
275
  `arms` 模块封装了阿里云 ARMS(应用实时监控服务)的前端监控功能,提供了一系列 API 用于上报性能数据、错误信息、用户行为等。
@@ -1 +1,2 @@
1
1
  export declare function getCookie(name: string): string | null;
2
+ export declare function getRUID(): string | null;
@@ -0,0 +1,5 @@
1
+ export declare class EnvironmentStrategyFactory {
2
+ private static strategies;
3
+ private static localStrategy;
4
+ static getStrategy(domain: string): EnvironmentStrategy;
5
+ }
@@ -0,0 +1,31 @@
1
+ // src/environment/factory.ts
2
+ import { CinmooreEnvironmentStrategy } from "./strategies/cinmoore";
3
+ import { LocalEnvironmentStrategy } from "./strategies/local";
4
+ import { Safio365EnvironmentStrategy } from "./strategies/safio365";
5
+ import { SensforgeEnvironmentStrategy } from "./strategies/sensforge";
6
+ import { SuperAcmeEnvironmentStrategy } from "./strategies/superacme";
7
+ var _EnvironmentStrategyFactory = class {
8
+ static getStrategy(domain) {
9
+ if (this.localStrategy.getEnvironment(domain) === "local") {
10
+ return this.localStrategy;
11
+ }
12
+ const strategy = this.strategies.get(domain);
13
+ if (!strategy) {
14
+ throw new Error(`Unsupported domain: ${domain}`);
15
+ }
16
+ return strategy;
17
+ }
18
+ };
19
+ var EnvironmentStrategyFactory = _EnvironmentStrategyFactory;
20
+ EnvironmentStrategyFactory.strategies = /* @__PURE__ */ new Map();
21
+ EnvironmentStrategyFactory.localStrategy = new LocalEnvironmentStrategy();
22
+ (() => {
23
+ _EnvironmentStrategyFactory.strategies.set("superacme.com", new SuperAcmeEnvironmentStrategy());
24
+ _EnvironmentStrategyFactory.strategies.set("safio365.com", new Safio365EnvironmentStrategy());
25
+ _EnvironmentStrategyFactory.strategies.set("sensforge.com", new SensforgeEnvironmentStrategy());
26
+ _EnvironmentStrategyFactory.strategies.set("cinmoore.com", new CinmooreEnvironmentStrategy());
27
+ _EnvironmentStrategyFactory.strategies.set("cinmoore.cn", new CinmooreEnvironmentStrategy());
28
+ })();
29
+ export {
30
+ EnvironmentStrategyFactory
31
+ };
@@ -0,0 +1,4 @@
1
+ export declare const SUPPORTED_DOMAINS: string[];
2
+ export declare function environment(): string;
3
+ export declare function region(): string;
4
+ export declare function isAWSDomain(): boolean;
@@ -0,0 +1,59 @@
1
+ // src/environment/index.ts
2
+ import { EnvironmentStrategyFactory } from "./factory";
3
+ var SUPPORTED_DOMAINS = [
4
+ "superacme.com",
5
+ "cinmoore.com",
6
+ "cinmoore.cn",
7
+ "safio365.com",
8
+ "sensforge.com",
9
+ "0.0.0.0",
10
+ "127.0.0.1",
11
+ "localhost"
12
+ ];
13
+ function getCurrentDomain() {
14
+ if (typeof location === "undefined") {
15
+ throw new Error("location is not defined");
16
+ }
17
+ const host = location.host;
18
+ const domain = SUPPORTED_DOMAINS.find((d) => host.endsWith(d));
19
+ if (!domain) {
20
+ throw new Error(`Unsupported domain: ${host}`);
21
+ }
22
+ return domain;
23
+ }
24
+ function environment() {
25
+ try {
26
+ const domain = getCurrentDomain();
27
+ const strategy = EnvironmentStrategyFactory.getStrategy(domain);
28
+ return strategy.getEnvironment(location.host);
29
+ } catch (error) {
30
+ console.warn("Failed to determine environment:", error);
31
+ return "unknown";
32
+ }
33
+ }
34
+ function region() {
35
+ try {
36
+ const domain = getCurrentDomain();
37
+ const strategy = EnvironmentStrategyFactory.getStrategy(domain);
38
+ return strategy.getRegion(location.host);
39
+ } catch (error) {
40
+ console.warn("Failed to determine region:", error);
41
+ return "unknown";
42
+ }
43
+ }
44
+ function isAWSDomain() {
45
+ try {
46
+ const domain = getCurrentDomain();
47
+ const strategy = EnvironmentStrategyFactory.getStrategy(domain);
48
+ return strategy.isAWS(location.host);
49
+ } catch (error) {
50
+ console.warn("Failed to determine AWS domain:", error);
51
+ return false;
52
+ }
53
+ }
54
+ export {
55
+ SUPPORTED_DOMAINS,
56
+ environment,
57
+ isAWSDomain,
58
+ region
59
+ };
@@ -0,0 +1,7 @@
1
+ export declare class CinmooreEnvironmentStrategy implements EnvironmentStrategy {
2
+ features: EnvironmentFeatures;
3
+ private readonly ENV_PATTERNS;
4
+ getEnvironment(host: string): string;
5
+ getRegion(): string;
6
+ isAWS(): boolean;
7
+ }
@@ -0,0 +1,42 @@
1
+ // src/environment/strategies/cinmoore.ts
2
+ var CinmooreEnvironmentStrategy = class {
3
+ constructor() {
4
+ this.features = {
5
+ supportEnv: true,
6
+ supportRegion: false,
7
+ supportAWS: false
8
+ };
9
+ this.ENV_PATTERNS = {
10
+ // 匹配 -dev、-dev1、-dev-1、-dev. 等
11
+ dev: /-dev(?:\d*)?(?:-|\.|\b|$)/,
12
+ // 匹配 -test、-test1、-test-1、-test. 等
13
+ test: /-test(?:\d*)?(?:-|\.|\b|$)/,
14
+ // 匹配 -pre、-pre1、-pre-1、-pre. 等
15
+ pre: /-pre(?:\d*)?(?:-|\.|\b|$)/,
16
+ // 匹配 -gray、-gray1、-gray-1、-gray. 等
17
+ gray: /-gray(?:\d*)?(?:-|\.|\b|$)/,
18
+ // 本地环境
19
+ local: /^(localhost|127\.0\.0\.1|0\.0\.0\.0)(?::\d+)?$/
20
+ };
21
+ }
22
+ getEnvironment(host) {
23
+ if (this.ENV_PATTERNS.local.test(host)) {
24
+ return "local";
25
+ }
26
+ for (const [env, pattern] of Object.entries(this.ENV_PATTERNS)) {
27
+ if (pattern.test(host)) {
28
+ return env;
29
+ }
30
+ }
31
+ return "prod";
32
+ }
33
+ getRegion() {
34
+ return "unknown";
35
+ }
36
+ isAWS() {
37
+ return false;
38
+ }
39
+ };
40
+ export {
41
+ CinmooreEnvironmentStrategy
42
+ };
@@ -0,0 +1,7 @@
1
+ export declare class LocalEnvironmentStrategy implements EnvironmentStrategy {
2
+ features: EnvironmentFeatures;
3
+ private readonly LOCAL_PATTERN;
4
+ getEnvironment(host: string): string;
5
+ getRegion(): string;
6
+ isAWS(): boolean;
7
+ }
@@ -0,0 +1,23 @@
1
+ // src/environment/strategies/local.ts
2
+ var LocalEnvironmentStrategy = class {
3
+ constructor() {
4
+ this.features = {
5
+ supportEnv: true,
6
+ supportRegion: false,
7
+ supportAWS: false
8
+ };
9
+ this.LOCAL_PATTERN = /^(localhost|127\.0\.0\.1|0\.0\.0\.0)(?::\d+)?$/;
10
+ }
11
+ getEnvironment(host) {
12
+ return this.LOCAL_PATTERN.test(host) ? "local" : "unknown";
13
+ }
14
+ getRegion() {
15
+ return "unknown";
16
+ }
17
+ isAWS() {
18
+ return false;
19
+ }
20
+ };
21
+ export {
22
+ LocalEnvironmentStrategy
23
+ };
@@ -0,0 +1,7 @@
1
+ export declare class Safio365EnvironmentStrategy implements EnvironmentStrategy {
2
+ features: EnvironmentFeatures;
3
+ private readonly ENV_PATTERNS;
4
+ getEnvironment(host: string): string;
5
+ getRegion(): string;
6
+ isAWS(): boolean;
7
+ }
@@ -0,0 +1,42 @@
1
+ // src/environment/strategies/safio365.ts
2
+ var Safio365EnvironmentStrategy = class {
3
+ constructor() {
4
+ this.features = {
5
+ supportEnv: true,
6
+ supportRegion: false,
7
+ supportAWS: false
8
+ };
9
+ this.ENV_PATTERNS = {
10
+ // 匹配 -dev、-dev1、-dev-1、-dev. 等
11
+ dev: /-dev(?:\d*)?(?:-|\.|\b|$)/,
12
+ // 匹配 -test、-test1、-test-1、-test. 等
13
+ test: /-test(?:\d*)?(?:-|\.|\b|$)/,
14
+ // 匹配 -pre、-pre1、-pre-1、-pre. 等
15
+ pre: /-pre(?:\d*)?(?:-|\.|\b|$)/,
16
+ // 匹配 -gray、-gray1、-gray-1、-gray. 等
17
+ gray: /-gray(?:\d*)?(?:-|\.|\b|$)/,
18
+ // 本地环境
19
+ local: /^(localhost|127\.0\.0\.1|0\.0\.0\.0)(?::\d+)?$/
20
+ };
21
+ }
22
+ getEnvironment(host) {
23
+ if (this.ENV_PATTERNS.local.test(host)) {
24
+ return "local";
25
+ }
26
+ for (const [env, pattern] of Object.entries(this.ENV_PATTERNS)) {
27
+ if (pattern.test(host)) {
28
+ return env;
29
+ }
30
+ }
31
+ return "prod";
32
+ }
33
+ getRegion() {
34
+ return "unknown";
35
+ }
36
+ isAWS() {
37
+ return false;
38
+ }
39
+ };
40
+ export {
41
+ Safio365EnvironmentStrategy
42
+ };
@@ -0,0 +1,7 @@
1
+ export declare class SensforgeEnvironmentStrategy implements EnvironmentStrategy {
2
+ features: EnvironmentFeatures;
3
+ private readonly ENV_PATTERNS;
4
+ getEnvironment(host: string): string;
5
+ getRegion(): string;
6
+ isAWS(): boolean;
7
+ }
@@ -0,0 +1,42 @@
1
+ // src/environment/strategies/sensforge.ts
2
+ var SensforgeEnvironmentStrategy = class {
3
+ constructor() {
4
+ this.features = {
5
+ supportEnv: true,
6
+ supportRegion: false,
7
+ supportAWS: false
8
+ };
9
+ this.ENV_PATTERNS = {
10
+ // 匹配 -dev、-dev1、-dev-1、-dev. 等
11
+ dev: /-dev(?:\d*)?(?:-|\.|\b|$)/,
12
+ // 匹配 -test、-test1、-test-1、-test. 等
13
+ test: /-test(?:\d*)?(?:-|\.|\b|$)/,
14
+ // 匹配 -pre、-pre1、-pre-1、-pre. 等
15
+ pre: /-pre(?:\d*)?(?:-|\.|\b|$)/,
16
+ // 匹配 -gray、-gray1、-gray-1、-gray. 等
17
+ gray: /-gray(?:\d*)?(?:-|\.|\b|$)/,
18
+ // 本地环境
19
+ local: /^(localhost|127\.0\.0\.1|0\.0\.0\.0)(?::\d+)?$/
20
+ };
21
+ }
22
+ getEnvironment(host) {
23
+ if (this.ENV_PATTERNS.local.test(host)) {
24
+ return "local";
25
+ }
26
+ for (const [env, pattern] of Object.entries(this.ENV_PATTERNS)) {
27
+ if (pattern.test(host)) {
28
+ return env;
29
+ }
30
+ }
31
+ return "prod";
32
+ }
33
+ getRegion() {
34
+ return "unknown";
35
+ }
36
+ isAWS() {
37
+ return false;
38
+ }
39
+ };
40
+ export {
41
+ SensforgeEnvironmentStrategy
42
+ };
@@ -0,0 +1,8 @@
1
+ export declare class SuperAcmeEnvironmentStrategy implements EnvironmentStrategy {
2
+ features: EnvironmentFeatures;
3
+ private readonly ENV_PATTERNS;
4
+ private readonly REGION_PATTERN;
5
+ getEnvironment(host: string): string;
6
+ getRegion(host: string): string;
7
+ isAWS(host: string): boolean;
8
+ }
@@ -0,0 +1,45 @@
1
+ // src/environment/strategies/superacme.ts
2
+ var SuperAcmeEnvironmentStrategy = class {
3
+ constructor() {
4
+ this.features = {
5
+ supportEnv: true,
6
+ supportRegion: true,
7
+ supportAWS: true
8
+ };
9
+ this.ENV_PATTERNS = {
10
+ // 匹配 -dev、-dev1、-dev-1、-dev.、-dev-aws 等
11
+ dev: /-dev(?:\d*)?(?:-|\.|\b|$)/,
12
+ // 匹配 -test、-test1、-test-1、-test.、-test-aws 等
13
+ test: /-test(?:\d*)?(?:-|\.|\b|$)/,
14
+ // 匹配 -pre、-pre1、-pre-1、-pre.、-pre-aws 等
15
+ pre: /-pre(?:\d*)?(?:-|\.|\b|$)/,
16
+ // 匹配 -gray、-gray1、-gray-1、-gray.、-gray-aws 等
17
+ gray: /-gray(?:\d*)?(?:-|\.|\b|$)/,
18
+ // 本地环境保持不变
19
+ local: /^(localhost|127\.0\.0\.1|0\.0\.0\.0)(?::\d+)?$/
20
+ };
21
+ this.REGION_PATTERN = new RegExp("-(?<region>cn|us|usa|jpn|deu|gbr|fra|can|sgp|aus|kor|ind|bra|rus|zaf|mex|idn|tur|sau|che|twn|hkg|mac)(?=-|\\.|\\d|$)");
22
+ }
23
+ getEnvironment(host) {
24
+ if (this.ENV_PATTERNS.local.test(host)) {
25
+ return "local";
26
+ }
27
+ for (const [env, pattern] of Object.entries(this.ENV_PATTERNS)) {
28
+ if (pattern.test(host)) {
29
+ return env;
30
+ }
31
+ }
32
+ return "prod";
33
+ }
34
+ getRegion(host) {
35
+ var _a;
36
+ const match = host.match(this.REGION_PATTERN);
37
+ return ((_a = match == null ? void 0 : match.groups) == null ? void 0 : _a.region) || "unknown";
38
+ }
39
+ isAWS(host) {
40
+ return host.includes("-aws");
41
+ }
42
+ };
43
+ export {
44
+ SuperAcmeEnvironmentStrategy
45
+ };
@@ -0,0 +1,4 @@
1
+ export declare class URLStrategyFactory {
2
+ private static strategies;
3
+ static getStrategy(domain: string): URLStrategy;
4
+ }
@@ -0,0 +1,25 @@
1
+ // src/gateway/factory.ts
2
+ import {
3
+ Safio365URLStrategy,
4
+ SensforgeURLStrategy,
5
+ SuperAcmeURLStrategy
6
+ } from "./strategies/index";
7
+ var _URLStrategyFactory = class {
8
+ static getStrategy(domain) {
9
+ const strategy = this.strategies.get(domain);
10
+ if (!strategy) {
11
+ throw new Error(`Unsupported domain: ${domain}`);
12
+ }
13
+ return strategy;
14
+ }
15
+ };
16
+ var URLStrategyFactory = _URLStrategyFactory;
17
+ URLStrategyFactory.strategies = /* @__PURE__ */ new Map();
18
+ (() => {
19
+ _URLStrategyFactory.strategies.set("superacme.com", new SuperAcmeURLStrategy());
20
+ _URLStrategyFactory.strategies.set("sensforge.com", new SensforgeURLStrategy());
21
+ _URLStrategyFactory.strategies.set("safio365.com", new Safio365URLStrategy());
22
+ })();
23
+ export {
24
+ URLStrategyFactory
25
+ };
@@ -0,0 +1,33 @@
1
+ /**
2
+ * 根据当前域名生成基于 name 和 path 的 baseUrl
3
+ *
4
+ * @param params 配置参数
5
+ * @param params.name - 必填,API 服务名称(例如:'api', 'iot-api')
6
+ * @param params.path - 可选,API 路径(例如:'/saas')
7
+ * @returns 生成的 URL。如果生成失败则返回传入的 path 值
8
+ *
9
+ * @example
10
+ * // SuperAcme 域名系列
11
+ * // 中国区开发环境
12
+ * baseUrl({ name: 'api' }) // => '//api-cn-dev.superacme.com'
13
+ * baseUrl({ name: 'iot-api', path: '/saas' }) // => '//iot-api-cn-dev.superacme.com/saas'
14
+ *
15
+ * // 美区 AWS 环境
16
+ * // location.host: 'mall-us-aws.superacme.com'
17
+ * baseUrl({ name: 'api' }) // => '//api-us-aws.superacme.com'
18
+ *
19
+ * // Safio365 域名系列
20
+ * // location.host: 'iot-dev.safio365.com'
21
+ * baseUrl({ name: 'iot-api', path: '/saas' }) // => '//iot-api-dev.safio365.com/saas'
22
+ *
23
+ * // Sensforge 域名系列
24
+ * // location.host: 'api-dev.sensforge.com'
25
+ * baseUrl({ name: 'iot-api' }) // => '//iot-api-dev.sensforge.com'
26
+ *
27
+ * @throws 如果 name 参数为空,将抛出错误并返回 path
28
+ * @throws 如果域名不在支持列表中,将返回 path
29
+ */
30
+ export declare function baseUrl(params: {
31
+ name: string;
32
+ path?: string;
33
+ }): string | undefined;
@@ -0,0 +1,44 @@
1
+ // src/gateway/index.ts
2
+ import { environment, isAWSDomain, region, SUPPORTED_DOMAINS } from "../environment/index";
3
+ import { URLStrategyFactory } from "./factory";
4
+ function getCurrentDomain() {
5
+ const host = location.host;
6
+ const domain = SUPPORTED_DOMAINS.find((domain2) => host.endsWith(domain2));
7
+ if (!domain) {
8
+ throw new Error(`Unsupported domain: ${host}`);
9
+ }
10
+ return domain;
11
+ }
12
+ function baseUrl(params) {
13
+ try {
14
+ if (!params.name) {
15
+ throw new Error("name parameter is required");
16
+ }
17
+ const domain = getCurrentDomain();
18
+ const strategy = URLStrategyFactory.getStrategy(domain);
19
+ const urlParams = {
20
+ name: params.name,
21
+ path: params.path || ""
22
+ };
23
+ if (strategy.features.supportEnv) {
24
+ const env = environment();
25
+ if (env === "unknown") {
26
+ return params.path;
27
+ }
28
+ urlParams.env = env;
29
+ }
30
+ if (strategy.features.supportRegion) {
31
+ urlParams.region = region();
32
+ }
33
+ if (strategy.features.supportAWS) {
34
+ urlParams.isAWS = isAWSDomain();
35
+ }
36
+ return strategy.generateURL(urlParams);
37
+ } catch (error) {
38
+ console.warn("Failed to generate base URL:", error);
39
+ return params.path;
40
+ }
41
+ }
42
+ export {
43
+ baseUrl
44
+ };
@@ -0,0 +1,3 @@
1
+ export * from './safio365';
2
+ export * from './sensforge';
3
+ export * from './superacme';
@@ -0,0 +1,4 @@
1
+ // src/gateway/strategies/index.ts
2
+ export * from "./safio365";
3
+ export * from "./sensforge";
4
+ export * from "./superacme";
@@ -0,0 +1,4 @@
1
+ export declare class Safio365URLStrategy implements URLStrategy {
2
+ features: DomainFeatures;
3
+ generateURL({ name, env, path }: URLStrategyConfig): string;
4
+ }
@@ -0,0 +1,19 @@
1
+ // src/gateway/strategies/safio365.ts
2
+ var Safio365URLStrategy = class {
3
+ constructor() {
4
+ this.features = {
5
+ supportEnv: true,
6
+ supportRegion: false,
7
+ supportAWS: false
8
+ };
9
+ }
10
+ generateURL({ name, env = "prod", path = "" }) {
11
+ if (env === "prod" || !env) {
12
+ return `//${name}.safio365.com${path}`;
13
+ }
14
+ return `//${name}-${env}.safio365.com${path}`;
15
+ }
16
+ };
17
+ export {
18
+ Safio365URLStrategy
19
+ };
@@ -0,0 +1,4 @@
1
+ export declare class SensforgeURLStrategy implements URLStrategy {
2
+ features: DomainFeatures;
3
+ generateURL({ name, env, path }: URLStrategyConfig): string;
4
+ }
@@ -0,0 +1,19 @@
1
+ // src/gateway/strategies/sensforge.ts
2
+ var SensforgeURLStrategy = class {
3
+ constructor() {
4
+ this.features = {
5
+ supportEnv: true,
6
+ supportRegion: false,
7
+ supportAWS: false
8
+ };
9
+ }
10
+ generateURL({ name, env = "prod", path = "" }) {
11
+ if (env === "prod" || !env) {
12
+ return `//${name}.sensforge.com${path}`;
13
+ }
14
+ return `//${name}-${env}.sensforge.com${path}`;
15
+ }
16
+ };
17
+ export {
18
+ SensforgeURLStrategy
19
+ };
@@ -0,0 +1,4 @@
1
+ export declare class SuperAcmeURLStrategy implements URLStrategy {
2
+ features: DomainFeatures;
3
+ generateURL({ name, env, region, isAWS, path, }: URLStrategyConfig): string;
4
+ }
@@ -0,0 +1,27 @@
1
+ // src/gateway/strategies/superacme.ts
2
+ var SuperAcmeURLStrategy = class {
3
+ constructor() {
4
+ this.features = {
5
+ supportEnv: true,
6
+ supportRegion: true,
7
+ supportAWS: true
8
+ };
9
+ }
10
+ generateURL({
11
+ name,
12
+ env = "prod",
13
+ region = "",
14
+ isAWS = false,
15
+ path = ""
16
+ }) {
17
+ const regionPart = region ? `-${region}` : "";
18
+ const awsPart = isAWS ? "-aws" : "";
19
+ if (env === "prod" || !env) {
20
+ return `//${name}${regionPart}${awsPart}.superacme.com${path}`;
21
+ }
22
+ return `//${name}${regionPart}-${env}${awsPart}.superacme.com${path}`;
23
+ }
24
+ };
25
+ export {
26
+ SuperAcmeURLStrategy
27
+ };
@@ -39,6 +39,43 @@ declare global {
39
39
  addBehavior(data: { name: string; message: string }, page: string): void;
40
40
  };
41
41
  }
42
+
43
+ type URLStrategyConfig = {
44
+ name: string;
45
+ env?: string;
46
+ region?: string;
47
+ isAWS?: boolean;
48
+ path?: string;
49
+ };
50
+
51
+ interface DomainFeatures {
52
+ supportEnv: boolean;
53
+ supportRegion: boolean;
54
+ supportAWS: boolean;
55
+ }
56
+ interface URLStrategy {
57
+ features: DomainFeatures;
58
+ generateURL(params: URLStrategyConfig): string;
59
+ }
60
+
61
+ interface EnvironmentFeatures {
62
+ supportEnv: boolean; // 是否支持环境判断
63
+ supportRegion: boolean; // 是否支持区域判断
64
+ supportAWS: boolean; // 是否支持 AWS 判断
65
+ }
66
+
67
+ interface EnvironmentStrategy {
68
+ features: EnvironmentFeatures;
69
+ getEnvironment(host: string): string;
70
+ getRegion(host: string): string;
71
+ isAWS(host: string): boolean;
72
+ }
73
+
74
+ interface EnvironmentConfig {
75
+ env?: string;
76
+ region?: string;
77
+ isAWS?: boolean;
78
+ }
42
79
  }
43
80
 
44
81
  export {};
@@ -1,7 +1,7 @@
1
1
  export * from './arms';
2
2
  export * from './cookie';
3
- export * from './environment';
4
- export * from './gateway';
3
+ export * from './environment/index';
4
+ export * from './gateway/index';
5
5
  export * from './hybrid';
6
6
  export * from './local';
7
7
  export * from './platform';
package/dist/esm/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  // src/index.ts
2
2
  export * from "./arms";
3
3
  export * from "./cookie";
4
- export * from "./environment";
5
- export * from "./gateway";
4
+ export * from "./environment/index";
5
+ export * from "./gateway/index";
6
6
  export * from "./hybrid";
7
7
  export * from "./local";
8
8
  export * from "./platform";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@microbt/environment",
3
- "version": "0.2.0",
3
+ "version": "0.3.1",
4
4
  "description": "microbt app library for environment",
5
5
  "keywords": [],
6
6
  "license": "MIT",
@@ -1,15 +0,0 @@
1
- /**
2
- * 获取当前运行的环境
3
- * @returns {string} 环境名称(local, dev, test, pre, gray, prod, unknown)
4
- */
5
- export declare const environment: () => string;
6
- /**
7
- * 获取当前运行的区域
8
- * @returns {string} 区域名称(如 cn, us 等)
9
- */
10
- export declare const region: () => string;
11
- /**
12
- * 判断当前域名是否为 AWS 域名
13
- * @returns {boolean} 是否为 AWS 域名
14
- */
15
- export declare function isAWSDomain(): boolean;
@@ -1 +0,0 @@
1
- export declare function baseUrl(): string | undefined;