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

Re-designs the RequestHandler API #561

Merged
merged 6 commits into from
Feb 13, 2021
Merged

Conversation

kettanaito
Copy link
Member

@kettanaito kettanaito commented Jan 22, 2021

This pull request introduces a BREAKING CHANGE.
This request does not introduce any direct public-facing changes. The exception is custom request handlers authors.

GitHub

Motivation

The Request Handler API can be reviewed and improved based on the usage scenarios and request handling workflow that we've learned.

  • Revisit and standardize the lifecycle of a request handler.
  • Return a clear execution result upon calling handler.run() that includes mocked response, as well as additional request information (i.e. parsing results).
  • Simplify the creation of custom request handlers by extending a base RequestHandler class, or any standard request handler exported by the library (i.e. RestHandler or GraphQLHandler).

Example

// Example of a REST API request handler that extends a base `RequestHandler` class.
const handler = new RestHandler('GET', '/user', resolver)

// Test if a given request satisfies the handler.
handler.test(req)

// Resolve the request: parses, tests, and produces a mocked response.
handler.run(req)

Roadmap

  • Introduce a new Request Handler API.
  • Migrate rest request handlers.
  • Migrate graphql request handlers.
  • Clean up.
  • Resolve all @fixme comments.
  • Ensure that test/msw-api/setup-worker/start/on-unhandled-request/suggestions.test.ts doesn't error and can perform requests that intentionally fail without polluting the stdout.

@kettanaito kettanaito force-pushed the refactor/request-handlers branch from b24f764 to 550d5d2 Compare January 24, 2021 08:58
@codesandbox-ci
Copy link

codesandbox-ci bot commented Jan 24, 2021

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 b96fd21:

Sandbox Source
MSW React Configuration

@kettanaito kettanaito added the BREAKING CHANGE Pull request introducing breaking changes. label Jan 24, 2021
@kettanaito kettanaito force-pushed the refactor/request-handlers branch from 550d5d2 to a3d4474 Compare January 26, 2021 14:42
@kettanaito kettanaito force-pushed the refactor/request-handlers branch from a3d4474 to 663d96c Compare January 27, 2021 08:55
@kettanaito
Copy link
Member Author

REST API request handlers have been successfully migrated to the new Request Handler API.

@kettanaito kettanaito force-pushed the refactor/request-handlers branch 5 times, most recently from 5ecda68 to c8348d3 Compare January 28, 2021 08:17
@kettanaito kettanaito force-pushed the refactor/request-handlers branch from c8348d3 to 932b210 Compare February 12, 2021 10:29
@kettanaito kettanaito force-pushed the refactor/request-handlers branch from 81ed9fa to 245e97d Compare February 12, 2021 17:09
@kettanaito
Copy link
Member Author

GraphQL API request handlers have been successfully migrated to the new Request Handler API.

@kettanaito kettanaito marked this pull request as ready for review February 13, 2021 09:50
@kettanaito kettanaito force-pushed the refactor/request-handlers branch from b9c35eb to dbd5bef Compare February 13, 2021 14:21
@kettanaito
Copy link
Member Author

@timdeschryver I'm addressing the types validation in dbd5bef with the test:ts command. It's going to validate the usage scenarios in terms of type compliance against the built library. I've checked and it also detects type violations in d.ts and glossary modules (something we tried to address in #579).

@kettanaito kettanaito force-pushed the refactor/request-handlers branch from dbd5bef to b96fd21 Compare February 13, 2021 16:45
@kettanaito kettanaito merged commit 4a5285e into master Feb 13, 2021
@kettanaito kettanaito deleted the refactor/request-handlers branch February 13, 2021 16:57
@cdomigan
Copy link

cdomigan commented Apr 6, 2021

It looks like the docs are now out of date on creating Custom Handlers? https://mswjs.io/docs/recipes/custom-request-handler

@kettanaito
Copy link
Member Author

kettanaito commented Apr 6, 2021

@cdomigan, looks like you're right. That recipe needs updating to be in sync with these changes.

Please, would you be interested in adjusting that recipe? Its source is here.

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. release candidate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Non-GraphQL requests with 'query' in URL params fail with AbortError
2 participants