Nest js 带有 swagger 装饰器,重新装饰方法

Pop*_*ssa 5 decorator node.js swagger typescript nestjs

我正在使用 Nest js 和 swagger 构建一个 API,我使用 CRUD 方法构建了一个抽象类“BaseController”,这个想法是从 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);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

例如,如果创建“TodoController 扩展 BaseController”,我如何应用 swagger 装饰器?有没有办法制作类似“动态装饰器”的东西?