@microbt/environment 1.0.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 +178 -0
- package/dist/esm/environment.d.ts +15 -0
- package/dist/esm/gateway.d.ts +1 -0
- package/dist/esm/global.d.ts +23 -0
- package/dist/esm/hybrid.d.ts +11 -0
- package/dist/esm/index.d.ts +5 -0
- package/dist/esm/local.d.ts +1 -0
- package/dist/esm/platform.d.ts +2 -0
- package/dist/umd/environment.min.js +1 -0
- package/jest.config.js +4 -0
- package/package.json +55 -0
package/README.md
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
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
|
+
```
|
|
@@ -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,23 @@
|
|
|
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
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
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;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var r=t();for(var o in r)("object"==typeof exports?exports:e)[o]=r[o]}}(self,(function(){return function(){var e={803:function(e){e.exports=function(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,o=new Array(t);r<t;r++)o[r]=e[r];return o},e.exports.__esModule=!0,e.exports.default=e.exports},473:function(e){e.exports=function(e){if(Array.isArray(e))return e},e.exports.__esModule=!0,e.exports.default=e.exports},502:function(e,t,r){var o=r(582);e.exports=function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),Object.defineProperty(e,"prototype",{writable:!1}),t&&o(e,t)},e.exports.__esModule=!0,e.exports.default=e.exports},941:function(e){e.exports=function(e,t){var r=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=r){var o,n,u,i,a=[],c=!0,s=!1;try{if(u=(r=r.call(e)).next,0===t){if(Object(r)!==r)return;c=!1}else for(;!(c=(o=u.call(r)).done)&&(a.push(o.value),a.length!==t);c=!0);}catch(e){s=!0,n=e}finally{try{if(!c&&null!=r.return&&(i=r.return(),Object(i)!==i))return}finally{if(s)throw n}}return a}},e.exports.__esModule=!0,e.exports.default=e.exports},551:function(e){e.exports=function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")},e.exports.__esModule=!0,e.exports.default=e.exports},582:function(e){function t(r,o){return e.exports=t=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},e.exports.__esModule=!0,e.exports.default=e.exports,t(r,o)}e.exports=t,e.exports.__esModule=!0,e.exports.default=e.exports},216:function(e,t,r){var o=r(473),n=r(941),u=r(317),i=r(551);e.exports=function(e,t){return o(e)||n(e,t)||u(e,t)||i()},e.exports.__esModule=!0,e.exports.default=e.exports},71:function(e){function t(r){return e.exports=t="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},e.exports.__esModule=!0,e.exports.default=e.exports,t(r)}e.exports=t,e.exports.__esModule=!0,e.exports.default=e.exports},317:function(e,t,r){var o=r(803);e.exports=function(e,t){if(e){if("string"==typeof e)return o(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?o(e,t):void 0}},e.exports.__esModule=!0,e.exports.default=e.exports},583:function(e,t,r){var o=r(71).default,n=r(582),u=r(502);function i(){e.exports=i=function(e,t){return new a(e,void 0,t)},e.exports.__esModule=!0,e.exports.default=e.exports;var t=RegExp.prototype,r=new WeakMap;function a(e,t,o){var u=new RegExp(e,t);return r.set(u,o||r.get(e)),n(u,a.prototype)}function c(e,t){var o=r.get(t);return Object.keys(o).reduce((function(t,r){var n=o[r];if("number"==typeof n)t[r]=e[n];else{for(var u=0;void 0===e[n[u]]&&u+1<n.length;)u++;t[r]=e[n[u]]}return t}),Object.create(null))}return u(a,RegExp),a.prototype.exec=function(e){var r=t.exec.call(this,e);if(r){r.groups=c(r,this);var o=r.indices;o&&(o.groups=c(o,this))}return r},a.prototype[Symbol.replace]=function(e,n){if("string"==typeof n){var u=r.get(this);return t[Symbol.replace].call(this,e,n.replace(/\$<([^>]+)>/g,(function(e,t){var r=u[t];return"$"+(Array.isArray(r)?r.join("$"):r)})))}if("function"==typeof n){var i=this;return t[Symbol.replace].call(this,e,(function(){var e=arguments;return"object"!=o(e[e.length-1])&&(e=[].slice.call(e)).push(c(e,i)),n.apply(this,e)}))}return t[Symbol.replace].call(this,e,n)},i.apply(this,arguments)}e.exports=i,e.exports.__esModule=!0,e.exports.default=e.exports}},t={};function r(o){var n=t[o];if(void 0!==n)return n.exports;var u=t[o]={exports:{}};return e[o](u,u.exports,r),u.exports}r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,{a:t}),t},r.d=function(e,t){for(var o in t)r.o(t,o)&&!r.o(e,o)&&Object.defineProperty(e,o,{enumerable:!0,get:t[o]})},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var o={};return function(){"use strict";r.r(o),r.d(o,{androidWebView:function(){return x},baseUrl:function(){return f},bridgeBootstrap:function(){return g},bridgeCall:function(){return b},environment:function(){return s},getLocale:function(){return S},iOSWebview:function(){return y},inSuperAcme:function(){return h},isAWSDomain:function(){return l},region:function(){return p},superAcmeVersion:function(){return _}});var e=r(583),t=r.n(e)()(/(dev|test|pre|gray)/,{env:1}),n=["us","usa","cn","chn","jp","jpn","de","deu","uk","gbr","fr","fra","ca","can","sg","sgp","au","aus","kr","kor","in","ind","br","bra","ru","rus","za","zaf","mx","mex","id","idn","tr","tur","sa","sau","ch","che","tw","twn","hk","hkg","mo","mac"],u=new RegExp("-(?<region>".concat(n.join("|"),")(-|$|\\.)")),i=/-aws\./,a=/^(localhost|127\.0\.0\.1|0\.0\.0\.0)(:\d+)?$/,c=new RegExp("^(?:[a-z0-9-]+\\.)*(?:".concat(["superacme.com","cinmoore.com","cinmoore.cn","safio365.com","sensforge.com"].join("|"),")$")),s=function(){var e=location.host;if(a.test(e))return"local";if(!c.test(e))return"unknown";var r=e.match(t);return r&&r.groups&&r.groups.env?r.groups.env:"prod"},p=function(){var e=location.host;if(!c.test(e))return"unknown";var t=e.match(new RegExp("-(".concat(n.join("|"),")-(dev|test|pre|gray)(?=-|\\.|$)")));if(t)return t[1];var r=e.match(u);return r&&r.groups&&r.groups.region?r.groups.region:"unknown"};function l(){var e=location.host;return!!c.test(e)&&i.test(e)}function f(){var e=s(),t=p();return"dev"===e||"test"===e||"pre"===e?"//api-".concat(t,"-").concat(e).concat(l()?"-aws":"",".superacme.com").concat(""):"prod"===e?"//api-".concat(t).concat(l()?"-aws":"",".superacme.com").concat(""):void 0}var d,v=r(216),m=r.n(v),y=(d=window.navigator.userAgent.toLocaleLowerCase()).includes("ios")||d.includes("iphone")||d.includes("ipad"),x=window.navigator.userAgent.toLocaleLowerCase().includes("android"),w=[];function b(e,t,r){window.SuperAcme&&"function"==typeof window.SuperAcme.call?window.SuperAcme.call(e,t,r):w.push({method:e,params:t,callback:r})}function g(){y&&function(e){if(window.SuperAcme&&"function"==typeof window.SuperAcme.call)return e();if(window.WKWVJBCallbacks)return window.WKWVJBCallbacks.push(e);var t,r;window.WKWVJBCallbacks=[e],window.webkit&&(null===(t=window.webkit.messageHandlers)||void 0===t||null===(t=t.iOS_Native_InjectJavascript)||void 0===t||null===(r=t.postMessage)||void 0===r||r.call(t,null))}((function(){var e=new CustomEvent("WebViewJavascriptBridgeReady",{detail:{platform:"ios"}});document.dispatchEvent(e)}))}function h(){return navigator.userAgent.toLowerCase().includes("superacme")}function _(){var e=navigator.userAgent,t={version:"unknown"};if(!h())return t;var r=e.match(/AppVersion\s([\d.]+)/);if(r){var o=r[1],n=o.split(".").map(Number),u=m()(n,3);return{version:o,major:u[0],minor:u[1],patch:u[2]}}return t}function S(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"zh-CN",t=e,r=window.location.search,o=window.navigator.userAgent;if(o.toLocaleLowerCase().indexOf("en-us")>-1&&(t="en-US"),r){for(var n={},u=r.substring(1),i=u.split("&"),a=0;a<i.length;a++){var c=i[a].split("="),s=decodeURIComponent(c[0]),p=decodeURIComponent(c[1]||"");n[s]=p}n.locale&&(t=n.locale)}return t}document.addEventListener("WebViewJavascriptBridgeReady",(function(){w.forEach((function(e){var t=e.method,r=e.params,o=e.callback;window.SuperAcme&&"function"==typeof window.SuperAcme.call&&window.SuperAcme.call(t,r,o)})),w.length=0}))}(),o}()}));
|
package/jest.config.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@microbt/environment",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "microbt app library for environment",
|
|
5
|
+
"keywords": [],
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"main": "dist/umd/shared.min.js",
|
|
8
|
+
"types": "dist/esm/index.d.ts",
|
|
9
|
+
"lint-staged": {
|
|
10
|
+
"*.{md,json}": [
|
|
11
|
+
"prettier --cache --write"
|
|
12
|
+
],
|
|
13
|
+
"*.{js,jsx}": [
|
|
14
|
+
"eslint --fix",
|
|
15
|
+
"prettier --cache --write"
|
|
16
|
+
],
|
|
17
|
+
"*.ts?(x)": [
|
|
18
|
+
"eslint --fix",
|
|
19
|
+
"prettier --cache --parser=typescript --write"
|
|
20
|
+
]
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"alife-logger": "^1.8.30"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@commitlint/cli": "^17.4.4",
|
|
27
|
+
"@commitlint/config-conventional": "^17.4.4",
|
|
28
|
+
"@types/jest": "^29.5.14",
|
|
29
|
+
"@typescript-eslint/eslint-plugin": "^5.54.1",
|
|
30
|
+
"@typescript-eslint/parser": "^5.54.1",
|
|
31
|
+
"eslint": "^8.36.0",
|
|
32
|
+
"father": "^4.1.0",
|
|
33
|
+
"husky": "^8.0.3",
|
|
34
|
+
"jest": "^29.7.0",
|
|
35
|
+
"jest-environment-jsdom": "^29.7.0",
|
|
36
|
+
"lint-staged": "^13.2.0",
|
|
37
|
+
"prettier": "^2.8.4",
|
|
38
|
+
"prettier-plugin-organize-imports": "^3.2.2",
|
|
39
|
+
"prettier-plugin-packagejson": "^2.4.3",
|
|
40
|
+
"ts-jest": "^29.2.5"
|
|
41
|
+
},
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public",
|
|
44
|
+
"registry": "https://registry.npmjs.org"
|
|
45
|
+
},
|
|
46
|
+
"authors": [],
|
|
47
|
+
"scripts": {
|
|
48
|
+
"build": "father build",
|
|
49
|
+
"build:deps": "father prebundle",
|
|
50
|
+
"dev": "father dev",
|
|
51
|
+
"format": "prettier --cache --write .",
|
|
52
|
+
"lint": "eslint . --fix ",
|
|
53
|
+
"test": "jest"
|
|
54
|
+
}
|
|
55
|
+
}
|