Giter Site home page Giter Site logo

Comments (4)

ingmothman avatar ingmothman commented on April 27, 2024 1

WOW, thank for the quick response. I will upgrade and let you know.
Thanks.

from msw.

kettanaito avatar kettanaito commented on April 27, 2024

Hello, @osmancode. That sounds like a great suggestion.

Technically, it should be possible to achieve this at the moment by using a wildcard ('*') to match all resources, and providing a header presence check:

import { composeMocks, rest } from 'msw'

const { start } = composeMocks(
  rest.get('*', (req, res, { json }) => {
    if (req.headers['X-Mock-Response']) {
      // In case the header is present, mock the response
      return res(json({ mocked: true }))
    }

    // Perform the original request otherwise
    return fetch(req)
  })
)

start()

I am not fully sure about the fetch() part, as the recommended way to get the original response. However, that's how it's achieved in the Mock Service Worker.

Implementation details

I think it would be suitable to adjust the call signature of the handler functions passed to composeMocks to be more generic, while those handlers grouped under the rest namespace would be a high-order functions with certain predicates built in (matching a fixed method and URL path). Referring to this interface:

https://github.com/open-draft/msw/blob/3bb93d7d53c7d8334f2b432a0ddf8384d2648af3/src/handlers/createHandler.ts#L32

  1. Schema entry getter (handler) should be a function of request that returns Boolean (actual request match verdict) and executes a resolver function when the request matches. Generally, I see no reason for the schema entry to assume any kind of matching, which would allow to provide a custom handler exactly how you require.
type SchemaEntryGetter = (req: Request, handler: ResponseHandler) => Boolean
  1. Standard rest namespace method then could be implemented as abstractions over the aforementioned generic schema entry getter function. Here's a basic schematics:
// rest.ts
export const get = (mask: Mask, handler: ResponseHandler): SchemaEntryGetter => {
  return (req) => {
    // each of the "rest[abc]" methods can be generated, as they follow the same
    // request method matching wrapper on top of a resolver execution.
    if (req.method === 'GET' && internalMatchMask(mask, req.url)) {
      resolver(...)
    }
  }
}
  1. The usage of custom handlers would look roughly as follows:
import { SchemaEntryGetter, ResponseHandler, composeMocks } from 'msw'

const handleRequestGroup = (handler: ResponseHandler): SchemaEntryGetter => {
  return (req, res, context) => {
    if (req.headers['X-Mock-Response']) {
      handler(req, res, context)
    }
  }
}

const { start } = composeMocks(
  handleRequestGroup((req, res, { json }) => {
    res(json({ mocked: true })
  })
)

start()

from msw.

kettanaito avatar kettanaito commented on April 27, 2024

I've added the support for this. Please see how to declare custom request handler.

Please update to [email protected] to have this support. Let me know if this solves the issue for you.

from msw.

kettanaito avatar kettanaito commented on April 27, 2024

@osmancode, hi! Please, did those changes help you achieve what you wanted?

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.