如何在运行时在 nestjs 中扫描所有装饰器值
how to scan all decorators value at runtime in nestjs
我必须收集出现在我的应用程序中不同位置的所有装饰器值作为字符串,然后在 运行 时将它们保存到数据库中,我不必将它们添加两次(在数据库中和代码),
我试过了,但我不知道我用的是什么
Reflector
api 来自 nestjs 如下
this.reflector.getAll<string>('access', context.getHandler())
但我无法在 运行 时间内获得 context.getHandler()
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalPipes(new ValidationPipe());
// Here is where i want to save
await app.listen(3000);
}
bootstrap();
这是我的装饰器
@HashPermission('access_value')
请协助
对于这样的东西,你要么需要使用像 Nest's undocumented DiscoveryService
or a package like @golevelup/nestjs-discovery
这样的东西,它是 Nest 包的友好包装器。然后,您可以使用 this.discoveryService.methodsAndControllerMethodsWithMetaAtKey
之类的方法来获取具有该元数据的 classes 和方法,然后您可以在每个方法上使用反射器 class 来获取元数据值。
我必须收集出现在我的应用程序中不同位置的所有装饰器值作为字符串,然后在 运行 时将它们保存到数据库中,我不必将它们添加两次(在数据库中和代码),
我试过了,但我不知道我用的是什么
Reflector
api 来自 nestjs 如下
this.reflector.getAll<string>('access', context.getHandler())
但我无法在 运行 时间内获得 context.getHandler()
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalPipes(new ValidationPipe());
// Here is where i want to save
await app.listen(3000);
}
bootstrap();
这是我的装饰器
@HashPermission('access_value')
请协助
对于这样的东西,你要么需要使用像 Nest's undocumented DiscoveryService
or a package like @golevelup/nestjs-discovery
这样的东西,它是 Nest 包的友好包装器。然后,您可以使用 this.discoveryService.methodsAndControllerMethodsWithMetaAtKey
之类的方法来获取具有该元数据的 classes 和方法,然后您可以在每个方法上使用反射器 class 来获取元数据值。