Skip to content
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

Feature Request: Support Passthrough from msw Endpoint to msw Endpoint #2003

Closed
1 task
TakashiAihara opened this issue Jan 29, 2024 · 2 comments
Closed
1 task
Labels

Comments

@TakashiAihara
Copy link

Scope

Improves an existing behavior

Compatibility

  • This is a breaking change

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

const handlers = [
  // override endpoint in other files
  http.get("/users", async ({ request }) => {
    if (request.headers.get("Accept") !== "text/csv") {
      return passthrough(); // 404 response
    }
    return new HttpResponse("some data", {
      status: 200,
      headers: {
        "Content-Type": "text/csv",
      },
    });
  }),

  // auto generated endpoint ( expected passthrough into this )
  http.get("/users", async () => {
    return new HttpResponse(JSON.stringify(getUsersMock()), {
      status: 200,
      headers: {
        "Content-Type": "application/json",
      },
    });
  }),
];

setupWorker(...handlers);
@kettanaito
Copy link
Member

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.
  return HttpResponse.json({ mock: true })
})

@kettanaito
Copy link
Member

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!

@github-actions github-actions bot locked and limited conversation to collaborators Oct 31, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants