@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/dist/index.mjs
CHANGED
|
@@ -1,14 +1,508 @@
|
|
|
1
|
-
//#region src/
|
|
1
|
+
//#region src/core/adapter.ts
|
|
2
|
+
const GLOBAL_ADAPTER_KEYS = [
|
|
3
|
+
{
|
|
4
|
+
platform: "wx",
|
|
5
|
+
key: "wx"
|
|
6
|
+
},
|
|
7
|
+
{
|
|
8
|
+
platform: "my",
|
|
9
|
+
key: "my"
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
platform: "tt",
|
|
13
|
+
key: "tt"
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
platform: "qq",
|
|
17
|
+
key: "qq"
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
platform: "swan",
|
|
21
|
+
key: "swan"
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
platform: "ks",
|
|
25
|
+
key: "ks"
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
platform: "jd",
|
|
29
|
+
key: "jd"
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
platform: "xhs",
|
|
33
|
+
key: "xhs"
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
platform: "dd",
|
|
37
|
+
key: "dd"
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
platform: "qa",
|
|
41
|
+
key: "qa"
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
platform: "qapp",
|
|
45
|
+
key: "qapp"
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
platform: "uni",
|
|
49
|
+
key: "uni"
|
|
50
|
+
}
|
|
51
|
+
];
|
|
52
|
+
function resolveGlobalThis() {
|
|
53
|
+
if (typeof globalThis !== "undefined") return globalThis;
|
|
54
|
+
if (typeof window !== "undefined") return window;
|
|
55
|
+
}
|
|
56
|
+
function isAdapterCandidate(value) {
|
|
57
|
+
return typeof value === "object" || typeof value === "function";
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* @description 侦测当前运行环境的全局 API 对象
|
|
61
|
+
*/
|
|
62
|
+
function detectGlobalAdapter() {
|
|
63
|
+
const root = resolveGlobalThis();
|
|
64
|
+
if (!root) return {};
|
|
65
|
+
for (const item of GLOBAL_ADAPTER_KEYS) {
|
|
66
|
+
const candidate = root[item.key];
|
|
67
|
+
if (isAdapterCandidate(candidate)) return {
|
|
68
|
+
adapter: candidate,
|
|
69
|
+
platform: item.platform
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
return {};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
//#endregion
|
|
76
|
+
//#region src/core/utils.ts
|
|
77
|
+
function isPlainObject(value) {
|
|
78
|
+
return Object.prototype.toString.call(value) === "[object Object]";
|
|
79
|
+
}
|
|
80
|
+
function hasCallbacks(value) {
|
|
81
|
+
if (!isPlainObject(value)) return false;
|
|
82
|
+
return typeof value.success === "function" || typeof value.fail === "function" || typeof value.complete === "function";
|
|
83
|
+
}
|
|
84
|
+
function isSyncMethod(name) {
|
|
85
|
+
return name.endsWith("Sync");
|
|
86
|
+
}
|
|
87
|
+
function isEventMethod(name) {
|
|
88
|
+
return /^on[A-Z]/.test(name) || /^off[A-Z]/.test(name);
|
|
89
|
+
}
|
|
90
|
+
function shouldSkipPromise(name) {
|
|
91
|
+
return isSyncMethod(name) || isEventMethod(name);
|
|
92
|
+
}
|
|
93
|
+
function createNotSupportedError(methodName, platform) {
|
|
94
|
+
return { errMsg: `${platform ? `${platform}.${methodName}` : methodName}:fail method not supported` };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
//#endregion
|
|
98
|
+
//#region src/core/methodMapping.ts
|
|
99
|
+
const PLATFORM_ALIASES$1 = {
|
|
100
|
+
alipay: "my",
|
|
101
|
+
douyin: "tt"
|
|
102
|
+
};
|
|
103
|
+
function mapToastType(type) {
|
|
104
|
+
if (type === "error") return "fail";
|
|
105
|
+
if (type === "loading") return "none";
|
|
106
|
+
return type;
|
|
107
|
+
}
|
|
108
|
+
function mapToastArgs(args) {
|
|
109
|
+
if (args.length === 0) return args;
|
|
110
|
+
const nextArgs = [...args];
|
|
111
|
+
const lastIndex = nextArgs.length - 1;
|
|
112
|
+
const lastArg = nextArgs[lastIndex];
|
|
113
|
+
if (!isPlainObject(lastArg)) return nextArgs;
|
|
114
|
+
const nextOptions = { ...lastArg };
|
|
115
|
+
if (!Object.prototype.hasOwnProperty.call(nextOptions, "content") && Object.prototype.hasOwnProperty.call(nextOptions, "title")) nextOptions.content = nextOptions.title;
|
|
116
|
+
if (Object.prototype.hasOwnProperty.call(nextOptions, "icon")) nextOptions.type = mapToastType(nextOptions.icon);
|
|
117
|
+
nextArgs[lastIndex] = nextOptions;
|
|
118
|
+
return nextArgs;
|
|
119
|
+
}
|
|
120
|
+
function mapDouyinToastArgs(args) {
|
|
121
|
+
if (args.length === 0) return args;
|
|
122
|
+
const nextArgs = [...args];
|
|
123
|
+
const lastIndex = nextArgs.length - 1;
|
|
124
|
+
const lastArg = nextArgs[lastIndex];
|
|
125
|
+
if (!isPlainObject(lastArg)) return nextArgs;
|
|
126
|
+
const nextOptions = { ...lastArg };
|
|
127
|
+
if (nextOptions.icon === "error") nextOptions.icon = "fail";
|
|
128
|
+
nextArgs[lastIndex] = nextOptions;
|
|
129
|
+
return nextArgs;
|
|
130
|
+
}
|
|
131
|
+
function mapLoadingArgs(args) {
|
|
132
|
+
if (args.length === 0) return args;
|
|
133
|
+
const nextArgs = [...args];
|
|
134
|
+
const lastIndex = nextArgs.length - 1;
|
|
135
|
+
const lastArg = nextArgs[lastIndex];
|
|
136
|
+
if (!isPlainObject(lastArg)) return nextArgs;
|
|
137
|
+
const nextOptions = { ...lastArg };
|
|
138
|
+
if (!Object.prototype.hasOwnProperty.call(nextOptions, "content") && Object.prototype.hasOwnProperty.call(nextOptions, "title")) nextOptions.content = nextOptions.title;
|
|
139
|
+
nextArgs[lastIndex] = nextOptions;
|
|
140
|
+
return nextArgs;
|
|
141
|
+
}
|
|
142
|
+
function mapActionSheetArgs(args) {
|
|
143
|
+
if (args.length === 0) return args;
|
|
144
|
+
const nextArgs = [...args];
|
|
145
|
+
const lastIndex = nextArgs.length - 1;
|
|
146
|
+
const lastArg = nextArgs[lastIndex];
|
|
147
|
+
if (!isPlainObject(lastArg)) return nextArgs;
|
|
148
|
+
const nextOptions = { ...lastArg };
|
|
149
|
+
if (!Object.prototype.hasOwnProperty.call(nextOptions, "items") && Array.isArray(nextOptions.itemList)) nextOptions.items = nextOptions.itemList;
|
|
150
|
+
nextArgs[lastIndex] = nextOptions;
|
|
151
|
+
return nextArgs;
|
|
152
|
+
}
|
|
153
|
+
function mapActionSheetResult(result) {
|
|
154
|
+
if (!isPlainObject(result)) return result;
|
|
155
|
+
if (!Object.prototype.hasOwnProperty.call(result, "tapIndex") && Object.prototype.hasOwnProperty.call(result, "index")) return {
|
|
156
|
+
...result,
|
|
157
|
+
tapIndex: result.index
|
|
158
|
+
};
|
|
159
|
+
return result;
|
|
160
|
+
}
|
|
161
|
+
function mapModalArgs(args) {
|
|
162
|
+
if (args.length === 0) return args;
|
|
163
|
+
const nextArgs = [...args];
|
|
164
|
+
const lastIndex = nextArgs.length - 1;
|
|
165
|
+
const lastArg = nextArgs[lastIndex];
|
|
166
|
+
if (!isPlainObject(lastArg)) return nextArgs;
|
|
167
|
+
const nextOptions = { ...lastArg };
|
|
168
|
+
if (!Object.prototype.hasOwnProperty.call(nextOptions, "confirmButtonText") && Object.prototype.hasOwnProperty.call(nextOptions, "confirmText")) nextOptions.confirmButtonText = nextOptions.confirmText;
|
|
169
|
+
if (!Object.prototype.hasOwnProperty.call(nextOptions, "cancelButtonText") && Object.prototype.hasOwnProperty.call(nextOptions, "cancelText")) nextOptions.cancelButtonText = nextOptions.cancelText;
|
|
170
|
+
nextArgs[lastIndex] = nextOptions;
|
|
171
|
+
return nextArgs;
|
|
172
|
+
}
|
|
173
|
+
function mapModalResult(result) {
|
|
174
|
+
if (!isPlainObject(result)) return result;
|
|
175
|
+
if (!Object.prototype.hasOwnProperty.call(result, "cancel") && Object.prototype.hasOwnProperty.call(result, "confirm")) return {
|
|
176
|
+
...result,
|
|
177
|
+
cancel: !result.confirm
|
|
178
|
+
};
|
|
179
|
+
return result;
|
|
180
|
+
}
|
|
181
|
+
function mapChooseImageResult(result) {
|
|
182
|
+
if (!isPlainObject(result)) return result;
|
|
183
|
+
if (!Object.prototype.hasOwnProperty.call(result, "tempFilePaths") && Array.isArray(result.apFilePaths)) return {
|
|
184
|
+
...result,
|
|
185
|
+
tempFilePaths: result.apFilePaths
|
|
186
|
+
};
|
|
187
|
+
return result;
|
|
188
|
+
}
|
|
189
|
+
function mapDouyinChooseImageResult(result) {
|
|
190
|
+
if (!isPlainObject(result)) return result;
|
|
191
|
+
if (typeof result.tempFilePaths === "string") return {
|
|
192
|
+
...result,
|
|
193
|
+
tempFilePaths: [result.tempFilePaths]
|
|
194
|
+
};
|
|
195
|
+
if (!Array.isArray(result.tempFilePaths) && Array.isArray(result.tempFiles)) {
|
|
196
|
+
const fallbackPaths = result.tempFiles.map((item) => {
|
|
197
|
+
if (!isPlainObject(item)) return;
|
|
198
|
+
const path = item.path;
|
|
199
|
+
if (typeof path === "string" && path) return path;
|
|
200
|
+
const filePath = item.filePath;
|
|
201
|
+
if (typeof filePath === "string" && filePath) return filePath;
|
|
202
|
+
}).filter((item) => typeof item === "string");
|
|
203
|
+
if (fallbackPaths.length > 0) return {
|
|
204
|
+
...result,
|
|
205
|
+
tempFilePaths: fallbackPaths
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
return result;
|
|
209
|
+
}
|
|
210
|
+
function mapSaveFileArgs(args) {
|
|
211
|
+
if (args.length === 0) return args;
|
|
212
|
+
const nextArgs = [...args];
|
|
213
|
+
const lastIndex = nextArgs.length - 1;
|
|
214
|
+
const lastArg = nextArgs[lastIndex];
|
|
215
|
+
if (!isPlainObject(lastArg)) return nextArgs;
|
|
216
|
+
const nextOptions = { ...lastArg };
|
|
217
|
+
if (!Object.prototype.hasOwnProperty.call(nextOptions, "apFilePath") && Object.prototype.hasOwnProperty.call(nextOptions, "tempFilePath")) nextOptions.apFilePath = nextOptions.tempFilePath;
|
|
218
|
+
nextArgs[lastIndex] = nextOptions;
|
|
219
|
+
return nextArgs;
|
|
220
|
+
}
|
|
221
|
+
function mapSaveFileResult(result) {
|
|
222
|
+
if (!isPlainObject(result)) return result;
|
|
223
|
+
if (!Object.prototype.hasOwnProperty.call(result, "savedFilePath") && Object.prototype.hasOwnProperty.call(result, "apFilePath")) return {
|
|
224
|
+
...result,
|
|
225
|
+
savedFilePath: result.apFilePath
|
|
226
|
+
};
|
|
227
|
+
return result;
|
|
228
|
+
}
|
|
229
|
+
function mapDouyinSaveFileResult(result) {
|
|
230
|
+
if (!isPlainObject(result)) return result;
|
|
231
|
+
if (!Object.prototype.hasOwnProperty.call(result, "savedFilePath") && Object.prototype.hasOwnProperty.call(result, "filePath")) return {
|
|
232
|
+
...result,
|
|
233
|
+
savedFilePath: result.filePath
|
|
234
|
+
};
|
|
235
|
+
return result;
|
|
236
|
+
}
|
|
237
|
+
function normalizePlatformName$1(value) {
|
|
238
|
+
if (!value) return;
|
|
239
|
+
const normalized = value.trim().toLowerCase();
|
|
240
|
+
if (!normalized) return;
|
|
241
|
+
return PLATFORM_ALIASES$1[normalized] ?? normalized;
|
|
242
|
+
}
|
|
243
|
+
function mapSetClipboardArgs(args) {
|
|
244
|
+
if (args.length === 0) return args;
|
|
245
|
+
const nextArgs = [...args];
|
|
246
|
+
const lastIndex = nextArgs.length - 1;
|
|
247
|
+
const lastArg = nextArgs[lastIndex];
|
|
248
|
+
if (!isPlainObject(lastArg)) return nextArgs;
|
|
249
|
+
const nextOptions = { ...lastArg };
|
|
250
|
+
if (!Object.prototype.hasOwnProperty.call(nextOptions, "text") && Object.prototype.hasOwnProperty.call(nextOptions, "data")) nextOptions.text = nextOptions.data;
|
|
251
|
+
nextArgs[lastIndex] = nextOptions;
|
|
252
|
+
return nextArgs;
|
|
253
|
+
}
|
|
254
|
+
function mapClipboardResult(result) {
|
|
255
|
+
if (!isPlainObject(result)) return result;
|
|
256
|
+
if (!Object.prototype.hasOwnProperty.call(result, "data") && Object.prototype.hasOwnProperty.call(result, "text")) return {
|
|
257
|
+
...result,
|
|
258
|
+
data: result.text
|
|
259
|
+
};
|
|
260
|
+
return result;
|
|
261
|
+
}
|
|
262
|
+
const METHOD_MAPPINGS = {
|
|
263
|
+
my: {
|
|
264
|
+
showToast: {
|
|
265
|
+
target: "showToast",
|
|
266
|
+
mapArgs: mapToastArgs
|
|
267
|
+
},
|
|
268
|
+
showLoading: {
|
|
269
|
+
target: "showLoading",
|
|
270
|
+
mapArgs: mapLoadingArgs
|
|
271
|
+
},
|
|
272
|
+
showActionSheet: {
|
|
273
|
+
target: "showActionSheet",
|
|
274
|
+
mapArgs: mapActionSheetArgs,
|
|
275
|
+
mapResult: mapActionSheetResult
|
|
276
|
+
},
|
|
277
|
+
showModal: {
|
|
278
|
+
target: "confirm",
|
|
279
|
+
mapArgs: mapModalArgs,
|
|
280
|
+
mapResult: mapModalResult
|
|
281
|
+
},
|
|
282
|
+
chooseImage: {
|
|
283
|
+
target: "chooseImage",
|
|
284
|
+
mapResult: mapChooseImageResult
|
|
285
|
+
},
|
|
286
|
+
saveFile: {
|
|
287
|
+
target: "saveFile",
|
|
288
|
+
mapArgs: mapSaveFileArgs,
|
|
289
|
+
mapResult: mapSaveFileResult
|
|
290
|
+
},
|
|
291
|
+
setClipboardData: {
|
|
292
|
+
target: "setClipboard",
|
|
293
|
+
mapArgs: mapSetClipboardArgs
|
|
294
|
+
},
|
|
295
|
+
getClipboardData: {
|
|
296
|
+
target: "getClipboard",
|
|
297
|
+
mapResult: mapClipboardResult
|
|
298
|
+
}
|
|
299
|
+
},
|
|
300
|
+
tt: {
|
|
301
|
+
showToast: {
|
|
302
|
+
target: "showToast",
|
|
303
|
+
mapArgs: mapDouyinToastArgs
|
|
304
|
+
},
|
|
305
|
+
showLoading: { target: "showLoading" },
|
|
306
|
+
showActionSheet: {
|
|
307
|
+
target: "showActionSheet",
|
|
308
|
+
mapResult: mapActionSheetResult
|
|
309
|
+
},
|
|
310
|
+
showModal: { target: "showModal" },
|
|
311
|
+
chooseImage: {
|
|
312
|
+
target: "chooseImage",
|
|
313
|
+
mapResult: mapDouyinChooseImageResult
|
|
314
|
+
},
|
|
315
|
+
saveFile: {
|
|
316
|
+
target: "saveFile",
|
|
317
|
+
mapResult: mapDouyinSaveFileResult
|
|
318
|
+
},
|
|
319
|
+
setClipboardData: { target: "setClipboardData" },
|
|
320
|
+
getClipboardData: { target: "getClipboardData" }
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
/**
|
|
324
|
+
* @description 解析平台 API 映射规则
|
|
325
|
+
*/
|
|
326
|
+
function resolveMethodMapping(platform, methodName) {
|
|
327
|
+
const normalizedPlatform = normalizePlatformName$1(platform);
|
|
328
|
+
if (!normalizedPlatform) return;
|
|
329
|
+
const platformMappings = METHOD_MAPPINGS[normalizedPlatform];
|
|
330
|
+
if (!platformMappings) return;
|
|
331
|
+
return platformMappings[methodName];
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
//#endregion
|
|
335
|
+
//#region src/core/createWeapi.ts
|
|
336
|
+
const INTERNAL_KEYS = new Set([
|
|
337
|
+
"setAdapter",
|
|
338
|
+
"getAdapter",
|
|
339
|
+
"platform",
|
|
340
|
+
"raw"
|
|
341
|
+
]);
|
|
342
|
+
const PLATFORM_ALIASES = {
|
|
343
|
+
alipay: "my",
|
|
344
|
+
douyin: "tt"
|
|
345
|
+
};
|
|
346
|
+
function normalizePlatformName(value) {
|
|
347
|
+
if (!value) return;
|
|
348
|
+
const normalized = value.trim().toLowerCase();
|
|
349
|
+
if (!normalized) return;
|
|
350
|
+
return PLATFORM_ALIASES[normalized] ?? normalized;
|
|
351
|
+
}
|
|
352
|
+
function resolveOptionsArg(args) {
|
|
353
|
+
const nextArgs = args.slice();
|
|
354
|
+
const lastArg = nextArgs.length > 0 ? nextArgs[nextArgs.length - 1] : void 0;
|
|
355
|
+
if (isPlainObject(lastArg)) {
|
|
356
|
+
const options = { ...lastArg };
|
|
357
|
+
nextArgs[nextArgs.length - 1] = options;
|
|
358
|
+
return {
|
|
359
|
+
args: nextArgs,
|
|
360
|
+
options
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
const options = {};
|
|
364
|
+
nextArgs.push(options);
|
|
365
|
+
return {
|
|
366
|
+
args: nextArgs,
|
|
367
|
+
options
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
function callWithPromise(context, method, args, mapResult) {
|
|
371
|
+
return new Promise((resolve, reject) => {
|
|
372
|
+
const { args: nextArgs, options } = resolveOptionsArg(args);
|
|
373
|
+
let settled = false;
|
|
374
|
+
options.success = (res) => {
|
|
375
|
+
settled = true;
|
|
376
|
+
resolve(mapResult ? mapResult(res) : res);
|
|
377
|
+
};
|
|
378
|
+
options.fail = (err) => {
|
|
379
|
+
settled = true;
|
|
380
|
+
reject(err);
|
|
381
|
+
};
|
|
382
|
+
options.complete = (res) => {
|
|
383
|
+
if (!settled) resolve(mapResult ? mapResult(res) : res);
|
|
384
|
+
};
|
|
385
|
+
try {
|
|
386
|
+
method.apply(context, nextArgs);
|
|
387
|
+
} catch (err) {
|
|
388
|
+
reject(err);
|
|
389
|
+
}
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
function callMissingApi(methodName, platform, args) {
|
|
393
|
+
const lastArg = args.length > 0 ? args[args.length - 1] : void 0;
|
|
394
|
+
const error = createNotSupportedError(methodName, platform);
|
|
395
|
+
if (hasCallbacks(lastArg)) {
|
|
396
|
+
lastArg.fail?.(error);
|
|
397
|
+
lastArg.complete?.(error);
|
|
398
|
+
return;
|
|
399
|
+
}
|
|
400
|
+
return Promise.reject(error);
|
|
401
|
+
}
|
|
2
402
|
/**
|
|
3
|
-
* @description
|
|
403
|
+
* @description 创建跨平台 API 实例
|
|
4
404
|
*/
|
|
5
|
-
function
|
|
6
|
-
|
|
405
|
+
function createWeapi(options = {}) {
|
|
406
|
+
let adapter = options.adapter;
|
|
407
|
+
let platformName = normalizePlatformName(options.platform);
|
|
408
|
+
const cache = /* @__PURE__ */ new Map();
|
|
409
|
+
const resolveAdapter = () => {
|
|
410
|
+
if (adapter) return adapter;
|
|
411
|
+
const detected = detectGlobalAdapter();
|
|
412
|
+
if (detected.adapter) {
|
|
413
|
+
adapter = detected.adapter;
|
|
414
|
+
platformName = platformName ?? normalizePlatformName(detected.platform);
|
|
415
|
+
}
|
|
416
|
+
return adapter;
|
|
417
|
+
};
|
|
418
|
+
const setAdapter = (nextAdapter, nextPlatform) => {
|
|
419
|
+
adapter = nextAdapter;
|
|
420
|
+
platformName = normalizePlatformName(nextPlatform);
|
|
421
|
+
cache.clear();
|
|
422
|
+
};
|
|
423
|
+
const getAdapter = () => {
|
|
424
|
+
if (!adapter) resolveAdapter();
|
|
425
|
+
return adapter;
|
|
426
|
+
};
|
|
427
|
+
const getPlatform = () => {
|
|
428
|
+
if (!platformName) resolveAdapter();
|
|
429
|
+
return platformName;
|
|
430
|
+
};
|
|
431
|
+
return new Proxy({}, { get(_target, prop) {
|
|
432
|
+
if (prop === Symbol.toStringTag) return "Weapi";
|
|
433
|
+
if (prop === "then") return;
|
|
434
|
+
if (INTERNAL_KEYS.has(prop)) {
|
|
435
|
+
if (prop === "setAdapter") return setAdapter;
|
|
436
|
+
if (prop === "getAdapter") return getAdapter;
|
|
437
|
+
if (prop === "platform") return getPlatform();
|
|
438
|
+
if (prop === "raw") return getAdapter();
|
|
439
|
+
}
|
|
440
|
+
if (cache.has(prop)) return cache.get(prop);
|
|
441
|
+
const currentAdapter = resolveAdapter();
|
|
442
|
+
if (!currentAdapter) {
|
|
443
|
+
if (typeof prop !== "string") return;
|
|
444
|
+
return (...args) => callMissingApi(prop, getPlatform(), args);
|
|
445
|
+
}
|
|
446
|
+
const platform = getPlatform();
|
|
447
|
+
const value = currentAdapter[(typeof prop === "string" ? resolveMethodMapping(platform, prop) : void 0)?.target ?? prop];
|
|
448
|
+
if (typeof value !== "function") {
|
|
449
|
+
if (value === void 0 && typeof prop === "string") {
|
|
450
|
+
const missing = (...args) => callMissingApi(prop, getPlatform(), args);
|
|
451
|
+
cache.set(prop, missing);
|
|
452
|
+
return missing;
|
|
453
|
+
}
|
|
454
|
+
cache.set(prop, value);
|
|
455
|
+
return value;
|
|
456
|
+
}
|
|
457
|
+
const wrapped = (...args) => {
|
|
458
|
+
const runtimeAdapter = resolveAdapter();
|
|
459
|
+
const mappingRule = resolveMethodMapping(getPlatform(), prop);
|
|
460
|
+
const methodName = mappingRule?.target ?? prop;
|
|
461
|
+
const runtimeMethod = runtimeAdapter ? runtimeAdapter[methodName] : void 0;
|
|
462
|
+
const runtimeArgs = mappingRule?.mapArgs ? mappingRule.mapArgs(args) : args;
|
|
463
|
+
if (typeof runtimeMethod !== "function") return callMissingApi(prop, getPlatform(), args);
|
|
464
|
+
if (shouldSkipPromise(prop)) {
|
|
465
|
+
const result = runtimeMethod.apply(runtimeAdapter, runtimeArgs);
|
|
466
|
+
return mappingRule?.mapResult ? mappingRule.mapResult(result) : result;
|
|
467
|
+
}
|
|
468
|
+
const lastArg = runtimeArgs.length > 0 ? runtimeArgs[runtimeArgs.length - 1] : void 0;
|
|
469
|
+
if (hasCallbacks(lastArg)) {
|
|
470
|
+
if (mappingRule?.mapResult && isPlainObject(lastArg)) {
|
|
471
|
+
const options = lastArg;
|
|
472
|
+
const originalSuccess = options.success;
|
|
473
|
+
const originalComplete = options.complete;
|
|
474
|
+
options.success = (res) => {
|
|
475
|
+
originalSuccess?.(mappingRule.mapResult(res));
|
|
476
|
+
};
|
|
477
|
+
options.complete = (res) => {
|
|
478
|
+
originalComplete?.(mappingRule.mapResult(res));
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
const result = runtimeMethod.apply(runtimeAdapter, runtimeArgs);
|
|
482
|
+
return mappingRule?.mapResult ? mappingRule.mapResult(result) : result;
|
|
483
|
+
}
|
|
484
|
+
return callWithPromise(runtimeAdapter, runtimeMethod, runtimeArgs, mappingRule?.mapResult);
|
|
485
|
+
};
|
|
486
|
+
cache.set(prop, wrapped);
|
|
487
|
+
return wrapped;
|
|
488
|
+
} });
|
|
7
489
|
}
|
|
490
|
+
|
|
491
|
+
//#endregion
|
|
492
|
+
//#region src/index.ts
|
|
8
493
|
/**
|
|
9
|
-
* @description
|
|
494
|
+
* @description 默认跨平台 API 实例(推荐使用)
|
|
495
|
+
*
|
|
496
|
+
* @generated weapi-platform-matrix:start
|
|
497
|
+
* | 平台 | 类型来源 | 支持度 |
|
|
498
|
+
* | --- | --- | --- |
|
|
499
|
+
* | 微信小程序 (`wx`) | `miniprogram-api-typings` | ✅ 全量 |
|
|
500
|
+
* | 支付宝小程序 (`my`) | `@mini-types/alipay` | ✅ 全量 |
|
|
501
|
+
* | 抖音小程序 (`tt`) | `@douyin-microapp/typings` | ✅ 全量 |
|
|
502
|
+
* | 其他平台对象 (`swan/jd/xhs/...`) | 运行时对象透传 | ⚠️ 按宿主能力支持 |
|
|
503
|
+
* @generated weapi-platform-matrix:end
|
|
10
504
|
*/
|
|
11
|
-
const
|
|
505
|
+
const wpi = createWeapi();
|
|
12
506
|
|
|
13
507
|
//#endregion
|
|
14
|
-
export {
|
|
508
|
+
export { createWeapi, wpi };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wevu/api",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.1.1",
|
|
5
5
|
"description": "tsdown bundler template",
|
|
6
6
|
"author": "ice breaker <1324318532@qq.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -17,23 +17,29 @@
|
|
|
17
17
|
"sideEffects": false,
|
|
18
18
|
"exports": {
|
|
19
19
|
".": {
|
|
20
|
-
"types": "./
|
|
20
|
+
"types": "./types/index.d.ts",
|
|
21
21
|
"import": {
|
|
22
|
-
"types": "./
|
|
22
|
+
"types": "./types/index.d.ts",
|
|
23
23
|
"default": "./dist/index.mjs"
|
|
24
24
|
},
|
|
25
25
|
"require": {
|
|
26
|
-
"types": "./
|
|
26
|
+
"types": "./types/index.d.ts",
|
|
27
27
|
"default": "./dist/index.cjs"
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
},
|
|
31
31
|
"main": "./dist/index.cjs",
|
|
32
32
|
"module": "./dist/index.mjs",
|
|
33
|
-
"types": "./
|
|
33
|
+
"types": "./types/index.d.ts",
|
|
34
34
|
"files": [
|
|
35
|
-
"dist"
|
|
35
|
+
"dist",
|
|
36
|
+
"types"
|
|
36
37
|
],
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@douyin-microapp/typings": "^1.3.1",
|
|
40
|
+
"@mini-types/alipay": "^3.0.14",
|
|
41
|
+
"miniprogram-api-typings": "^5.0.0"
|
|
42
|
+
},
|
|
37
43
|
"publishConfig": {
|
|
38
44
|
"access": "public"
|
|
39
45
|
},
|
|
@@ -42,8 +48,12 @@
|
|
|
42
48
|
"build": "tsdown",
|
|
43
49
|
"test": "vitest run",
|
|
44
50
|
"test:dev": "vitest",
|
|
51
|
+
"test:types": "tsd",
|
|
45
52
|
"release": "pnpm publish",
|
|
46
53
|
"lint": "eslint .",
|
|
47
|
-
"lint:fix": "eslint . --fix"
|
|
54
|
+
"lint:fix": "eslint . --fix",
|
|
55
|
+
"docs:sync": "node --experimental-strip-types ./scripts/sync-support-matrix.ts",
|
|
56
|
+
"docs:check": "node --experimental-strip-types ./scripts/sync-support-matrix.ts --check",
|
|
57
|
+
"prebuild": "pnpm run docs:check"
|
|
48
58
|
}
|
|
49
59
|
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/// <reference types="miniprogram-api-typings" />
|
|
2
|
+
/// <reference types="@mini-types/alipay" />
|
|
3
|
+
/// <reference types="@douyin-microapp/typings" />
|
|
4
|
+
|
|
5
|
+
// eslint-disable-next-line antfu/no-import-dist
|
|
6
|
+
import type {
|
|
7
|
+
CreateWeapiOptions,
|
|
8
|
+
WeapiAdapter,
|
|
9
|
+
WeapiAlipayAdapter,
|
|
10
|
+
WeapiAlipayRawAdapter,
|
|
11
|
+
WeapiCrossPlatformAdapter,
|
|
12
|
+
WeapiCrossPlatformRawAdapter,
|
|
13
|
+
WeapiDouyinAdapter,
|
|
14
|
+
WeapiDouyinRawAdapter,
|
|
15
|
+
WeapiInstance,
|
|
16
|
+
WeapiPromisify,
|
|
17
|
+
WeapiWxAdapter,
|
|
18
|
+
WeapiWxRawAdapter,
|
|
19
|
+
} from '../dist/index.d.mts'
|
|
20
|
+
|
|
21
|
+
export type {
|
|
22
|
+
CreateWeapiOptions,
|
|
23
|
+
WeapiAdapter,
|
|
24
|
+
WeapiAlipayAdapter,
|
|
25
|
+
WeapiAlipayRawAdapter,
|
|
26
|
+
WeapiCrossPlatformAdapter,
|
|
27
|
+
WeapiCrossPlatformRawAdapter,
|
|
28
|
+
WeapiDouyinAdapter,
|
|
29
|
+
WeapiDouyinRawAdapter,
|
|
30
|
+
WeapiInstance,
|
|
31
|
+
WeapiPromisify,
|
|
32
|
+
WeapiWxAdapter,
|
|
33
|
+
WeapiWxRawAdapter,
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function createWeapi<TAdapter extends WeapiAdapter = WeapiCrossPlatformRawAdapter>(
|
|
37
|
+
options?: CreateWeapiOptions<TAdapter>,
|
|
38
|
+
): WeapiInstance<TAdapter>
|
|
39
|
+
|
|
40
|
+
export type WeapiDefaultInstance = WeapiInstance<WeapiCrossPlatformRawAdapter>
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* 默认跨平台 API 实例。
|
|
44
|
+
*
|
|
45
|
+
* @generated weapi-platform-matrix:start
|
|
46
|
+
* | 平台 | 全局对象 | 类型来源 | 对齐状态 |
|
|
47
|
+
* | --- | --- | --- | --- |
|
|
48
|
+
* | 微信小程序 | `wx` | `miniprogram-api-typings` | ✅ 全量 |
|
|
49
|
+
* | 支付宝小程序 | `my` | `@mini-types/alipay` | ✅ 全量 |
|
|
50
|
+
* | 抖音小程序 | `tt` | `@douyin-microapp/typings` | ✅ 全量 |
|
|
51
|
+
* | 其他平台(swan/jd/xhs 等) | 运行时宿主对象 | 运行时透传 | ⚠️ 按宿主能力支持 |
|
|
52
|
+
* @generated weapi-platform-matrix:end
|
|
53
|
+
*/
|
|
54
|
+
export const wpi: WeapiDefaultInstance
|