英文:
Make nests cli swagger plugin work for external models
问题
我想尝试使用nestjs附带的swagger插件。
如果我定义了某个类,比如CreateUserDto
,它可以正常工作,但我想使用来自第三方库的一些类型,我的情况是@adyen/api-library
,它有类似Notification
、NotificationResponse
等类,我想将它们用作我的请求/响应DTO。这个插件是否支持这样做?
我尝试过像这样继承:
export class AdyenNotificationDto extends Notification {}
然后将它放在adyen-notification.request.dto.ts
中,但没有帮助。
如果不可能,这是否意味着我必须复制?也就是说,我必须创建一个包含来自Notification
类的所有属性的DTO,然后从那时起保持它们同步?
英文:
So I would like to try putting to use this swagger plugin that comes with nestjs.
It works if I define some class, let's say CreateUserDto
but I would like to use some types from a third party library, in my case it's @adyen/api-library
and it has classes like Notification
, NotificationResponse
etc, I'd like to use them as my request/response dtos. Is it possible with this plugin?
I tried inheriting like:
export class AdyenNotificationDto extends Notification {}
and putting that in adyen-notification.request.dto.ts
But it didn't help.
And in case it's not possible, does it mean that I'd have to duplicate? That's, I would have to create a dto with all the props from, say, the Notification
class and then keep them in sync from then onwards?
答案1
得分: 1
你需要在使用Swagger插件时,使用nest build
命令来编译外部包。原因是在构建过程中,Nest会读取类的AST并在tsc
进程启动之前添加适当的装饰器。当你只构建已经构建好的包的应用程序时,没有AST可供读取和装饰,所以你不能为已编译的包添加装饰器。
英文:
You would need to compile the external package with the nest build
command while using the swagger plugin. The reason being is that during the build, Nest reads the AST of the classes and adds the appropriate decorators before the tsc
process gets kicked off. When you are just building the application that makes use of the already built package, there's no AST to read and decorate, so you don't get the ability to decorator an already compiled package
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论