@hylid/env 3.3.0-alpha.13 → 3.3.0-alpha.19

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.
@@ -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,26 @@
1
+ import { CLIENT } from "../constants";
2
+ export var mpChecker = [{
3
+ name: CLIENT.DEMO,
4
+ match: [/demo/]
5
+ }, {
6
+ name: CLIENT.ALIPAY,
7
+ match: [/alipay/]
8
+ }, {
9
+ name: CLIENT.ALIPAYHK,
10
+ match: [/hk/]
11
+ }, {
12
+ name: CLIENT.TNGD,
13
+ match: [/tngd/]
14
+ }, {
15
+ name: CLIENT.GCASH,
16
+ match: [/gcash/]
17
+ }, {
18
+ name: CLIENT.TRUEMONEY,
19
+ match: [/tmn/]
20
+ }, {
21
+ name: CLIENT.DANA,
22
+ match: [/dana/]
23
+ }, {
24
+ name: CLIENT.TOSSPAY,
25
+ match: [/toss/]
26
+ }];
@@ -0,0 +1,2 @@
1
+ import { Checker } from '../types';
2
+ export declare const mpWebChecker: Checker[];
@@ -0,0 +1,26 @@
1
+ import { CLIENT, HAVE_GRIVER, HAVE_MINIPROGRAM, NO_GRIVER } from "../constants";
2
+ // 优先从 URL 里带过来 __app__ 参数进行识别
3
+ export var mpWebChecker = [{
4
+ name: CLIENT.DEMO,
5
+ match: [/alipayIDE/i] // IDE 识别为 demo
6
+ }, {
7
+ name: CLIENT.ALIPAYHK,
8
+ match: [HAVE_MINIPROGRAM, NO_GRIVER, /AlipayClientHK/]
9
+ }, {
10
+ name: CLIENT.TNGD,
11
+ match: [HAVE_MINIPROGRAM, /TNGKit|Touch 'n Go/]
12
+ }, {
13
+ name: CLIENT.GCASH,
14
+ match: [HAVE_MINIPROGRAM, HAVE_GRIVER, /GCash/]
15
+ }, {
16
+ name: CLIENT.TRUEMONEY,
17
+ match: [HAVE_MINIPROGRAM, HAVE_GRIVER, /TrueMoney/]
18
+ }, {
19
+ name: CLIENT.ALIPAY,
20
+ match: [HAVE_MINIPROGRAM, NO_GRIVER, /alipay/i]
21
+ },
22
+ // TODO: A+ 钱包套壳 UA 待真机测试后补充
23
+ {
24
+ name: CLIENT.DEMO,
25
+ match: [HAVE_MINIPROGRAM, HAVE_GRIVER]
26
+ }];
@@ -0,0 +1,2 @@
1
+ import { Checker } from '../types';
2
+ export declare const webChecker: Checker[];
@@ -0,0 +1,38 @@
1
+ import { CLIENT, NO_MINIPROGRAM, NO_GRIVER, HAVE_GRIVER } from "../constants";
2
+ export var webChecker = [{
3
+ name: CLIENT.ALIPAYHK,
4
+ match: [NO_MINIPROGRAM, NO_GRIVER, /AlipayClientHK/]
5
+ }, {
6
+ name: CLIENT.TNGD,
7
+ match: [NO_MINIPROGRAM, HAVE_GRIVER, /TNGKit|Touch 'n Go/]
8
+ }, {
9
+ name: CLIENT.TNGD_H5NG,
10
+ match: [NO_MINIPROGRAM, /TNGKit|Touch 'n Go/, /IAPWebH5NG/]
11
+ }, {
12
+ name: CLIENT.GCASH,
13
+ match: [NO_MINIPROGRAM, HAVE_GRIVER, /GCash/]
14
+ }, {
15
+ name: CLIENT.TRUEMONEY,
16
+ match: [NO_MINIPROGRAM, HAVE_GRIVER, /TrueMoney/i]
17
+ }, {
18
+ name: CLIENT.KAKAOPAY,
19
+ match: [NO_MINIPROGRAM, /PAYCLIENT|KAKAOTALK|KAKAOPAY/]
20
+ }, {
21
+ name: CLIENT.KAKAOTALK,
22
+ match: [NO_MINIPROGRAM, /ALIPAYCONNECT_KAKAO_TALK/]
23
+ }, {
24
+ name: CLIENT.NAVERPAY,
25
+ match: [NO_MINIPROGRAM, /NAVER/]
26
+ }, {
27
+ name: CLIENT.MPAY,
28
+ match: [NO_MINIPROGRAM, /mpay|macaupay/i]
29
+ }, {
30
+ name: CLIENT.TOSSPAY,
31
+ match: [NO_MINIPROGRAM, /Toss/i]
32
+ }, {
33
+ name: CLIENT.ALIPAY,
34
+ match: [NO_MINIPROGRAM, NO_GRIVER, /AlipayClient/]
35
+ }, {
36
+ name: CLIENT.DEMO,
37
+ match: [NO_MINIPROGRAM, HAVE_GRIVER, /GriverDemo/]
38
+ }];
@@ -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,9 @@
1
+ import { mpChecker } from "../checker";
2
+ import { CLIENT } from "../constants";
3
+ import { getMatchName } from "../utils";
4
+ export function getName4MP() {
5
+ // 微信小程序
6
+ // @ts-ignore
7
+ if (typeof wx !== 'undefined') return CLIENT.WECHAT;
8
+ return getMatchName(mpChecker, my.getSystemInfoSync().app);
9
+ }
@@ -0,0 +1 @@
1
+ export declare function getName4MpWeb(): string | undefined;
@@ -0,0 +1,5 @@
1
+ import { mpWebChecker } from "../checker";
2
+ import { getMatchName } from "../utils";
3
+ export function getName4MpWeb() {
4
+ return getMatchName(mpWebChecker, window.navigator.userAgent);
5
+ }
@@ -0,0 +1 @@
1
+ export declare function getName4Web(): string;
@@ -0,0 +1,6 @@
1
+ import { webChecker } from "../checker";
2
+ import { CLIENT } from "../constants";
3
+ import { getMatchName } from "../utils";
4
+ export function getName4Web() {
5
+ return getMatchName(webChecker, window.navigator.userAgent) || CLIENT.H5;
6
+ }
@@ -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,16 @@
1
+ import { ClientEnv, PlatformEnv } from './types';
2
+ export declare function getDefaultDiscernEnv(): {
3
+ client: string | undefined;
4
+ platform: string | undefined;
5
+ };
6
+ declare let client: string | undefined, platform: string | undefined;
7
+ interface CustomEnv {
8
+ client?: string;
9
+ platform?: string;
10
+ }
11
+ export declare function customEnv(config: CustomEnv | ((env: CustomEnv) => CustomEnv)): void;
12
+ declare const clientEnv: ClientEnv;
13
+ declare const platformEnv: PlatformEnv;
14
+ export { CLIENT, PLATFORM } from './constants';
15
+ export { client, platform, clientEnv, platformEnv };
16
+ export type { ClientEnv, PlatformEnv, CustomEnv };
package/lib/index.js ADDED
@@ -0,0 +1,56 @@
1
+ import { createCommonEnv, getSettingName, isMP, notice } from "./utils";
2
+ import { getName4MP, getName4MpWeb, getName4Web } from "./getClientName";
3
+ import { CLIENT, PLATFORM } from "./constants";
4
+ export function getDefaultDiscernEnv() {
5
+ var client;
6
+ var platform;
7
+ if (isMP()) {
8
+ client = getName4MP();
9
+ platform = PLATFORM.MP;
10
+ } else {
11
+ var setting = getSettingName();
12
+ if (setting.platform) {
13
+ platform = setting.platform;
14
+ client = setting.client || (platform === 'mpweb' ? getName4MpWeb() : getName4Web());
15
+ } else {
16
+ // 如果只设置了 client,默认是套壳场景
17
+ client = setting.client || getName4MpWeb();
18
+ if (client) {
19
+ platform = PLATFORM.MPWEB;
20
+ } else {
21
+ client = getName4Web();
22
+ platform = PLATFORM.WEB;
23
+ }
24
+ }
25
+ }
26
+ if (!platform || !client) {
27
+ notice('Cannot identify your client.');
28
+ }
29
+ return {
30
+ client: client,
31
+ platform: platform
32
+ };
33
+ }
34
+ var _a = getDefaultDiscernEnv(),
35
+ client = _a.client,
36
+ platform = _a.platform;
37
+ export function customEnv(config) {
38
+ var custom = typeof config === 'function' ? config({
39
+ client: client,
40
+ platform: platform
41
+ }) : config;
42
+ if (custom.client) {
43
+ client = custom.client;
44
+ // @ts-ignore
45
+ clientEnv["is".concat(client.toUpperCase())] = true;
46
+ }
47
+ if (custom.platform) {
48
+ platform = custom.platform;
49
+ // @ts-ignore
50
+ platformEnv["is".concat(platform.toUpperCase())] = true;
51
+ }
52
+ }
53
+ var clientEnv = createCommonEnv(CLIENT, client);
54
+ var platformEnv = createCommonEnv(PLATFORM, platform);
55
+ export { CLIENT, PLATFORM } from "./constants";
56
+ 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,6 @@
1
+ import { Checker } from './types';
2
+ export declare function isMP(): 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 getSettingName(): any;
package/lib/utils.js ADDED
@@ -0,0 +1,48 @@
1
+ export function isMP() {
2
+ // @ts-ignore
3
+ if (typeof wx !== 'undefined') return true;
4
+ // 小程序一定有 my
5
+ if (typeof my === 'undefined') return false;
6
+ // SDKVersion 只在小程序有
7
+ if (typeof my.SDKVersion === 'string') return true;
8
+ return false;
9
+ }
10
+ export function getMatchName(checks, pattern) {
11
+ var matches = checks.filter(function (check) {
12
+ return check.match.every(function (reg) {
13
+ return reg.test(pattern);
14
+ });
15
+ });
16
+ if (!matches.length) return;
17
+ var defaultName = matches[0].name;
18
+ if (matches.length > 1) {
19
+ var matchedName = matches.map(function (i) {
20
+ return i.name;
21
+ }).join(',');
22
+ notice("Multiple clients have been identified\uFF08".concat(matchedName, "\uFF09, and the first one\uFF08").concat(defaultName, "\uFF09is used by default."));
23
+ }
24
+ return defaultName;
25
+ }
26
+ export function createCommonEnv(source, current) {
27
+ return Object.keys(source).reduce(function (env, key) {
28
+ env["is".concat(key)] = current === source[key];
29
+ return env;
30
+ }, {});
31
+ }
32
+ export function notice(text) {
33
+ console.info("[hylid-env]: ".concat(text));
34
+ }
35
+ export function getSettingName() {
36
+ var _a;
37
+ var clientMatch = ((_a = window.location.search) === null || _a === void 0 ? void 0 : _a.match(/__app__=([^&]*)/)) || [];
38
+ var client = clientMatch[1];
39
+ var platform = (window.location.search.match(/[?&]__platform__=([^?&=]+)/) || [])[1];
40
+ if (client || platform) {
41
+ localStorage.setItem('__hy_env__', JSON.stringify({
42
+ client: client,
43
+ platform: platform
44
+ }));
45
+ }
46
+ var cached = localStorage.getItem('__hy_env__');
47
+ return cached ? JSON.parse(cached) : {};
48
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hylid/env",
3
- "version": "3.3.0-alpha.13",
3
+ "version": "3.3.0-alpha.19",
4
4
  "main": "lib/index.js",
5
5
  "files": [
6
6
  "lib"