miniprogram-platform 0.0.1-beta → 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 ADDED
@@ -0,0 +1,103 @@
1
+ # miniprogram-platform
2
+
3
+ 零依赖、极简的跨小程序平台检测工具。将各平台特有的全局变量(`wx`、`my`、`tt` 等)统一抽象为 `{ mp, name }`,方便跨小程序库的开发。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ npm install miniprogram-platform
9
+ ```
10
+
11
+ ## 使用
12
+
13
+ ```js
14
+ import { getPlatform } from 'miniprogram-platform';
15
+
16
+ const platform = getPlatform();
17
+
18
+ if (platform) {
19
+ console.log(platform.name); // "WeChat" | "Alipay" | "ByteDance" | ...
20
+ console.log(platform.mp); // wx | my | tt | ...
21
+
22
+ // 直接调用平台 API,无需关心当前是哪个小程序环境
23
+ platform.mp.request({ url: 'https://api.example.com/data' });
24
+ }
25
+ ```
26
+
27
+ > **注意**:`getPlatform()` 在首次调用时完成检测并缓存结果,后续调用均为 O(1)。
28
+
29
+ TypeScript 中可将 `mp` 断言为目标平台类型以获得完整的类型提示:
30
+
31
+ ```ts
32
+ if (platform?.name === 'WeChat') {
33
+ const wx = platform.mp as typeof wx;
34
+ wx.login({ ... }); // 完整的类型推断与补全
35
+ }
36
+ ```
37
+
38
+ JavaScript 中可通过 JSDoc 达到同样效果:
39
+
40
+ ```js
41
+ if (platform?.name === 'WeChat') {
42
+ /** @type {typeof wx} */
43
+ const wx = platform.mp;
44
+ wx.login({ ... }); // VSCode 中同样有类型提示
45
+ }
46
+ ```
47
+
48
+ ## API
49
+
50
+ ### `getPlatform()`
51
+
52
+ 返回当前运行环境的平台信息,若不在任何小程序环境中则返回 `undefined`。
53
+
54
+ ```ts
55
+ function getPlatform(): { mp: unknown; name: string } | undefined;
56
+ ```
57
+
58
+ | 属性 | 类型 | 说明 |
59
+ | ------ | --------- | ------------------------------------------- |
60
+ | `mp` | `unknown` | 平台全局 API 对象(如 `wx`、`my`、`tt` 等) |
61
+ | `name` | `string` | 平台名称 |
62
+
63
+ ## 支持的平台
64
+
65
+ | 平台名称 | 全局对象 | 说明 |
66
+ | --------- | -------- | ----------------- |
67
+ | WeChat | `wx` | 微信小程序 |
68
+ | Alipay | `my` | 支付宝小程序 |
69
+ | QQ | `qq` | QQ 小程序 |
70
+ | JD | `jd` | 京东小程序 |
71
+ | Baidu | `swan` | 百度智能小程序 |
72
+ | ByteDance | `tt` | 抖音 / 飞书小程序 |
73
+ | Kwai | `ks` | 快手小程序 |
74
+ | Qihu | `qh` | 360 小程序 |
75
+ | RedNote | `xhs` | 小红书小程序 |
76
+ | UniApp | `uni` | UniApp 跨端框架 |
77
+ | Taro | `Taro` | Taro 跨端框架 |
78
+
79
+ 检测逻辑:按优先级依次检查各平台的全局对象是否存在,并验证该对象是否具有 `request` 方法。匹配到第一个即返回,若全部未匹配则返回 `undefined`。
80
+
81
+ ## License
82
+
83
+ MIT License
84
+
85
+ Copyright (c) 2026
86
+
87
+ Permission is hereby granted, free of charge, to any person obtaining a copy
88
+ of this software and associated documentation files (the "Software"), to deal
89
+ in the Software without restriction, including without limitation the rights
90
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
91
+ copies of the Software, and to permit persons to whom the Software is
92
+ furnished to do so, subject to the following conditions:
93
+
94
+ The above copyright notice and this permission notice shall be included in all
95
+ copies or substantial portions of the Software.
96
+
97
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
98
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
99
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
100
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
101
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
102
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
103
+ SOFTWARE.
package/dist/cjs/index.js CHANGED
@@ -1,7 +1,25 @@
1
1
  'use strict';
2
2
 
3
- var platform = require('./platform.js');
3
+ // @ts-nocheck
4
+ const platform = { value: null };
5
+ function getPlatform() {
6
+ if (platform.value !== null)
7
+ return platform.value;
8
+ const u = "undefined", f = "function", r = "request";
9
+ platform.value =
10
+ (typeof wx !== u && typeof (wx === null || wx === void 0 ? void 0 : wx[r]) === f && wx && { mp: wx, name: "WeChat" }) || // 微信
11
+ (typeof my !== u && typeof (my === null || my === void 0 ? void 0 : my[r]) === f && my && { mp: my, name: "Alipay" }) || // 支付宝
12
+ (typeof qq !== u && typeof (qq === null || qq === void 0 ? void 0 : qq[r]) === f && qq && { mp: qq, name: "QQ" }) || // QQ
13
+ (typeof jd !== u && typeof (jd === null || jd === void 0 ? void 0 : jd[r]) === f && jd && { mp: jd, name: "JD" }) || // 京东
14
+ (typeof swan !== u && typeof (swan === null || swan === void 0 ? void 0 : swan[r]) === f && swan && { mp: swan, name: "Baidu" }) || // 百度
15
+ (typeof tt !== u && typeof (tt === null || tt === void 0 ? void 0 : tt[r]) === f && tt && { mp: tt, name: "ByteDance" }) || // 抖音 | 飞书
16
+ (typeof ks !== u && typeof (ks === null || ks === void 0 ? void 0 : ks[r]) === f && ks && { mp: ks, name: "Kwai" }) || // 快手
17
+ (typeof qh !== u && typeof (qh === null || qh === void 0 ? void 0 : qh[r]) === f && qh && { mp: qh, name: "Qihu" }) || // 360
18
+ (typeof xhs !== u && typeof (xhs === null || xhs === void 0 ? void 0 : xhs[r]) === f && xhs && { mp: xhs, name: "RedNote" }) || // 小红书
19
+ (typeof uni !== u && typeof (uni === null || uni === void 0 ? void 0 : uni[r]) === f && uni && { mp: uni, name: "UniApp" }) || // UniApp
20
+ (typeof Taro !== u && typeof (Taro === null || Taro === void 0 ? void 0 : Taro[r]) === f && Taro && { mp: Taro, name: "Taro" }) || // Taro
21
+ undefined;
22
+ return platform.value;
23
+ }
4
24
 
5
-
6
-
7
- exports.getPlatform = platform.getPlatform;
25
+ exports.getPlatform = getPlatform;
package/dist/esm/index.js CHANGED
@@ -1 +1,23 @@
1
- export { getPlatform } from './platform.js';
1
+ // @ts-nocheck
2
+ const platform = { value: null };
3
+ function getPlatform() {
4
+ if (platform.value !== null)
5
+ return platform.value;
6
+ const u = "undefined", f = "function", r = "request";
7
+ platform.value =
8
+ (typeof wx !== u && typeof (wx === null || wx === void 0 ? void 0 : wx[r]) === f && wx && { mp: wx, name: "WeChat" }) || // 微信
9
+ (typeof my !== u && typeof (my === null || my === void 0 ? void 0 : my[r]) === f && my && { mp: my, name: "Alipay" }) || // 支付宝
10
+ (typeof qq !== u && typeof (qq === null || qq === void 0 ? void 0 : qq[r]) === f && qq && { mp: qq, name: "QQ" }) || // QQ
11
+ (typeof jd !== u && typeof (jd === null || jd === void 0 ? void 0 : jd[r]) === f && jd && { mp: jd, name: "JD" }) || // 京东
12
+ (typeof swan !== u && typeof (swan === null || swan === void 0 ? void 0 : swan[r]) === f && swan && { mp: swan, name: "Baidu" }) || // 百度
13
+ (typeof tt !== u && typeof (tt === null || tt === void 0 ? void 0 : tt[r]) === f && tt && { mp: tt, name: "ByteDance" }) || // 抖音 | 飞书
14
+ (typeof ks !== u && typeof (ks === null || ks === void 0 ? void 0 : ks[r]) === f && ks && { mp: ks, name: "Kwai" }) || // 快手
15
+ (typeof qh !== u && typeof (qh === null || qh === void 0 ? void 0 : qh[r]) === f && qh && { mp: qh, name: "Qihu" }) || // 360
16
+ (typeof xhs !== u && typeof (xhs === null || xhs === void 0 ? void 0 : xhs[r]) === f && xhs && { mp: xhs, name: "RedNote" }) || // 小红书
17
+ (typeof uni !== u && typeof (uni === null || uni === void 0 ? void 0 : uni[r]) === f && uni && { mp: uni, name: "UniApp" }) || // UniApp
18
+ (typeof Taro !== u && typeof (Taro === null || Taro === void 0 ? void 0 : Taro[r]) === f && Taro && { mp: Taro, name: "Taro" }) || // Taro
19
+ undefined;
20
+ return platform.value;
21
+ }
22
+
23
+ export { getPlatform };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,9 @@
1
- declare function getPlatform(): {
2
- mp: unknown;
3
- name: string;
1
+ declare const platform: {
2
+ value: null | {
3
+ mp: unknown;
4
+ name: string;
5
+ };
4
6
  };
7
+ declare function getPlatform(): NonNullable<typeof platform.value> | undefined;
5
8
 
6
9
  export { getPlatform };
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "miniprogram-platform",
3
- "version": "0.0.1-beta",
3
+ "version": "1.0.0",
4
4
  "license": "MIT",
5
5
  "author": "Xingzeng Bao",
6
- "homepage": "",
7
- "description": "",
6
+ "homepage": "https://github.com/baoxingzeng/miniprogram-platform",
7
+ "description": "Zero-dependency platform detection for mini-program ecosystems.",
8
8
  "type": "module",
9
9
  "main": "dist/cjs/index.js",
10
10
  "module": "dist/esm/index.js",
@@ -1,23 +0,0 @@
1
- 'use strict';
2
-
3
- // @ts-nocheck
4
- function getPlatform() {
5
- let u = "undefined", f = "function", r = "request";
6
- let platform;
7
- platform =
8
- (typeof wx !== u && typeof (wx === null || wx === void 0 ? void 0 : wx[r]) === f && wx && { mp: wx, name: "WeChat" }) || // 微信
9
- (typeof my !== u && typeof (my === null || my === void 0 ? void 0 : my[r]) === f && my && { mp: my, name: "Alipay" }) || // 支付宝
10
- (typeof qq !== u && typeof (qq === null || qq === void 0 ? void 0 : qq[r]) === f && qq && { mp: qq, name: "QQ" }) || // QQ
11
- (typeof jd !== u && typeof (jd === null || jd === void 0 ? void 0 : jd[r]) === f && jd && { mp: jd, name: "JD" }) || // 京东
12
- (typeof swan !== u && typeof (swan === null || swan === void 0 ? void 0 : swan[r]) === f && swan && { mp: swan, name: "Baidu" }) || // 百度
13
- (typeof tt !== u && typeof (tt === null || tt === void 0 ? void 0 : tt[r]) === f && tt && { mp: tt, name: "ByteDance" }) || // 抖音 | 飞书
14
- (typeof ks !== u && typeof (ks === null || ks === void 0 ? void 0 : ks[r]) === f && ks && { mp: ks, name: "Kwai" }) || // 快手
15
- (typeof qh !== u && typeof (qh === null || qh === void 0 ? void 0 : qh[r]) === f && qh && { mp: qh, name: "Qihu" }) || // 360
16
- (typeof xhs !== u && typeof (xhs === null || xhs === void 0 ? void 0 : xhs[r]) === f && xhs && { mp: xhs, name: "RedNote" }) || // 小红书
17
- (typeof uni !== u && typeof (uni === null || uni === void 0 ? void 0 : uni[r]) === f && uni && { mp: uni, name: "UniApp" }) || // UniApp
18
- (typeof Taro !== u && typeof (Taro === null || Taro === void 0 ? void 0 : Taro[r]) === f && Taro && { mp: Taro, name: "Taro" }) || // Taro
19
- undefined;
20
- return platform;
21
- }
22
-
23
- exports.getPlatform = getPlatform;
@@ -1,21 +0,0 @@
1
- // @ts-nocheck
2
- function getPlatform() {
3
- let u = "undefined", f = "function", r = "request";
4
- let platform;
5
- platform =
6
- (typeof wx !== u && typeof (wx === null || wx === void 0 ? void 0 : wx[r]) === f && wx && { mp: wx, name: "WeChat" }) || // 微信
7
- (typeof my !== u && typeof (my === null || my === void 0 ? void 0 : my[r]) === f && my && { mp: my, name: "Alipay" }) || // 支付宝
8
- (typeof qq !== u && typeof (qq === null || qq === void 0 ? void 0 : qq[r]) === f && qq && { mp: qq, name: "QQ" }) || // QQ
9
- (typeof jd !== u && typeof (jd === null || jd === void 0 ? void 0 : jd[r]) === f && jd && { mp: jd, name: "JD" }) || // 京东
10
- (typeof swan !== u && typeof (swan === null || swan === void 0 ? void 0 : swan[r]) === f && swan && { mp: swan, name: "Baidu" }) || // 百度
11
- (typeof tt !== u && typeof (tt === null || tt === void 0 ? void 0 : tt[r]) === f && tt && { mp: tt, name: "ByteDance" }) || // 抖音 | 飞书
12
- (typeof ks !== u && typeof (ks === null || ks === void 0 ? void 0 : ks[r]) === f && ks && { mp: ks, name: "Kwai" }) || // 快手
13
- (typeof qh !== u && typeof (qh === null || qh === void 0 ? void 0 : qh[r]) === f && qh && { mp: qh, name: "Qihu" }) || // 360
14
- (typeof xhs !== u && typeof (xhs === null || xhs === void 0 ? void 0 : xhs[r]) === f && xhs && { mp: xhs, name: "RedNote" }) || // 小红书
15
- (typeof uni !== u && typeof (uni === null || uni === void 0 ? void 0 : uni[r]) === f && uni && { mp: uni, name: "UniApp" }) || // UniApp
16
- (typeof Taro !== u && typeof (Taro === null || Taro === void 0 ? void 0 : Taro[r]) === f && Taro && { mp: Taro, name: "Taro" }) || // Taro
17
- undefined;
18
- return platform;
19
- }
20
-
21
- export { getPlatform };