mswjs / msw

Industry standard API mocking for JavaScript.
https://mswjs.io
MIT License
15.97k stars 519 forks source link

feat!: use `URLPattern` for request matching #2209

Open kettanaito opened 4 months ago

kettanaito commented 4 months ago

[!WARNING] As much as I hate this, this is a breaking change. Some of your existing handlers may start matching differently if they contain one of the dropped features (path params merging or trailing slashes). Even if this is a change for the better, this should land in v3.0.

This also significantly improves the test coverage for path matching in MSW.

This change does not intend to support URLPattern as the input. For now, we will use it as internal mechanism (thus a polyfill version). Once the API gains global browser support, we may consider shipping an input support.

Changes to document

Roadmap

kettanaito commented 4 months ago

This failing test:

 FAIL  src/core/utils/matching/matchRequestUrl.test.ts > merges multiple same-named groups into an array of values
TypeError: Failed to construct 'URLPattern': invalid pathname pattern '/user/:segment/bar/:segment'.
 ❯ new me node_modules/.pnpm/urlpattern-polyfill@9.0.0/node_modules/urlpattern-polyfill/dist/urlpattern.js:1:14389
 ❯ Module.matchRequestUrl src/core/utils/matching/matchRequestUrl.ts:96:19

Is caused by https://github.com/whatwg/urlpattern/issues/226.