提交 a3f381a2 编写于 作者: DCloud-WZF's avatar DCloud-WZF 💬

feat(interceptor): pass params to callback (question/129195)

上级 5897c827
......@@ -202,9 +202,9 @@ function removeInterceptor (method, option) {
}
}
function wrapperHook (hook) {
function wrapperHook (hook, params) {
return function (data) {
return hook(data) || data
return hook(data, params) || data
}
}
......@@ -212,14 +212,14 @@ 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);
}
......@@ -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
});
......@@ -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)
......
......@@ -809,9 +809,9 @@ var serviceContext = (function () {
}
}
function wrapperHook (hook) {
function wrapperHook (hook, params) {
return function (data) {
return hook(data) || data
return hook(data, params) || data
}
}
......@@ -819,14 +819,14 @@ var serviceContext = (function () {
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);
}
......@@ -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
});
......@@ -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)
......
......@@ -202,9 +202,9 @@ function removeInterceptor (method, option) {
}
}
function wrapperHook (hook) {
function wrapperHook (hook, params) {
return function (data) {
return hook(data) || data
return hook(data, params) || data
}
}
......@@ -212,14 +212,14 @@ 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);
}
......@@ -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
});
......@@ -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)
......
......@@ -202,9 +202,9 @@ function removeInterceptor (method, option) {
}
}
function wrapperHook (hook) {
function wrapperHook (hook, params) {
return function (data) {
return hook(data) || data
return hook(data, params) || data
}
}
......@@ -212,14 +212,14 @@ 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);
}
......@@ -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
});
......@@ -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)
......
......@@ -202,9 +202,9 @@ function removeInterceptor (method, option) {
}
}
function wrapperHook (hook) {
function wrapperHook (hook, params) {
return function (data) {
return hook(data) || data
return hook(data, params) || data
}
}
......@@ -212,14 +212,14 @@ 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);
}
......@@ -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
});
......@@ -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)
......
......@@ -202,9 +202,9 @@ function removeInterceptor (method, option) {
}
}
function wrapperHook (hook) {
function wrapperHook (hook, params) {
return function (data) {
return hook(data) || data
return hook(data, params) || data
}
}
......@@ -212,14 +212,14 @@ 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);
}
......@@ -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
});
......@@ -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)
......
......@@ -202,9 +202,9 @@ function removeInterceptor (method, option) {
}
}
function wrapperHook (hook) {
function wrapperHook (hook, params) {
return function (data) {
return hook(data) || data
return hook(data, params) || data
}
}
......@@ -212,14 +212,14 @@ 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);
}
......@@ -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
});
......@@ -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)
......
......@@ -202,9 +202,9 @@ function removeInterceptor (method, option) {
}
}
function wrapperHook (hook) {
function wrapperHook (hook, params) {
return function (data) {
return hook(data) || data
return hook(data, params) || data
}
}
......@@ -212,14 +212,14 @@ 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);
}
......@@ -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
});
......@@ -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)
......
......@@ -202,9 +202,9 @@ function removeInterceptor (method, option) {
}
}
function wrapperHook (hook) {
function wrapperHook (hook, params) {
return function (data) {
return hook(data) || data
return hook(data, params) || data
}
}
......@@ -212,14 +212,14 @@ 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);
}
......@@ -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
});
......@@ -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)
......
......@@ -212,9 +212,9 @@ function removeInterceptor (method, option) {
}
}
function wrapperHook (hook) {
function wrapperHook (hook, params) {
return function (data) {
return hook(data) || data
return hook(data, params) || data
}
}
......@@ -222,14 +222,14 @@ 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);
}
......@@ -252,7 +252,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
});
......@@ -301,7 +301,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)
......
......@@ -202,9 +202,9 @@ function removeInterceptor (method, option) {
}
}
function wrapperHook (hook) {
function wrapperHook (hook, params) {
return function (data) {
return hook(data) || data
return hook(data, params) || data
}
}
......@@ -212,14 +212,14 @@ 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);
}
......@@ -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
});
......@@ -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)
......
......@@ -202,9 +202,9 @@ function removeInterceptor (method, option) {
}
}
function wrapperHook (hook) {
function wrapperHook (hook, params) {
return function (data) {
return hook(data) || data
return hook(data, params) || data
}
}
......@@ -212,14 +212,14 @@ 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);
}
......@@ -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
});
......@@ -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)
......
......@@ -82,9 +82,9 @@ export function removeInterceptor (method, option) {
}
}
function wrapperHook (hook) {
function wrapperHook (hook, params) {
return function (data) {
return hook(data) || data
return hook(data, params) || data
}
}
......@@ -92,14 +92,14 @@ 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)
}
......@@ -122,7 +122,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
})
......@@ -171,7 +171,11 @@ export 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)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册