@hylid/env 2.12.0-alpha.41 → 2.12.0-alpha.9

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.
@@ -1,11 +1,9 @@
1
1
  import { mpChecker } from "../checker";
2
2
  import { CLIENT } from "../constants";
3
- import { getMatchName, isNotMP } from "../utils";
3
+ import { getMatchName } from "../utils";
4
4
  export function getName4MP() {
5
5
  // 微信小程序
6
6
  // @ts-ignore
7
7
  if (typeof wx !== 'undefined') return CLIENT.WECHAT;
8
- // 支付宝系小程序
9
- if (isNotMP()) return;
10
8
  return getMatchName(mpChecker, my.getSystemInfoSync().app);
11
9
  }
@@ -1,6 +1 @@
1
- /**
2
- * 1. 优先从 URL 解析 __app__
3
- * 2. 其次从 Storage 解析 __app__
4
- * 3. 再之解析 userAgent
5
- */
6
1
  export declare function getName4MpWeb(): string | undefined;
@@ -1,20 +1,5 @@
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
- */
1
+ import { mpWebChecker } from "../checker";
2
+ import { getMatchName } from "../utils";
9
3
  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
4
  return getMatchName(mpWebChecker, window.navigator.userAgent);
20
5
  }
@@ -1 +1 @@
1
- export declare function getName4Web(): string | undefined;
1
+ export declare function getName4Web(): string;
@@ -1,16 +1,6 @@
1
- import { webChecker, mpChecker } from "../checker";
1
+ import { webChecker } from "../checker";
2
2
  import { CLIENT } from "../constants";
3
- import { getMatchName, getUrlClientName, getStorageClientName } from "../utils";
3
+ import { getMatchName } from "../utils";
4
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
5
  return getMatchName(webChecker, window.navigator.userAgent) || CLIENT.H5;
16
6
  }
package/lib/index.d.ts CHANGED
@@ -1,6 +1,9 @@
1
1
  import { ClientEnv, PlatformEnv } from './types';
2
- declare let client: string | undefined;
3
- declare let platform: string | undefined;
2
+ export declare function getDefaultDiscernEnv(): {
3
+ client: string | undefined;
4
+ platform: string | undefined;
5
+ };
6
+ declare let client: string | undefined, platform: string | undefined;
4
7
  interface CustomEnv {
5
8
  client?: string;
6
9
  platform?: string;
package/lib/index.js CHANGED
@@ -1,34 +1,39 @@
1
- import { createCommonEnv, notice } from "./utils";
1
+ import { createCommonEnv, getSettingName, isMP, notice } from "./utils";
2
2
  import { getName4MP, getName4MpWeb, getName4Web } from "./getClientName";
3
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;
4
+ export function getDefaultDiscernEnv() {
5
+ var client;
6
+ var platform;
7
+ if (isMP()) {
8
+ client = getName4MP();
9
+ platform = PLATFORM.MP;
21
10
  } else {
22
- // 端内/端外 H5
23
- client = getName4Web();
24
- if (client) {
25
- platform = PLATFORM.WEB;
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
+ }
26
24
  }
27
25
  }
26
+ if (!platform || !client) {
27
+ notice('Cannot identify your client.');
28
+ }
29
+ return {
30
+ client: client,
31
+ platform: platform
32
+ };
28
33
  }
29
- if (!platform) {
30
- notice('Cannot identify your client.');
31
- }
34
+ var _a = getDefaultDiscernEnv(),
35
+ client = _a.client,
36
+ platform = _a.platform;
32
37
  export function customEnv(config) {
33
38
  var custom = typeof config === 'function' ? config({
34
39
  client: client,
package/lib/utils.d.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import { Checker } from './types';
2
- export declare function isNotMP(): boolean;
2
+ export declare function isMP(): boolean;
3
3
  export declare function getMatchName(checks: Checker[], pattern: string): string | undefined;
4
4
  export declare function createCommonEnv<T>(source: Record<string, string>, current?: string): T;
5
5
  export declare function notice(text: string): void;
6
- export declare function getUrlClientName(): string;
7
- export declare function getStorageClientName(): string | null;
6
+ export declare function getSettingName(): any;
package/lib/utils.js CHANGED
@@ -1,8 +1,10 @@
1
- export function isNotMP() {
1
+ export function isMP() {
2
+ // @ts-ignore
3
+ if (typeof wx !== 'undefined') return true;
2
4
  // 小程序一定有 my
3
- if (typeof my == 'undefined') return true;
5
+ if (typeof my === 'undefined') return false;
4
6
  // SDKVersion 只在小程序有
5
- if (typeof my.SDKVersion !== 'string') return true;
7
+ if (typeof my.SDKVersion === 'string') return true;
6
8
  return false;
7
9
  }
8
10
  export function getMatchName(checks, pattern) {
@@ -30,10 +32,15 @@ export function createCommonEnv(source, current) {
30
32
  export function notice(text) {
31
33
  console.info("[hylid-env]: ".concat(text));
32
34
  }
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__');
35
+ export function getSettingName() {
36
+ var client = (window.location.search.match(/[?&]__app__=([^?&=]+)/) || [])[1];
37
+ var platform = (window.location.search.match(/[?&]__platform__=([^?&=]+)/) || [])[1];
38
+ if (client || platform) {
39
+ localStorage.setItem('__hy_env__', JSON.stringify({
40
+ client: client,
41
+ platform: platform
42
+ }));
43
+ }
44
+ var cached = localStorage.getItem('__hy_env__');
45
+ return cached ? JSON.parse(cached) : {};
39
46
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hylid/env",
3
- "version": "2.12.0-alpha.41",
3
+ "version": "2.12.0-alpha.9",
4
4
  "main": "lib/index.js",
5
5
  "files": [
6
6
  "lib"
@@ -10,6 +10,5 @@
10
10
  },
11
11
  "license": "MIT",
12
12
  "sideEffects": false,
13
- "registry": "https://registry.npmjs.org/",
14
- "repository": "https://code.alipay.com/ant-ife/hylid-bridge.git"
15
- }
13
+ "registry": "https://registry.npmjs.org/"
14
+ }