import {Filter} from './filters';
export enum Join {
BEFORE,
AFTER,
ERROR,
}
export type JoinOptions = {
target: unknown;
filter?: Filter;
join: Join;
original: (...args: unknown[]) => unknown;
execute: (err?: unknown, result?: unknown) => void;
}
function joinError(options: JoinOptions, ...args: unknown[]): any {
try {
const result = options.original.apply(options.target, args);
if (result instanceof Promise) {
result.catch(err => options.execute(err));
}
return result;
} catch (err) {
options.execute(err);
}
}
export function wrapJoin(options: JoinOptions, ...args: unknown[]): any {
let result: unknown;
switch (options.join) {
case Join.AFTER:
result = options.original.apply(options.target, args);
options.execute(undefined, result);
break;
case Join.BEFORE:
options.execute();
result = options.original.apply(options.target, args);
break;
case Join.ERROR:
result = joinError(options);
break;
}
return result;
} |