Skip to content

Commit

Permalink
feat: add @response and @res context annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
sahachide committed Oct 22, 2020
1 parent 944b3d1 commit c7a29ea
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/decorators/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,9 @@ export function request(target: Class, propertyKey: string, parameterIndex: numb
}

export const req = request

export function response(target: Class, propertyKey: string, parameterIndex: number): void {
Reflect.defineMetadata(REFLECT_METADATA.CONTEXT_RESPONSE, parameterIndex, target, propertyKey)
}

export const res = response
2 changes: 2 additions & 0 deletions src/dependencies/Injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { CookieContextAction } from './InjectorAction/CookieContextAction'
import type { ModuleContext } from './ModuleContext'
import { QueryContextAction } from './InjectorAction/QueryContextAction'
import { RequestContextAction } from './InjectorAction/RequestContextAction'
import { ResponseContextAction } from './InjectorAction/ResponseContextAction'
import type { Session } from '../security/Session'

export class Injector {
Expand Down Expand Up @@ -57,6 +58,7 @@ export class Injector {
new QueryContextAction(this, context),
new CookieContextAction(this, context),
new RequestContextAction(this, context),
new ResponseContextAction(this, context),
]
let params: InjectorFunctionParameter[] = []

Expand Down
33 changes: 33 additions & 0 deletions src/dependencies/InjectorAction/ResponseContextAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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 ResponseContextAction 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_RESPONSE, instance, method)) {
return
}

const metadata = Reflect.getMetadata(
REFLECT_METADATA.CONTEXT_RESPONSE,
instance,
method,
) as number

return {
index: metadata,
value: this.context.response,
}
}
}
1 change: 1 addition & 0 deletions src/types/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export enum REFLECT_METADATA {
CONTEXT_QUERY = 'contextQuery',
CONTEXT_PARAMS = 'contextParams',
CONTEXT_REQUEST = 'contextRequest',
CONTEXT_RESPONSE = 'contextResponse',
CONTROLLER_KEY = 'controllerKey',
DATABASE_CONNECTION = 'database:connection',
DATABASE_EM = 'database:em',
Expand Down

0 comments on commit c7a29ea

Please sign in to comment.