node.js - Nest js cu decoratori swagger, re-decorarea metodelor
Construiesc un API cu Nest JS și Swagger, am creat o clasă abstractă „BaseController” cu metodele CRUD, ideea este să extind din BaseController către controllerele care trec entitatea. Întrebarea mea este cum aș putea redecorate metodele moștenite pentru a implementa decoratorii Swagger în diferitele controllere (cu entități diferite)?
Acesta este BaseController-ul meu:
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);
}
}
}
De exemplu, dacă creez „TodoController extinde BaseController”, cum aș putea aplica decoratorii Swagger? Există o modalitate de a face ceva de genul „decoratori dinamici”?