-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dependency injection compatibility or how to share code between client/backend #4487
Comments
Nest is an independent framework that can be used with any front-end technology. There's no reason to stay coherent with Angular implementation.
See above.
You can also use symbols and classes as tokens, see the documentation. We don't plan to introduce another way.
There's no reason to make them compatible. Tbh I don't think that it's even feasible anymore with Ivy.
Nope
Please, report this issue in the Nx repo. |
Don't you think that this should be at least discussed ? IMHO having the ability to share dependencies between front-end and back-end development is an important subject also is allowing tools to do so. I don't see what Ivy has to do with the dependency injection system it's just simpler then before Ivy. About the token story, I can't import an Interface and therefore can't use this as a value in
|
You can share interfaces, DTOs, TS classes, everything that simply doesn't require NestJS-related (or Angular) decorators. |
Although I think the provided example of @soyuka displays not the full potential value for DI compatibility, it should at least be properly discussed! A big benefit of a Nodejs-Backend like Nestjs in general is the isomorphism of the programming language between server and client. This enables reusability of data and functions across both network nodes, thus saving development time and preventing programming errors. To further embrace this, there should be a DI implementation which can be used in both ends of an application. At the moment injectable classes of nestjs cannot be reused in the client, thus disallowing the sharing of services between client and server. This behavior prevents the easy usage of DI for node-independent systems like event-sourcing or offline-ability. To get it working, you have to make a workaround by creating a DI-class for every source class in a separate file, which inherits the source class. (Although this approach comes with its own problems) Angular DI uses (basically) injection-js, which is usable in the client as well as in the server. Sadly Nestjs DI is not based on injection-js, so the compatibility is missing. As Nestjs DI-API is nearly identical to injection-js, it should not be a huge effort to exchange or adjust it. |
If you want to use classes annotated with NestJS-specific decorators you can always instruct webpack (or any other bundler you use) to swap NestJS imports with shim files (see example shim file here https://github.com/nestjs/swagger/blob/master/lib/extra/swagger-shim.ts). This should be pretty simple to accomplish. Please, use our Discord channel (support) for such questions. We are using GitHub to track bugs, feature requests, and potential improvements. |
Feature Request
Explanation details
Let's say we have a full-stack application, using Angular and Nest. Side note, the repository is setup with https://nx.dev. Now, the project needs a Logger, you set up a new library
ng g @nrwl/workspace:lib logger
and start working on sending your logs to Loggly via http:Logger implementation
Now comes the interesting part, first the HttpImplementation in angular you'd have
HttpClient
from@angular/common/http
, in NestHttpService
from@nest/common
. I had to type something like:But there's probably something better to do here.
About injection I first thought naively that both
Injectable
implementations would work together and added:This fails building with errors like:
Let's see what we can do with the
LogglyConfiguration
interface. Note that apparently we can't import interfaces from other packages as nest logs:Also, when using Angular you'd typically use a token to inject the configuration for example:
Where
LOGGLY_CONFIG
is a token (useful for typings):In Nest, the recommandation is to use a string, but this looses the advantages of strict typing with the token.
Today I have to use two different implementations in the Nest project and in the Angular project see this gist.
Solution
Ideally both injection annotation (Angular's and Nest's) should be compatible and declaring a shared service could be easier then this. Do you think that this would be possible or are they limitation that would interfere that I'm not aware of? Is your injection system based on injection-js (didn't took the time to check Nest's code).
Also, WDYT about adding an InjectionToken to Nest's injection system ?
The text was updated successfully, but these errors were encountered: