Use case
Framework
Owner
Msw/2/Upgrade Recipe
This recipe is a set of codemods that will upgrade your project from using msw v1 to v2.
Codemod verified
Regularly tested and maintained by our engineers and codemod expert community
by@alexbit-codemodMsw/2/Type Args
There is a change to generic type interface of
rest.method()
calls. This codemod puts the generic arguments in the correct order to keep type safety.Codemod verified
Regularly tested and maintained by our engineers and codemod expert community
by@codemodMsw/2/Response Usages
To send a response from MSW handler, one would previously use something like
res(ctx.text("Hello world"))
. In msw v2, this is achieved by returning a native WebAPI Response object. msw v2 conveniently exposes aHttpResponse
function that has useful methods for creating just that object with a desired body. This codemod replaces the oldres
calls with the newHttpResponse
function calls and a bunch of ctx utilities that usually go with it. See examples below.Codemod verified
Regularly tested and maintained by our engineers and codemod expert community
by@codemodMsw/2/Request Changes
Following the original msw upgrade guide, the signature of the request handler have changed. Some of the parameters have changed their type, some widely used objects are available directly on the callback argument object for convenience. Following changes are applied by this codemod:
Codemod verified
Regularly tested and maintained by our engineers and codemod expert community
by@codemodMsw/2/Req Passthrough
A new way of calling a passthrough is available in msw v2. This codemod replaces
req.passthrough()
calls with the new way of doing that using exported function.Codemod verified
Regularly tested and maintained by our engineers and codemod expert community
by@codemodMsw/2/Print Handler
A new way of listing all handlers is preferred in msw v2. This codemod replaces
printHandlers()
calls with the new way of doing that.Codemod verified
Regularly tested and maintained by our engineers and codemod expert community
by@codemodMsw/2/Lifecycle Events Signature
In msw v2, lifecycle events callback methods have changed their signature. This codemod replaces usages if its arguments with the new ones.
Codemod verified
Regularly tested and maintained by our engineers and codemod expert community
by@codemodMsw/2/Imports
Following the original msw upgrade guide, there are certain imports that changed their location and/or naming. This codemod will adjust your imports to the new location and naming.
Codemod verified
Regularly tested and maintained by our engineers and codemod expert community
by@codemodMsw/2/Ctx Fetch
ctx.fetch(req)
is now meant to be called asfetch(bypass(req))
wherebypass
is a new function available in themsw
library. Changes applied by this codemod:Codemod verified
Regularly tested and maintained by our engineers and codemod expert community
by@codemodMsw/2/Callback Signature
Following the original msw upgrade guide, the signature of the request handler have changed. This codemod hard replaces the callback signature to the new one and cleans up unused variables.
Codemod verified
Regularly tested and maintained by our engineers and codemod expert community
by@codemod