Skip to content

Question: Should we use msw to mock server-side apis based on the request body? #2148

Answered by kettanaito
ellsworthjc asked this question in Q&A
Discussion options

You must be logged in to vote

Hi, @ellsworthjc. Thank you for your kinds words!

You can choose whichever setup makes sense in the context of your request client, so what you've showcased makes sense if the request body contains the predicate as to how to handle the outgoing requests.

That being said, I feel you can take advantage of MSW's built-in routing if you unfold the requests. Let me show you how.

export const handler = [
  // This is a generic handler that captures the
  // immediate requests your app makes and "unfolds" them.
  http.post('/api', async ({ request }) => {
    const requestJson = await request.json()
    const url = new URL('https://placeholder.com/' + requestJson.path)
    const proxiedRequest = n…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@ellsworthjc
Comment options

Answer selected by kettanaito
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #2147 on May 11, 2024 17:30.