Как сохранить контекст, применяя к методу InversifyJS Container
Я использую inversify-express-utils и создаю декоратор для метода. Контроллеры - это Контейнеры. Я создал декоратор для. Но я вижу, что некоторые свойства больше недоступны после применения декоратора. В источниках библиотеки я вижу, что target.constructor передается как контекст. Почему не цель? Я называю метод завернутым.
return fn.call(target.constructor, request, response)
Это декоратор:
import { Handler, NextFunction, Request, Response } from 'express'
import { interfaces } from 'inversify-express-utils'
import HttpContext = interfaces.HttpContext
export function permission (permission: string): any {
return function (target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<Function>) {
const fn = descriptor.value as Handler
descriptor.value = async (request: Request, response: Response, next: NextFunction) => {
const context: HttpContext = Reflect.getMetadata(
'inversify-express-utils:httpcontext',
request
)
const hasAccess = await context.user.isResourceOwner(permission)
if (hasAccess) {
return fn.call(target.constructor, request, response)
// return fn.call(target request, response)
} else {
response
.status(403)
.send({ error: 'The authenticated user does not have an access to the resource.' })
return response
}
}
}
}