I'm having some challenges while creating the swagger reusable schema components.
I have a lot of APIs in the project and most of them have the same headers and common error responses, of course, every API has its different response schema, to overcome this challenge I have created a decorator as bellow:
swagger-essentials.decorator.ts
type apiOkResponse = Type<unknown>;
export const SwaggerEssentials = (okRes?: apiOkResponse) => {
return applyDecorators(
SwaggerApiHeaders(),
ApiResponse({ type: okRes, status: 200 }),
HttpCode(200),
ApiResponse({ type: ConnectTimeoutDTO, status: 599 }),
ApiResponse({ type: JsonValidationErrorDTO, status: 501 }),
SwaggerCommonResponses(),
);
};
response.dto.ts
class Response {
@ApiProperty()
id: number;
@ApiProperty()
name:string;
}
export class RESPONSE_SCHEMA {
@ApiProperty({ example: 'Success' })
message: string;
@ApiProperty({example: {}})
data: Response;
}
And then in my controller, I use it like below:
xyz.controller.ts
@Post('/test')
@SwaggerEssentials(RESPONSE_SCHEMA)
fetchDetails(){}
Till here everything is fine and no problem, but as every API has its different response, however, there are two properties that remain the same in case of a successful response which are message
and data
of course the data will be different.
So, I want to have something through which I can get rid of above mentioned two properties and they are automatically defined/attached to the actual response.
I have tried PartialType()
but it's not worthy to solve this problem.