Auth module with deferred User module

2019-08-31 03:49发布

I have an AuthModule in a generalized library which requires interacting with a UserModule similar to the docs.

What I want to do is define an Interface that the UserService must adhere to instead of an actual implementation. This will leave the implementation details to the users of the library.

I've tried a few different approaches such as having a string token APP_USER_SERVICE initially null then be overridden by the implementor, but this seemed to run into trouble injecting a null value and not injecting the actual value.

Another approach I tried which I kind of like is doing AuthModule.withUserModule(UserModule) where AuthModule imports the dynamic UserModule and looks for the APP_USER_SERVICE token defined by the UserModule implementation. The problem with this approach is it seems to run into circular dependency hell.

I'm new to the NestJS project, so maybe I'm missing something obvious. Any pointers on how to organize this workflow is greatly appreciated, thanks.

In summary this is the structure I am going for:

-> = depends on

LIBRARY:

AuthModule -> IUserModule

USER:

AuthModule.withUserModule(UserModule) to fill in the IUserModule requirement.

UserModule -> AuthModule

Here is the code (broken atm):

Library
Sample Implementation

1条回答
Summer. ? 凉城
2楼-- · 2019-08-31 04:24

Working with forwardRef() should only be your last resort. Alternatively, consider splitting one of the modules into two or more parts to break the circular dependencies, e.g.:

AuthModule into

  • AuthLoginModule gets imported by the UserModule
  • ValidateAuthModule imports the UserModule

or respectively the UserModule into two parts.

查看更多
登录 后发表回答