小编A. *_*tre的帖子

类转换器和类验证器:在类验证器错误中显示 @expose 名称

我有一个 NestJS 项目,其中同时使用类验证器和类转换器,并且我需要在类验证器抛出错误之前执行类转换器。

给定以下课程:

export class CreateProfileDto {
  @IsString()
  @Expose({ name: 'name' })
  profileName!: string;

  @IsBoolean()
  @Expose({ name: 'active'})
  profileActive!: boolean;
}
Run Code Online (Sandbox Code Playgroud)

我需要用属性名称而不是属性 profileName 来公开错误,其他属性也是如此。

有什么直接的想法来管理这个吗?无法要求前端向我发送具有不同名称的属性,这就是我需要调整它们的原因。

我想通过管道来做到这一点,但在错误爆发之前无法使用它。

当前错误格式:

{
    "statusCode": 400,
    "error": "Bad Request",
    "message": [
        {
            "target": {
                "profileName": 1
            },
            "value": 1,
            "property": "profileName",
            "children": [],
            "constraints": {
                "isString": "profileName must be a string"
            }
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

所需的错误格式:

{
    "statusCode": 400,
    "error": "Bad Request",
    "message": [
        {
            "target": {
                "name": 1
            },
            "value": 1,
            "property": "name",
            "children": …
Run Code Online (Sandbox Code Playgroud)

class-validator nestjs class-transformer

8
推荐指数
1
解决办法
2612
查看次数

如何使用 TypeORM 0.3.x 在 NestJS 9 中创建自定义(单独文件)存储库

这不是重复的问题。请不要将其标记为那样。

以下不是我想要的

import { EntityRepository, Repository } from "typeorm";
import { Test } from "./test.model";
import { Injectable } from "@nestjs/common";

@EntityRepository(Test)
export class TestRepository extends Repository<Test> {}
Run Code Online (Sandbox Code Playgroud)

装饰@EntityRepository器现在已被弃用。

我也不想像这里一样创建一个假存储库: /sf/answers/5134658581/

我也不想要这个,因为我必须从中提取managerdataSource我不想要这个,因为我认为这不是最好的方法。

    export const UserRepository = dataSource.getRepository(User).extend({
        //                        ^^^^^^^^^^ from where this came from
        findByName(firstName: string, lastName: string) {
            return this.createQueryBuilder("user")
                .where("user.firstName = :firstName", { firstName })
                .andWhere("user.lastName = :lastName", { lastName })
                .getMany()
        },
    })
Run Code Online (Sandbox Code Playgroud)

上面找到: https: //orkhan.gitbook.io/typeorm/docs/custom-repository#how-to-create-custom-repository

我不认为这是在 NestJS 上下文中。

我想要什么想知道在最新版本的 NestJS …

repository-pattern typeorm nestjs

8
推荐指数
1
解决办法
4360
查看次数

如何从控制器中的标头获取 JWT 令牌

请帮我找到如何优化我的代码。我需要限制登录用户的数据。为此,我需要从 Request 的 JWT 令牌中获取 UUID。但我不喜欢我的方法,因为我有重复的代码:

const jwt = request.headers.authorization.replace('Bearer ', '');
const json = this.jwtService.decode(jwt, { json: true }) as { uuid: string };
Run Code Online (Sandbox Code Playgroud)

有谁知道我如何优化它?

这是我的控制器代码。

import { Controller, Get, Put, Body, Param, UseGuards, Req } from '@nestjs/common';
import { SettingService } from '../services';
import { AuthGuard } from '@nestjs/passport';
import { ResultInterface } from '../interfaces';
import { Request } from 'express';
import { JwtService } from '@nestjs/jwt';

@Controller('settings')
export class SettingController {
  /**
   * @param service
   * @param …
Run Code Online (Sandbox Code Playgroud)

controller request http-headers nestjs

7
推荐指数
1
解决办法
6900
查看次数

所有控制器的全局标头(nestJs swagger)

有没有办法将所需的标头全局添加到 NestJS 中的所有端点/控制器?

有一个控制器绑定装饰器@ApiHeader。有没有办法将其应用于所有端点?

swagger nestjs

2
推荐指数
1
解决办法
1057
查看次数

Jwt 策略后 ExecutionContext 中用户丢失

目前我已经实现了 jwt 防护,它工作得很好,使用 Passport,jwt 正在验证颁发的令牌,我可以通过 @Request 通过 req.user 查看用户,在实现基于角色的身份验证作为已经工作的附加组件后出现问题JWT 的守卫。

我遵循了 Nestjs.com 提供的指导,但没有帮助。 https://docs.nestjs.com/guards

基本角色配置:

角色装饰器.ts

import { SetMetadata } from '@nestjs/common';
export const Roles = (...roles: string[]) => SetMetadata('roles', roles);
Run Code Online (Sandbox Code Playgroud)

角色.guard.ts

import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';
import { Reflector } from '@nestjs/core';

@Injectable()
export class RolesGuard implements CanActivate {
  constructor(private readonly reflector: Reflector) {}

  canActivate(context: ExecutionContext): boolean {
    const roles = this.reflector.get<string[]>('roles', context.getHandler());
    if (!roles) {
      return true;
    }
    const request = context.switchToHttp().getRequest();
    const user = …
Run Code Online (Sandbox Code Playgroud)

jwt nestjs

1
推荐指数
1
解决办法
2782
查看次数