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

feat: add "req.passthrough" #1204

Merged
merged 2 commits into from Apr 13, 2022
Merged

feat: add "req.passthrough" #1204

merged 2 commits into from Apr 13, 2022

Conversation

kettanaito
Copy link
Member

@kettanaito kettanaito commented Apr 13, 2022

This is a breaking change.

Changes

  • Adds req.passthrough to explicitly instruct MSW to perform an intercepted request as-is.
  • Returning undefined/null from handlers now treats the request as unhandled.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Apr 13, 2022

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 69c9e3e:

Sandbox Source
MSW React Configuration

export function passthrough(): MockedResponse<null> {
// Constructing a dummy "101 Continue" mocked response
// to keep the return type of the resolver consistent.
return {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm returning a MockedResponse from passthrough to keep the same return type of the response resolver. I don't like this approach but I'll use it for now.

We need to refactor the return type of the resolver to be some sort of Instruction, which could be:

  • Passthrough
  • Fallthrough
  • Use a mocked response
  • Return a network error
  • Anything else in the future

It should be a strictly defined object representing the library's next steps. Resolvers don't necessarily return mocked responses, as in the Passthrough/Fallthrough scenarios. Each intention can also be permanent or one-time (like res.once()). This is a great opportunity to solve #413.

The source for these instructions can also differ:

  • res() instructs to use a mocked response.
  • req.passthrough() instructs to passthrough this request.
  • res.networkError() (or a different API) instructs to return a network error.

Decoupling the source from the instruction will allow us to design semantic APIs without locking ourselves in a MockedResponse structure.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proposing this in #1207.

@kettanaito kettanaito added the BREAKING CHANGE Pull request introducing breaking changes. label Apr 13, 2022
@kentcdodds
Copy link
Contributor

Sweet! Where are the docs for this?

@kettanaito
Copy link
Member Author

@kentcdodds, I've added the documentation in mswjs/mswjs.io#192.

@abhishekarun
Copy link

When is this coming in formal release?

@kettanaito
Copy link
Member Author

kettanaito commented Apr 18, 2022

@abhishekarun, as soon as #1175 is addressed. If you have some spare time I'd certainly appreciate some help with that issue.

Also, keep in mind that this is not a new feature but rather an improvement over the existing bypass/passthrough behavior.

kettanaito added a commit that referenced this pull request Apr 18, 2022
kettanaito added a commit that referenced this pull request Apr 18, 2022
kettanaito added a commit that referenced this pull request May 17, 2022
@kettanaito
Copy link
Member Author

Released: v0.40.0 🎉

This has been released in v0.40.0!

Make sure to always update to the latest version (npm i msw@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.

@yshrsmz
Copy link

yshrsmz commented May 23, 2022

This PR seems to break the compatibility with @mswjs/http-middleware. I've created a PR to fix the issue mswjs/http-middleware#10

@taylorkline
Copy link

Returning undefined/null from handlers now treats the request as unhandled.

I would politely suggest @kettanaito that the release notes for this release mention this as a breaking change.

@kettanaito
Copy link
Member Author

@taylorkline, updated. Breaking changes between minor releases in pre-1.0 packages are implied but I agree this should've been communicated better.

@taylorkline
Copy link

@taylorkline, updated. Breaking changes between minor releases in pre-1.0 packages are implied but I agree this should've been communicated better.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BREAKING CHANGE Pull request introducing breaking changes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add "ctx.forward" for forwarding (bypassing) requests
5 participants