Ugo Evola
Ugo Evola

Reputation: 647

Npm Library with dependencies varying from one project to another

I work in typescript with npm dependencies

Theory:

Let's say a library A And two projects B and C

My library A uses dependencies needed in B but not in C

How to install this dependency in project B but not in C without having a compilation error ?

project B
 |----library A
 |----library D

project C
 |----library A

library A
 |----library D

Practical:

As I am probably on the wrong track, here is the concrete case! I am on a project with an Angular front-end and a NestJs back-end. I have created a common library in which there are DTOs. I want to use the @ApiProperty() annotation from @nestjs/swagger. But I don't want to install all @nestjs/swagger dependencies on my Angular front-end.

In this case, I saw that the Swagger pluggin could help me: https://docs.nestjs.com/openapi/cli-plugin
But after testing, it doesn't work on node_modules files

api
 |----dto library
 |----@nestjs/swagger

front
 |----dto library

dto library
 |----@nestjs/swagger (for annotation)

Upvotes: 0

Views: 126

Answers (1)

funkizer
funkizer

Reputation: 4897

There is no way to get that working without some custom build script that reads files in your dto lib, removes all @nestjs/swagger imports and decorators, and writes interfaces into another library folder, from which you can import them in your front app. Or somehow mock Swagger in the front app so that the decorators would be no-op functions, not imported from the actual Swagger lib.

Simpler way:

Let your dto lib only have pure interfaces. Don't import Swagger or anything there. Just export interfaces.

Then you can import the interfaces in your frontapp, and in your api app you implement the interfaces into classes and add all the swagger decorators. And why not toss class-validator in there too, it's awesome with Nest's ValidationPipe.

Upvotes: 1

Related Questions