提交 c9188a20 编写于 作者: fxy060608's avatar fxy060608

feat(interceptor): pass params to callback

上级 665d51a1
......@@ -20,20 +20,24 @@ export type Interceptors = { [P in HOOKS]?: Function[] }
export const globalInterceptors: Interceptors = {}
export const scopedInterceptors: { [key: string]: Interceptors } = {}
function wrapperHook(hook: Function) {
function wrapperHook(hook: Function, params?: Record<string, any>) {
return function (data: unknown) {
return hook(data) || data
return hook(data, params) || data
}
}
function queue(hooks: Function[], data: unknown): Promise<any> {
function queue(
hooks: Function[],
data: unknown,
params?: Record<string, any>
): Promise<any> {
let promise: any = 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)
}
......@@ -66,7 +70,7 @@ function wrapperOptions(
}
const oldCallback = options[name]
options[name] = function callbackInterceptor(res: unknown) {
queue(hooks, res).then((res: unknown) => {
queue(hooks, res, options).then((res: unknown) => {
return (isFunction(oldCallback) && oldCallback(res)) || res
})
}
......@@ -122,7 +126,11 @@ export function invokeApi(
if (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)
......
......@@ -358,20 +358,20 @@ const HOOK_FAIL = 'fail';
const HOOK_COMPLETE = 'complete';
const globalInterceptors = {};
const scopedInterceptors = {};
function wrapperHook(hook) {
function wrapperHook(hook, params) {
return function (data) {
return hook(data) || data;
return hook(data, params) || data;
};
}
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);
}
......@@ -398,7 +398,7 @@ function wrapperOptions(interceptors, options = {}) {
}
const oldCallback = options[name];
options[name] = function callbackInterceptor(res) {
queue(hooks, res).then((res) => {
queue(hooks, res, options).then((res) => {
return (isFunction(oldCallback) && oldCallback(res)) || res;
});
};
......@@ -442,7 +442,8 @@ function invokeApi(method, api, options, params) {
if (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 {
......
......@@ -1273,19 +1273,19 @@ const HOOK_FAIL = "fail";
const HOOK_COMPLETE = "complete";
const globalInterceptors = {};
const scopedInterceptors = {};
function wrapperHook(hook) {
function wrapperHook(hook, params) {
return function(data) {
return hook(data) || data;
return hook(data, params) || data;
};
}
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 (shared.isPromise(res)) {
promise = Promise.resolve(res);
}
......@@ -1315,7 +1315,7 @@ function wrapperOptions(interceptors, options = {}) {
}
const oldCallback = options[name];
options[name] = function callbackInterceptor(res) {
queue(hooks, res).then((res2) => {
queue(hooks, res, options).then((res2) => {
return shared.isFunction(oldCallback) && oldCallback(res2) || res2;
});
};
......@@ -1361,7 +1361,10 @@ function invokeApi(method, api2, options, params) {
if (shared.isArray(interceptor.invoke)) {
const res = queue(interceptor.invoke, options);
return res.then((options2) => {
return api2(wrapperOptions(interceptor, options2), ...params);
return api2(
wrapperOptions(getApiInterceptorHooks(method), options2),
...params
);
});
} else {
return api2(wrapperOptions(interceptor, options), ...params);
......
......@@ -2866,19 +2866,19 @@ const HOOK_FAIL = "fail";
const HOOK_COMPLETE = "complete";
const globalInterceptors = {};
const scopedInterceptors = {};
function wrapperHook(hook) {
function wrapperHook(hook, params) {
return function(data) {
return hook(data) || data;
return hook(data, params) || data;
};
}
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);
}
......@@ -2908,7 +2908,7 @@ function wrapperOptions(interceptors2, options = {}) {
}
const oldCallback = options[name];
options[name] = function callbackInterceptor(res) {
queue(hooks, res).then((res2) => {
queue(hooks, res, options).then((res2) => {
return isFunction(oldCallback) && oldCallback(res2) || res2;
});
};
......@@ -2954,7 +2954,10 @@ function invokeApi(method, api2, options, params) {
if (isArray(interceptor.invoke)) {
const res = queue(interceptor.invoke, options);
return res.then((options2) => {
return api2(wrapperOptions(interceptor, options2), ...params);
return api2(
wrapperOptions(getApiInterceptorHooks(method), options2),
...params
);
});
} else {
return api2(wrapperOptions(interceptor, options), ...params);
......
......@@ -227,20 +227,20 @@ const HOOK_FAIL = 'fail';
const HOOK_COMPLETE = 'complete';
const globalInterceptors = {};
const scopedInterceptors = {};
function wrapperHook(hook) {
function wrapperHook(hook, params) {
return function (data) {
return hook(data) || data;
return hook(data, params) || data;
};
}
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);
}
......@@ -267,7 +267,7 @@ function wrapperOptions(interceptors, options = {}) {
}
const oldCallback = options[name];
options[name] = function callbackInterceptor(res) {
queue(hooks, res).then((res) => {
queue(hooks, res, options).then((res) => {
return (isFunction(oldCallback) && oldCallback(res)) || res;
});
};
......@@ -311,7 +311,8 @@ function invokeApi(method, api, options, params) {
if (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 {
......
......@@ -227,20 +227,20 @@ const HOOK_FAIL = 'fail';
const HOOK_COMPLETE = 'complete';
const globalInterceptors = {};
const scopedInterceptors = {};
function wrapperHook(hook) {
function wrapperHook(hook, params) {
return function (data) {
return hook(data) || data;
return hook(data, params) || data;
};
}
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);
}
......@@ -267,7 +267,7 @@ function wrapperOptions(interceptors, options = {}) {
}
const oldCallback = options[name];
options[name] = function callbackInterceptor(res) {
queue(hooks, res).then((res) => {
queue(hooks, res, options).then((res) => {
return (isFunction(oldCallback) && oldCallback(res)) || res;
});
};
......@@ -311,7 +311,8 @@ function invokeApi(method, api, options, params) {
if (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 {
......
......@@ -227,20 +227,20 @@ const HOOK_FAIL = 'fail';
const HOOK_COMPLETE = 'complete';
const globalInterceptors = {};
const scopedInterceptors = {};
function wrapperHook(hook) {
function wrapperHook(hook, params) {
return function (data) {
return hook(data) || data;
return hook(data, params) || data;
};
}
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);
}
......@@ -267,7 +267,7 @@ function wrapperOptions(interceptors, options = {}) {
}
const oldCallback = options[name];
options[name] = function callbackInterceptor(res) {
queue(hooks, res).then((res) => {
queue(hooks, res, options).then((res) => {
return (isFunction(oldCallback) && oldCallback(res)) || res;
});
};
......@@ -311,7 +311,8 @@ function invokeApi(method, api, options, params) {
if (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 {
......
......@@ -227,20 +227,20 @@ const HOOK_FAIL = 'fail';
const HOOK_COMPLETE = 'complete';
const globalInterceptors = {};
const scopedInterceptors = {};
function wrapperHook(hook) {
function wrapperHook(hook, params) {
return function (data) {
return hook(data) || data;
return hook(data, params) || data;
};
}
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);
}
......@@ -267,7 +267,7 @@ function wrapperOptions(interceptors, options = {}) {
}
const oldCallback = options[name];
options[name] = function callbackInterceptor(res) {
queue(hooks, res).then((res) => {
queue(hooks, res, options).then((res) => {
return (isFunction(oldCallback) && oldCallback(res)) || res;
});
};
......@@ -311,7 +311,8 @@ function invokeApi(method, api, options, params) {
if (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 {
......
......@@ -227,20 +227,20 @@ const HOOK_FAIL = 'fail';
const HOOK_COMPLETE = 'complete';
const globalInterceptors = {};
const scopedInterceptors = {};
function wrapperHook(hook) {
function wrapperHook(hook, params) {
return function (data) {
return hook(data) || data;
return hook(data, params) || data;
};
}
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);
}
......@@ -267,7 +267,7 @@ function wrapperOptions(interceptors, options = {}) {
}
const oldCallback = options[name];
options[name] = function callbackInterceptor(res) {
queue(hooks, res).then((res) => {
queue(hooks, res, options).then((res) => {
return (isFunction(oldCallback) && oldCallback(res)) || res;
});
};
......@@ -311,7 +311,8 @@ function invokeApi(method, api, options, params) {
if (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 {
......
......@@ -227,20 +227,20 @@ const HOOK_FAIL = 'fail';
const HOOK_COMPLETE = 'complete';
const globalInterceptors = {};
const scopedInterceptors = {};
function wrapperHook(hook) {
function wrapperHook(hook, params) {
return function (data) {
return hook(data) || data;
return hook(data, params) || data;
};
}
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);
}
......@@ -267,7 +267,7 @@ function wrapperOptions(interceptors, options = {}) {
}
const oldCallback = options[name];
options[name] = function callbackInterceptor(res) {
queue(hooks, res).then((res) => {
queue(hooks, res, options).then((res) => {
return (isFunction(oldCallback) && oldCallback(res)) || res;
});
};
......@@ -311,7 +311,8 @@ function invokeApi(method, api, options, params) {
if (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 {
......
......@@ -227,20 +227,20 @@ const HOOK_FAIL = 'fail';
const HOOK_COMPLETE = 'complete';
const globalInterceptors = {};
const scopedInterceptors = {};
function wrapperHook(hook) {
function wrapperHook(hook, params) {
return function (data) {
return hook(data) || data;
return hook(data, params) || data;
};
}
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);
}
......@@ -267,7 +267,7 @@ function wrapperOptions(interceptors, options = {}) {
}
const oldCallback = options[name];
options[name] = function callbackInterceptor(res) {
queue(hooks, res).then((res) => {
queue(hooks, res, options).then((res) => {
return (isFunction(oldCallback) && oldCallback(res)) || res;
});
};
......@@ -311,7 +311,8 @@ function invokeApi(method, api, options, params) {
if (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 {
......
......@@ -227,20 +227,20 @@ const HOOK_FAIL = 'fail';
const HOOK_COMPLETE = 'complete';
const globalInterceptors = {};
const scopedInterceptors = {};
function wrapperHook(hook) {
function wrapperHook(hook, params) {
return function (data) {
return hook(data) || data;
return hook(data, params) || data;
};
}
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);
}
......@@ -267,7 +267,7 @@ function wrapperOptions(interceptors, options = {}) {
}
const oldCallback = options[name];
options[name] = function callbackInterceptor(res) {
queue(hooks, res).then((res) => {
queue(hooks, res, options).then((res) => {
return (isFunction(oldCallback) && oldCallback(res)) || res;
});
};
......@@ -311,7 +311,8 @@ function invokeApi(method, api, options, params) {
if (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 {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册