Msw/2/Request Changes

1.0.1Last update Jul 24, 2024
by@Codemod
Msw
migration

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:

  • req.url is now obtained as new URL(request.url), request being a new object available for destructure from the single callback argument
  • req.params are now exposed in the same callback argument
  • req.cookies are now exposed in the same callback argument
  • req.body is now removed instead of being deprecated. New response object now has a .json() method that should be the preferred way.

This codemod does not update the mentioned signatures of callback methods due to the fact that there are more changes in other codemods included in the upgrade-recipe that rely on the old signature. To apply the changes, you will have to run the recipe or run a callback-signature codemod that will do only that and replace all the references of old signature arguments.

Before

import { rest } from 'msw';
rest.get('/user', (req, res, ctx) => {
const search = req.url.searchParams;
const { cookies, body: reqBody, thing } = req;
const { params } = req;
const userCookies = req.cookies.user;
const requestParams = req.params.thing;
return res(ctx.json({ firstName: 'John' }));
});

After

import { http as caller, type HttpHandler } from 'msw';
import { setupWorker } from 'msw/browser';
const handlers: HttpHandler[] = [
caller.get('/user', (req, res, ctx) => {
const url = new URL(request.url);
const search = url.searchParams;
const { thing } = req;
const userCookies = cookies.user;
const requestParams = params.thing;
return res(ctx.json({ firstName: 'John' }));
}),
];

Build custom codemods

Use AI-powered codemod studio and automate undifferentiated tasks for yourself, colleagues or the community

background illustrationGet Started Now