-
-
Notifications
You must be signed in to change notification settings - Fork 536
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
Provide request url matching #1
Comments
Something like url-matcher can likely handle the matching part. Simple after that. |
@bebraw Thing is, it is not enough to simply determine if a string matches a given mask. Here's the draft unit case of how this should work: const parsedRoute = parseRoute(
'http://foo.com/user/:username/:messageId',
'http://foo.com/user/bebraw/21'
)
expect(parsedRoute).toEqual({
params: {
username: 'bebraw',
messageId: '21'
}
}) So the |
Yeah, I see. It's a bit more complicated. |
@bebraw I have a stupid idea to gather the map of param names and their indexes, and then reduce the route string into the map of params. |
Worth a go. |
So, there is a small function that check if a route matches the mask, and gathers its params. It's limited, but works. function parseRoute(mask, route) {
let paramsList = []
const replacedMask = mask.replace(/:(\w+)/g, (a, b) => {
paramsList.push(b)
return '(\\w+)'
})
const match = new RegExp(replacedMask).exec(route)
const params = match && match.slice(1, match.length).reduce((acc, paramValue, index) => {
const paramName = paramsList[index]
return Object.assign(acc, {
[paramName]: paramValue
})
}, {})
return {
url: route,
matches: !!match,
params,
}
} Update: I've integrated this to the Service Worker right now and it's great so far. |
The current implementation looks sufficient at the moment, closing. |
What:
I propose to provide a smart request url matching.
https://github.com/kettanaito/msw/blob/a1196004c390d115d29941de405d9db571357107/serviceWorker.js#L38
Why:
To support placeholders, params, and other parts of url:
How
To match all the following:
We can also parse the params and expose them within the
req
reference forhandler(req)
to access in the mocking handler.The challenge here is that the logic happens inside a Service Worker, thus it should be done without requiring any package.
The text was updated successfully, but these errors were encountered: