From b74ce0ad1cbb538946d5fbeb3013585f1d02f276 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Thu, 18 Feb 2021 20:39:46 +0800 Subject: [PATCH] feat(api): promisify --- packages/uni-api/src/helpers/api/callback.ts | 9 +- packages/uni-api/src/helpers/api/index.ts | 40 +++---- packages/uni-api/src/helpers/api/promise.ts | 39 +++++++ packages/uni-api/src/index.ts | 6 +- .../service/ui/createIntersectionObserver.ts | 4 +- packages/uni-h5/dist/uni-h5.esm.js | 101 +++++++++++------- packages/uni-h5/vite.config.ts | 43 ++++---- packages/uni-mp-alipay/dist/uni.api.esm.js | 49 +++++---- packages/uni-mp-baidu/dist/uni.api.esm.js | 49 +++++---- packages/uni-mp-core/src/api/index.ts | 9 +- .../src/api}/promise.ts | 20 +--- packages/uni-mp-core/src/api/wrapper.ts | 4 +- packages/uni-mp-qq/dist/uni.api.esm.js | 49 +++++---- packages/uni-mp-toutiao/dist/uni.api.esm.js | 49 +++++---- packages/uni-mp-weixin/dist/uni.api.esm.js | 49 +++++---- .../uni-quickapp-webview/dist/uni.api.esm.js | 49 +++++---- packages/vite-plugin-uni/src/config/define.ts | 3 +- .../src/configResolved/plugins/inject.ts | 7 +- packages/vite-plugin-uni/src/index.ts | 3 + 19 files changed, 339 insertions(+), 243 deletions(-) create mode 100644 packages/uni-api/src/helpers/api/promise.ts rename packages/{uni-api/src/helpers => uni-mp-core/src/api}/promise.ts (85%) diff --git a/packages/uni-api/src/helpers/api/callback.ts b/packages/uni-api/src/helpers/api/callback.ts index f307897c2..7923b1e70 100644 --- a/packages/uni-api/src/helpers/api/callback.ts +++ b/packages/uni-api/src/helpers/api/callback.ts @@ -70,11 +70,11 @@ export function createKeepAliveApiCallback(name: string, callback: Function) { ) } -const API_SUCCSS = 'success' -const API_FAIL = 'fail' -const API_COMPLETE = 'complete' +export const API_SUCCESS = 'success' +export const API_FAIL = 'fail' +export const API_COMPLETE = 'complete' -type CALLBACK_TYPES = typeof API_SUCCSS | typeof API_FAIL | typeof API_COMPLETE +type CALLBACK_TYPES = typeof API_SUCCESS | typeof API_FAIL | typeof API_COMPLETE type ApiCallbacks = { [key in CALLBACK_TYPES]?: Function @@ -121,6 +121,7 @@ export function createAsyncApiCallback( callbackId, createInvokeCallbackName(name, callbackId), (res: ApiRes) => { + res = res || {} res.errMsg = normalizeErrMsg(res.errMsg, name) isFunction(beforeAll) && beforeAll(res) if (res.errMsg === name + ':ok') { diff --git a/packages/uni-api/src/helpers/api/index.ts b/packages/uni-api/src/helpers/api/index.ts index b4daa56f6..8adf514c5 100644 --- a/packages/uni-api/src/helpers/api/index.ts +++ b/packages/uni-api/src/helpers/api/index.ts @@ -4,19 +4,20 @@ import { createKeepAliveApiCallback, invokeCallback, } from './callback' +import { promisify } from './promise' type ApiProtocols = ApiProtocol | ProtocolOptions[] export const API_TYPE_ON = 0 -export const API_TYPE_SYNC = 1 -export const API_TYPE_ASYNC = 2 -export const API_TYPE_RETURN = 3 +export const API_TYPE_TASK = 1 +export const API_TYPE_SYNC = 2 +export const API_TYPE_ASYNC = 3 type API_TYPES = | typeof API_TYPE_ON + | typeof API_TYPE_TASK | typeof API_TYPE_SYNC | typeof API_TYPE_ASYNC - | typeof API_TYPE_RETURN function validateProtocol( _name: string, @@ -35,6 +36,11 @@ function wrapperOnApi(name: string, fn: Function) { fn.apply(null, createKeepAliveApiCallback(name, callback)) } +function wrapperTaskApi(name: string, fn: Function, options?: ApiOptions) { + return (args: Record) => + fn.apply(null, [args, createAsyncApiCallback(name, args, options)]) +} + function wrapperSyncApi(fn: Function) { return (...args: any[]) => fn.apply(null, args) } @@ -42,15 +48,13 @@ function wrapperSyncApi(fn: Function) { function wrapperAsyncApi(name: string, fn: Function, options?: ApiOptions) { return (args: Record) => { const callbackId = createAsyncApiCallback(name, args, options) - return invokeCallback(callbackId, fn.apply(null, [args, callbackId])) + const res = fn.apply(null, [args, callbackId]) + if (res) { + invokeCallback(callbackId, res) + } } } -function wrapperReturnApi(name: string, fn: Function, options?: ApiOptions) { - return (args: Record) => - fn.apply(null, [args, createAsyncApiCallback(name, args, options)]) -} - function wrapperApi( fn: Function, name?: string, @@ -73,31 +77,31 @@ export function createOnApi( return createApi(API_TYPE_ON, name, fn, protocol, options) } -export function createSyncApi( +export function createTaskApi( name: string, fn: T, protocol?: ApiProtocols, options?: ApiOptions ) { - return createApi(API_TYPE_SYNC, name, fn, protocol, options) + return createApi(API_TYPE_TASK, name, fn, protocol, options) } -export function createAsyncApi( +export function createSyncApi( name: string, fn: T, protocol?: ApiProtocols, options?: ApiOptions ) { - return createApi(API_TYPE_ASYNC, name, fn, protocol, options) + return createApi(API_TYPE_SYNC, name, fn, protocol, options) } -export function createReturnApi( +export function createAsyncApi( name: string, fn: T, protocol?: ApiProtocols, options?: ApiOptions ) { - return createApi(API_TYPE_RETURN, name, fn, protocol, options) + return promisify(createApi(API_TYPE_ASYNC, name, fn, protocol, options)) } function createApi( @@ -110,6 +114,8 @@ function createApi( switch (type) { case API_TYPE_ON: return wrapperApi(wrapperOnApi(name, fn), name, protocol, options) + case API_TYPE_TASK: + return wrapperApi(wrapperTaskApi(name, fn), name, protocol, options) case API_TYPE_SYNC: return wrapperApi(wrapperSyncApi(fn), name, protocol, options) case API_TYPE_ASYNC: @@ -119,7 +125,5 @@ function createApi( protocol, options ) - case API_TYPE_RETURN: - return wrapperApi(wrapperReturnApi(name, fn), name, protocol, options) } } diff --git a/packages/uni-api/src/helpers/api/promise.ts b/packages/uni-api/src/helpers/api/promise.ts new file mode 100644 index 000000000..74c11b302 --- /dev/null +++ b/packages/uni-api/src/helpers/api/promise.ts @@ -0,0 +1,39 @@ +import { isPlainObject, isFunction } from '@vue/shared' + +import { API_SUCCESS, API_FAIL, API_COMPLETE } from './callback' + +const callbacks = [API_SUCCESS, API_FAIL, API_COMPLETE] + +function hasCallback(args: unknown) { + if ( + isPlainObject(args) && + callbacks.find((cb) => isFunction((args as Record)[cb])) + ) { + return true + } + return false +} + +export function handlePromise(promise: Promise) { + if (__UNI_PROMISE_API__) { + return promise + .then((data) => { + return [null, data] + }) + .catch((err) => [err]) + } + return promise +} + +export function promisify(fn: Function) { + return (args = {}) => { + if (hasCallback(args)) { + return fn(args) + } + return handlePromise( + new Promise((resolve, reject) => { + fn(Object.assign(args, { success: resolve, fail: reject })) + }) + ) + } +} diff --git a/packages/uni-api/src/index.ts b/packages/uni-api/src/index.ts index 961867a09..76dcb5924 100644 --- a/packages/uni-api/src/index.ts +++ b/packages/uni-api/src/index.ts @@ -24,8 +24,10 @@ export * from './protocols/media/getImageInfo' // helpers export { createOnApi, + createTaskApi, createSyncApi, createAsyncApi, - createReturnApi, } from './helpers/api' -export { isSyncApi, isContextApi, promisify } from './helpers/promise' + +export { handlePromise } from './helpers/api/promise' +export { invokeApi, wrapperReturnValue } from './helpers/interceptor' diff --git a/packages/uni-api/src/service/ui/createIntersectionObserver.ts b/packages/uni-api/src/service/ui/createIntersectionObserver.ts index 3d8265d20..ed1c6e3f0 100644 --- a/packages/uni-api/src/service/ui/createIntersectionObserver.ts +++ b/packages/uni-api/src/service/ui/createIntersectionObserver.ts @@ -2,7 +2,7 @@ import { extend } from '@vue/shared' import { ServiceJSBridge } from '@dcloudio/uni-core' -import { createReturnApi } from '../../helpers/api' +import { createSyncApi } from '../../helpers/api' import { getCurrentPageVm } from '../utils' const defaultOptions = { @@ -120,7 +120,7 @@ class ServiceIntersectionObserver { } } -export const createIntersectionObserver = createReturnApi< +export const createIntersectionObserver = createSyncApi< typeof uni.createIntersectionObserver >('createIntersectionObserver', (context?, options?) => { if (!context) { diff --git a/packages/uni-h5/dist/uni-h5.esm.js b/packages/uni-h5/dist/uni-h5.esm.js index e14fc58d7..153d6e929 100644 --- a/packages/uni-h5/dist/uni-h5.esm.js +++ b/packages/uni-h5/dist/uni-h5.esm.js @@ -1335,6 +1335,9 @@ function createKeepAliveApiCallback(name, callback) { const id2 = invokeCallbackId++; return addInvokeCallback(id2, createInvokeCallbackName(name, id2), callback, true); } +const API_SUCCESS = "success"; +const API_FAIL = "fail"; +const API_COMPLETE = "complete"; function getApiCallbacks(args) { const apiCallbacks = {}; for (const name in args) { @@ -1362,6 +1365,7 @@ function createAsyncApiCallback(name, args = {}, {beforeAll, beforeSuccess} = {} const hasComplete = isFunction(complete); const callbackId = invokeCallbackId++; addInvokeCallback(callbackId, createInvokeCallbackName(name, callbackId), (res) => { + res = res || {}; res.errMsg = normalizeErrMsg(res.errMsg, name); isFunction(beforeAll) && beforeAll(res); if (res.errMsg === name + ":ok") { @@ -1374,10 +1378,35 @@ function createAsyncApiCallback(name, args = {}, {beforeAll, beforeSuccess} = {} }); return callbackId; } +const callbacks$1 = [API_SUCCESS, API_FAIL, API_COMPLETE]; +function hasCallback(args) { + if (isPlainObject(args) && callbacks$1.find((cb) => isFunction(args[cb]))) { + return true; + } + return false; +} +function handlePromise(promise) { + if (__UNI_PROMISE_API__) { + return promise.then((data) => { + return [null, data]; + }).catch((err) => [err]); + } + return promise; +} +function promisify(fn) { + return (args = {}) => { + if (hasCallback(args)) { + return fn(args); + } + return handlePromise(new Promise((resolve, reject) => { + fn(Object.assign(args, {success: resolve, fail: reject})); + })); + }; +} const API_TYPE_ON = 0; -const API_TYPE_SYNC = 1; -const API_TYPE_ASYNC = 2; -const API_TYPE_RETURN = 3; +const API_TYPE_TASK = 1; +const API_TYPE_SYNC = 2; +const API_TYPE_ASYNC = 3; function validateProtocol(_name, _args, _protocol) { return true; } @@ -1387,18 +1416,21 @@ function formatApiArgs(args, options) { function wrapperOnApi(name, fn) { return (callback) => fn.apply(null, createKeepAliveApiCallback(name, callback)); } +function wrapperTaskApi(name, fn, options) { + return (args) => fn.apply(null, [args, createAsyncApiCallback(name, args, options)]); +} function wrapperSyncApi(fn) { return (...args) => fn.apply(null, args); } function wrapperAsyncApi(name, fn, options) { return (args) => { const callbackId = createAsyncApiCallback(name, args, options); - return invokeCallback(callbackId, fn.apply(null, [args, callbackId])); + const res = fn.apply(null, [args, callbackId]); + if (res) { + invokeCallback(callbackId, res); + } }; } -function wrapperReturnApi(name, fn, options) { - return (args) => fn.apply(null, [args, createAsyncApiCallback(name, args, options)]); -} function wrapperApi(fn, name, protocol, options) { return function(...args) { if (!(process.env.NODE_ENV !== "production" && protocol && !validateProtocol())) { @@ -1407,24 +1439,21 @@ function wrapperApi(fn, name, protocol, options) { }; } function createSyncApi(name, fn, protocol, options) { - return /* @__PURE__ */ createApi(API_TYPE_SYNC, name, fn, protocol, options); + return createApi(API_TYPE_SYNC, name, fn, protocol, options); } function createAsyncApi(name, fn, protocol, options) { - return /* @__PURE__ */ createApi(API_TYPE_ASYNC, name, fn, protocol, options); -} -function createReturnApi(name, fn, protocol, options) { - return /* @__PURE__ */ createApi(API_TYPE_RETURN, name, fn, protocol, options); + return promisify(createApi(API_TYPE_ASYNC, name, fn, protocol, options)); } function createApi(type, name, fn, protocol, options) { switch (type) { case API_TYPE_ON: return wrapperApi(wrapperOnApi(name, fn), name, protocol); + case API_TYPE_TASK: + return wrapperApi(wrapperTaskApi(name, fn), name, protocol); case API_TYPE_SYNC: return wrapperApi(wrapperSyncApi(fn), name, protocol); case API_TYPE_ASYNC: return wrapperApi(wrapperAsyncApi(name, fn, options), name, protocol); - case API_TYPE_RETURN: - return wrapperApi(wrapperReturnApi(name, fn), name, protocol); } } const Base64ToArrayBufferProtocol = [ @@ -1441,10 +1470,10 @@ const ArrayBufferToBase64Protocol = [ required: true } ]; -const base64ToArrayBuffer = createSyncApi("base64ToArrayBuffer", (base64) => { +const base64ToArrayBuffer = /* @__PURE__ */ createSyncApi("base64ToArrayBuffer", (base64) => { return decode(base64); }, Base64ToArrayBufferProtocol); -const arrayBufferToBase64 = createSyncApi("arrayBufferToBase64", (arrayBuffer) => { +const arrayBufferToBase64 = /* @__PURE__ */ createSyncApi("arrayBufferToBase64", (arrayBuffer) => { return encode(arrayBuffer); }, ArrayBufferToBase64Protocol); const Upx2pxProtocol = [ @@ -1465,7 +1494,7 @@ function checkDeviceWidth() { deviceDPR = pixelRatio2; isIOS = platform === "ios"; } -const upx2px = createSyncApi("upx2px", (number, newDeviceWidth) => { +const upx2px = /* @__PURE__ */ createSyncApi("upx2px", (number, newDeviceWidth) => { if (deviceWidth === 0) { checkDeviceWidth(); } @@ -1544,14 +1573,14 @@ function removeHook(hooks, hook) { hooks.splice(index2, 1); } } -const addInterceptor = createSyncApi("addInterceptor", (method, interceptor) => { +const addInterceptor = /* @__PURE__ */ createSyncApi("addInterceptor", (method, interceptor) => { if (typeof method === "string" && isPlainObject(interceptor)) { mergeInterceptorHook(scopedInterceptors[method] || (scopedInterceptors[method] = {}), interceptor); } else if (isPlainObject(method)) { mergeInterceptorHook(globalInterceptors, method); } }, AddInterceptorProtocol); -const removeInterceptor = createSyncApi("removeInterceptor", (method, interceptor) => { +const removeInterceptor = /* @__PURE__ */ createSyncApi("removeInterceptor", (method, interceptor) => { if (typeof method === "string") { if (isPlainObject(interceptor)) { removeInterceptorHook(scopedInterceptors[method], interceptor); @@ -1639,7 +1668,7 @@ class ServiceIntersectionObserver { }, this._pageId); } } -const createIntersectionObserver$1 = createReturnApi("createIntersectionObserver", (context, options) => { +const createIntersectionObserver$1 = /* @__PURE__ */ createSyncApi("createIntersectionObserver", (context, options) => { if (!context) { context = getCurrentPageVm(); } @@ -1687,14 +1716,6 @@ const GetImageInfoProtocol = { required: true } }; -if (!Promise.prototype.finally) { - Promise.prototype.finally = function(onfinally) { - const promise = this.constructor; - return this.then((value) => promise.resolve(onfinally && onfinally()).then(() => value), (reason) => promise.resolve(onfinally && onfinally()).then(() => { - throw reason; - })); - }; -} function cssSupports(css) { return window.CSS && window.CSS.supports && window.CSS.supports(css); } @@ -1703,19 +1724,19 @@ const SCHEMA_CSS = { "css.env": cssSupports("top:env(a)"), "css.constant": cssSupports("top:constant(a)") }; -const canIUse = createSyncApi("canIUse", (schema) => { +const canIUse = /* @__PURE__ */ createSyncApi("canIUse", (schema) => { if (hasOwn(SCHEMA_CSS, schema)) { return SCHEMA_CSS[schema]; } return true; }, CanIUseProtocol); -const makePhoneCall = createAsyncApi("makePhoneCall", (option) => { +const makePhoneCall = /* @__PURE__ */ createAsyncApi("makePhoneCall", (option) => { window.location.href = `tel:${option.phoneNumber}`; }, MakePhoneCallProtocol); const ua = navigator.userAgent; const isAndroid = /android/i.test(ua); const isIOS$1 = /iphone|ipad|ipod/i.test(ua); -const getSystemInfoSync = createSyncApi("getSystemInfoSync", () => { +const getSystemInfoSync = /* @__PURE__ */ createSyncApi("getSystemInfoSync", () => { var screen = window.screen; var pixelRatio2 = window.devicePixelRatio; const screenFix = /^Apple/.test(navigator.vendor) && typeof window.orientation === "number"; @@ -1816,16 +1837,16 @@ const getSystemInfoSync = createSyncApi("getSystemInfoSync", () => { } }; }); -const getSystemInfo = createAsyncApi("getSystemInfo", () => { +const getSystemInfo = /* @__PURE__ */ createAsyncApi("getSystemInfo", () => { return getSystemInfoSync(); }); -const openDocument = createAsyncApi("openDocument", (option) => { +const openDocument = /* @__PURE__ */ createAsyncApi("openDocument", (option) => { window.open(option.filePath); }, OpenDocumentProtocol); function _getServiceAddress() { return window.location.protocol + "//" + window.location.host; } -const getImageInfo = createAsyncApi("getImageInfo", ({src}, callback) => { +const getImageInfo = /* @__PURE__ */ createAsyncApi("getImageInfo", ({src}, callback) => { const img = new Image(); img.onload = function() { callback({ @@ -1842,9 +1863,9 @@ const getImageInfo = createAsyncApi("getImageInfo", ({src}, callback) => { }; img.src = src; }, GetImageInfoProtocol, GetImageInfoOptions); -const navigateBack = createAsyncApi("navigateBack", () => { +const navigateBack = /* @__PURE__ */ createAsyncApi("navigateBack", () => { }); -const navigateTo = createAsyncApi("navigateTo", (options) => { +const navigateTo = /* @__PURE__ */ createAsyncApi("navigateTo", (options) => { const router = getApp().$router; router.push({ path: options.url, @@ -1852,13 +1873,13 @@ const navigateTo = createAsyncApi("navigateTo", (options) => { state: createPageState("navigateTo") }); }); -const redirectTo = createAsyncApi("redirectTo", () => { +const redirectTo = /* @__PURE__ */ createAsyncApi("redirectTo", () => { }); -const reLaunch = createAsyncApi("reLaunch", () => { +const reLaunch = /* @__PURE__ */ createAsyncApi("reLaunch", () => { }); -const switchTab = createAsyncApi("switchTab", () => { +const switchTab = /* @__PURE__ */ createAsyncApi("switchTab", () => { }); -const getRealPath = createSyncApi("getRealPath", (path) => { +const getRealPath = /* @__PURE__ */ createSyncApi("getRealPath", (path) => { return path; }); var api = /* @__PURE__ */ Object.freeze({ diff --git a/packages/uni-h5/vite.config.ts b/packages/uni-h5/vite.config.ts index d066e5001..bd913527c 100644 --- a/packages/uni-h5/vite.config.ts +++ b/packages/uni-h5/vite.config.ts @@ -18,24 +18,26 @@ export default defineConfig({ __DEV__: `(process.env.NODE_ENV !== 'production')`, __PLATFORM__: JSON.stringify('h5'), }, - alias: [ - { - find: '@dcloudio/uni-api', - replacement: resolve('../uni-api/src/index.ts'), - }, - { - find: '@dcloudio/uni-vue', - replacement: resolve('../uni-vue/src/index.ts'), - }, - { - find: '@dcloudio/uni-core', - replacement: resolve('../uni-core/src/index.ts'), - }, - { - find: '@dcloudio/uni-components', - replacement: resolve('../uni-components/src/index.ts'), - }, - ], + resolve: { + alias: [ + { + find: '@dcloudio/uni-api', + replacement: resolve('../uni-api/src/index.ts'), + }, + { + find: '@dcloudio/uni-vue', + replacement: resolve('../uni-vue/src/index.ts'), + }, + { + find: '@dcloudio/uni-core', + replacement: resolve('../uni-core/src/index.ts'), + }, + { + find: '@dcloudio/uni-components', + replacement: resolve('../uni-components/src/index.ts'), + }, + ], + }, plugins: [ vue({ template: { @@ -54,7 +56,10 @@ export default defineConfig({ preserveEntrySignatures: 'strict', plugins: [ replace({ - createApi: `/*#__PURE__*/ createApi`, + createOnApi: `/*#__PURE__*/ createOnApi`, + createTaskApi: `/*#__PURE__*/ createTaskApi`, + createSyncApi: `/*#__PURE__*/ createSyncApi`, + createAsyncApi: `/*#__PURE__*/ createAsyncApi`, }), ], output: { diff --git a/packages/uni-mp-alipay/dist/uni.api.esm.js b/packages/uni-mp-alipay/dist/uni.api.esm.js index e83a6c691..3b2ba3eda 100644 --- a/packages/uni-mp-alipay/dist/uni.api.esm.js +++ b/packages/uni-mp-alipay/dist/uni.api.esm.js @@ -83,6 +83,7 @@ function createAsyncApiCallback(name, args = {}, { beforeAll, beforeSuccess } = const hasComplete = isFunction(complete); const callbackId = invokeCallbackId++; addInvokeCallback(callbackId, createInvokeCallbackName(name, callbackId), (res) => { + res = res || {}; res.errMsg = normalizeErrMsg(res.errMsg, name); isFunction(beforeAll) && beforeAll(res); if (res.errMsg === name + ':ok') { @@ -97,10 +98,21 @@ function createAsyncApiCallback(name, args = {}, { beforeAll, beforeSuccess } = return callbackId; } +function handlePromise(promise) { + if (__UNI_PROMISE_API__) { + return promise + .then((data) => { + return [null, data]; + }) + .catch((err) => [err]); + } + return promise; +} + const API_TYPE_ON = 0; -const API_TYPE_SYNC = 1; -const API_TYPE_ASYNC = 2; -const API_TYPE_RETURN = 3; +const API_TYPE_TASK = 1; +const API_TYPE_SYNC = 2; +const API_TYPE_ASYNC = 3; function validateProtocol(_name, _args, _protocol) { return true; } @@ -110,18 +122,21 @@ function formatApiArgs(args, options) { function wrapperOnApi(name, fn) { return (callback) => fn.apply(null, createKeepAliveApiCallback(name, callback)); } +function wrapperTaskApi(name, fn, options) { + return (args) => fn.apply(null, [args, createAsyncApiCallback(name, args, options)]); +} function wrapperSyncApi(fn) { return (...args) => fn.apply(null, args); } function wrapperAsyncApi(name, fn, options) { return (args) => { const callbackId = createAsyncApiCallback(name, args, options); - return invokeCallback(callbackId, fn.apply(null, [args, callbackId])); + const res = fn.apply(null, [args, callbackId]); + if (res) { + invokeCallback(callbackId, res); + } }; } -function wrapperReturnApi(name, fn, options) { - return (args) => fn.apply(null, [args, createAsyncApiCallback(name, args, options)]); -} function wrapperApi(fn, name, protocol, options) { return function (...args) { if (!((process.env.NODE_ENV !== 'production') && protocol && !validateProtocol())) { @@ -136,12 +151,12 @@ function createApi(type, name, fn, protocol, options) { switch (type) { case API_TYPE_ON: return wrapperApi(wrapperOnApi(name, fn), name, protocol); + case API_TYPE_TASK: + return wrapperApi(wrapperTaskApi(name, fn), name, protocol); case API_TYPE_SYNC: return wrapperApi(wrapperSyncApi(fn), name, protocol); case API_TYPE_ASYNC: return wrapperApi(wrapperAsyncApi(name, fn, options), name, protocol); - case API_TYPE_RETURN: - return wrapperApi(wrapperReturnApi(name, fn), name, protocol); } } @@ -384,16 +399,6 @@ function isSyncApi(name) { function isCallbackApi(name) { return CALLBACK_API_RE.test(name) && name !== 'onPush'; } -function handlePromise(promise) { - if (!__UNI_PROMISE_API__) { - return promise; - } - return promise - .then((data) => { - return [null, data]; - }) - .catch((err) => [err]); -} function shouldPromise(name) { if (isContextApi(name) || isSyncApi(name) || isCallbackApi(name)) { return false; @@ -416,17 +421,17 @@ function promisify(name, api) { if (!isFunction(api)) { return api; } - return function promiseApi(options = {}, ...params) { + return function promiseApi(options = {}) { if (isFunction(options.success) || isFunction(options.fail) || isFunction(options.complete)) { - return wrapperReturnValue(name, invokeApi(name, api, options, ...params)); + return wrapperReturnValue(name, invokeApi(name, api, options)); } return wrapperReturnValue(name, handlePromise(new Promise((resolve, reject) => { invokeApi(name, api, Object.assign({}, options, { success: resolve, fail: reject, - }), ...params); + })); }))); }; } diff --git a/packages/uni-mp-baidu/dist/uni.api.esm.js b/packages/uni-mp-baidu/dist/uni.api.esm.js index 65a6277e6..70b9624e1 100644 --- a/packages/uni-mp-baidu/dist/uni.api.esm.js +++ b/packages/uni-mp-baidu/dist/uni.api.esm.js @@ -83,6 +83,7 @@ function createAsyncApiCallback(name, args = {}, { beforeAll, beforeSuccess } = const hasComplete = isFunction(complete); const callbackId = invokeCallbackId++; addInvokeCallback(callbackId, createInvokeCallbackName(name, callbackId), (res) => { + res = res || {}; res.errMsg = normalizeErrMsg(res.errMsg, name); isFunction(beforeAll) && beforeAll(res); if (res.errMsg === name + ':ok') { @@ -97,10 +98,21 @@ function createAsyncApiCallback(name, args = {}, { beforeAll, beforeSuccess } = return callbackId; } +function handlePromise(promise) { + if (__UNI_PROMISE_API__) { + return promise + .then((data) => { + return [null, data]; + }) + .catch((err) => [err]); + } + return promise; +} + const API_TYPE_ON = 0; -const API_TYPE_SYNC = 1; -const API_TYPE_ASYNC = 2; -const API_TYPE_RETURN = 3; +const API_TYPE_TASK = 1; +const API_TYPE_SYNC = 2; +const API_TYPE_ASYNC = 3; function validateProtocol(_name, _args, _protocol) { return true; } @@ -110,18 +122,21 @@ function formatApiArgs(args, options) { function wrapperOnApi(name, fn) { return (callback) => fn.apply(null, createKeepAliveApiCallback(name, callback)); } +function wrapperTaskApi(name, fn, options) { + return (args) => fn.apply(null, [args, createAsyncApiCallback(name, args, options)]); +} function wrapperSyncApi(fn) { return (...args) => fn.apply(null, args); } function wrapperAsyncApi(name, fn, options) { return (args) => { const callbackId = createAsyncApiCallback(name, args, options); - return invokeCallback(callbackId, fn.apply(null, [args, callbackId])); + const res = fn.apply(null, [args, callbackId]); + if (res) { + invokeCallback(callbackId, res); + } }; } -function wrapperReturnApi(name, fn, options) { - return (args) => fn.apply(null, [args, createAsyncApiCallback(name, args, options)]); -} function wrapperApi(fn, name, protocol, options) { return function (...args) { if (!((process.env.NODE_ENV !== 'production') && protocol && !validateProtocol())) { @@ -136,12 +151,12 @@ function createApi(type, name, fn, protocol, options) { switch (type) { case API_TYPE_ON: return wrapperApi(wrapperOnApi(name, fn), name, protocol); + case API_TYPE_TASK: + return wrapperApi(wrapperTaskApi(name, fn), name, protocol); case API_TYPE_SYNC: return wrapperApi(wrapperSyncApi(fn), name, protocol); case API_TYPE_ASYNC: return wrapperApi(wrapperAsyncApi(name, fn, options), name, protocol); - case API_TYPE_RETURN: - return wrapperApi(wrapperReturnApi(name, fn), name, protocol); } } @@ -384,16 +399,6 @@ function isSyncApi(name) { function isCallbackApi(name) { return CALLBACK_API_RE.test(name) && name !== 'onPush'; } -function handlePromise(promise) { - if (!__UNI_PROMISE_API__) { - return promise; - } - return promise - .then((data) => { - return [null, data]; - }) - .catch((err) => [err]); -} function shouldPromise(name) { if (isContextApi(name) || isSyncApi(name) || isCallbackApi(name)) { return false; @@ -416,17 +421,17 @@ function promisify(name, api) { if (!isFunction(api)) { return api; } - return function promiseApi(options = {}, ...params) { + return function promiseApi(options = {}) { if (isFunction(options.success) || isFunction(options.fail) || isFunction(options.complete)) { - return wrapperReturnValue(name, invokeApi(name, api, options, ...params)); + return wrapperReturnValue(name, invokeApi(name, api, options)); } return wrapperReturnValue(name, handlePromise(new Promise((resolve, reject) => { invokeApi(name, api, Object.assign({}, options, { success: resolve, fail: reject, - }), ...params); + })); }))); }; } diff --git a/packages/uni-mp-core/src/api/index.ts b/packages/uni-mp-core/src/api/index.ts index 29e243b42..969a636d8 100644 --- a/packages/uni-mp-core/src/api/index.ts +++ b/packages/uni-mp-core/src/api/index.ts @@ -1,13 +1,10 @@ import { hasOwn } from '@vue/shared' -import { - upx2px, - addInterceptor, - removeInterceptor, - promisify, -} from '@dcloudio/uni-api' +import { upx2px, addInterceptor, removeInterceptor } from '@dcloudio/uni-api' +import { promisify } from './promise' import { initWrapper } from './wrapper' + import { MPProtocols } from './protocols' const baseApis = { upx2px, addInterceptor, removeInterceptor } diff --git a/packages/uni-api/src/helpers/promise.ts b/packages/uni-mp-core/src/api/promise.ts similarity index 85% rename from packages/uni-api/src/helpers/promise.ts rename to packages/uni-mp-core/src/api/promise.ts index 33768b086..41ee9269e 100644 --- a/packages/uni-api/src/helpers/promise.ts +++ b/packages/uni-mp-core/src/api/promise.ts @@ -1,6 +1,6 @@ import { isFunction } from '@vue/shared' -import { invokeApi, wrapperReturnValue } from './interceptor' +import { invokeApi, handlePromise, wrapperReturnValue } from '@dcloudio/uni-api' const SYNC_API_RE = /^\$|sendNativeEvent|restoreGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64/ @@ -31,17 +31,6 @@ export function isTaskApi(name: string) { return TASK_APIS.indexOf(name) !== -1 } -function handlePromise(promise: Promise) { - if (!__UNI_PROMISE_API__) { - return promise - } - return promise - .then((data) => { - return [null, data] - }) - .catch((err) => [err]) -} - export function shouldPromise(name: string) { if (isContextApi(name) || isSyncApi(name) || isCallbackApi(name)) { return false @@ -72,13 +61,13 @@ export function promisify(name: string, api: unknown) { if (!isFunction(api)) { return api } - return function promiseApi(options: Record = {}, ...params: []) { + return function promiseApi(options: Record = {}) { if ( isFunction(options.success) || isFunction(options.fail) || isFunction(options.complete) ) { - return wrapperReturnValue(name, invokeApi(name, api, options, ...params)) + return wrapperReturnValue(name, invokeApi(name, api, options)) } return wrapperReturnValue( name, @@ -90,8 +79,7 @@ export function promisify(name: string, api: unknown) { Object.assign({}, options, { success: resolve, fail: reject, - }), - ...params + }) ) }) ) diff --git a/packages/uni-mp-core/src/api/wrapper.ts b/packages/uni-mp-core/src/api/wrapper.ts index 55d11fd7c..14ae0c8e6 100644 --- a/packages/uni-mp-core/src/api/wrapper.ts +++ b/packages/uni-mp-core/src/api/wrapper.ts @@ -1,7 +1,5 @@ import { isFunction, isString, hasOwn, isPlainObject } from '@vue/shared' -import { isSyncApi, isContextApi } from '@dcloudio/uni-api' - import { MPProtocols, MPProtocolArgsValue, @@ -9,6 +7,8 @@ import { MPProtocolObject, } from './protocols' +import { isSyncApi, isContextApi } from './promise' + const CALLBACKS = ['success', 'fail', 'cancel', 'complete'] export function initWrapper(protocols: MPProtocols) { diff --git a/packages/uni-mp-qq/dist/uni.api.esm.js b/packages/uni-mp-qq/dist/uni.api.esm.js index b8e3d8c70..2d24b44a4 100644 --- a/packages/uni-mp-qq/dist/uni.api.esm.js +++ b/packages/uni-mp-qq/dist/uni.api.esm.js @@ -83,6 +83,7 @@ function createAsyncApiCallback(name, args = {}, { beforeAll, beforeSuccess } = const hasComplete = isFunction(complete); const callbackId = invokeCallbackId++; addInvokeCallback(callbackId, createInvokeCallbackName(name, callbackId), (res) => { + res = res || {}; res.errMsg = normalizeErrMsg(res.errMsg, name); isFunction(beforeAll) && beforeAll(res); if (res.errMsg === name + ':ok') { @@ -97,10 +98,21 @@ function createAsyncApiCallback(name, args = {}, { beforeAll, beforeSuccess } = return callbackId; } +function handlePromise(promise) { + if (__UNI_PROMISE_API__) { + return promise + .then((data) => { + return [null, data]; + }) + .catch((err) => [err]); + } + return promise; +} + const API_TYPE_ON = 0; -const API_TYPE_SYNC = 1; -const API_TYPE_ASYNC = 2; -const API_TYPE_RETURN = 3; +const API_TYPE_TASK = 1; +const API_TYPE_SYNC = 2; +const API_TYPE_ASYNC = 3; function validateProtocol(_name, _args, _protocol) { return true; } @@ -110,18 +122,21 @@ function formatApiArgs(args, options) { function wrapperOnApi(name, fn) { return (callback) => fn.apply(null, createKeepAliveApiCallback(name, callback)); } +function wrapperTaskApi(name, fn, options) { + return (args) => fn.apply(null, [args, createAsyncApiCallback(name, args, options)]); +} function wrapperSyncApi(fn) { return (...args) => fn.apply(null, args); } function wrapperAsyncApi(name, fn, options) { return (args) => { const callbackId = createAsyncApiCallback(name, args, options); - return invokeCallback(callbackId, fn.apply(null, [args, callbackId])); + const res = fn.apply(null, [args, callbackId]); + if (res) { + invokeCallback(callbackId, res); + } }; } -function wrapperReturnApi(name, fn, options) { - return (args) => fn.apply(null, [args, createAsyncApiCallback(name, args, options)]); -} function wrapperApi(fn, name, protocol, options) { return function (...args) { if (!((process.env.NODE_ENV !== 'production') && protocol && !validateProtocol())) { @@ -136,12 +151,12 @@ function createApi(type, name, fn, protocol, options) { switch (type) { case API_TYPE_ON: return wrapperApi(wrapperOnApi(name, fn), name, protocol); + case API_TYPE_TASK: + return wrapperApi(wrapperTaskApi(name, fn), name, protocol); case API_TYPE_SYNC: return wrapperApi(wrapperSyncApi(fn), name, protocol); case API_TYPE_ASYNC: return wrapperApi(wrapperAsyncApi(name, fn, options), name, protocol); - case API_TYPE_RETURN: - return wrapperApi(wrapperReturnApi(name, fn), name, protocol); } } @@ -384,16 +399,6 @@ function isSyncApi(name) { function isCallbackApi(name) { return CALLBACK_API_RE.test(name) && name !== 'onPush'; } -function handlePromise(promise) { - if (!__UNI_PROMISE_API__) { - return promise; - } - return promise - .then((data) => { - return [null, data]; - }) - .catch((err) => [err]); -} function shouldPromise(name) { if (isContextApi(name) || isSyncApi(name) || isCallbackApi(name)) { return false; @@ -416,17 +421,17 @@ function promisify(name, api) { if (!isFunction(api)) { return api; } - return function promiseApi(options = {}, ...params) { + return function promiseApi(options = {}) { if (isFunction(options.success) || isFunction(options.fail) || isFunction(options.complete)) { - return wrapperReturnValue(name, invokeApi(name, api, options, ...params)); + return wrapperReturnValue(name, invokeApi(name, api, options)); } return wrapperReturnValue(name, handlePromise(new Promise((resolve, reject) => { invokeApi(name, api, Object.assign({}, options, { success: resolve, fail: reject, - }), ...params); + })); }))); }; } diff --git a/packages/uni-mp-toutiao/dist/uni.api.esm.js b/packages/uni-mp-toutiao/dist/uni.api.esm.js index cf0870220..feb3b69b4 100644 --- a/packages/uni-mp-toutiao/dist/uni.api.esm.js +++ b/packages/uni-mp-toutiao/dist/uni.api.esm.js @@ -83,6 +83,7 @@ function createAsyncApiCallback(name, args = {}, { beforeAll, beforeSuccess } = const hasComplete = isFunction(complete); const callbackId = invokeCallbackId++; addInvokeCallback(callbackId, createInvokeCallbackName(name, callbackId), (res) => { + res = res || {}; res.errMsg = normalizeErrMsg(res.errMsg, name); isFunction(beforeAll) && beforeAll(res); if (res.errMsg === name + ':ok') { @@ -97,10 +98,21 @@ function createAsyncApiCallback(name, args = {}, { beforeAll, beforeSuccess } = return callbackId; } +function handlePromise(promise) { + if (__UNI_PROMISE_API__) { + return promise + .then((data) => { + return [null, data]; + }) + .catch((err) => [err]); + } + return promise; +} + const API_TYPE_ON = 0; -const API_TYPE_SYNC = 1; -const API_TYPE_ASYNC = 2; -const API_TYPE_RETURN = 3; +const API_TYPE_TASK = 1; +const API_TYPE_SYNC = 2; +const API_TYPE_ASYNC = 3; function validateProtocol(_name, _args, _protocol) { return true; } @@ -110,18 +122,21 @@ function formatApiArgs(args, options) { function wrapperOnApi(name, fn) { return (callback) => fn.apply(null, createKeepAliveApiCallback(name, callback)); } +function wrapperTaskApi(name, fn, options) { + return (args) => fn.apply(null, [args, createAsyncApiCallback(name, args, options)]); +} function wrapperSyncApi(fn) { return (...args) => fn.apply(null, args); } function wrapperAsyncApi(name, fn, options) { return (args) => { const callbackId = createAsyncApiCallback(name, args, options); - return invokeCallback(callbackId, fn.apply(null, [args, callbackId])); + const res = fn.apply(null, [args, callbackId]); + if (res) { + invokeCallback(callbackId, res); + } }; } -function wrapperReturnApi(name, fn, options) { - return (args) => fn.apply(null, [args, createAsyncApiCallback(name, args, options)]); -} function wrapperApi(fn, name, protocol, options) { return function (...args) { if (!((process.env.NODE_ENV !== 'production') && protocol && !validateProtocol())) { @@ -136,12 +151,12 @@ function createApi(type, name, fn, protocol, options) { switch (type) { case API_TYPE_ON: return wrapperApi(wrapperOnApi(name, fn), name, protocol); + case API_TYPE_TASK: + return wrapperApi(wrapperTaskApi(name, fn), name, protocol); case API_TYPE_SYNC: return wrapperApi(wrapperSyncApi(fn), name, protocol); case API_TYPE_ASYNC: return wrapperApi(wrapperAsyncApi(name, fn, options), name, protocol); - case API_TYPE_RETURN: - return wrapperApi(wrapperReturnApi(name, fn), name, protocol); } } @@ -384,16 +399,6 @@ function isSyncApi(name) { function isCallbackApi(name) { return CALLBACK_API_RE.test(name) && name !== 'onPush'; } -function handlePromise(promise) { - if (!__UNI_PROMISE_API__) { - return promise; - } - return promise - .then((data) => { - return [null, data]; - }) - .catch((err) => [err]); -} function shouldPromise(name) { if (isContextApi(name) || isSyncApi(name) || isCallbackApi(name)) { return false; @@ -416,17 +421,17 @@ function promisify(name, api) { if (!isFunction(api)) { return api; } - return function promiseApi(options = {}, ...params) { + return function promiseApi(options = {}) { if (isFunction(options.success) || isFunction(options.fail) || isFunction(options.complete)) { - return wrapperReturnValue(name, invokeApi(name, api, options, ...params)); + return wrapperReturnValue(name, invokeApi(name, api, options)); } return wrapperReturnValue(name, handlePromise(new Promise((resolve, reject) => { invokeApi(name, api, Object.assign({}, options, { success: resolve, fail: reject, - }), ...params); + })); }))); }; } diff --git a/packages/uni-mp-weixin/dist/uni.api.esm.js b/packages/uni-mp-weixin/dist/uni.api.esm.js index eb851da88..5233245a8 100644 --- a/packages/uni-mp-weixin/dist/uni.api.esm.js +++ b/packages/uni-mp-weixin/dist/uni.api.esm.js @@ -83,6 +83,7 @@ function createAsyncApiCallback(name, args = {}, { beforeAll, beforeSuccess } = const hasComplete = isFunction(complete); const callbackId = invokeCallbackId++; addInvokeCallback(callbackId, createInvokeCallbackName(name, callbackId), (res) => { + res = res || {}; res.errMsg = normalizeErrMsg(res.errMsg, name); isFunction(beforeAll) && beforeAll(res); if (res.errMsg === name + ':ok') { @@ -97,10 +98,21 @@ function createAsyncApiCallback(name, args = {}, { beforeAll, beforeSuccess } = return callbackId; } +function handlePromise(promise) { + if (__UNI_PROMISE_API__) { + return promise + .then((data) => { + return [null, data]; + }) + .catch((err) => [err]); + } + return promise; +} + const API_TYPE_ON = 0; -const API_TYPE_SYNC = 1; -const API_TYPE_ASYNC = 2; -const API_TYPE_RETURN = 3; +const API_TYPE_TASK = 1; +const API_TYPE_SYNC = 2; +const API_TYPE_ASYNC = 3; function validateProtocol(_name, _args, _protocol) { return true; } @@ -110,18 +122,21 @@ function formatApiArgs(args, options) { function wrapperOnApi(name, fn) { return (callback) => fn.apply(null, createKeepAliveApiCallback(name, callback)); } +function wrapperTaskApi(name, fn, options) { + return (args) => fn.apply(null, [args, createAsyncApiCallback(name, args, options)]); +} function wrapperSyncApi(fn) { return (...args) => fn.apply(null, args); } function wrapperAsyncApi(name, fn, options) { return (args) => { const callbackId = createAsyncApiCallback(name, args, options); - return invokeCallback(callbackId, fn.apply(null, [args, callbackId])); + const res = fn.apply(null, [args, callbackId]); + if (res) { + invokeCallback(callbackId, res); + } }; } -function wrapperReturnApi(name, fn, options) { - return (args) => fn.apply(null, [args, createAsyncApiCallback(name, args, options)]); -} function wrapperApi(fn, name, protocol, options) { return function (...args) { if (!((process.env.NODE_ENV !== 'production') && protocol && !validateProtocol())) { @@ -136,12 +151,12 @@ function createApi(type, name, fn, protocol, options) { switch (type) { case API_TYPE_ON: return wrapperApi(wrapperOnApi(name, fn), name, protocol); + case API_TYPE_TASK: + return wrapperApi(wrapperTaskApi(name, fn), name, protocol); case API_TYPE_SYNC: return wrapperApi(wrapperSyncApi(fn), name, protocol); case API_TYPE_ASYNC: return wrapperApi(wrapperAsyncApi(name, fn, options), name, protocol); - case API_TYPE_RETURN: - return wrapperApi(wrapperReturnApi(name, fn), name, protocol); } } @@ -384,16 +399,6 @@ function isSyncApi(name) { function isCallbackApi(name) { return CALLBACK_API_RE.test(name) && name !== 'onPush'; } -function handlePromise(promise) { - if (!__UNI_PROMISE_API__) { - return promise; - } - return promise - .then((data) => { - return [null, data]; - }) - .catch((err) => [err]); -} function shouldPromise(name) { if (isContextApi(name) || isSyncApi(name) || isCallbackApi(name)) { return false; @@ -416,17 +421,17 @@ function promisify(name, api) { if (!isFunction(api)) { return api; } - return function promiseApi(options = {}, ...params) { + return function promiseApi(options = {}) { if (isFunction(options.success) || isFunction(options.fail) || isFunction(options.complete)) { - return wrapperReturnValue(name, invokeApi(name, api, options, ...params)); + return wrapperReturnValue(name, invokeApi(name, api, options)); } return wrapperReturnValue(name, handlePromise(new Promise((resolve, reject) => { invokeApi(name, api, Object.assign({}, options, { success: resolve, fail: reject, - }), ...params); + })); }))); }; } diff --git a/packages/uni-quickapp-webview/dist/uni.api.esm.js b/packages/uni-quickapp-webview/dist/uni.api.esm.js index 7035964d2..4e0025454 100644 --- a/packages/uni-quickapp-webview/dist/uni.api.esm.js +++ b/packages/uni-quickapp-webview/dist/uni.api.esm.js @@ -83,6 +83,7 @@ function createAsyncApiCallback(name, args = {}, { beforeAll, beforeSuccess } = const hasComplete = isFunction(complete); const callbackId = invokeCallbackId++; addInvokeCallback(callbackId, createInvokeCallbackName(name, callbackId), (res) => { + res = res || {}; res.errMsg = normalizeErrMsg(res.errMsg, name); isFunction(beforeAll) && beforeAll(res); if (res.errMsg === name + ':ok') { @@ -97,10 +98,21 @@ function createAsyncApiCallback(name, args = {}, { beforeAll, beforeSuccess } = return callbackId; } +function handlePromise(promise) { + if (__UNI_PROMISE_API__) { + return promise + .then((data) => { + return [null, data]; + }) + .catch((err) => [err]); + } + return promise; +} + const API_TYPE_ON = 0; -const API_TYPE_SYNC = 1; -const API_TYPE_ASYNC = 2; -const API_TYPE_RETURN = 3; +const API_TYPE_TASK = 1; +const API_TYPE_SYNC = 2; +const API_TYPE_ASYNC = 3; function validateProtocol(_name, _args, _protocol) { return true; } @@ -110,18 +122,21 @@ function formatApiArgs(args, options) { function wrapperOnApi(name, fn) { return (callback) => fn.apply(null, createKeepAliveApiCallback(name, callback)); } +function wrapperTaskApi(name, fn, options) { + return (args) => fn.apply(null, [args, createAsyncApiCallback(name, args, options)]); +} function wrapperSyncApi(fn) { return (...args) => fn.apply(null, args); } function wrapperAsyncApi(name, fn, options) { return (args) => { const callbackId = createAsyncApiCallback(name, args, options); - return invokeCallback(callbackId, fn.apply(null, [args, callbackId])); + const res = fn.apply(null, [args, callbackId]); + if (res) { + invokeCallback(callbackId, res); + } }; } -function wrapperReturnApi(name, fn, options) { - return (args) => fn.apply(null, [args, createAsyncApiCallback(name, args, options)]); -} function wrapperApi(fn, name, protocol, options) { return function (...args) { if (!((process.env.NODE_ENV !== 'production') && protocol && !validateProtocol())) { @@ -136,12 +151,12 @@ function createApi(type, name, fn, protocol, options) { switch (type) { case API_TYPE_ON: return wrapperApi(wrapperOnApi(name, fn), name, protocol); + case API_TYPE_TASK: + return wrapperApi(wrapperTaskApi(name, fn), name, protocol); case API_TYPE_SYNC: return wrapperApi(wrapperSyncApi(fn), name, protocol); case API_TYPE_ASYNC: return wrapperApi(wrapperAsyncApi(name, fn, options), name, protocol); - case API_TYPE_RETURN: - return wrapperApi(wrapperReturnApi(name, fn), name, protocol); } } @@ -384,16 +399,6 @@ function isSyncApi(name) { function isCallbackApi(name) { return CALLBACK_API_RE.test(name) && name !== 'onPush'; } -function handlePromise(promise) { - if (!__UNI_PROMISE_API__) { - return promise; - } - return promise - .then((data) => { - return [null, data]; - }) - .catch((err) => [err]); -} function shouldPromise(name) { if (isContextApi(name) || isSyncApi(name) || isCallbackApi(name)) { return false; @@ -416,17 +421,17 @@ function promisify(name, api) { if (!isFunction(api)) { return api; } - return function promiseApi(options = {}, ...params) { + return function promiseApi(options = {}) { if (isFunction(options.success) || isFunction(options.fail) || isFunction(options.complete)) { - return wrapperReturnValue(name, invokeApi(name, api, options, ...params)); + return wrapperReturnValue(name, invokeApi(name, api, options)); } return wrapperReturnValue(name, handlePromise(new Promise((resolve, reject) => { invokeApi(name, api, Object.assign({}, options, { success: resolve, fail: reject, - }), ...params); + })); }))); }; } diff --git a/packages/vite-plugin-uni/src/config/define.ts b/packages/vite-plugin-uni/src/config/define.ts index 05021591c..314c43635 100644 --- a/packages/vite-plugin-uni/src/config/define.ts +++ b/packages/vite-plugin-uni/src/config/define.ts @@ -2,11 +2,12 @@ import { UserConfig } from 'vite' import { VitePluginUniResolvedOptions } from '..' export function createDefine( - _options: VitePluginUniResolvedOptions + options: VitePluginUniResolvedOptions ): UserConfig['define'] { return { __UNI_WX_API__: true, __UNI_WXS_API__: true, + __UNI_PROMISE_API__: !!(options.feature && options.feature.promise), __UNI_ROUTER_MODE__: JSON.stringify('hash'), } } diff --git a/packages/vite-plugin-uni/src/configResolved/plugins/inject.ts b/packages/vite-plugin-uni/src/configResolved/plugins/inject.ts index 3e997095b..5fe59c917 100644 --- a/packages/vite-plugin-uni/src/configResolved/plugins/inject.ts +++ b/packages/vite-plugin-uni/src/configResolved/plugins/inject.ts @@ -34,7 +34,12 @@ type Injectment = string | [string, string] export interface InjectOptions extends UniPluginFilterOptions { sourceMap?: boolean - [str: string]: Injectment | InjectOptions['include'] | Boolean | ViteDevServer + [str: string]: + | Injectment + | InjectOptions['include'] + | Boolean + | ViteDevServer + | any } const debugInject = debug('uni:inject') diff --git a/packages/vite-plugin-uni/src/index.ts b/packages/vite-plugin-uni/src/index.ts index 15795887a..10433d648 100644 --- a/packages/vite-plugin-uni/src/index.ts +++ b/packages/vite-plugin-uni/src/index.ts @@ -10,6 +10,9 @@ import { createConfigResolved } from './configResolved' import { createConfigureServer } from './configureServer' export interface VitePluginUniOptions { inputDir?: string + feature?: { + promise: boolean + } } export interface VitePluginUniResolvedOptions extends VitePluginUniOptions { root: string -- GitLab