@wevu/api 0.0.1 → 0.1.1
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 +95 -9
- package/dist/index.cjs +503 -8
- package/dist/index.d.cts +196 -6
- package/dist/index.d.mts +196 -6
- package/dist/index.mjs +501 -7
- package/package.json +17 -7
- package/types/index.d.ts +54 -0
package/README.md
CHANGED
|
@@ -2,13 +2,59 @@
|
|
|
2
2
|
|
|
3
3
|
## 简介
|
|
4
4
|
|
|
5
|
-
`@wevu/api`
|
|
5
|
+
`@wevu/api` 提供跨平台的小程序 API 封装,默认推荐 Promise 风格,同时兼容传统回调风格。
|
|
6
6
|
|
|
7
7
|
## 特性
|
|
8
8
|
|
|
9
|
-
-
|
|
10
|
-
-
|
|
11
|
-
-
|
|
9
|
+
- 动态代理,覆盖微信小程序全量 API
|
|
10
|
+
- 跨平台适配(微信/支付宝/百度/字节/QQ/快应用/京东/小红书/快手/淘宝等)
|
|
11
|
+
- Promise 风格优先,回调风格可用
|
|
12
|
+
- 支持显式注入平台适配器
|
|
13
|
+
|
|
14
|
+
## 类型对齐与平台支持
|
|
15
|
+
|
|
16
|
+
`@wevu/api` 的默认导出 `wpi` 会同时对齐:
|
|
17
|
+
|
|
18
|
+
- 微信类型:`miniprogram-api-typings`
|
|
19
|
+
- 支付宝类型:`@mini-types/alipay`
|
|
20
|
+
- 抖音类型:`@douyin-microapp/typings`
|
|
21
|
+
|
|
22
|
+
<!-- prettier-ignore-start -->
|
|
23
|
+
<!-- @generated weapi-support-matrix:start -->
|
|
24
|
+
### 平台类型对齐矩阵
|
|
25
|
+
|
|
26
|
+
| 平台 | 全局对象 | 类型来源 | 支持度 |
|
|
27
|
+
| --- | --- | --- | --- |
|
|
28
|
+
| 微信小程序 | `wx` | `miniprogram-api-typings` | ✅ 全量 |
|
|
29
|
+
| 支付宝小程序 | `my` | `@mini-types/alipay` | ✅ 全量 |
|
|
30
|
+
| 抖音小程序 | `tt` | `@douyin-microapp/typings` | ✅ 全量 |
|
|
31
|
+
| 其他平台(swan/jd/xhs 等) | 运行时宿主对象 | 运行时透传 | ⚠️ 按宿主能力支持 |
|
|
32
|
+
|
|
33
|
+
### API 支持覆盖率报告
|
|
34
|
+
|
|
35
|
+
| 平台 | 已支持 API 数 | API 总数 | 覆盖率 |
|
|
36
|
+
| --- | --- | --- | --- |
|
|
37
|
+
| 微信小程序 (`wx`) | 8 | 8 | 100.00% |
|
|
38
|
+
| 支付宝小程序 (`my`) | 8 | 8 | 100.00% |
|
|
39
|
+
| 抖音小程序 (`tt`) | 8 | 8 | 100.00% |
|
|
40
|
+
| 三端完全对齐 (wx/my/tt) | 8 | 8 | 100.00% |
|
|
41
|
+
|
|
42
|
+
> 该报告由 `WEAPI_METHOD_SUPPORT_MATRIX` 与映射规则自动计算生成。
|
|
43
|
+
|
|
44
|
+
### 核心跨端映射矩阵
|
|
45
|
+
|
|
46
|
+
| API | 说明 | 微信策略 | 支付宝策略 | 抖音策略 | 支持度 |
|
|
47
|
+
| --- | --- | --- | --- | --- | --- |
|
|
48
|
+
| `showToast` | 显示消息提示框。 | 直连 `wx.showToast` | `title/icon` 映射到 `content/type` 后调用 `my.showToast` | `icon=error` 映射为 `fail` 后调用 `tt.showToast` | ✅ |
|
|
49
|
+
| `showLoading` | 显示 loading 提示框。 | 直连 `wx.showLoading` | `title` 映射到 `content` 后调用 `my.showLoading` | 直连 `tt.showLoading` | ✅ |
|
|
50
|
+
| `showActionSheet` | 显示操作菜单。 | 直连 `wx.showActionSheet` | `itemList` ↔ `items`、`index` ↔ `tapIndex` 双向对齐 | 直连 `tt.showActionSheet`,并兼容 `index` → `tapIndex` | ✅ |
|
|
51
|
+
| `showModal` | 显示模态弹窗。 | 直连 `wx.showModal` | 调用 `my.confirm` 并对齐按钮字段与 `cancel` 结果 | 直连 `tt.showModal` | ✅ |
|
|
52
|
+
| `chooseImage` | 选择图片。 | 直连 `wx.chooseImage` | 返回值 `apFilePaths` 映射到 `tempFilePaths` | `tempFilePaths` 字符串转数组,缺失时从 `tempFiles.path` 兜底 | ✅ |
|
|
53
|
+
| `saveFile` | 保存文件。 | 直连 `wx.saveFile` | 请求参数 `tempFilePath` ↔ `apFilePath`、结果映射为 `savedFilePath` | 直连 `tt.saveFile`,并在缺失时用 `filePath` 兜底 `savedFilePath` | ✅ |
|
|
54
|
+
| `setClipboardData` | 设置剪贴板内容。 | 直连 `wx.setClipboardData` | 转调 `my.setClipboard` 并映射 `data` → `text` | 直连 `tt.setClipboardData` | ✅ |
|
|
55
|
+
| `getClipboardData` | 获取剪贴板内容。 | 直连 `wx.getClipboardData` | 转调 `my.getClipboard` 并映射 `text` → `data` | 直连 `tt.getClipboardData` | ✅ |
|
|
56
|
+
<!-- @generated weapi-support-matrix:end -->
|
|
57
|
+
<!-- prettier-ignore-end -->
|
|
12
58
|
|
|
13
59
|
## 安装
|
|
14
60
|
|
|
@@ -18,16 +64,56 @@ pnpm add @wevu/api
|
|
|
18
64
|
|
|
19
65
|
## 使用
|
|
20
66
|
|
|
67
|
+
### Promise 风格(推荐)
|
|
68
|
+
|
|
21
69
|
```ts
|
|
22
|
-
import {
|
|
70
|
+
import { wpi } from '@wevu/api'
|
|
71
|
+
|
|
72
|
+
const res = await wpi.request({
|
|
73
|
+
url: 'https://example.com',
|
|
74
|
+
method: 'GET',
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
console.log(res)
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### 回调风格(兼容)
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
import { wpi } from '@wevu/api'
|
|
84
|
+
|
|
85
|
+
wpi.request({
|
|
86
|
+
url: 'https://example.com',
|
|
87
|
+
method: 'GET',
|
|
88
|
+
success(res) {
|
|
89
|
+
console.log(res)
|
|
90
|
+
},
|
|
91
|
+
fail(err) {
|
|
92
|
+
console.error(err)
|
|
93
|
+
},
|
|
94
|
+
})
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### 显式注入平台适配器
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
import { createWeapi } from '@wevu/api'
|
|
101
|
+
|
|
102
|
+
const api = createWeapi({
|
|
103
|
+
adapter: wx,
|
|
104
|
+
platform: 'wx',
|
|
105
|
+
})
|
|
23
106
|
|
|
24
|
-
|
|
25
|
-
console.log(VERSION)
|
|
107
|
+
await api.getSystemInfo()
|
|
26
108
|
```
|
|
27
109
|
|
|
28
|
-
##
|
|
110
|
+
## 行为说明
|
|
29
111
|
|
|
30
|
-
|
|
112
|
+
- **只在不传回调时返回 Promise**
|
|
113
|
+
- 同步 API(`*Sync`)与事件 API(`onXxx/offXxx`)直接透传
|
|
114
|
+
- 缺失 API 时:
|
|
115
|
+
- 回调风格触发 `fail/complete`
|
|
116
|
+
- Promise 风格返回 rejected Promise
|
|
31
117
|
|
|
32
118
|
## 相关链接
|
|
33
119
|
|
package/dist/index.cjs
CHANGED
|
@@ -1,16 +1,511 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
1
2
|
|
|
2
|
-
//#region src/
|
|
3
|
+
//#region src/core/adapter.ts
|
|
4
|
+
const GLOBAL_ADAPTER_KEYS = [
|
|
5
|
+
{
|
|
6
|
+
platform: "wx",
|
|
7
|
+
key: "wx"
|
|
8
|
+
},
|
|
9
|
+
{
|
|
10
|
+
platform: "my",
|
|
11
|
+
key: "my"
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
platform: "tt",
|
|
15
|
+
key: "tt"
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
platform: "qq",
|
|
19
|
+
key: "qq"
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
platform: "swan",
|
|
23
|
+
key: "swan"
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
platform: "ks",
|
|
27
|
+
key: "ks"
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
platform: "jd",
|
|
31
|
+
key: "jd"
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
platform: "xhs",
|
|
35
|
+
key: "xhs"
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
platform: "dd",
|
|
39
|
+
key: "dd"
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
platform: "qa",
|
|
43
|
+
key: "qa"
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
platform: "qapp",
|
|
47
|
+
key: "qapp"
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
platform: "uni",
|
|
51
|
+
key: "uni"
|
|
52
|
+
}
|
|
53
|
+
];
|
|
54
|
+
function resolveGlobalThis() {
|
|
55
|
+
if (typeof globalThis !== "undefined") return globalThis;
|
|
56
|
+
if (typeof window !== "undefined") return window;
|
|
57
|
+
}
|
|
58
|
+
function isAdapterCandidate(value) {
|
|
59
|
+
return typeof value === "object" || typeof value === "function";
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* @description 侦测当前运行环境的全局 API 对象
|
|
63
|
+
*/
|
|
64
|
+
function detectGlobalAdapter() {
|
|
65
|
+
const root = resolveGlobalThis();
|
|
66
|
+
if (!root) return {};
|
|
67
|
+
for (const item of GLOBAL_ADAPTER_KEYS) {
|
|
68
|
+
const candidate = root[item.key];
|
|
69
|
+
if (isAdapterCandidate(candidate)) return {
|
|
70
|
+
adapter: candidate,
|
|
71
|
+
platform: item.platform
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
return {};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
//#endregion
|
|
78
|
+
//#region src/core/utils.ts
|
|
79
|
+
function isPlainObject(value) {
|
|
80
|
+
return Object.prototype.toString.call(value) === "[object Object]";
|
|
81
|
+
}
|
|
82
|
+
function hasCallbacks(value) {
|
|
83
|
+
if (!isPlainObject(value)) return false;
|
|
84
|
+
return typeof value.success === "function" || typeof value.fail === "function" || typeof value.complete === "function";
|
|
85
|
+
}
|
|
86
|
+
function isSyncMethod(name) {
|
|
87
|
+
return name.endsWith("Sync");
|
|
88
|
+
}
|
|
89
|
+
function isEventMethod(name) {
|
|
90
|
+
return /^on[A-Z]/.test(name) || /^off[A-Z]/.test(name);
|
|
91
|
+
}
|
|
92
|
+
function shouldSkipPromise(name) {
|
|
93
|
+
return isSyncMethod(name) || isEventMethod(name);
|
|
94
|
+
}
|
|
95
|
+
function createNotSupportedError(methodName, platform) {
|
|
96
|
+
return { errMsg: `${platform ? `${platform}.${methodName}` : methodName}:fail method not supported` };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
//#endregion
|
|
100
|
+
//#region src/core/methodMapping.ts
|
|
101
|
+
const PLATFORM_ALIASES$1 = {
|
|
102
|
+
alipay: "my",
|
|
103
|
+
douyin: "tt"
|
|
104
|
+
};
|
|
105
|
+
function mapToastType(type) {
|
|
106
|
+
if (type === "error") return "fail";
|
|
107
|
+
if (type === "loading") return "none";
|
|
108
|
+
return type;
|
|
109
|
+
}
|
|
110
|
+
function mapToastArgs(args) {
|
|
111
|
+
if (args.length === 0) return args;
|
|
112
|
+
const nextArgs = [...args];
|
|
113
|
+
const lastIndex = nextArgs.length - 1;
|
|
114
|
+
const lastArg = nextArgs[lastIndex];
|
|
115
|
+
if (!isPlainObject(lastArg)) return nextArgs;
|
|
116
|
+
const nextOptions = { ...lastArg };
|
|
117
|
+
if (!Object.prototype.hasOwnProperty.call(nextOptions, "content") && Object.prototype.hasOwnProperty.call(nextOptions, "title")) nextOptions.content = nextOptions.title;
|
|
118
|
+
if (Object.prototype.hasOwnProperty.call(nextOptions, "icon")) nextOptions.type = mapToastType(nextOptions.icon);
|
|
119
|
+
nextArgs[lastIndex] = nextOptions;
|
|
120
|
+
return nextArgs;
|
|
121
|
+
}
|
|
122
|
+
function mapDouyinToastArgs(args) {
|
|
123
|
+
if (args.length === 0) return args;
|
|
124
|
+
const nextArgs = [...args];
|
|
125
|
+
const lastIndex = nextArgs.length - 1;
|
|
126
|
+
const lastArg = nextArgs[lastIndex];
|
|
127
|
+
if (!isPlainObject(lastArg)) return nextArgs;
|
|
128
|
+
const nextOptions = { ...lastArg };
|
|
129
|
+
if (nextOptions.icon === "error") nextOptions.icon = "fail";
|
|
130
|
+
nextArgs[lastIndex] = nextOptions;
|
|
131
|
+
return nextArgs;
|
|
132
|
+
}
|
|
133
|
+
function mapLoadingArgs(args) {
|
|
134
|
+
if (args.length === 0) return args;
|
|
135
|
+
const nextArgs = [...args];
|
|
136
|
+
const lastIndex = nextArgs.length - 1;
|
|
137
|
+
const lastArg = nextArgs[lastIndex];
|
|
138
|
+
if (!isPlainObject(lastArg)) return nextArgs;
|
|
139
|
+
const nextOptions = { ...lastArg };
|
|
140
|
+
if (!Object.prototype.hasOwnProperty.call(nextOptions, "content") && Object.prototype.hasOwnProperty.call(nextOptions, "title")) nextOptions.content = nextOptions.title;
|
|
141
|
+
nextArgs[lastIndex] = nextOptions;
|
|
142
|
+
return nextArgs;
|
|
143
|
+
}
|
|
144
|
+
function mapActionSheetArgs(args) {
|
|
145
|
+
if (args.length === 0) return args;
|
|
146
|
+
const nextArgs = [...args];
|
|
147
|
+
const lastIndex = nextArgs.length - 1;
|
|
148
|
+
const lastArg = nextArgs[lastIndex];
|
|
149
|
+
if (!isPlainObject(lastArg)) return nextArgs;
|
|
150
|
+
const nextOptions = { ...lastArg };
|
|
151
|
+
if (!Object.prototype.hasOwnProperty.call(nextOptions, "items") && Array.isArray(nextOptions.itemList)) nextOptions.items = nextOptions.itemList;
|
|
152
|
+
nextArgs[lastIndex] = nextOptions;
|
|
153
|
+
return nextArgs;
|
|
154
|
+
}
|
|
155
|
+
function mapActionSheetResult(result) {
|
|
156
|
+
if (!isPlainObject(result)) return result;
|
|
157
|
+
if (!Object.prototype.hasOwnProperty.call(result, "tapIndex") && Object.prototype.hasOwnProperty.call(result, "index")) return {
|
|
158
|
+
...result,
|
|
159
|
+
tapIndex: result.index
|
|
160
|
+
};
|
|
161
|
+
return result;
|
|
162
|
+
}
|
|
163
|
+
function mapModalArgs(args) {
|
|
164
|
+
if (args.length === 0) return args;
|
|
165
|
+
const nextArgs = [...args];
|
|
166
|
+
const lastIndex = nextArgs.length - 1;
|
|
167
|
+
const lastArg = nextArgs[lastIndex];
|
|
168
|
+
if (!isPlainObject(lastArg)) return nextArgs;
|
|
169
|
+
const nextOptions = { ...lastArg };
|
|
170
|
+
if (!Object.prototype.hasOwnProperty.call(nextOptions, "confirmButtonText") && Object.prototype.hasOwnProperty.call(nextOptions, "confirmText")) nextOptions.confirmButtonText = nextOptions.confirmText;
|
|
171
|
+
if (!Object.prototype.hasOwnProperty.call(nextOptions, "cancelButtonText") && Object.prototype.hasOwnProperty.call(nextOptions, "cancelText")) nextOptions.cancelButtonText = nextOptions.cancelText;
|
|
172
|
+
nextArgs[lastIndex] = nextOptions;
|
|
173
|
+
return nextArgs;
|
|
174
|
+
}
|
|
175
|
+
function mapModalResult(result) {
|
|
176
|
+
if (!isPlainObject(result)) return result;
|
|
177
|
+
if (!Object.prototype.hasOwnProperty.call(result, "cancel") && Object.prototype.hasOwnProperty.call(result, "confirm")) return {
|
|
178
|
+
...result,
|
|
179
|
+
cancel: !result.confirm
|
|
180
|
+
};
|
|
181
|
+
return result;
|
|
182
|
+
}
|
|
183
|
+
function mapChooseImageResult(result) {
|
|
184
|
+
if (!isPlainObject(result)) return result;
|
|
185
|
+
if (!Object.prototype.hasOwnProperty.call(result, "tempFilePaths") && Array.isArray(result.apFilePaths)) return {
|
|
186
|
+
...result,
|
|
187
|
+
tempFilePaths: result.apFilePaths
|
|
188
|
+
};
|
|
189
|
+
return result;
|
|
190
|
+
}
|
|
191
|
+
function mapDouyinChooseImageResult(result) {
|
|
192
|
+
if (!isPlainObject(result)) return result;
|
|
193
|
+
if (typeof result.tempFilePaths === "string") return {
|
|
194
|
+
...result,
|
|
195
|
+
tempFilePaths: [result.tempFilePaths]
|
|
196
|
+
};
|
|
197
|
+
if (!Array.isArray(result.tempFilePaths) && Array.isArray(result.tempFiles)) {
|
|
198
|
+
const fallbackPaths = result.tempFiles.map((item) => {
|
|
199
|
+
if (!isPlainObject(item)) return;
|
|
200
|
+
const path = item.path;
|
|
201
|
+
if (typeof path === "string" && path) return path;
|
|
202
|
+
const filePath = item.filePath;
|
|
203
|
+
if (typeof filePath === "string" && filePath) return filePath;
|
|
204
|
+
}).filter((item) => typeof item === "string");
|
|
205
|
+
if (fallbackPaths.length > 0) return {
|
|
206
|
+
...result,
|
|
207
|
+
tempFilePaths: fallbackPaths
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
return result;
|
|
211
|
+
}
|
|
212
|
+
function mapSaveFileArgs(args) {
|
|
213
|
+
if (args.length === 0) return args;
|
|
214
|
+
const nextArgs = [...args];
|
|
215
|
+
const lastIndex = nextArgs.length - 1;
|
|
216
|
+
const lastArg = nextArgs[lastIndex];
|
|
217
|
+
if (!isPlainObject(lastArg)) return nextArgs;
|
|
218
|
+
const nextOptions = { ...lastArg };
|
|
219
|
+
if (!Object.prototype.hasOwnProperty.call(nextOptions, "apFilePath") && Object.prototype.hasOwnProperty.call(nextOptions, "tempFilePath")) nextOptions.apFilePath = nextOptions.tempFilePath;
|
|
220
|
+
nextArgs[lastIndex] = nextOptions;
|
|
221
|
+
return nextArgs;
|
|
222
|
+
}
|
|
223
|
+
function mapSaveFileResult(result) {
|
|
224
|
+
if (!isPlainObject(result)) return result;
|
|
225
|
+
if (!Object.prototype.hasOwnProperty.call(result, "savedFilePath") && Object.prototype.hasOwnProperty.call(result, "apFilePath")) return {
|
|
226
|
+
...result,
|
|
227
|
+
savedFilePath: result.apFilePath
|
|
228
|
+
};
|
|
229
|
+
return result;
|
|
230
|
+
}
|
|
231
|
+
function mapDouyinSaveFileResult(result) {
|
|
232
|
+
if (!isPlainObject(result)) return result;
|
|
233
|
+
if (!Object.prototype.hasOwnProperty.call(result, "savedFilePath") && Object.prototype.hasOwnProperty.call(result, "filePath")) return {
|
|
234
|
+
...result,
|
|
235
|
+
savedFilePath: result.filePath
|
|
236
|
+
};
|
|
237
|
+
return result;
|
|
238
|
+
}
|
|
239
|
+
function normalizePlatformName$1(value) {
|
|
240
|
+
if (!value) return;
|
|
241
|
+
const normalized = value.trim().toLowerCase();
|
|
242
|
+
if (!normalized) return;
|
|
243
|
+
return PLATFORM_ALIASES$1[normalized] ?? normalized;
|
|
244
|
+
}
|
|
245
|
+
function mapSetClipboardArgs(args) {
|
|
246
|
+
if (args.length === 0) return args;
|
|
247
|
+
const nextArgs = [...args];
|
|
248
|
+
const lastIndex = nextArgs.length - 1;
|
|
249
|
+
const lastArg = nextArgs[lastIndex];
|
|
250
|
+
if (!isPlainObject(lastArg)) return nextArgs;
|
|
251
|
+
const nextOptions = { ...lastArg };
|
|
252
|
+
if (!Object.prototype.hasOwnProperty.call(nextOptions, "text") && Object.prototype.hasOwnProperty.call(nextOptions, "data")) nextOptions.text = nextOptions.data;
|
|
253
|
+
nextArgs[lastIndex] = nextOptions;
|
|
254
|
+
return nextArgs;
|
|
255
|
+
}
|
|
256
|
+
function mapClipboardResult(result) {
|
|
257
|
+
if (!isPlainObject(result)) return result;
|
|
258
|
+
if (!Object.prototype.hasOwnProperty.call(result, "data") && Object.prototype.hasOwnProperty.call(result, "text")) return {
|
|
259
|
+
...result,
|
|
260
|
+
data: result.text
|
|
261
|
+
};
|
|
262
|
+
return result;
|
|
263
|
+
}
|
|
264
|
+
const METHOD_MAPPINGS = {
|
|
265
|
+
my: {
|
|
266
|
+
showToast: {
|
|
267
|
+
target: "showToast",
|
|
268
|
+
mapArgs: mapToastArgs
|
|
269
|
+
},
|
|
270
|
+
showLoading: {
|
|
271
|
+
target: "showLoading",
|
|
272
|
+
mapArgs: mapLoadingArgs
|
|
273
|
+
},
|
|
274
|
+
showActionSheet: {
|
|
275
|
+
target: "showActionSheet",
|
|
276
|
+
mapArgs: mapActionSheetArgs,
|
|
277
|
+
mapResult: mapActionSheetResult
|
|
278
|
+
},
|
|
279
|
+
showModal: {
|
|
280
|
+
target: "confirm",
|
|
281
|
+
mapArgs: mapModalArgs,
|
|
282
|
+
mapResult: mapModalResult
|
|
283
|
+
},
|
|
284
|
+
chooseImage: {
|
|
285
|
+
target: "chooseImage",
|
|
286
|
+
mapResult: mapChooseImageResult
|
|
287
|
+
},
|
|
288
|
+
saveFile: {
|
|
289
|
+
target: "saveFile",
|
|
290
|
+
mapArgs: mapSaveFileArgs,
|
|
291
|
+
mapResult: mapSaveFileResult
|
|
292
|
+
},
|
|
293
|
+
setClipboardData: {
|
|
294
|
+
target: "setClipboard",
|
|
295
|
+
mapArgs: mapSetClipboardArgs
|
|
296
|
+
},
|
|
297
|
+
getClipboardData: {
|
|
298
|
+
target: "getClipboard",
|
|
299
|
+
mapResult: mapClipboardResult
|
|
300
|
+
}
|
|
301
|
+
},
|
|
302
|
+
tt: {
|
|
303
|
+
showToast: {
|
|
304
|
+
target: "showToast",
|
|
305
|
+
mapArgs: mapDouyinToastArgs
|
|
306
|
+
},
|
|
307
|
+
showLoading: { target: "showLoading" },
|
|
308
|
+
showActionSheet: {
|
|
309
|
+
target: "showActionSheet",
|
|
310
|
+
mapResult: mapActionSheetResult
|
|
311
|
+
},
|
|
312
|
+
showModal: { target: "showModal" },
|
|
313
|
+
chooseImage: {
|
|
314
|
+
target: "chooseImage",
|
|
315
|
+
mapResult: mapDouyinChooseImageResult
|
|
316
|
+
},
|
|
317
|
+
saveFile: {
|
|
318
|
+
target: "saveFile",
|
|
319
|
+
mapResult: mapDouyinSaveFileResult
|
|
320
|
+
},
|
|
321
|
+
setClipboardData: { target: "setClipboardData" },
|
|
322
|
+
getClipboardData: { target: "getClipboardData" }
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
/**
|
|
326
|
+
* @description 解析平台 API 映射规则
|
|
327
|
+
*/
|
|
328
|
+
function resolveMethodMapping(platform, methodName) {
|
|
329
|
+
const normalizedPlatform = normalizePlatformName$1(platform);
|
|
330
|
+
if (!normalizedPlatform) return;
|
|
331
|
+
const platformMappings = METHOD_MAPPINGS[normalizedPlatform];
|
|
332
|
+
if (!platformMappings) return;
|
|
333
|
+
return platformMappings[methodName];
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
//#endregion
|
|
337
|
+
//#region src/core/createWeapi.ts
|
|
338
|
+
const INTERNAL_KEYS = new Set([
|
|
339
|
+
"setAdapter",
|
|
340
|
+
"getAdapter",
|
|
341
|
+
"platform",
|
|
342
|
+
"raw"
|
|
343
|
+
]);
|
|
344
|
+
const PLATFORM_ALIASES = {
|
|
345
|
+
alipay: "my",
|
|
346
|
+
douyin: "tt"
|
|
347
|
+
};
|
|
348
|
+
function normalizePlatformName(value) {
|
|
349
|
+
if (!value) return;
|
|
350
|
+
const normalized = value.trim().toLowerCase();
|
|
351
|
+
if (!normalized) return;
|
|
352
|
+
return PLATFORM_ALIASES[normalized] ?? normalized;
|
|
353
|
+
}
|
|
354
|
+
function resolveOptionsArg(args) {
|
|
355
|
+
const nextArgs = args.slice();
|
|
356
|
+
const lastArg = nextArgs.length > 0 ? nextArgs[nextArgs.length - 1] : void 0;
|
|
357
|
+
if (isPlainObject(lastArg)) {
|
|
358
|
+
const options = { ...lastArg };
|
|
359
|
+
nextArgs[nextArgs.length - 1] = options;
|
|
360
|
+
return {
|
|
361
|
+
args: nextArgs,
|
|
362
|
+
options
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
const options = {};
|
|
366
|
+
nextArgs.push(options);
|
|
367
|
+
return {
|
|
368
|
+
args: nextArgs,
|
|
369
|
+
options
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
function callWithPromise(context, method, args, mapResult) {
|
|
373
|
+
return new Promise((resolve, reject) => {
|
|
374
|
+
const { args: nextArgs, options } = resolveOptionsArg(args);
|
|
375
|
+
let settled = false;
|
|
376
|
+
options.success = (res) => {
|
|
377
|
+
settled = true;
|
|
378
|
+
resolve(mapResult ? mapResult(res) : res);
|
|
379
|
+
};
|
|
380
|
+
options.fail = (err) => {
|
|
381
|
+
settled = true;
|
|
382
|
+
reject(err);
|
|
383
|
+
};
|
|
384
|
+
options.complete = (res) => {
|
|
385
|
+
if (!settled) resolve(mapResult ? mapResult(res) : res);
|
|
386
|
+
};
|
|
387
|
+
try {
|
|
388
|
+
method.apply(context, nextArgs);
|
|
389
|
+
} catch (err) {
|
|
390
|
+
reject(err);
|
|
391
|
+
}
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
function callMissingApi(methodName, platform, args) {
|
|
395
|
+
const lastArg = args.length > 0 ? args[args.length - 1] : void 0;
|
|
396
|
+
const error = createNotSupportedError(methodName, platform);
|
|
397
|
+
if (hasCallbacks(lastArg)) {
|
|
398
|
+
lastArg.fail?.(error);
|
|
399
|
+
lastArg.complete?.(error);
|
|
400
|
+
return;
|
|
401
|
+
}
|
|
402
|
+
return Promise.reject(error);
|
|
403
|
+
}
|
|
3
404
|
/**
|
|
4
|
-
* @description
|
|
405
|
+
* @description 创建跨平台 API 实例
|
|
5
406
|
*/
|
|
6
|
-
function
|
|
7
|
-
|
|
407
|
+
function createWeapi(options = {}) {
|
|
408
|
+
let adapter = options.adapter;
|
|
409
|
+
let platformName = normalizePlatformName(options.platform);
|
|
410
|
+
const cache = /* @__PURE__ */ new Map();
|
|
411
|
+
const resolveAdapter = () => {
|
|
412
|
+
if (adapter) return adapter;
|
|
413
|
+
const detected = detectGlobalAdapter();
|
|
414
|
+
if (detected.adapter) {
|
|
415
|
+
adapter = detected.adapter;
|
|
416
|
+
platformName = platformName ?? normalizePlatformName(detected.platform);
|
|
417
|
+
}
|
|
418
|
+
return adapter;
|
|
419
|
+
};
|
|
420
|
+
const setAdapter = (nextAdapter, nextPlatform) => {
|
|
421
|
+
adapter = nextAdapter;
|
|
422
|
+
platformName = normalizePlatformName(nextPlatform);
|
|
423
|
+
cache.clear();
|
|
424
|
+
};
|
|
425
|
+
const getAdapter = () => {
|
|
426
|
+
if (!adapter) resolveAdapter();
|
|
427
|
+
return adapter;
|
|
428
|
+
};
|
|
429
|
+
const getPlatform = () => {
|
|
430
|
+
if (!platformName) resolveAdapter();
|
|
431
|
+
return platformName;
|
|
432
|
+
};
|
|
433
|
+
return new Proxy({}, { get(_target, prop) {
|
|
434
|
+
if (prop === Symbol.toStringTag) return "Weapi";
|
|
435
|
+
if (prop === "then") return;
|
|
436
|
+
if (INTERNAL_KEYS.has(prop)) {
|
|
437
|
+
if (prop === "setAdapter") return setAdapter;
|
|
438
|
+
if (prop === "getAdapter") return getAdapter;
|
|
439
|
+
if (prop === "platform") return getPlatform();
|
|
440
|
+
if (prop === "raw") return getAdapter();
|
|
441
|
+
}
|
|
442
|
+
if (cache.has(prop)) return cache.get(prop);
|
|
443
|
+
const currentAdapter = resolveAdapter();
|
|
444
|
+
if (!currentAdapter) {
|
|
445
|
+
if (typeof prop !== "string") return;
|
|
446
|
+
return (...args) => callMissingApi(prop, getPlatform(), args);
|
|
447
|
+
}
|
|
448
|
+
const platform = getPlatform();
|
|
449
|
+
const value = currentAdapter[(typeof prop === "string" ? resolveMethodMapping(platform, prop) : void 0)?.target ?? prop];
|
|
450
|
+
if (typeof value !== "function") {
|
|
451
|
+
if (value === void 0 && typeof prop === "string") {
|
|
452
|
+
const missing = (...args) => callMissingApi(prop, getPlatform(), args);
|
|
453
|
+
cache.set(prop, missing);
|
|
454
|
+
return missing;
|
|
455
|
+
}
|
|
456
|
+
cache.set(prop, value);
|
|
457
|
+
return value;
|
|
458
|
+
}
|
|
459
|
+
const wrapped = (...args) => {
|
|
460
|
+
const runtimeAdapter = resolveAdapter();
|
|
461
|
+
const mappingRule = resolveMethodMapping(getPlatform(), prop);
|
|
462
|
+
const methodName = mappingRule?.target ?? prop;
|
|
463
|
+
const runtimeMethod = runtimeAdapter ? runtimeAdapter[methodName] : void 0;
|
|
464
|
+
const runtimeArgs = mappingRule?.mapArgs ? mappingRule.mapArgs(args) : args;
|
|
465
|
+
if (typeof runtimeMethod !== "function") return callMissingApi(prop, getPlatform(), args);
|
|
466
|
+
if (shouldSkipPromise(prop)) {
|
|
467
|
+
const result = runtimeMethod.apply(runtimeAdapter, runtimeArgs);
|
|
468
|
+
return mappingRule?.mapResult ? mappingRule.mapResult(result) : result;
|
|
469
|
+
}
|
|
470
|
+
const lastArg = runtimeArgs.length > 0 ? runtimeArgs[runtimeArgs.length - 1] : void 0;
|
|
471
|
+
if (hasCallbacks(lastArg)) {
|
|
472
|
+
if (mappingRule?.mapResult && isPlainObject(lastArg)) {
|
|
473
|
+
const options = lastArg;
|
|
474
|
+
const originalSuccess = options.success;
|
|
475
|
+
const originalComplete = options.complete;
|
|
476
|
+
options.success = (res) => {
|
|
477
|
+
originalSuccess?.(mappingRule.mapResult(res));
|
|
478
|
+
};
|
|
479
|
+
options.complete = (res) => {
|
|
480
|
+
originalComplete?.(mappingRule.mapResult(res));
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
const result = runtimeMethod.apply(runtimeAdapter, runtimeArgs);
|
|
484
|
+
return mappingRule?.mapResult ? mappingRule.mapResult(result) : result;
|
|
485
|
+
}
|
|
486
|
+
return callWithPromise(runtimeAdapter, runtimeMethod, runtimeArgs, mappingRule?.mapResult);
|
|
487
|
+
};
|
|
488
|
+
cache.set(prop, wrapped);
|
|
489
|
+
return wrapped;
|
|
490
|
+
} });
|
|
8
491
|
}
|
|
492
|
+
|
|
493
|
+
//#endregion
|
|
494
|
+
//#region src/index.ts
|
|
9
495
|
/**
|
|
10
|
-
* @description
|
|
496
|
+
* @description 默认跨平台 API 实例(推荐使用)
|
|
497
|
+
*
|
|
498
|
+
* @generated weapi-platform-matrix:start
|
|
499
|
+
* | 平台 | 类型来源 | 支持度 |
|
|
500
|
+
* | --- | --- | --- |
|
|
501
|
+
* | 微信小程序 (`wx`) | `miniprogram-api-typings` | ✅ 全量 |
|
|
502
|
+
* | 支付宝小程序 (`my`) | `@mini-types/alipay` | ✅ 全量 |
|
|
503
|
+
* | 抖音小程序 (`tt`) | `@douyin-microapp/typings` | ✅ 全量 |
|
|
504
|
+
* | 其他平台对象 (`swan/jd/xhs/...`) | 运行时对象透传 | ⚠️ 按宿主能力支持 |
|
|
505
|
+
* @generated weapi-platform-matrix:end
|
|
11
506
|
*/
|
|
12
|
-
const
|
|
507
|
+
const wpi = createWeapi();
|
|
13
508
|
|
|
14
509
|
//#endregion
|
|
15
|
-
exports.
|
|
16
|
-
exports.
|
|
510
|
+
exports.createWeapi = createWeapi;
|
|
511
|
+
exports.wpi = wpi;
|