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 custom transformers #441

Merged
merged 3 commits into from
Nov 8, 2020
Merged

feat: add custom transformers #441

merged 3 commits into from
Nov 8, 2020

Conversation

marcosvega91
Copy link
Member

fix #440

This PR will add support for defaultTransformers. This could be useful if you want to apply your own body transformation as for the issue.

Now the function createResponseComposition is exported from the library. Users could create there own response function like

import fetch from 'node-fetch'
import { rest, createResponseComposition } from 'msw'
import { setupServer } from 'msw/node'
import * as JSONbig from 'json-bigint'

const customReponse = createResponseComposition({
  defaultTransformers: [
    (res) => {
      if (res.body && res.headers?.get('content-type')?.endsWith('json')) {
        res.body = JSONbig.stringify(res.body)
      }
      return res
    },
  ],
})

const server = setupServer(
  rest.get('http://test.mswjs.io/me', (req, res, ctx) => {
    const me = {
      username: 'Dude',
      balance: BigInt(1597928668063727616),
    }
    return customReponse(ctx.json(me))
  }),
)

src/response.ts Outdated Show resolved Hide resolved
src/response.ts Outdated Show resolved Hide resolved
Copy link
Member Author

@marcosvega91 marcosvega91 left a comment

Choose a reason for hiding this comment

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

I have added some comments

@JesusTheHun
Copy link

Works for me !

Copy link
Member

@kettanaito kettanaito left a comment

Choose a reason for hiding this comment

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

Great work on this, @marcosvega91!

I've flattened the createResponseComposition options so we don't have to deal with default + custom options merging when given partial options object. The library now publicly exposes certain response-related TypeScript definitions so it's possible to use our internal functions and have type safety.

Looks great, I'm looking forward for the CI to pass.

@kettanaito kettanaito merged commit 8b90f10 into mswjs:master Nov 8, 2020
@timdeschryver
Copy link
Member

timdeschryver commented Nov 11, 2020

I think it would be great to add this as a recipe to the docs.
Since you implemented this feature @marcosvega91 , are you planning to add this to the recipes?
If not, I can add it (if we want to add this to the docs) 🙂

@marcosvega91
Copy link
Member Author

I'm really bad writing things. Also my English is not very good, so feel free to open a PR with your changes 😉. Thanks @timdeschryver :)

@marcosvega91 marcosvega91 deleted the pr/add_default_transformers branch November 11, 2020 20:39
@kettanaito
Copy link
Member

@marcosvega91, thank you once more for adding these changes, Marco :)
Please, know that I'm always happy to proof-read and help with the writing. You're not alone in this.

@marcosvega91
Copy link
Member Author

It's always a pleasure 🙂

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

Successfully merging this pull request may close these issues.

Support alternative JSON library
4 participants