Intercept requests with query parameters #1315
-
Hello, I was considering using Best, |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 8 replies
-
Hey, @AndrianStoikov. That's a great question. Since you're writing a server-side behavior with MSW, we disregard request query parameters during request matching. We do so because query parameters are additional information to the request but not request identifier like the request URL/path. You can still access the query parameters so they are not ignored in that manner. Due to the constrains of your setup, I don't think it's viable to perform conditional checks for the query parameters in all handlers every time. I would recommend creating a custom request handler in your case where you'd abstract that check. Custom request handlerfunction toResource(queryParams, resolver) {
return [
// Here I'm assuming that all requests happen to the same static
// endpoint, and you want to base your request matching off the query params.
staticEndpoint,
(req, res, ctx) => {
const matches = Object.entries(queryParams).every(([name, value]) => {
return req.url.searchParams.get(name) === value
})
if (matches) {
return resolver(req, res, ctx)
}
}
]
} export const handlers = [
rest.post(...toResource({ resource: 'customer' }, (req, res, ctx) => {
return res(ctx.json({ id: 123 }))
}))
] You can design whichever query parameter matching logic you want. I went with an object matching above, you can do a plain string if it's only the Would this abstraction work for your case? |
Beta Was this translation helpful? Give feedback.
-
thanks a lot @kettanaito & @AndrianStoikov, your approach help me a lot to build this fetch interceptor handler constructor. const fetchInterceptionHandler=(targetUrl, responseStatus, method, response) =>
rest[method](targetUrl, (interceptedRequest, fakeResponse, ctx) => {
if (
targetUrl === interceptedRequest.url.href
) {
return fakeResponse(
ctx.status(responseStatus),
ctx.json(response)
);
}
}); |
Beta Was this translation helpful? Give feedback.
-
Thanks for posting this @kettanaito. Do you think it's possible create a similar abstraction that accesses the request body instead of query params? One thing I'm stuck on is dealing w/ the async nature of accessing ex.
|
Beta Was this translation helpful? Give feedback.
-
FWIW, here's my scenario, and only returning undefined if it doesn 't match seems to be working for me:
get util:
usage:
maybe it is better to have 1 resolver in this scenario vs 2? |
Beta Was this translation helpful? Give feedback.
Hey, @AndrianStoikov. That's a great question.
Since you're writing a server-side behavior with MSW, we disregard request query parameters during request matching. We do so because query parameters are additional information to the request but not request identifier like the request URL/path. You can still access the query parameters so they are not ignored in that manner.
Due to the constrains of your setup, I don't think it's viable to perform conditional checks for the query parameters in all handlers every time. I would recommend creating a custom request handler in your case where you'd abstract that check.
Custom request handler