From e140d655e06102e38f14e534003379a003201403 Mon Sep 17 00:00:00 2001 From: sahachide Date: Thu, 22 Oct 2020 10:03:07 +0200 Subject: [PATCH] feat: add @error context annotation --- src/decorators/context.ts | 4 +++ src/dependencies/Injector.ts | 2 ++ .../InjectorAction/ErrorContextAction.ts | 29 +++++++++++++++++++ src/types/enums.ts | 1 + 4 files changed, 36 insertions(+) create mode 100644 src/dependencies/InjectorAction/ErrorContextAction.ts diff --git a/src/decorators/context.ts b/src/decorators/context.ts index 494da6d..4989941 100644 --- a/src/decorators/context.ts +++ b/src/decorators/context.ts @@ -28,3 +28,7 @@ export function response(target: Class, propertyKey: string, parameterIndex: num } export const res = response + +export function error(target: Class, propertyKey: string, parameterIndex: number): void { + Reflect.defineMetadata(REFLECT_METADATA.CONTEXT_ERROR, parameterIndex, target, propertyKey) +} diff --git a/src/dependencies/Injector.ts b/src/dependencies/Injector.ts index 1645056..53f85a4 100644 --- a/src/dependencies/Injector.ts +++ b/src/dependencies/Injector.ts @@ -18,6 +18,7 @@ import { BodyContextAction } from './InjectorAction/BodyContextAction' import type { Class } from 'type-fest' import type { Context } from '../http/Context' import { CookieContextAction } from './InjectorAction/CookieContextAction' +import { ErrorContextAction } from './InjectorAction/ErrorContextAction' import type { ModuleContext } from './ModuleContext' import { QueryContextAction } from './InjectorAction/QueryContextAction' import { RequestContextAction } from './InjectorAction/RequestContextAction' @@ -59,6 +60,7 @@ export class Injector { new CookieContextAction(this, context), new RequestContextAction(this, context), new ResponseContextAction(this, context), + new ErrorContextAction(this, context), ] let params: InjectorFunctionParameter[] = [] diff --git a/src/dependencies/InjectorAction/ErrorContextAction.ts b/src/dependencies/InjectorAction/ErrorContextAction.ts new file mode 100644 index 0000000..b8d85c5 --- /dev/null +++ b/src/dependencies/InjectorAction/ErrorContextAction.ts @@ -0,0 +1,29 @@ +import type { GenericControllerInstance, InjectorFunctionParameter } from '../../types/interfaces' + +import { AbstractAction } from './AbstractAction' +import type { Context } from '../../http/Context' +import type { Injector } from '../Injector' +import { REFLECT_METADATA } from '../../types' + +export class ErrorContextAction extends AbstractAction { + protected readonly context: Context + + constructor(injector: Injector, context: Context) { + super(injector) + + this.context = context + } + + public run(instance: GenericControllerInstance, method: string): InjectorFunctionParameter { + if (!Reflect.hasMetadata(REFLECT_METADATA.CONTEXT_ERROR, instance, method)) { + return + } + + const metadata = Reflect.getMetadata(REFLECT_METADATA.CONTEXT_ERROR, instance, method) as number + + return { + index: metadata, + value: this.context.error, + } + } +} diff --git a/src/types/enums.ts b/src/types/enums.ts index d8f0465..a82a8f8 100644 --- a/src/types/enums.ts +++ b/src/types/enums.ts @@ -32,6 +32,7 @@ export enum REFLECT_METADATA { AUTH_PROVIDER = 'authProvider', CONTEXT_BODY = 'contextBody', CONTEXT_COOKIE = 'contextCookie', + CONTEXT_ERROR = 'contextError', CONTEXT_QUERY = 'contextQuery', CONTEXT_PARAMS = 'contextParams', CONTEXT_REQUEST = 'contextRequest',