'Create a custom response in Swagger Nestjs

I have my controller with

  @Post('email/register')
  @HttpCode(HttpStatus.CREATED)
  @ApiOkResponse(AuthConfigSwagger.API_OP_CREATE)
  @ApiCreatedResponse(AuthConfigSwagger.API_RES_CREATE)
  async register(@Body() authRegisterLoginDto: AuthRegisterLoginDto) {
    return this.authService.register(authRegisterLoginDto);
  }

where AuthConfigSwagger.API_RES_CREATE is

static readonly API_RES_CREATE: ApiResponseOptions = {
    description: 'The user has been successfully created.',
    type: User
  };

the response is not the real response that I create. In that way, I show the entire document (I'm using mongoDb)

I need to put my custom response, like

{
  "statusCode": 201,
  "message": "",
  "status": "success"
}

And Sometimes I need to use User properties.

I read the documentation, and I can't find any properties for custom response.

UPDATE:

I can create a class like:

import { ApiProperty } from "@nestjs/swagger";

export class successResponse {

    @ApiProperty({
        example: 'success',
        description: 'status',
    })
    status: string;
    @ApiProperty({
        description: 'status',
    })
    message?: string;

    @ApiProperty({
        description: 'could contain some info',        
    })
    data?: object;
    
}

And I have

{
  "status": "success",
  "message": "string",
  "data": {}
}

in my Example value on swagger. But, for example in the Login route, I'd like to see something like that on 200 Response:

{
  "data": {
    "expiresIn": number,
    "accessToken": "string",
    "user": {
      "name": "string",
      "email": "string",
      "id": "string"
    }
  },
  "statusCode": number,
  "status": "string"
}

I don't want to create a custom response for each API.



Solution 1:[1]

Use an Interface for example.

interface IResponseWrapper<T>{ 
  status: string;
  statusCode: number;
  data: T
}

Now use this Interface to extends your class

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Subhadip Majumder