-
-
Notifications
You must be signed in to change notification settings - Fork 532
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
Do not exhaust one-time handlers with custom predicate in resolver #2399
Comments
One of my temporary solutions: /**
* reset isUsed by myself
*/
function resetIsUsed(fn: MSWHttpResponseResolver, handler?: HttpHandler): MSWHttpResponseResolver {
return async (args) => {
const res = await fn(args)
if (handler) {
if (a) // custom logic
handler.isUsed = false
}
return res
}
} |
Hi, @YunYouJun. That's a good point, thanks for bringing it up. This is related to #1804 I raised some time ago. Response resolver function is, technically, not the best place for custom predicates because the resolver phase happens after the predicate phase internally. I wouldn't go into shipping that new API to address your use case though. I think a far more incremental approach would be this:
For resolvers that trigger, do their predicate, but return nothing (don't handle the request), keep that resolver as non-exhausted (new behavior). For those that handle the request, exhaust them (current behavior). What do you think? |
Currently, we mark the handler as used immediately if the internal predicate phase has matched: msw/src/core/handlers/RequestHandler.ts Line 280 in e3234fd
But that won't work for handlers where the user defines a custom predicate within the response resolver, as you've pointed out. Instead, we could move this update after the execution result is known: msw/src/core/handlers/RequestHandler.ts Line 309 in e3234fd
And do something like: if (executionResult != null) {
this.isUsed = true
} If you wish to give this a try, please start with a test case! |
I think it looks good. But is there a case where the user's internal resolver returns So I thought that allowing the user to return a specific function or Symbol would satisfy more situations without breaking past behavior. |
Scope
Improves an existing behavior
Compatibility
Feature description
Background
As stated on https://mswjs.io/docs/best-practices/custom-request-predicate, we can implement predicate filtering by wrapping the resolver.
However, if we want to use
options.once
at this point, it will result in theonce
being consumed even if the predicate is not passed.This makes it hard for our implemented predicate to support both
once with predicate
simultaneously.restoreHandlers()
can restore once.But it will also reset all the handlers.
Solution
So I think
Custom request predicate function
is very important. It gives us more control.I would like to know when we can expect to have this feature available, or if there are any alternative solutions to address the current issue?
The text was updated successfully, but these errors were encountered: