Búsqueda de sitios web

node.js - Nest js con decoradores de swagger, redecorar métodos


Estoy construyendo una API con Nest JS y Swagger, he creado una clase abstracta "BaseController" con los métodos CRUD, la idea es extender desde el BaseController hacia los controladores pasando la entidad. Mi pregunta es cómo podría volver a decorar los métodos heredados para implementar los decoradores de Swagger en los diferentes controladores (con diferentes entidades).

Este es mi BaseController:

import {
  Body,
  Delete,
  Get,
  HttpException,
  HttpStatus,
  InternalServerErrorException,
  Param,
  Post,
  Put,
  Query,
} from '@nestjs/common';
import { UpdateResult } from 'typeorm';
import { BaseService } from './base.service';
import { ApiException } from '@models/api-exception.model';

export abstract class BaseController<T> {
  protected readonly service: BaseService<T>;

  constructor(service: BaseService<T>) {
    this.service = service;
  }

  @Get()
  root(@Query('filter') filter = {}): Promise<T[]> {
    try {
      return this.service.find(filter);
    } catch(e) {
      throw new HttpException(e, HttpStatus.INTERNAL_SERVER_ERROR);
    }
  }

  @Get(':id')
  getById(@Param('id') id: string | number): Promise<T> {
    try {
      return this.service.findById(id);
    } catch(e) {
      throw new HttpException(e, HttpStatus.INTERNAL_SERVER_ERROR);
    }
  }

  @Post()
  create(@Body() body: T): Promise<T> {
    try {
      return this.service.create(body);
    } catch(e) {
      throw new HttpException(e, HttpStatus.INTERNAL_SERVER_ERROR);
    }
  }

  @Put()
  update(@Body() body: { id: string | number } & T): Promise<UpdateResult> {
    return this.service.update(body.id, body);
  }

  @Delete(':id')
  delete(@Param('id') id: string | number) {
    try {
      return this.service.delete(id);
    } catch(e) {
      throw new InternalServerErrorException(e);
    }
  }
}

Por ejemplo, si creo "TodoController extiende BaseController", ¿cómo podría aplicar los decoradores de Swagger? ¿Hay alguna forma de hacer algo como "decoradores dinámicos"?

Artículos relacionados