使巢穴命令行 Swagger 插件适用于外部模型

huangapple go评论72阅读模式
英文:

Make nests cli swagger plugin work for external models

问题

我想尝试使用nestjs附带的swagger插件。

如果我定义了某个类,比如CreateUserDto,它可以正常工作,但我想使用来自第三方库的一些类型,我的情况是@adyen/api-library,它有类似NotificationNotificationResponse等类,我想将它们用作我的请求/响应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

huangapple
  • 本文由 发表于 2023年7月20日 20:38:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729972.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定