我正在使用
nestjs-asyncapi
构建 API 文档。目前,API 已为包含主体 (DTO) 的端点正确生成并正确注释。但是,如果我为没有主体的方法添加 @AsyncApiSub
注释(带有 payload: null
),我会收到错误:
.../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.
以下是生成此类错误的代码示例:
...
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);
}
}
如何将操作
ActivityActions.GENERATE_DEMO_ACTIVITY
添加到我的 API 文档中?
您尝试了什么以及您期待什么?
我尝试添加带有空负载的
AsyncApiSub
注释:@AsyncApiSub({ channel: ActivityActions.GENERATE_DEMO_ACTIVITY, message: { payload: null } })
我还尝试添加空注释AsyncApiSub()
。ActivityActions.GENERATE_DEMO_ACTIVITY
。
要解决此问题,您应该提供有效的有效负载类型,即使它是空类型或没有属性的对象。
以下是修改代码的方法:
创建空 DTO:定义一个空 DTO 类,可以表示空负载。这是一个占位符,不会影响实际功能。
export class EmptyDto {}
更新@AsyncApiSub注解:在payload字段中使用这个EmptyDto而不是null
@SubscribeMessage(ActivityActions.GENERATE_DEMO_ACTIVITY)
@AsyncApiSub({
channel: ActivityActions.GENERATE_DEMO_ACTIVITY,
message: { payload: EmptyDto }, // Use the EmptyDto class
})
public async generateDemoActivity(@ConnectedSocket() socket: ClientSocket): Promise<Activity[]> {
return this.activityService.generateDemoActivity(socket.userId);
}
完整的类应该如下所示:
import { AsyncApiSub } from 'nestjs-asyncapi';
import { SubscribeMessage, WebSocketGateway } from '@nestjs/websockets';
import { ConnectedSocket, MessageBody } from '@nestjs/websockets';
import { ClientSocket } from './client-socket.interface'; // adjust as needed
import { CreateActivityTemplateDto } from './create-activity-template.dto'; // adjust as needed
import { ActivitySettings } from './activity-settings.dto'; // adjust as needed
import { Activity } from './activity.dto'; // adjust as needed
import { ActivityService } from './activity.service'; // adjust as needed
// Define an empty DTO for cases with no payload
export class EmptyDto {}
@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: EmptyDto }, // Use the EmptyDto class
})
public async generateDemoActivity(@ConnectedSocket() socket: ClientSocket): Promise<Activity[]> {
return this.activityService.generateDemoActivity(socket.userId);
}
}
总结 通过使用 EmptyDto 类或类似的占位符,您可以避免将 null 作为有效负载类型传递,这应该可以解决您遇到的错误。这种方法可确保 @AsyncApiSub 装饰器正常工作,并且您的 API 文档可以正确列出 ActivityActions.GENERATE_DEMO_ACTIVITY 操作。