Skip to content

Commit

Permalink
feat(interceptor): pass params to callback (question/129195)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenyuWang committed Mar 31, 2023
1 parent 5897c82 commit a3f381a
Show file tree
Hide file tree
Showing 16 changed files with 146 additions and 94 deletions.
18 changes: 11 additions & 7 deletions packages/uni-app-plus/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,24 +202,24 @@ function removeInterceptor (method, option) {
}
}

function wrapperHook (hook) {
function wrapperHook (hook, params) {
return function (data) {
return hook(data) || data
return hook(data, params) || data
}
}

function isPromise (obj) {
return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function'
}

function queue (hooks, data) {
function queue (hooks, data, params) {
let promise = false;
for (let i = 0; i < hooks.length; i++) {
const hook = hooks[i];
if (promise) {
promise = Promise.resolve(wrapperHook(hook));
promise = Promise.resolve(wrapperHook(hook, params));
} else {
const res = hook(data);
const res = hook(data, params);
if (isPromise(res)) {
promise = Promise.resolve(res);
}
Expand All @@ -242,7 +242,7 @@ function wrapperOptions (interceptor, options = {}) {
if (Array.isArray(interceptor[name])) {
const oldCallback = options[name];
options[name] = function callbackInterceptor (res) {
queue(interceptor[name], res).then((res) => {
queue(interceptor[name], res, options).then((res) => {
/* eslint-disable no-mixed-operators */
return isFn(oldCallback) && oldCallback(res) || res
});
Expand Down Expand Up @@ -291,7 +291,11 @@ function invokeApi (method, api, options, ...params) {
if (Array.isArray(interceptor.invoke)) {
const res = queue(interceptor.invoke, options);
return res.then((options) => {
return api(wrapperOptions(interceptor, options), ...params)
// 重新访问 getApiInterceptorHooks, 允许 invoke 中再次调用 addInterceptor,removeInterceptor
return api(
wrapperOptions(getApiInterceptorHooks(method), options),
...params
)
})
} else {
return api(wrapperOptions(interceptor, options), ...params)
Expand Down
18 changes: 11 additions & 7 deletions packages/uni-app-plus/dist/index.v3.js
Original file line number Diff line number Diff line change
Expand Up @@ -809,24 +809,24 @@ var serviceContext = (function () {
}
}

function wrapperHook (hook) {
function wrapperHook (hook, params) {
return function (data) {
return hook(data) || data
return hook(data, params) || data
}
}

function isPromise (obj) {
return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function'
}

function queue (hooks, data) {
function queue (hooks, data, params) {
let promise = false;
for (let i = 0; i < hooks.length; i++) {
const hook = hooks[i];
if (promise) {
promise = Promise.resolve(wrapperHook(hook));
promise = Promise.resolve(wrapperHook(hook, params));
} else {
const res = hook(data);
const res = hook(data, params);
if (isPromise(res)) {
promise = Promise.resolve(res);
}
Expand All @@ -849,7 +849,7 @@ var serviceContext = (function () {
if (Array.isArray(interceptor[name])) {
const oldCallback = options[name];
options[name] = function callbackInterceptor (res) {
queue(interceptor[name], res).then((res) => {
queue(interceptor[name], res, options).then((res) => {
/* eslint-disable no-mixed-operators */
return isFn(oldCallback) && oldCallback(res) || res
});
Expand Down Expand Up @@ -898,7 +898,11 @@ var serviceContext = (function () {
if (Array.isArray(interceptor.invoke)) {
const res = queue(interceptor.invoke, options);
return res.then((options) => {
return api(wrapperOptions(interceptor, options), ...params)
// 重新访问 getApiInterceptorHooks, 允许 invoke 中再次调用 addInterceptor,removeInterceptor
return api(
wrapperOptions(getApiInterceptorHooks(method), options),
...params
)
})
} else {
return api(wrapperOptions(interceptor, options), ...params)
Expand Down
2 changes: 1 addition & 1 deletion packages/uni-h5/dist/index.umd.min.js

Large diffs are not rendered by default.

18 changes: 11 additions & 7 deletions packages/uni-mp-alipay/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,24 +202,24 @@ function removeInterceptor (method, option) {
}
}

function wrapperHook (hook) {
function wrapperHook (hook, params) {
return function (data) {
return hook(data) || data
return hook(data, params) || data
}
}

function isPromise (obj) {
return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function'
}

function queue (hooks, data) {
function queue (hooks, data, params) {
let promise = false;
for (let i = 0; i < hooks.length; i++) {
const hook = hooks[i];
if (promise) {
promise = Promise.resolve(wrapperHook(hook));
promise = Promise.resolve(wrapperHook(hook, params));
} else {
const res = hook(data);
const res = hook(data, params);
if (isPromise(res)) {
promise = Promise.resolve(res);
}
Expand All @@ -242,7 +242,7 @@ function wrapperOptions (interceptor, options = {}) {
if (Array.isArray(interceptor[name])) {
const oldCallback = options[name];
options[name] = function callbackInterceptor (res) {
queue(interceptor[name], res).then((res) => {
queue(interceptor[name], res, options).then((res) => {
/* eslint-disable no-mixed-operators */
return isFn(oldCallback) && oldCallback(res) || res
});
Expand Down Expand Up @@ -291,7 +291,11 @@ function invokeApi (method, api, options, ...params) {
if (Array.isArray(interceptor.invoke)) {
const res = queue(interceptor.invoke, options);
return res.then((options) => {
return api(wrapperOptions(interceptor, options), ...params)
// 重新访问 getApiInterceptorHooks, 允许 invoke 中再次调用 addInterceptor,removeInterceptor
return api(
wrapperOptions(getApiInterceptorHooks(method), options),
...params
)
})
} else {
return api(wrapperOptions(interceptor, options), ...params)
Expand Down
18 changes: 11 additions & 7 deletions packages/uni-mp-baidu/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,24 +202,24 @@ function removeInterceptor (method, option) {
}
}

function wrapperHook (hook) {
function wrapperHook (hook, params) {
return function (data) {
return hook(data) || data
return hook(data, params) || data
}
}

function isPromise (obj) {
return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function'
}

function queue (hooks, data) {
function queue (hooks, data, params) {
let promise = false;
for (let i = 0; i < hooks.length; i++) {
const hook = hooks[i];
if (promise) {
promise = Promise.resolve(wrapperHook(hook));
promise = Promise.resolve(wrapperHook(hook, params));
} else {
const res = hook(data);
const res = hook(data, params);
if (isPromise(res)) {
promise = Promise.resolve(res);
}
Expand All @@ -242,7 +242,7 @@ function wrapperOptions (interceptor, options = {}) {
if (Array.isArray(interceptor[name])) {
const oldCallback = options[name];
options[name] = function callbackInterceptor (res) {
queue(interceptor[name], res).then((res) => {
queue(interceptor[name], res, options).then((res) => {
/* eslint-disable no-mixed-operators */
return isFn(oldCallback) && oldCallback(res) || res
});
Expand Down Expand Up @@ -291,7 +291,11 @@ function invokeApi (method, api, options, ...params) {
if (Array.isArray(interceptor.invoke)) {
const res = queue(interceptor.invoke, options);
return res.then((options) => {
return api(wrapperOptions(interceptor, options), ...params)
// 重新访问 getApiInterceptorHooks, 允许 invoke 中再次调用 addInterceptor,removeInterceptor
return api(
wrapperOptions(getApiInterceptorHooks(method), options),
...params
)
})
} else {
return api(wrapperOptions(interceptor, options), ...params)
Expand Down
18 changes: 11 additions & 7 deletions packages/uni-mp-jd/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,24 +202,24 @@ function removeInterceptor (method, option) {
}
}

function wrapperHook (hook) {
function wrapperHook (hook, params) {
return function (data) {
return hook(data) || data
return hook(data, params) || data
}
}

function isPromise (obj) {
return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function'
}

function queue (hooks, data) {
function queue (hooks, data, params) {
let promise = false;
for (let i = 0; i < hooks.length; i++) {
const hook = hooks[i];
if (promise) {
promise = Promise.resolve(wrapperHook(hook));
promise = Promise.resolve(wrapperHook(hook, params));
} else {
const res = hook(data);
const res = hook(data, params);
if (isPromise(res)) {
promise = Promise.resolve(res);
}
Expand All @@ -242,7 +242,7 @@ function wrapperOptions (interceptor, options = {}) {
if (Array.isArray(interceptor[name])) {
const oldCallback = options[name];
options[name] = function callbackInterceptor (res) {
queue(interceptor[name], res).then((res) => {
queue(interceptor[name], res, options).then((res) => {
/* eslint-disable no-mixed-operators */
return isFn(oldCallback) && oldCallback(res) || res
});
Expand Down Expand Up @@ -291,7 +291,11 @@ function invokeApi (method, api, options, ...params) {
if (Array.isArray(interceptor.invoke)) {
const res = queue(interceptor.invoke, options);
return res.then((options) => {
return api(wrapperOptions(interceptor, options), ...params)
// 重新访问 getApiInterceptorHooks, 允许 invoke 中再次调用 addInterceptor,removeInterceptor
return api(
wrapperOptions(getApiInterceptorHooks(method), options),
...params
)
})
} else {
return api(wrapperOptions(interceptor, options), ...params)
Expand Down
18 changes: 11 additions & 7 deletions packages/uni-mp-kuaishou/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,24 +202,24 @@ function removeInterceptor (method, option) {
}
}

function wrapperHook (hook) {
function wrapperHook (hook, params) {
return function (data) {
return hook(data) || data
return hook(data, params) || data
}
}

function isPromise (obj) {
return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function'
}

function queue (hooks, data) {
function queue (hooks, data, params) {
let promise = false;
for (let i = 0; i < hooks.length; i++) {
const hook = hooks[i];
if (promise) {
promise = Promise.resolve(wrapperHook(hook));
promise = Promise.resolve(wrapperHook(hook, params));
} else {
const res = hook(data);
const res = hook(data, params);
if (isPromise(res)) {
promise = Promise.resolve(res);
}
Expand All @@ -242,7 +242,7 @@ function wrapperOptions (interceptor, options = {}) {
if (Array.isArray(interceptor[name])) {
const oldCallback = options[name];
options[name] = function callbackInterceptor (res) {
queue(interceptor[name], res).then((res) => {
queue(interceptor[name], res, options).then((res) => {
/* eslint-disable no-mixed-operators */
return isFn(oldCallback) && oldCallback(res) || res
});
Expand Down Expand Up @@ -291,7 +291,11 @@ function invokeApi (method, api, options, ...params) {
if (Array.isArray(interceptor.invoke)) {
const res = queue(interceptor.invoke, options);
return res.then((options) => {
return api(wrapperOptions(interceptor, options), ...params)
// 重新访问 getApiInterceptorHooks, 允许 invoke 中再次调用 addInterceptor,removeInterceptor
return api(
wrapperOptions(getApiInterceptorHooks(method), options),
...params
)
})
} else {
return api(wrapperOptions(interceptor, options), ...params)
Expand Down
18 changes: 11 additions & 7 deletions packages/uni-mp-lark/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,24 +202,24 @@ function removeInterceptor (method, option) {
}
}

function wrapperHook (hook) {
function wrapperHook (hook, params) {
return function (data) {
return hook(data) || data
return hook(data, params) || data
}
}

function isPromise (obj) {
return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function'
}

function queue (hooks, data) {
function queue (hooks, data, params) {
let promise = false;
for (let i = 0; i < hooks.length; i++) {
const hook = hooks[i];
if (promise) {
promise = Promise.resolve(wrapperHook(hook));
promise = Promise.resolve(wrapperHook(hook, params));
} else {
const res = hook(data);
const res = hook(data, params);
if (isPromise(res)) {
promise = Promise.resolve(res);
}
Expand All @@ -242,7 +242,7 @@ function wrapperOptions (interceptor, options = {}) {
if (Array.isArray(interceptor[name])) {
const oldCallback = options[name];
options[name] = function callbackInterceptor (res) {
queue(interceptor[name], res).then((res) => {
queue(interceptor[name], res, options).then((res) => {
/* eslint-disable no-mixed-operators */
return isFn(oldCallback) && oldCallback(res) || res
});
Expand Down Expand Up @@ -291,7 +291,11 @@ function invokeApi (method, api, options, ...params) {
if (Array.isArray(interceptor.invoke)) {
const res = queue(interceptor.invoke, options);
return res.then((options) => {
return api(wrapperOptions(interceptor, options), ...params)
// 重新访问 getApiInterceptorHooks, 允许 invoke 中再次调用 addInterceptor,removeInterceptor
return api(
wrapperOptions(getApiInterceptorHooks(method), options),
...params
)
})
} else {
return api(wrapperOptions(interceptor, options), ...params)
Expand Down
Loading

0 comments on commit a3f381a

Please sign in to comment.