通过微服务(通过 npm)共享 API TS 类型的正确方法是什么?

Rom*_*iuk 5 npm typescript webpack microservices nestjs

我在实施微服务网络时遇到问题。接下来的情况是:我刚刚完成了第一个微服务(顺其自然micro-A),现在我开始开发另一个微服务(micro-B)。我意识到我需要共享 DTO 和一些接口才能micro-Amicro-B. 因为我需要它们之间的类型安全通信。我查了一半的互联网,没有找到这种情况的答案。人们有两种方法可以做到这一点 - 使用 monorepo 和 npm 包。我想使用 npm 包。因此,此时,我决定api-interfaces在我的微服务中创建一个单独的文件夹(让我们命名为 ),我将在其中使用以下代码创建 index.ts 文件:

export * from './src/users/dto/profile.res.dto';
export * from './src/users/dto/user.res.dto';
Run Code Online (Sandbox Code Playgroud)

然后创建一个 webpack 配置,它将复制api-interfaces/index.ts to的所有依赖项api-interfaces/lib/*,然后将其发布到 npm。看起来不错,但后来我遇到了下一个错误:

Module not found: Error: Can't resolve '@nestjs/swagger' in '/micro-user/src/profiles/dto'
 @ ../profiles/dto/profile.res.dto.ts 14:18-44
 @ ../users/dto/user.res.dto.ts
 @ ./index.ts

ERROR in ../profiles/dto/profile.res.dto.ts
Module not found: Error: Can't resolve 'class-transformer' in '/micro-user/src/profiles/dto'
 @ ../profiles/dto/profile.res.dto.ts 13:28-56
 @ ../users/dto/user.res.dto.ts
 @ ./index.ts
Run Code Online (Sandbox Code Playgroud)

另外,我通过类转换器使用序列化。所以此时此刻,我陷入了困境......我有两个问题: - 你知道如何在微服务之间共享 API DTO 和接口吗?- 你能帮我构建 webpack 吗?