@hylid/env 2.12.0-alpha.18

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 ADDED
@@ -0,0 +1,17 @@
1
+ # @hylid/env
2
+
3
+ ## Install
4
+
5
+ ```bash
6
+ npm i @hylid/env --save
7
+ ```
8
+
9
+ ## API
10
+
11
+ ```ts
12
+ import { getAppEnv, getPlatformEnv, getAppName, getPlatformName, APP, PLATFORM } from '@hylid/env';
13
+ ```
14
+
15
+ ## License
16
+
17
+ MIT
@@ -0,0 +1,3 @@
1
+ export { mpChecker } from './mp';
2
+ export { mpWebChecker } from './mpWeb';
3
+ export { webChecker } from './web';
@@ -0,0 +1,3 @@
1
+ export { mpChecker } from "./mp";
2
+ export { mpWebChecker } from "./mpWeb";
3
+ export { webChecker } from "./web";
@@ -0,0 +1,2 @@
1
+ import { Checker } from '../types';
2
+ export declare const mpChecker: Checker[];
@@ -0,0 +1,23 @@
1
+ import { CLIENT } from "../constants";
2
+ export var mpChecker = [{
3
+ name: CLIENT.DEMO,
4
+ match: [/demo/i]
5
+ }, {
6
+ name: CLIENT.ALIPAY,
7
+ match: [/alipay/i]
8
+ }, {
9
+ name: CLIENT.ALIPAYHK,
10
+ match: [/hk/i]
11
+ }, {
12
+ name: CLIENT.TNGD,
13
+ match: [/tngd/i]
14
+ }, {
15
+ name: CLIENT.GCASH,
16
+ match: [/gcash/i]
17
+ }, {
18
+ name: CLIENT.TRUEMONEY,
19
+ match: [/tmn/i]
20
+ }, {
21
+ name: CLIENT.DANA,
22
+ match: [/dana/i]
23
+ }];
@@ -0,0 +1,2 @@
1
+ import { Checker } from '../types';
2
+ export declare const mpWebChecker: Checker[];
@@ -0,0 +1,13 @@
1
+ import { CLIENT, HAVE_GRIVER, HAVE_MINIPROGRAM, NO_GRIVER } from "../constants";
2
+ export var mpWebChecker = [{
3
+ name: CLIENT.DEMO,
4
+ match: [/alipayIDE/i] // IDE 识别为 demo
5
+ }, {
6
+ name: CLIENT.ALIPAY,
7
+ match: [HAVE_MINIPROGRAM, NO_GRIVER, /alipay/i]
8
+ },
9
+ // TODO: A+ 钱包套壳 UA 待真机测试后补充
10
+ {
11
+ name: CLIENT.DEMO,
12
+ match: [HAVE_MINIPROGRAM, HAVE_GRIVER]
13
+ }];
@@ -0,0 +1,2 @@
1
+ import { Checker } from '../types';
2
+ export declare const webChecker: Checker[];
@@ -0,0 +1,35 @@
1
+ import { CLIENT, NO_MINIPROGRAM, NO_GRIVER, HAVE_GRIVER } from "../constants";
2
+ export var webChecker = [{
3
+ name: CLIENT.ALIPAY,
4
+ match: [NO_MINIPROGRAM, NO_GRIVER, /AlipayClient/i]
5
+ }, {
6
+ name: CLIENT.ALIPAYHK,
7
+ match: [NO_MINIPROGRAM, NO_GRIVER, /AlipayClientHK/i]
8
+ }, {
9
+ name: CLIENT.TNGD,
10
+ match: [NO_MINIPROGRAM, HAVE_GRIVER, /TNGKit|Touch 'n Go/i]
11
+ }, {
12
+ name: CLIENT.TNGD_H5NG,
13
+ match: [NO_MINIPROGRAM, /TNGKit|Touch 'n Go/i, /IAPWebH5NG/i]
14
+ }, {
15
+ name: CLIENT.GCASH,
16
+ match: [NO_MINIPROGRAM, HAVE_GRIVER, /GCash/i]
17
+ }, {
18
+ name: CLIENT.TRUEMONEY,
19
+ match: [NO_MINIPROGRAM, HAVE_GRIVER, /TrueMoney/i]
20
+ }, {
21
+ name: CLIENT.KAKAOPAY,
22
+ match: [NO_MINIPROGRAM, /PAYCLIENT|KAKAOTALK|KAKAOPAY/i]
23
+ }, {
24
+ name: CLIENT.KAKAOTALK,
25
+ match: [NO_MINIPROGRAM, /ALIPAYCONNECT_KAKAO_TALK/i]
26
+ }, {
27
+ name: CLIENT.NAVERPAY,
28
+ match: [NO_MINIPROGRAM, /NAVER/i]
29
+ }, {
30
+ name: CLIENT.MPAY,
31
+ match: [NO_MINIPROGRAM, /mpay|macaupay/i]
32
+ }, {
33
+ name: CLIENT.TOSSPAY,
34
+ match: [NO_MINIPROGRAM, /Toss/i]
35
+ }];
@@ -0,0 +1,35 @@
1
+ export declare const PLATFORM: {
2
+ WEB: string;
3
+ MPWEB: string;
4
+ MP: string;
5
+ };
6
+ export declare const CLIENT: {
7
+ DEMO: string;
8
+ ALIPAY: string;
9
+ ALIPAYHK: string;
10
+ TNGD: string;
11
+ GCASH: string;
12
+ TRUEMONEY: string;
13
+ DANA: string;
14
+ KAKAOPAY: string;
15
+ KAKAOTALK: string;
16
+ NAVERPAY: string;
17
+ MPAY: string;
18
+ TOSSPAY: string;
19
+ HIPAY: string;
20
+ CHOPE: string;
21
+ VODAPAY: string;
22
+ CHANGIPAY: string;
23
+ OCBC: string;
24
+ MANDIRI: string;
25
+ NAYAPAY: string;
26
+ TINABA: string;
27
+ KASPI: string;
28
+ H5: string;
29
+ WECHAT: string;
30
+ TNGD_H5NG: string;
31
+ };
32
+ export declare const HAVE_MINIPROGRAM: RegExp;
33
+ export declare const NO_MINIPROGRAM: RegExp;
34
+ export declare const HAVE_GRIVER: RegExp;
35
+ export declare const NO_GRIVER: RegExp;
@@ -0,0 +1,36 @@
1
+ export var PLATFORM = {
2
+ WEB: 'web',
3
+ MPWEB: 'mpweb',
4
+ MP: 'mp'
5
+ };
6
+ // bridge 下面新增加客户端时,文件夹名称和配置名称需要和这里保持对齐
7
+ export var CLIENT = {
8
+ DEMO: 'demo',
9
+ ALIPAY: 'alipay',
10
+ ALIPAYHK: 'alipayhk',
11
+ TNGD: 'tngd',
12
+ GCASH: 'gcash',
13
+ TRUEMONEY: 'truemoney',
14
+ DANA: 'dana',
15
+ KAKAOPAY: 'kakaopay',
16
+ KAKAOTALK: 'kakaotalk',
17
+ NAVERPAY: 'naverpay',
18
+ MPAY: 'mpay',
19
+ TOSSPAY: 'toss',
20
+ HIPAY: 'hipay',
21
+ CHOPE: 'chope',
22
+ VODAPAY: 'vodapay',
23
+ CHANGIPAY: 'changipay',
24
+ OCBC: 'ocbc',
25
+ MANDIRI: 'mandiri',
26
+ NAYAPAY: 'nayapay',
27
+ TINABA: 'tinaba',
28
+ KASPI: 'kaspi',
29
+ H5: 'h5',
30
+ WECHAT: 'wechat',
31
+ TNGD_H5NG: 'tngdh5ng'
32
+ };
33
+ export var HAVE_MINIPROGRAM = /miniprogram/i;
34
+ export var NO_MINIPROGRAM = /^((?!miniprogram).)*$/i;
35
+ export var HAVE_GRIVER = /griver/i;
36
+ export var NO_GRIVER = /^((?!griver).)*$/i;
@@ -0,0 +1 @@
1
+ export declare function getName4MP(): string | undefined;
@@ -0,0 +1,11 @@
1
+ import { mpChecker } from "../checker";
2
+ import { CLIENT } from "../constants";
3
+ import { getMatchName, isNotMP } from "../utils";
4
+ export function getName4MP() {
5
+ // 微信小程序
6
+ // @ts-ignore
7
+ if (typeof wx !== 'undefined') return CLIENT.WECHAT;
8
+ // 支付宝系小程序
9
+ if (isNotMP()) return;
10
+ return getMatchName(mpChecker, my.getSystemInfoSync().app);
11
+ }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * 1. 优先从 URL 解析 __app__
3
+ * 2. 其次从 Storage 解析 __app__
4
+ * 3. 再之解析 userAgent
5
+ */
6
+ export declare function getName4MpWeb(): string | undefined;
@@ -0,0 +1,20 @@
1
+ import { mpChecker, mpWebChecker } from "../checker";
2
+ import { CLIENT } from "../constants";
3
+ import { getMatchName, getUrlClientName, getStorageClientName } from "../utils";
4
+ /**
5
+ * 1. 优先从 URL 解析 __app__
6
+ * 2. 其次从 Storage 解析 __app__
7
+ * 3. 再之解析 userAgent
8
+ */
9
+ export function getName4MpWeb() {
10
+ var appName = getUrlClientName() || getStorageClientName();
11
+ if (appName) {
12
+ // 如果 hylid-bridge 的 client 枚举值找到了 app,则优先用 hylid-bridge 的
13
+ if (Object.keys(CLIENT).find(function (i) {
14
+ return CLIENT[i] === appName;
15
+ })) return appName;
16
+ // 套壳的 __app__ 参数,一般是壳子拼的,值从 getSystemInfoSync 取的,所以使用 mpChecker
17
+ return getMatchName(mpChecker, appName);
18
+ }
19
+ return getMatchName(mpWebChecker, window.navigator.userAgent);
20
+ }
@@ -0,0 +1 @@
1
+ export declare function getName4Web(): string | undefined;
@@ -0,0 +1,16 @@
1
+ import { webChecker, mpChecker } from "../checker";
2
+ import { CLIENT } from "../constants";
3
+ import { getMatchName, getUrlClientName, getStorageClientName } from "../utils";
4
+ export function getName4Web() {
5
+ var appName = getUrlClientName() || getStorageClientName();
6
+ // 无法识别的,可以手动指定 __app__ 参数
7
+ if (appName) {
8
+ // 如果 hylid-bridge 的 client 枚举值找到了 app,则优先用 hylid-bridge 的
9
+ if (Object.keys(CLIENT).find(function (i) {
10
+ return CLIENT[i] === appName;
11
+ })) return appName;
12
+ // 指定的 __app__ 和小程序返回值对齐
13
+ return getMatchName(mpChecker, appName);
14
+ }
15
+ return getMatchName(webChecker, window.navigator.userAgent) || CLIENT.H5;
16
+ }
@@ -0,0 +1,3 @@
1
+ export { getName4MP } from './getName4Mp';
2
+ export { getName4MpWeb } from './getName4MpWeb';
3
+ export { getName4Web } from './getName4Web';
@@ -0,0 +1,3 @@
1
+ export { getName4MP } from "./getName4Mp";
2
+ export { getName4MpWeb } from "./getName4MpWeb";
3
+ export { getName4Web } from "./getName4Web";
package/lib/index.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ import { ClientEnv, PlatformEnv } from './types';
2
+ declare let client: string | undefined;
3
+ declare let platform: string | undefined;
4
+ interface CustomEnv {
5
+ client?: string;
6
+ platform?: string;
7
+ }
8
+ export declare function customEnv(config: CustomEnv | ((env: CustomEnv) => CustomEnv)): void;
9
+ declare const clientEnv: ClientEnv;
10
+ declare const platformEnv: PlatformEnv;
11
+ export { CLIENT, PLATFORM } from './constants';
12
+ export { client, platform, clientEnv, platformEnv };
13
+ export type { ClientEnv, PlatformEnv, CustomEnv };
package/lib/index.js ADDED
@@ -0,0 +1,51 @@
1
+ import { createCommonEnv, notice } from "./utils";
2
+ import { getName4MP, getName4MpWeb, getName4Web } from "./getClientName";
3
+ import { CLIENT, PLATFORM } from "./constants";
4
+ var client;
5
+ var platform;
6
+ /**
7
+ * 优先判断小程序,根据 my.SDKVersion 是否存在进行判断
8
+ * 其次判断套壳 H5,根据 UA 是否存在 miniprogram 字段进行判断
9
+ * 再者判断端内 H5,UA 不存在 miniprogram 且含有钱包特征值进行判断
10
+ * 不满足上面的条件时,判断为端外 H5
11
+ */
12
+ // 小程序
13
+ client = getName4MP();
14
+ if (client) {
15
+ platform = PLATFORM.MP;
16
+ } else {
17
+ // 套壳 H5
18
+ client = getName4MpWeb();
19
+ if (client) {
20
+ platform = PLATFORM.MPWEB;
21
+ } else {
22
+ // 端内/端外 H5
23
+ client = getName4Web();
24
+ if (client) {
25
+ platform = PLATFORM.WEB;
26
+ }
27
+ }
28
+ }
29
+ if (!platform) {
30
+ notice('Cannot identify your client.');
31
+ }
32
+ export function customEnv(config) {
33
+ var custom = typeof config === 'function' ? config({
34
+ client: client,
35
+ platform: platform
36
+ }) : config;
37
+ if (custom.client) {
38
+ client = custom.client;
39
+ // @ts-ignore
40
+ clientEnv["is".concat(client.toUpperCase())] = true;
41
+ }
42
+ if (custom.platform) {
43
+ platform = custom.platform;
44
+ // @ts-ignore
45
+ platformEnv["is".concat(platform.toUpperCase())] = true;
46
+ }
47
+ }
48
+ var clientEnv = createCommonEnv(CLIENT, client);
49
+ var platformEnv = createCommonEnv(PLATFORM, platform);
50
+ export { CLIENT, PLATFORM } from "./constants";
51
+ export { client, platform, clientEnv, platformEnv };
package/lib/types.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ import { CLIENT, PLATFORM } from './constants';
2
+ export interface Checker {
3
+ name: string;
4
+ match: RegExp[];
5
+ }
6
+ declare type isClientEnv = `is${keyof typeof CLIENT}`;
7
+ declare type isPlatformEnv = `is${keyof typeof PLATFORM}`;
8
+ export declare type ClientEnv = {
9
+ [key in isClientEnv]: boolean;
10
+ };
11
+ export declare type PlatformEnv = {
12
+ [key in isPlatformEnv]: boolean;
13
+ };
14
+ export {};
package/lib/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/lib/utils.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ import { Checker } from './types';
2
+ export declare function isNotMP(): boolean;
3
+ export declare function getMatchName(checks: Checker[], pattern: string): string | undefined;
4
+ export declare function createCommonEnv<T>(source: Record<string, string>, current?: string): T;
5
+ export declare function notice(text: string): void;
6
+ export declare function getUrlClientName(): string;
7
+ export declare function getStorageClientName(): string | null;
package/lib/utils.js ADDED
@@ -0,0 +1,39 @@
1
+ export function isNotMP() {
2
+ // 小程序一定有 my
3
+ if (typeof my == 'undefined') return true;
4
+ // SDKVersion 只在小程序有
5
+ if (typeof my.SDKVersion !== 'string') return true;
6
+ return false;
7
+ }
8
+ export function getMatchName(checks, pattern) {
9
+ var matches = checks.filter(function (check) {
10
+ return check.match.every(function (reg) {
11
+ return reg.test(pattern);
12
+ });
13
+ });
14
+ if (!matches.length) return;
15
+ var defaultName = matches[0].name;
16
+ if (matches.length > 1) {
17
+ var matchedName = matches.map(function (i) {
18
+ return i.name;
19
+ }).join(',');
20
+ notice("Multiple clients have been identified\uFF08".concat(matchedName, "\uFF09, and the first one\uFF08").concat(defaultName, "\uFF09is used by default."));
21
+ }
22
+ return defaultName;
23
+ }
24
+ export function createCommonEnv(source, current) {
25
+ return Object.keys(source).reduce(function (env, key) {
26
+ env["is".concat(key)] = current === key;
27
+ return env;
28
+ }, {});
29
+ }
30
+ export function notice(text) {
31
+ console.info("[hylid-env]: ".concat(text));
32
+ }
33
+ export function getUrlClientName() {
34
+ return (window.location.search.match(/[?&]__app__=([^?&=]+)/) || [])[1];
35
+ }
36
+ export function getStorageClientName() {
37
+ // __app__ 值和 getSystemInfo 里的 app 保持对齐
38
+ return localStorage.getItem('__app__');
39
+ }
package/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "@hylid/env",
3
+ "version": "2.12.0-alpha.18",
4
+ "main": "lib/index.js",
5
+ "files": [
6
+ "lib"
7
+ ],
8
+ "publishConfig": {
9
+ "access": "public"
10
+ },
11
+ "license": "MIT",
12
+ "sideEffects": false,
13
+ "registry": "https://registry.npmjs.org/",
14
+ "repository": "https://code.alipay.com/ant-ife/hylid-bridge.git"
15
+ }