Skip to content

Commit

Permalink
feat: add @Body context annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
sahachide committed Oct 21, 2020
1 parent 3826bdb commit 7701b76
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/decorators/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { Class } from 'type-fest'
import { REFLECT_METADATA } from '../types/enums'

export function body(target: Class, propertyKey: string, parameterIndex: number): void {
Reflect.defineMetadata(REFLECT_METADATA.CONTEXT_BODY, parameterIndex, target, propertyKey)
}
1 change: 1 addition & 0 deletions src/decorators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './database'
export * from './controller'
export * from './redis'
export * from './security'
export * from './context'
4 changes: 3 additions & 1 deletion src/dependencies/Injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
RequestConfigControllerUser,
} from '../types/interfaces'

import { BodyContextAction } from './InjectorAction/BodyContextAction'
import type { Class } from 'type-fest'
import type { Context } from '../http/Context'
import type { ModuleContext } from './ModuleContext'
Expand Down Expand Up @@ -49,6 +50,7 @@ export class Injector {
new RepositoryAction(this),
new SecurityProviderAction(this),
new SessionAction(this, context, loadedUser, injectedSessions),
new BodyContextAction(this, context),
]
let params: InjectorFunctionParameter[] = []

Expand All @@ -59,7 +61,7 @@ export class Injector {
if (result.length) {
params = [...params, ...result.filter((val) => val !== null)]
}
} else {
} else if (result) {
params.push(result)
}
}
Expand Down
29 changes: 29 additions & 0 deletions src/dependencies/InjectorAction/BodyContextAction.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 BodyContextAction 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_BODY, instance, method)) {
return
}

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

return {
index: metadata,
value: this.context.body,
}
}
}
1 change: 1 addition & 0 deletions src/types/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export enum ERROR {

export enum REFLECT_METADATA {
AUTH_PROVIDER = 'authProvider',
CONTEXT_BODY = 'contextBody',
CONTROLLER_KEY = 'controllerKey',
DATABASE_CONNECTION = 'database:connection',
DATABASE_EM = 'database:em',
Expand Down

0 comments on commit 7701b76

Please sign in to comment.