@microbt/environment 0.2.0
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.
- package/README.md +235 -0
- package/dist/esm/arms.d.ts +39 -0
- package/dist/esm/cookie.d.ts +1 -0
- package/dist/esm/environment.d.ts +15 -0
- package/dist/esm/gateway.d.ts +1 -0
- package/dist/esm/global.d.ts +44 -0
- package/dist/esm/hybrid.d.ts +11 -0
- package/dist/esm/index.d.ts +7 -0
- package/dist/esm/index.js +8 -0
- package/dist/esm/local.d.ts +1 -0
- package/dist/esm/platform.d.ts +2 -0
- package/jest.config.js +4 -0
- package/package.json +58 -0
package/README.md
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
# @microbt/environment 使用文档
|
|
2
|
+
|
|
3
|
+
## 安装和使用
|
|
4
|
+
|
|
5
|
+
### 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @microbt/environment
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
### 使用
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { environment, baseUrl, getLocale } from '@microbt/environment';
|
|
15
|
+
const env = environment();
|
|
16
|
+
const url = baseUrl();
|
|
17
|
+
const locale = getLocale();
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## 环境相关函数
|
|
21
|
+
|
|
22
|
+
### `environment()`
|
|
23
|
+
|
|
24
|
+
获取当前运行的环境。
|
|
25
|
+
|
|
26
|
+
**返回值**: `string` - 环境名称(local, dev, test, pre, gray, prod, unknown)
|
|
27
|
+
|
|
28
|
+
**示例**:
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
const env = environment();
|
|
32
|
+
console.log(env); // 输出: 'prod'
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### `isAWSDomain()`
|
|
36
|
+
|
|
37
|
+
判断当前域名是否为 AWS 环境。
|
|
38
|
+
|
|
39
|
+
**返回值**: `boolean` - 如果是 AWS 环境返回 `true`,否则返回 `false`
|
|
40
|
+
|
|
41
|
+
**示例**:
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
const isAWS = isAWSDomain();
|
|
45
|
+
console.log(isAWS); // 输出: true 或 false
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### `region()`
|
|
49
|
+
|
|
50
|
+
获取当前区域代码。
|
|
51
|
+
|
|
52
|
+
**返回值**: `string` - 区域代码(如 'cn', 'us' 等)
|
|
53
|
+
|
|
54
|
+
**示例**:
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
const regionCode = region();
|
|
58
|
+
console.log(regionCode); // 输出: 'cn'
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## 网关相关函数
|
|
62
|
+
|
|
63
|
+
### `baseUrl()`
|
|
64
|
+
|
|
65
|
+
获取 API 的基础 URL。
|
|
66
|
+
|
|
67
|
+
**返回值**: `string | undefined` - 根据环境返回相应的 API 基础 URL,如果是本地或未知环境则返回 `undefined`
|
|
68
|
+
|
|
69
|
+
**示例**:
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
const url = baseUrl();
|
|
73
|
+
console.log(url); // 输出: '//api-cn-prod.superacme.com'
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## 混合应用相关函数
|
|
77
|
+
|
|
78
|
+
### `bridgeCall(method: string, params: Record<string, string>, callback: (resp: string) => void)`
|
|
79
|
+
|
|
80
|
+
调用原生桥接方法。
|
|
81
|
+
|
|
82
|
+
**参数**:
|
|
83
|
+
|
|
84
|
+
- `method`: `string` - 要调用的方法名
|
|
85
|
+
- `params`: `Record<string, string>` - 方法参数
|
|
86
|
+
- `callback`: `(resp: string) => void` - 回调函数
|
|
87
|
+
|
|
88
|
+
**示例**:
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
bridgeCall('getUserInfo', { userId: '123' }, (resp) => {
|
|
92
|
+
console.log(resp);
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### `bridgeBootstrap()`
|
|
96
|
+
|
|
97
|
+
初始化桥接环境。
|
|
98
|
+
|
|
99
|
+
**示例**:
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
bridgeBootstrap();
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### `inSuperAcme()`
|
|
106
|
+
|
|
107
|
+
判断当前是否在 SuperAcme 应用中。
|
|
108
|
+
|
|
109
|
+
**返回值**: `boolean` - 如果在 SuperAcme 应用中返回 `true`,否则返回 `false`
|
|
110
|
+
|
|
111
|
+
**示例**:
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
const isInApp = inSuperAcme();
|
|
115
|
+
console.log(isInApp); // 输出: true 或 false
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### `superAcmeVersion()`
|
|
119
|
+
|
|
120
|
+
获取 SuperAcme 应用的版本号。
|
|
121
|
+
|
|
122
|
+
**返回值**: `string | undefined` - 返回 SuperAcme 应用的版本号,如果不在应用中则返回 `undefined`
|
|
123
|
+
|
|
124
|
+
**示例**:
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
const version = superAcmeVersion();
|
|
128
|
+
console.log(version); // 输出: '1.2.3'
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## 平台相关函数
|
|
132
|
+
|
|
133
|
+
### `iOSWebview`
|
|
134
|
+
|
|
135
|
+
判断当前是否为 iOS Webview 环境。
|
|
136
|
+
|
|
137
|
+
**类型**: `boolean`
|
|
138
|
+
|
|
139
|
+
**示例**:
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
if (iOSWebview) {
|
|
143
|
+
console.log('当前为 iOS Webview 环境');
|
|
144
|
+
}
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### `androidWebView`
|
|
148
|
+
|
|
149
|
+
判断当前是否为 Android Webview 环境。
|
|
150
|
+
|
|
151
|
+
**类型**: `boolean`
|
|
152
|
+
|
|
153
|
+
**示例**:
|
|
154
|
+
|
|
155
|
+
```typescript
|
|
156
|
+
if (androidWebView) {
|
|
157
|
+
console.log('当前为 Android Webview 环境');
|
|
158
|
+
}
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
## 本地化相关函数
|
|
162
|
+
|
|
163
|
+
### `getLocale(defaultLocal = 'zh-CN')`
|
|
164
|
+
|
|
165
|
+
获取当前语言环境。
|
|
166
|
+
|
|
167
|
+
**参数**:
|
|
168
|
+
|
|
169
|
+
- `defaultLocal`: `string` - 默认语言环境,默认为 'zh-CN'
|
|
170
|
+
|
|
171
|
+
**返回值**: `string` - 当前语言环境
|
|
172
|
+
|
|
173
|
+
**示例**:
|
|
174
|
+
|
|
175
|
+
```typescript
|
|
176
|
+
const locale = getLocale();
|
|
177
|
+
console.log(locale); // 输出: 'zh-CN' 或 'en-US'
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## Cookie 相关函数
|
|
181
|
+
|
|
182
|
+
### `getCookie(name: string)`
|
|
183
|
+
|
|
184
|
+
获取指定名称的 Cookie 值。
|
|
185
|
+
|
|
186
|
+
**参数**:
|
|
187
|
+
|
|
188
|
+
- `name`: `string` - 要获取的 Cookie 名称
|
|
189
|
+
|
|
190
|
+
**返回值**: `string | null` - 返回指定名称的 Cookie 值,如果不存在或解析失败则返回 `null`
|
|
191
|
+
|
|
192
|
+
**功能描述**:
|
|
193
|
+
|
|
194
|
+
- 使用正则表达式匹配指定名称的 Cookie 值。
|
|
195
|
+
- 如果匹配成功,返回解码后的 Cookie 值。
|
|
196
|
+
- 如果匹配失败或解码过程中发生错误,返回 `null`。
|
|
197
|
+
|
|
198
|
+
**示例**:
|
|
199
|
+
|
|
200
|
+
```typescript
|
|
201
|
+
const userToken = getCookie('token');
|
|
202
|
+
if (userToken) {
|
|
203
|
+
console.log(`User token: ${userToken}`);
|
|
204
|
+
} else {
|
|
205
|
+
console.log('Token not found or invalid');
|
|
206
|
+
}
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
**注意事项**:
|
|
210
|
+
|
|
211
|
+
- 函数会对 Cookie 值进行解码,如果解码失败会捕获错误并返回 `null`。
|
|
212
|
+
- 确保传入的 `name` 参数是合法的 Cookie 名称。
|
|
213
|
+
|
|
214
|
+
## ARMS 模块
|
|
215
|
+
|
|
216
|
+
`arms` 模块封装了阿里云 ARMS(应用实时监控服务)的前端监控功能,提供了一系列 API 用于上报性能数据、错误信息、用户行为等。
|
|
217
|
+
|
|
218
|
+
### 可用 API
|
|
219
|
+
|
|
220
|
+
以下是 `arms` 模块中提供的 API 列表:
|
|
221
|
+
|
|
222
|
+
- **`api(params)`**: 上报 API 请求数据。
|
|
223
|
+
- **`error(e, pos)`**: 上报错误信息。
|
|
224
|
+
- **`sum(key, value)`**: 上报累加指标。
|
|
225
|
+
- **`avg(key, value)`**: 上报平均值指标。
|
|
226
|
+
- **`reportBehavior()`**: 上报用户行为数据。
|
|
227
|
+
- **`performance(params)`**: 上报性能数据。
|
|
228
|
+
- **`setConfig(params)`**: 设置 ARMS 配置。
|
|
229
|
+
- **`setPage(page, sendPv)`**: 设置当前页面信息,并可选择是否发送页面访问数据(PV)。
|
|
230
|
+
- **`setCommonInfo(params)`**: 设置通用信息。
|
|
231
|
+
- **`addBehavior(data, page)`**: 添加用户行为数据。
|
|
232
|
+
|
|
233
|
+
### 参考链接
|
|
234
|
+
|
|
235
|
+
- [阿里云 ARMS 文档 - API 参考](https://help.aliyun.com/zh/arms/browser-monitoring/developer-reference/api-reference)
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
declare const api: (params: {
|
|
2
|
+
api: string;
|
|
3
|
+
success: boolean;
|
|
4
|
+
time: number;
|
|
5
|
+
code?: string | number;
|
|
6
|
+
msg?: string;
|
|
7
|
+
begin?: number;
|
|
8
|
+
traceId?: string;
|
|
9
|
+
sid?: string;
|
|
10
|
+
}) => void;
|
|
11
|
+
export declare const arms: {
|
|
12
|
+
api: (params: {
|
|
13
|
+
api: string;
|
|
14
|
+
success: boolean;
|
|
15
|
+
time: number;
|
|
16
|
+
code?: string | number;
|
|
17
|
+
msg?: string;
|
|
18
|
+
begin?: number;
|
|
19
|
+
traceId?: string;
|
|
20
|
+
sid?: string;
|
|
21
|
+
}) => void;
|
|
22
|
+
error: (e: Error, pos: {
|
|
23
|
+
filename: string;
|
|
24
|
+
lineno: number;
|
|
25
|
+
colno: number;
|
|
26
|
+
}) => void;
|
|
27
|
+
sum: (key: string, value: number) => void;
|
|
28
|
+
avg: (key: string, value: number) => void;
|
|
29
|
+
reportBehavior: () => void;
|
|
30
|
+
performance: (params: unknown) => void;
|
|
31
|
+
setConfig: (params: unknown) => void;
|
|
32
|
+
setPage: (page: string, sendPv: boolean) => void;
|
|
33
|
+
setCommonInfo: (params: unknown) => void;
|
|
34
|
+
addBehavior: (data: {
|
|
35
|
+
name: string;
|
|
36
|
+
message: string;
|
|
37
|
+
}, page: string) => void;
|
|
38
|
+
};
|
|
39
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getCookie(name: string): string | null;
|
|
@@ -0,0 +1,15 @@
|
|
|
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;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function baseUrl(): string | undefined;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
declare global {
|
|
2
|
+
interface Window {
|
|
3
|
+
SuperAcme?: {
|
|
4
|
+
call: (
|
|
5
|
+
method: string,
|
|
6
|
+
params: Record<string, string>,
|
|
7
|
+
callback: (resp: string) => void,
|
|
8
|
+
) => void;
|
|
9
|
+
};
|
|
10
|
+
WKWVJBCallbacks?: {
|
|
11
|
+
push: (item: () => void) => void;
|
|
12
|
+
};
|
|
13
|
+
webkit?: {
|
|
14
|
+
messageHandlers: {
|
|
15
|
+
iOS_Native_InjectJavascript: {
|
|
16
|
+
postMessage: (p: null) => void;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
__bl?: {
|
|
21
|
+
api(params: {
|
|
22
|
+
api: string;
|
|
23
|
+
success: boolean;
|
|
24
|
+
time: number;
|
|
25
|
+
code?: string | number;
|
|
26
|
+
msg?: string;
|
|
27
|
+
begin?: number;
|
|
28
|
+
traceId?: string;
|
|
29
|
+
sid?: string;
|
|
30
|
+
}): void;
|
|
31
|
+
error(e: Error, pos: { filename: string; lineno: number; colno: number }): void;
|
|
32
|
+
sum(key: string, value: number): void;
|
|
33
|
+
avg(key: string, value: number): void;
|
|
34
|
+
reportBehavior(): void;
|
|
35
|
+
performance(params: unknown): void;
|
|
36
|
+
setConfig(params: unknown): void;
|
|
37
|
+
setPage(page: string, sendPv: boolean): void;
|
|
38
|
+
setCommonInfo(params: unknown): void;
|
|
39
|
+
addBehavior(data: { name: string; message: string }, page: string): void;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare function bridgeCall(method: string, params: Record<string, string>, callback: (resp: string) => void): void;
|
|
2
|
+
export declare function bridgeBootstrap(): void;
|
|
3
|
+
export declare function inSuperAcme(): boolean;
|
|
4
|
+
export declare function superAcmeVersion(): {
|
|
5
|
+
version: string;
|
|
6
|
+
} | {
|
|
7
|
+
version: string;
|
|
8
|
+
major: number;
|
|
9
|
+
minor: number;
|
|
10
|
+
patch: number;
|
|
11
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getLocale(defaultLocal?: string): string;
|
package/jest.config.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@microbt/environment",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "microbt app library for environment",
|
|
5
|
+
"keywords": [],
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"main": "dist/esm/index.js",
|
|
8
|
+
"types": "dist/esm/index.d.ts",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"build": "father build",
|
|
11
|
+
"build:deps": "father prebundle",
|
|
12
|
+
"dev": "father dev",
|
|
13
|
+
"format": "prettier --cache --write .",
|
|
14
|
+
"lint": "eslint . --fix ",
|
|
15
|
+
"prepare": "husky install",
|
|
16
|
+
"prepublishOnly": "father doctor && npm run build",
|
|
17
|
+
"postpublish": "cnpm sync @microbt/environment",
|
|
18
|
+
"test": "jest"
|
|
19
|
+
},
|
|
20
|
+
"lint-staged": {
|
|
21
|
+
"*.{md,json}": [
|
|
22
|
+
"prettier --cache --write"
|
|
23
|
+
],
|
|
24
|
+
"*.{js,jsx}": [
|
|
25
|
+
"eslint --fix",
|
|
26
|
+
"prettier --cache --write"
|
|
27
|
+
],
|
|
28
|
+
"*.ts?(x)": [
|
|
29
|
+
"eslint --fix",
|
|
30
|
+
"prettier --cache --parser=typescript --write"
|
|
31
|
+
]
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"alife-logger": "^1.8.30"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@commitlint/cli": "^17.4.4",
|
|
38
|
+
"@commitlint/config-conventional": "^17.4.4",
|
|
39
|
+
"@types/jest": "^29.5.14",
|
|
40
|
+
"@typescript-eslint/eslint-plugin": "^5.54.1",
|
|
41
|
+
"@typescript-eslint/parser": "^5.54.1",
|
|
42
|
+
"eslint": "^8.36.0",
|
|
43
|
+
"father": "^4.1.0",
|
|
44
|
+
"husky": "^8.0.3",
|
|
45
|
+
"jest": "^29.7.0",
|
|
46
|
+
"jest-environment-jsdom": "^29.7.0",
|
|
47
|
+
"lint-staged": "^13.2.0",
|
|
48
|
+
"prettier": "^2.8.4",
|
|
49
|
+
"prettier-plugin-organize-imports": "^3.2.2",
|
|
50
|
+
"prettier-plugin-packagejson": "^2.4.3",
|
|
51
|
+
"ts-jest": "^29.2.5"
|
|
52
|
+
},
|
|
53
|
+
"publishConfig": {
|
|
54
|
+
"access": "public",
|
|
55
|
+
"registry": "https://registry.npmjs.org"
|
|
56
|
+
},
|
|
57
|
+
"authors": []
|
|
58
|
+
}
|