-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
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
feat(core,common,testing): support overriding middleware for testing #12541
base: master
Are you sure you want to change the base?
feat(core,common,testing): support overriding middleware for testing #12541
Conversation
Pull Request Test Coverage Report for Build 1ff17a34-ae9b-45c7-afcc-8778ae899c7c
💛 - Coveralls |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's leave one line space, for the rest I would say it can be fine
Co-authored-by: Antonio Tripodi <[email protected]>
@kamilmysliwiec Any updates on this? |
* | ||
* @returns {MiddlewareConsumer} | ||
*/ | ||
replace( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm generally OK with this PR except for this change. Is there any chance we could get rid of this method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To remove that method, as far as I see, we need a way to swap the MiddlewareConsumer
instance with an implementation that can support middleware replacements. One could accomplish that through the following changes:
First, one enables the injection of MiddlewareModule
into a NestApplication
:
constructor(
container: NestContainer,
private readonly httpAdapter: HttpServer,
private readonly config: ApplicationConfig,
private readonly graphInspector: GraphInspector,
private readonly middlewareModule: MiddlewareModule = new MiddlewareModule(), // `middlewareModule` is now injected and not internally initialized
appOptions: NestApplicationOptions = {},
) {
...
In the MiddlewareModule
itself, we then allow the insertion of a custom MiddlewareBuilder
in loadConfiguration
like that:
public async loadConfiguration(
middlewareContainer: MiddlewareContainer,
moduleRef: Module,
moduleKey: string,
middlewareBuilder?: MiddlewareBuilder, // We now can specify the builder we want to use to configure the modules
) {
...
middlewareBuilder =
middlewareBuilder ??
new MiddlewareBuilder(
this.routesMapper,
this.httpAdapter,
this.routeInfoPathExtractor,
);
...
With that in place, we can then inject a TestMiddlewareModule
that inherits from MiddlewareModule
into our NestApplication
. Our custom TestMiddlewareModule
can then override loadConfiguration
which we call the loadConfiguration
from the parent MiddlewareModule
with a custom TestMiddlewareBuilder
. The TestMiddlewareBuilder
inherits from the MiddlewareBuilder
, which finally supports the replace
method we can use.
I am unsure of the consequences of the changes on central classes like NestApplication
, but in contrast to the replace
method in MiddlewareBuilder
, they make sense in the respective classes (I assume this is the reason why you want the method to be removed).
What do you (or others!) think about that?
closes: #4073
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
The
TestingModuleBuilder
in thetesting
package currently has no method available to override middleware à laoverrideProvider
oroverrideModule
.Issue Number: #4073
What is the new behavior?
Now, it is possible to override middleware as it is configured by the
configure
method of a module:As a by-product, the
MiddlewareBuilder
is extended by areplace
method that allows for swapping out middleware:Does this PR introduce a breaking change?
Other information