Wnioskowanie typów

Trzy automatyczne mechanizmy: interfejsy stają się schematem inline, class-validator staje się JSON Schema, @HttpCode() staje się właściwym statusem.

DTO typowane interfejsem

Gdy typ odpowiedzi lub body jest interface TypeScript (nie klasą), Swagger nie może użyć go jako wartości type:, bo interfejsy są usuwane w czasie działania. nestjs-docfy wykrywa to automatycznie i generuje obiekt inline schema:, bez żadnych zmian w Twoim kodzie.

Sua interface
// Your existing interface (no need to convert to a class)
export interface RegisterResponseDto {
  success: boolean;
  message: string | null;
}

Wygenerowany wynik:

Generated
ApiResponse({
  status: 201,
  description: 'Created',
  schema: {
    type: 'object',
    properties: {
      success: { type: 'boolean' },
      message: { type: 'string', nullable: true },
    },
    required: ['success'],
  },
}),

Obsługuje: prymitywy, unie nullowalne (T | null), tablice, zagnieżdżone interfejsy oraz właściwości opcjonalne (wykluczone z required).

Wnioskowanie z class-validator

Gdy klasa DTO używa dekoratorów class-validator i nie ma jeszcze @ApiProperty na swoich właściwościach, nestjs-docfy wnioskuje pełny JSON Schema z dekoratorów walidacji, bez potrzeby ręcznej adnotacji.

create-user.dto.ts
// create-user.dto.ts
import { IsString, IsEmail, MinLength, IsOptional } from 'class-validator';

export class CreateUserDto {
  @IsString()
  @MinLength(2)
  name: string;

  @IsEmail()
  email: string;

  @IsOptional()
  @IsString()
  bio?: string;
}

Wygenerowany wynik:

ts
ApiBody({
  schema: {
    type: 'object',
    properties: {
      name:  { type: 'string', minLength: 2 },
      email: { type: 'string', format: 'email' },
      bio:   { type: 'string' },
    },
    required: ['name', 'email'],
  },
}),

Wspierane dekoratory

Decorator
@IsString
@IsEmail
@IsUrl
@IsUUID
@IsDateString
@IsNumber
@IsInt
@IsBoolean
@IsArray
@Min
@Max
@MinLength
@MaxLength
@IsOptional
Zachowanie istniejących adnotacji

Jeśli jakakolwiek właściwość klasy ma już @ApiProperty, wnioskowanie jest pomijane i zamiast tego użyte jest type: ClassName; Twoje istniejące adnotacje Swaggera nigdy nie są nadpisywane.

Obsługa @HttpCode()

Dekorator NestJS @HttpCode() nadpisuje domyślny kod statusu HTTP dla handlera trasy. nestjs-docfy odczytuje go automatycznie i używa właściwego kodu w wygenerowanym ApiResponse.

users.controller.ts
// users.controller.ts
@Post('logout')
@HttpCode(204)
logout(): void { ... }

Wygenerowany wynik:

ts
logout: [
  ApiOperation({ summary: 'Logout' }),
  ApiResponse({ status: 204, description: 'No Content' }),
],

Bez @HttpCode() stosowane są domyślne kody: 201 dla @Post, 200 dla każdego innego czasownika HTTP.