I'm building an API documentation using nestjs-asyncapi
. Currently the API is properly generated for the endpoints containing a body (DTO) and properly annotated. However, if I add an @AsyncApiSub
annotation for a methods that don't have a body (with payload: null
) I get the error:
.../node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:73
const { prototype } = type;
^
TypeError: Cannot destructure property 'prototype' of 'type' as it is null.
Here is an example of the code that generates such an error:
...
import { AsyncApiSub } from 'nestjs-asyncapi';
@WebSocketGateway()
export class ActivityGateway {
constructor(
private activityService: ActivityService,
) {}
@SubscribeMessage(ActivityActions.ADD_ACTIVITY)
@AsyncApiSub({
channel: ActivityActions.ADD_ACTIVITY,
message: {
payload: CreateActivityTemplateDto,
},
})
public async addActivity(
@ConnectedSocket() socket: ClientSocket,
@MessageBody() body: CreateActivityTemplateDto,
): Promise<ActivitySettings> {
return this.activityService.createNewActivityTemplate(socket.userId, body);
}
@SubscribeMessage(ActivityActions.GENERATE_DEMO_ACTIVITY)
@AsyncApiSub({ channel: ActivityActions.GENERATE_DEMO_ACTIVITY, message: { payload: null } })
public async generateDemoActivity(@ConnectedSocket() socket: ClientSocket): Promise<Activity[]> {
return this.activityService.generateDemoActivity(socket.userId);
}
}
How can I add the Operation ActivityActions.GENERATE_DEMO_ACTIVITY
to my API documentation?
What did you try and what were you expecting?
I tried to add AsyncApiSub
annotation with null payload:
@AsyncApiSub({ channel: ActivityActions.GENERATE_DEMO_ACTIVITY, message: { payload: null } })
And I also tried to add the empty annotation AsyncApiSub()
.
I expect to see the operation ActivityActions.GENERATE_DEMO_ACTIVITY
listed in my API documentation.
To solve this problem I made the following dto
:
export class EmptyDto {}
Then I add this dto to annotations of all endpoints that don't have a body. Here is one example:
@AsyncApiSub({
channel: ActivityActions.GENERATE_DEMO_ACTIVITY,
message: {
payload: EmptyDto,
},
})