You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We have a http.get endpoint that is auto-generated.
I would like to override the endpoints here, and apply conditional branching based on headers to use different Responses.
Currently, I believe that using passthrough will do a fetch for the global endpoint regardless of the msw endpoint setting.
Instead, I would like to be able to pass it to another endpoint of msw.
If this is currently possible, I would appreciate it if you could let me know.
Sample code
consthandlers=[// override endpoint in other fileshttp.get("/users",async({ request })=>{if(request.headers.get("Accept")!=="text/csv"){returnpassthrough();// 404 response}returnnewHttpResponse("some data",{status: 200,headers: {"Content-Type": "text/csv",},});}),// auto generated endpoint ( expected passthrough into this )http.get("/users",async()=>{returnnewHttpResponse(JSON.stringify(getUsersMock()),{status: 200,headers: {"Content-Type": "application/json",},});}),];setupWorker(...handlers);
The text was updated successfully, but these errors were encountered:
Hi, @TakashiAihara. I believe what you want to achieve is already supported. If you return undefined from a response resolver, it will skip that handler and MSW will continue with the other matching request handlers. You can return at any point in the resolver to trigger this mechanism.
Here's an example:
http.get('/resource',({ request })=>{if(request.headers.get('Accept')==='text/csv'){// Exit this handler but don't return how to// resolve this request. This will tell MSW to// keep looking for other matching handlers.return}}),http.get('/resource',({ request })=>{// Handling a "Accept: text/csv" requests at this point.returnHttpResponse.json({mock: true})})
It looks like I've missed to mention this behavior in the docs. This behavior is called "fallthrough", and you can now read about it in the Fallthrough the request section of the response resolver page. Thanks for bringing this to my attention!
Scope
Improves an existing behavior
Compatibility
Feature description
Background
We have a http.get endpoint that is auto-generated.
I would like to override the endpoints here, and apply conditional branching based on headers to use different Responses.
Currently, I believe that using passthrough will do a fetch for the global endpoint regardless of the msw endpoint setting.
Instead, I would like to be able to pass it to another endpoint of msw.
If this is currently possible, I would appreciate it if you could let me know.
Sample code
The text was updated successfully, but these errors were encountered: