Skip to content

Commit

Permalink
feat: add @error context annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
sahachide committed Oct 22, 2020
1 parent c7a29ea commit e140d65
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/decorators/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 2 additions & 0 deletions src/dependencies/Injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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[] = []

Expand Down
29 changes: 29 additions & 0 deletions src/dependencies/InjectorAction/ErrorContextAction.ts
Original file line number Diff line number Diff line change
@@ -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,
}
}
}
1 change: 1 addition & 0 deletions src/types/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit e140d65

Please sign in to comment.