Nestjs框架中的元数据是什么以及何时使用方法@setmetadata?

发布于 2025-01-14 23:34:15 字数 91 浏览 5 评论 0原文

我正在从 Nest 文档中学习课程主题反思和元数据。他们使用了 @setmetadata('roles') 但我不知道元数据来自以及何时使用?

I am learning a course topic reflection and metadata from nest documentation. They used @setmetadata('roles') but I don't know metadata come from and when they are used?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

格子衫的從容 2025-01-21 23:34:15

我不知道元数据来自

首先让我们解释一下元数据的一般含义。
元数据通常是指关于数据的数据。它以更简单的术语对数据进行描述(例如有关图像的数据)。以此处为例。

输入图像描述这里

他们使用了@setmetadata('roles')

Nest 提供了通过 @SetMetadata 将自定义数据附加到路由处理程序的功能。它是一种以声明方式定义和存储有关控制器(端点)的数据的方法。

@SetMetadata 存储键值对。例如,

    SetMetadata('IS_PUBLIC_KEY', true)
    findAll(@Query() paginationQuery: PaginationQueryDto) {
            return this.testService.findAll(paginationQuery);
        }

这里我设置一个键 IS_PUBLIC_KEY ,并将值设置为 true

在这种情况下,您定义一个名为 role 的键(很可能并且看起来可能缺少一个值),它将定义哪些特定类型或角色可以访问此控制器。

什么时候使用它们?

当您想要定义守卫时可以使用它。例如,我使用上面的 findAll 控制器作为公共 api。在我的防护实现中,我检查并查看 IsPublic 的值是否为 true,然后允许任何使用者使用该 API。

  canActivate(
    context: ExecutionContext,
  ): boolean | Promise<boolean> | Observable<boolean> {
    const isPublic = this.reflector.get('IS_PUBLIC_KEY', context.getHandler());

    if (isPublic) {
      return true;
    }
}

希望这能回答您的问题。

I don't know metadata come from

First lets explain what metadata generally means.
Metadata in general means data about data. Its a description of the data in more simpler terms (for e.g data about an image). Taking an example from here.

enter image description here

They used @setmetadata('roles').

Nest provides the ability to attach custom data to route handlers through @SetMetadata. Its a way to declaratively define and store data about your controller(endpoint).

@SetMetadata stores the key value pairs. For example,

    SetMetadata('IS_PUBLIC_KEY', true)
    findAll(@Query() paginationQuery: PaginationQueryDto) {
            return this.testService.findAll(paginationQuery);
        }

Here I am setting a key IS_PUBLIC_KEY with a value set to true.

In this scenario you are defining a key named role (most probably and it seems it may be missing a value) which will define what certain types or role can access this controller.

When they are used?

You can use it when you want to define the Guards. For instance, I am using the above findAll controller as a public api. In my guard implementation, I check and see if the value of IsPublic is true then allow any consumer to consume the API.

  canActivate(
    context: ExecutionContext,
  ): boolean | Promise<boolean> | Observable<boolean> {
    const isPublic = this.reflector.get('IS_PUBLIC_KEY', context.getHandler());

    if (isPublic) {
      return true;
    }
}

Hope this answers your question.

野稚 2025-01-21 23:34:15

https://docs.nestjs.com/fundamentals/execution-context#反射和元数据

@SetMetadata() 装饰器是从 @nestjs/common 包导入的。

https://docs.nestjs.com/fundamentals/execution-context#reflection-and-metadata:

The @SetMetadata() decorator is imported from the @nestjs/common package.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文