Utilizo para este proyecto nest y la biblioteca nestjs/crud. Desafortunadamente, no puedo anular la función createOneBase para que devuelva una respuesta de persona. Busqué una solución en las últimas publicaciones del mismo tema. Me gustaría cambiar la respuesta cuando creo un usuario.
lo que tengo:
{ "id": 12, "username": "test", "password": "tests", "email": "test@foo.bar" }lo que espero:
{ "id": 12, "username": "test", "password": "tests", "email": "test@foo.bar", "token": "TOKEN" }mi controlador:
import { Controller } from '@nestjs/common'; import { UsersService } from './users.service'; import { Crud, Override, ParsedRequest, ParsedBody, CrudRequest, CrudController } from '@nestjsx/crud'; import { User, UserDTO } from './user'; @Crud({ model: { type: User, }, }) @Controller('users') export class UsersController { constructor(public service: UsersService) {} get base(): CrudController<User | UserDTO> { return this; } @Override() createOne( @ParsedRequest() req: CrudRequest, @ParsedBody() dto: User, ): Promise<User | UserDTO> { const userDto: UserDTO = { id: dto.id, username: dto.username, password: dto.password, email: dto.email, token: 'TOKEN', }; return this.base.createOneBase(req, userDto); } }mis entidades:
import { IsDefined, IsString, MinLength } from 'class-validator'; import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm'; @Entity() export class User { @PrimaryGeneratedColumn() id: number; @Column() @IsDefined({ always: true }) @IsString({ always: true }) @MinLength(1, { always: true }) username: string; @Column() @IsDefined({ always: true }) @IsString({ always: true }) @MinLength(5, { always: true }) password: string; @Column() @IsDefined({ always: true }) @IsString({ always: true }) @MinLength(1, { always: true }) email: string; } export class UserDTO { id: number; username: string; password: string; email: string; token: string; access_token?: string; }asi que no entiendo que le pasa, gracias por tu ayuda
Para anular un método Nestjs/Crud, use el decorador @Override(functionName) Nestjs/Crud Doc
@Override('createOneBase') createOne( @ParsedRequest() req: CrudRequest, @ParsedBody() dto: Hero) { return this.base.createOneBase(req, dto); }