Giter Site home page Giter Site logo

Custom response transformer API about msw HOT 3 CLOSED

mswjs avatar mswjs commented on April 28, 2024
Custom response transformer API

from msw.

Comments (3)

kettanaito avatar kettanaito commented on April 28, 2024 1

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.

from msw.

kettanaito avatar kettanaito commented on April 28, 2024

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.

from msw.

kettanaito avatar kettanaito commented on April 28, 2024

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.

from msw.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.