Comments (4)
WOW, thank for the quick response. I will upgrade and let you know.
Thanks.
from msw.
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:
- Schema entry getter (handler) should be a function of
request
that returnsBoolean
(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
- 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(...)
}
}
}
- 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.
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.
@osmancode, hi! Please, did those changes help you achieve what you wanted?
from msw.
Related Issues (20)
- MSW fails to start Mock because worker update() fails. HOT 1
- Mocking with react-query and graphql-request stopped working after upgrade to the LTS version of msw HOT 1
- MSW fails to recognize a registered handler HOT 1
- MSW mock does not work in jest test HOT 1
- Request in worker.events.on('response:mocked', {request} is always null HOT 5
- TextEncoder is not defined HOT 2
- Use "assertType" from Vitest for type testing HOT 2
- Type error when `graphql.query` infers type arguments from `TypedDocumentNode` HOT 8
- difficuly using axios patch or post call with payload using mock service worker 2.0.11 HOT 2
- feat: support TypeScript 5.3.x for msw 1.3.x HOT 3
- React Native Msw And Axios Not Working Together HOT 4
- difficulty using axios patch or post call with payload using mock service worker HOT 2
- "response.url" is empty in "response:*" life-cycle events in the browser HOT 4
- Two different jest globals fixes documented in two places under https://mswjs.io/docs/
- Add "http" and "graphql" exports HOT 1
- server.use not used in RN HOT 2
- JSON response with special chars truncated since 2.2.0 HOT 3
- Cannot load the application if the browser doesn't have slash at the end of the URL
- MSW does not intercept requests when the node http(s) library is imported using * as HOT 4
- Mocked stream response with multiple enqueues hangs process HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from msw.