Files
nest/packages/core/router/router-proxy.ts
2024-11-26 13:36:33 +01:00

55 lines
1.4 KiB
TypeScript

import { ExceptionsHandler } from '../exceptions/exceptions-handler';
import { ExecutionContextHost } from '../helpers/execution-context-host';
export type RouterProxyCallback = <TRequest, TResponse>(
req: TRequest,
res: TResponse,
next: () => void,
) => void | Promise<void>;
export class RouterProxy {
public createProxy(
targetCallback: RouterProxyCallback,
exceptionsHandler: ExceptionsHandler,
) {
return async <TRequest, TResponse>(
req: TRequest,
res: TResponse,
next: () => void,
) => {
try {
await targetCallback(req, res, next);
} catch (e) {
const host = new ExecutionContextHost([req, res, next]);
exceptionsHandler.next(e, host);
return res;
}
};
}
public createExceptionLayerProxy(
targetCallback: <TError, TRequest, TResponse>(
err: TError,
req: TRequest,
res: TResponse,
next: () => void,
) => void | Promise<void>,
exceptionsHandler: ExceptionsHandler,
) {
return async <TError, TRequest, TResponse>(
err: TError,
req: TRequest,
res: TResponse,
next: () => void,
) => {
try {
await targetCallback(err, req, res, next);
} catch (e) {
const host = new ExecutionContextHost([req, res, next]);
exceptionsHandler.next(e, host);
return res;
}
};
}
}