Commit Graph

5 Commits

Author SHA1 Message Date
Sebastian "Sebbie" Silbermann 50847afec4 Stop using deprecated downlevelIteration (#92652) 2026-04-14 19:34:44 +02:00
Sebastian "Sebbie" Silbermann d141874063 Bump TypeScript to 6.0 (#91257) 2026-04-14 12:22:38 +02:00
Jiwon Choi 4ceb050061 refactor(next-codemod): migrate to commander and prompts (#70409)
### Why?

Prerequisite of `@next/codemod upgrade`.

Current status of next-codemod is difficult to extend another commands
as it relies on [meow](https://www.npmjs.com/package/meow) which
requires to handle args and flags manually. For consistency with `next`
and `create-next-app`, migrated to `commander` and `prompts`.


https://github.com/user-attachments/assets/8d2e53be-4b7d-4755-9a3f-c73df1f9ef7e

No behavioral changes **except the help message**.
2024-09-24 16:23:29 -07:00
Jiwon Choi 0090faa253 feat(next): codemod for removed geo and ip of NextRequest (#70064)
> [!NOTE]
> This codemod requires to install the `@vercel/functions` package.

### Why?

Codemod support for breaking change at
https://github.com/vercel/next.js/pull/68379. It removed the `ip`, and
`geo` properties from the `NextRequest` type. The `NextRequest` type was
mostly used in the `middleware` and `route`.



### How?

The codemod replaces the `ip`, and `geo` properties from the
`NextRequest` with corresponding `@vercel/functions` features.

#### Accessing NextRequest Value

When a file uses `NextRequest`, we look if there's an access to the type
with `geo` or `ip`. This targets destructuring and direct access.

```ts
export function GET (req: NextRequest) {
  const { geo, ip: ipAlias, pathname } = req // destructuring
  // direct access
  req.geo
  req.ip
}
```

Declares a variable based on the identifier within the object. If it was
aliased, we use the aliased name.

```ts
import { geolocation, ipAddress } from '@vercel/functions'

export function GET (req: NextRequest) {
  const { pathname } = req // destructuring
  const geo = geolocation(req)
  const ipAlias = ipAddress(req)
  // direct access
  geolocation(req)
  ipAddress(req)
}
```

#### Accessing NextRequest Types

When the file has accessed the `geo | ip` type from `NextRequest`, we
replace with the corresponding types.

```ts
import type { NextRequest } from 'next/server'

export function GET (req: NextRequest) {
  const ip = '...' as NextRequest['ip']
  const geo = { ... } as NextRequest['geo']
}
```

Since the type for `ip` is `string | undefined`, we explicitly replace
it. For `geo`, we replace with the `Geo` type.

```ts
import type { NextRequest } from 'next/server'
import type { Geo } from '@vercel/functions'

export function GET (req: NextRequest) {
  const ip = '...' as string | undefined
  const geo = { ... } as Geo
}
```

### Ref

Behavior FYI:
https://github.com/vercel/next.js/pull/70064#discussion_r1768718840,
https://github.com/vercel/next.js/pull/70064#discussion_r1768722282
Closes NDX-291

---------

Co-authored-by: Sebastian Silbermann <silbermann.sebastian@gmail.com>
Co-authored-by: Donny/강동윤 <kdy1997.dev@gmail.com>
Co-authored-by: Jiachi Liu <inbox@huozhi.im>
2024-09-24 01:42:16 +09:00
Tim Neutkens 62031ff24f Move next-codemod to Next.js monorepo (#15536) 2020-08-10 11:14:53 +02:00