Skip to content
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

Update Codemods “msw-request-changes” #1

Merged
merged 1 commit into from
Nov 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 35 additions & 24 deletions cms/automations/msw-request-changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,68 +3,79 @@ created-on: 2023-11-16T14:11:04.212Z
f_long-description: >-
## Description


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.


* `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.


## Example



### Before

```ts


```typescript

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

```ts

import { setupWorker } from 'msw/browser';
import { http as caller, type HttpHandler } from 'msw';


```typescript

import { setupWorker } from 'msw/browser'; import { http as caller, type HttpHandler } from 'msw';

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' }));
}),
]

```



### Links for more info

- [msw v1 to v2 migration guide -> request changes](https://mswjs.io/docs/migrations/1.x-to-2.x/#request-changes)


* [msw v1 to v2 migration guide -> request changes](https://mswjs.io/docs/migrations/1.x-to-2.x/#request-changes)
f_github-link: https://github.com/intuita-inc/codemod-registry/tree/main/msw/2/request-changes
f_vs-code-link: vscode://intuita.intuita-vscode-extension/showCodemod?chd=wSWbFD-5ULW68QMp4rgGW9G5SNQ
f_cli-command: intuita msw/2/request-changes
f_framework: cms/framework/msw.md
f_applicability-criteria: "MSW version >= 1.0.0"
f_applicability-criteria: MSW version >= 1.0.0
f_verified-codemod: true
f_author: cms/authors/intuita.md
layout: "[automations].html"
Expand Down
Loading