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

Custom response transformer API #8

Closed
3 tasks done
kettanaito opened this issue Nov 21, 2018 · 3 comments
Closed
3 tasks done

Custom response transformer API #8

kettanaito opened this issue Nov 21, 2018 · 3 comments
Milestone

Comments

@kettanaito
Copy link
Member

kettanaito commented Nov 21, 2018

What:

I suggest to add a full support for custom response transformers.

Why:

With the functional composition it is easy to create custom response transformers.

  • The library must expose its default response transformers (text, json, delay, etc.) so the end developer may combine them to their liking.
  • The libarary must explain the call signature of response transformers, so custom functions can run as transformers.

How:

  • Expose default response transformers (context) from the library's module directly, so they can be reused to create custom transformers
  • Think how to make the writing of functional composition easier, especially on projects that do not use functional utils.
  • Think about the call signature of default transformers. It would be nice to have them curried, so they can be used outside of functional composition
@kettanaito kettanaito added the bug Something isn't working label Nov 21, 2018
@kettanaito kettanaito self-assigned this Nov 21, 2018
@kettanaito kettanaito removed the bug Something isn't working label Jan 19, 2019
@kettanaito kettanaito removed their assignment Jan 25, 2019
@kettanaito kettanaito changed the title Support custom response transformers Custom response transformer API Jun 27, 2019
@kettanaito kettanaito added this to the API milestone Jun 27, 2019
@kettanaito
Copy link
Member Author

Currently is possible to provide custom response transformers as long as they follow this call signature:

import { MockedResponse } from 'msw'

const myCustomTransformer = (res: MockedResponse) => res

Most likely you'd be introducing your own high-order functions that return response transformers. Here's an example:

import { MockedResponse } from 'msw'

const halJson = (body: Record<string, any>) => {
  return (res: MockedResponse) => {
    res.headers.set('Content-Type', 'application/hal+json')
    res.body = JSON.stringify(body)
    return res
  }
}

This is exactly how standard ctx utils are implemented at the moment. I think this should be put into documentation somewhere.

@kettanaito
Copy link
Member Author

I've added a recipe to the documentation on how to create your Custom context utility. It would be nice to add an integration test for such custom utility before closing this task.

@kettanaito
Copy link
Member Author

kettanaito commented Mar 24, 2020

Example of a custom response transformer is illustrated as a context utility function halJson() in this test suite:

https://github.com/open-draft/msw/blob/b2c10e623a099e64a7fe3b33aab3a00da4007f17/test/rest-api/custom-request-handler.mocks.ts#L44-L49

In the test above it's being used with a custom request handler and, thus, is provided in the defineContext property of the handler. If you wish to have an isolated context utility move it to a separate function and cover with typings according to those mentioned in the documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant