diff --git a/build/rollup.config.service.js b/build/rollup.config.service.js index fa1f0c9a9a954e0020bf641275ba55e2e7da0366..6e3cfd5fa4b40b869ec0736cf46494cc6db2e192 100644 --- a/build/rollup.config.service.js +++ b/build/rollup.config.service.js @@ -11,7 +11,8 @@ module.exports = { plugins: [ alias({ 'uni-core': path.resolve(__dirname, '../src/core'), - 'uni-shared': path.resolve(__dirname, '../src/shared/util.js') + 'uni-shared': path.resolve(__dirname, '../src/shared/util.js'), + 'uni-helpers': path.resolve(__dirname, '../src/core/helpers') }), replace({ __GLOBAL__: 'getGlobalUni()', diff --git a/packages/uni-app-plus-nvue/dist/service.legacy.js b/packages/uni-app-plus-nvue/dist/service.legacy.js index a15bfcd5740fec45efada77ea2fe164953385e81..d923de8a4dc57356d894be3b4c0acaea11432dfa 100644 --- a/packages/uni-app-plus-nvue/dist/service.legacy.js +++ b/packages/uni-app-plus-nvue/dist/service.legacy.js @@ -1,16 +1,97 @@ +const _toString = Object.prototype.toString; const hasOwnProperty = Object.prototype.hasOwnProperty; function isFn (fn) { return typeof fn === 'function' } +function isPlainObject (obj) { + return _toString.call(obj) === '[object Object]' +} + function hasOwn (obj, key) { return hasOwnProperty.call(obj, key) } +const HOOKS = [ + 'invoke', + 'success', + 'fail', + 'complete', + 'returnValue' +]; + const globalInterceptors = {}; const scopedInterceptors = {}; +function mergeHook (parentVal, childVal) { + const res = childVal + ? parentVal + ? parentVal.concat(childVal) + : Array.isArray(childVal) + ? childVal : [childVal] + : parentVal; + return res + ? dedupeHooks(res) + : res +} + +function dedupeHooks (hooks) { + const res = []; + for (let i = 0; i < hooks.length; i++) { + if (res.indexOf(hooks[i]) === -1) { + res.push(hooks[i]); + } + } + return res +} + +function removeHook (hooks, hook) { + const index = hooks.indexOf(hook); + if (index !== -1) { + hooks.splice(index, 1); + } +} + +function mergeInterceptorHook (interceptor, option) { + Object.keys(option).forEach(hook => { + if (HOOKS.indexOf(hook) !== -1 && isFn(option[hook])) { + interceptor[hook] = mergeHook(interceptor[hook], option[hook]); + } + }); +} + +function removeInterceptorHook (interceptor, option) { + if (!interceptor || !option) { + return + } + Object.keys(option).forEach(hook => { + if (HOOKS.indexOf(hook) !== -1 && isFn(option[hook])) { + removeHook(interceptor[hook], option[hook]); + } + }); +} + +function addInterceptor (method, option) { + if (typeof method === 'string' && isPlainObject(option)) { + mergeInterceptorHook(scopedInterceptors[method] || (scopedInterceptors[method] = {}), option); + } else if (isPlainObject(method)) { + mergeInterceptorHook(globalInterceptors, method); + } +} + +function removeInterceptor (method, option) { + if (typeof method === 'string') { + if (isPlainObject(option)) { + removeInterceptorHook(scopedInterceptors[method], option); + } else { + delete scopedInterceptors[method]; + } + } else if (isPlainObject(method)) { + removeInterceptorHook(globalInterceptors, method); + } +} + function wrapperHook (hook) { return function (data) { return hook(data) || data @@ -107,7 +188,20 @@ function invokeApi (method, api, options, ...params) { } } return api(options, ...params) -} +} + +const promiseInterceptor = { + returnValue (res) { + if (!isPromise(res)) { + return res + } + return res.then(res => { + return res[1] + }).catch(res => { + return res[0] + }) + } +}; const SYNC_API_RE = /^\$|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64/; @@ -655,6 +749,10 @@ function createLivePusherContext (id, vm) { return new LivePusherContext(id, elm) } +const interceptors = { + promiseInterceptor +}; + var apis = /*#__PURE__*/Object.freeze({ @@ -665,7 +763,10 @@ var apis = /*#__PURE__*/Object.freeze({ $emit: $emit, createMapContext: createMapContext, createVideoContext: createVideoContext, - createLivePusherContext: createLivePusherContext + createLivePusherContext: createLivePusherContext, + interceptors: interceptors, + addInterceptor: addInterceptor, + removeInterceptor: removeInterceptor }); function initUni (uni, nvue, plus, BroadcastChannel) { diff --git a/src/platforms/app-plus-nvue/services/api/legacy/api.js b/src/platforms/app-plus-nvue/services/api/legacy/api.js index 828fc608dad55e64c284f0a988757d561d2198b6..c4d3f573e5f8df0a0aad2dc65c13160aceeab978 100644 --- a/src/platforms/app-plus-nvue/services/api/legacy/api.js +++ b/src/platforms/app-plus-nvue/services/api/legacy/api.js @@ -14,3 +14,4 @@ export { export * from '../../../service/api/context/map' export * from '../../../service/api/context/video' export * from '../../../service/api/context/live-pusher' +export * from 'uni-core/service/api/base/interceptor'