我正在使用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
}
}
}
}