-
Notifications
You must be signed in to change notification settings - Fork 27k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
> [!NOTE] > This codemod requires to install the `@vercel/functions` package. ### Why? Codemod support for breaking change at #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: #70064 (comment), #70064 (comment) Closes NDX-291 --------- Co-authored-by: Sebastian Silbermann <[email protected]> Co-authored-by: Donny/강동윤 <[email protected]> Co-authored-by: Jiachi Liu <[email protected]>
- Loading branch information
1 parent
60a8a0b
commit 0090faa
Showing
39 changed files
with
945 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...s/next-codemod/transforms/__testfixtures__/next-request-geo-ip/destructure-alias.input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// @ts-nocheck | ||
import { type NextRequest, NextResponse } from "next/server"; | ||
|
||
export function GET(request: NextRequest) { | ||
const { buildId, geo: geoAlias, ip: ipAlias } = request; | ||
return NextResponse.json({ buildId, geo: geoAlias, ip: ipAlias }); | ||
} |
13 changes: 13 additions & 0 deletions
13
.../next-codemod/transforms/__testfixtures__/next-request-geo-ip/destructure-alias.output.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// @ts-nocheck | ||
import { type NextRequest, NextResponse } from "next/server"; | ||
|
||
import { geolocation, ipAddress } from "@vercel/functions"; | ||
|
||
export function GET(request: NextRequest) { | ||
const { | ||
buildId | ||
} = request; | ||
const ipAlias = ipAddress(request); | ||
const geoAlias = geolocation(request); | ||
return NextResponse.json({ buildId, geo: geoAlias, ip: ipAlias }); | ||
} |
7 changes: 7 additions & 0 deletions
7
packages/next-codemod/transforms/__testfixtures__/next-request-geo-ip/destructure.input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// @ts-nocheck | ||
import { type NextRequest, NextResponse } from "next/server"; | ||
|
||
export function GET(request: NextRequest) { | ||
const { buildId, geo, ip } = request; | ||
return NextResponse.json({ buildId, geo, ip }); | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/next-codemod/transforms/__testfixtures__/next-request-geo-ip/destructure.output.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// @ts-nocheck | ||
import { type NextRequest, NextResponse } from "next/server"; | ||
|
||
import { geolocation, ipAddress } from "@vercel/functions"; | ||
|
||
export function GET(request: NextRequest) { | ||
const { | ||
buildId | ||
} = request; | ||
const ip = ipAddress(request); | ||
const geo = geolocation(request); | ||
return NextResponse.json({ buildId, geo, ip }); | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/next-codemod/transforms/__testfixtures__/next-request-geo-ip/direct-access.input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// @ts-nocheck | ||
import { type NextRequest, NextResponse } from "next/server"; | ||
|
||
export function GET(request: NextRequest) { | ||
const geo = request.geo | ||
const ip = request.ip | ||
return NextResponse.json({ geo, ip }) | ||
} |
10 changes: 10 additions & 0 deletions
10
...ages/next-codemod/transforms/__testfixtures__/next-request-geo-ip/direct-access.output.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// @ts-nocheck | ||
import { type NextRequest, NextResponse } from "next/server"; | ||
|
||
import { geolocation, ipAddress } from "@vercel/functions"; | ||
|
||
export function GET(request: NextRequest) { | ||
const geo = geolocation(request) | ||
const ip = ipAddress(request) | ||
return NextResponse.json({ geo, ip }) | ||
} |
13 changes: 13 additions & 0 deletions
13
...xt-codemod/transforms/__testfixtures__/next-request-geo-ip/duplicate-identifiers.input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// @ts-nocheck | ||
import { type NextRequest, NextResponse } from "next/server"; | ||
|
||
// for duplicate identifier testing | ||
function ipAddress1() { | ||
return "127.0.0.1"; | ||
} | ||
const ipAddress2 = "127.0.0.1"; | ||
|
||
export function GET(request: NextRequest) { | ||
const ipAddress = request.ip; | ||
return NextResponse.json({ ipAddress }); | ||
} |
15 changes: 15 additions & 0 deletions
15
...t-codemod/transforms/__testfixtures__/next-request-geo-ip/duplicate-identifiers.output.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// @ts-nocheck | ||
import { type NextRequest, NextResponse } from "next/server"; | ||
|
||
import { ipAddress as ipAddress3 } from "@vercel/functions"; | ||
|
||
// for duplicate identifier testing | ||
function ipAddress1() { | ||
return "127.0.0.1"; | ||
} | ||
const ipAddress2 = "127.0.0.1"; | ||
|
||
export function GET(request: NextRequest) { | ||
const ipAddress = ipAddress3(request); | ||
return NextResponse.json({ ipAddress }); | ||
} |
16 changes: 16 additions & 0 deletions
16
...ages/next-codemod/transforms/__testfixtures__/next-request-geo-ip/function-param.input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// @ts-nocheck | ||
import { type NextRequest, NextResponse } from "next/server"; | ||
|
||
function getGeo(request: NextRequest) { | ||
return request.geo; | ||
} | ||
|
||
function getIp(request: NextRequest) { | ||
return request.ip; | ||
} | ||
|
||
export function GET(request: NextRequest) { | ||
const geo = getGeo(request); | ||
const ip = getIp(request); | ||
return NextResponse.json({ geo, ip }); | ||
} |
20 changes: 20 additions & 0 deletions
20
...ges/next-codemod/transforms/__testfixtures__/next-request-geo-ip/function-param.output.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// @ts-nocheck | ||
import { type NextRequest, NextResponse } from "next/server"; | ||
|
||
import { geolocation, ipAddress } from "@vercel/functions"; | ||
|
||
function getGeo(request: NextRequest) { | ||
return geolocation(request); | ||
} | ||
|
||
function getIp(request: NextRequest) { | ||
return ipAddress(request); | ||
} | ||
|
||
export function GET(request: NextRequest) { | ||
const geo = getGeo(request); | ||
const ip = getIp(request); | ||
return NextResponse.json({ geo, ip }); | ||
} | ||
|
||
|
9 changes: 9 additions & 0 deletions
9
...ages/next-codemod/transforms/__testfixtures__/next-request-geo-ip/geo-properties.input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// @ts-nocheck | ||
import { type NextRequest, NextResponse } from "next/server"; | ||
|
||
export function GET(request: NextRequest) { | ||
const { city, country, region } = request.geo; | ||
const latitude = request.geo.latitude; | ||
const longitude = request.geo.longitude; | ||
return NextResponse.json({ city, country, region, latitude, longitude }); | ||
} |
11 changes: 11 additions & 0 deletions
11
...ges/next-codemod/transforms/__testfixtures__/next-request-geo-ip/geo-properties.output.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// @ts-nocheck | ||
import { type NextRequest, NextResponse } from "next/server"; | ||
|
||
import { geolocation } from "@vercel/functions"; | ||
|
||
export function GET(request: NextRequest) { | ||
const { city, country, region } = geolocation(request); | ||
const latitude = geolocation(request).latitude; | ||
const longitude = geolocation(request).longitude; | ||
return NextResponse.json({ city, country, region, latitude, longitude }); | ||
} |
8 changes: 8 additions & 0 deletions
8
...ges/next-codemod/transforms/__testfixtures__/next-request-geo-ip/geo-type-import.input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// @ts-nocheck | ||
import { type NextRequest, NextResponse } from "next/server"; | ||
|
||
export function GET(request: NextRequest) { | ||
const geo = request.geo as NextRequest['geo'] | ||
const ip = request.ip | ||
return NextResponse.json({ geo, ip }) | ||
} |
10 changes: 10 additions & 0 deletions
10
...es/next-codemod/transforms/__testfixtures__/next-request-geo-ip/geo-type-import.output.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// @ts-nocheck | ||
import { type NextRequest, NextResponse } from "next/server"; | ||
|
||
import { geolocation, ipAddress, type Geo } from "@vercel/functions"; | ||
|
||
export function GET(request: NextRequest) { | ||
const geo = geolocation(request) as Geo | ||
const ip = ipAddress(request) | ||
return NextResponse.json({ geo, ip }) | ||
} |
9 changes: 9 additions & 0 deletions
9
...s/next-codemod/transforms/__testfixtures__/next-request-geo-ip/has-imports-alias.input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// @ts-nocheck | ||
import { type NextRequest, NextResponse } from "next/server"; | ||
import { geolocation as geo, ipAddress as ip, Geo as GeoType } from "@vercel/functions"; | ||
|
||
export function GET(request: NextRequest) { | ||
const geo = request.geo as GeoType | ||
const ip = request.ip | ||
return NextResponse.json({ geo, ip }) | ||
} |
9 changes: 9 additions & 0 deletions
9
.../next-codemod/transforms/__testfixtures__/next-request-geo-ip/has-imports-alias.output.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// @ts-nocheck | ||
import { type NextRequest, NextResponse } from "next/server"; | ||
import { geolocation as geo, ipAddress as ip, Geo as GeoType } from "@vercel/functions"; | ||
|
||
export function GET(request: NextRequest) { | ||
const geo = geo(request) as GeoType | ||
const ip = ip(request) | ||
return NextResponse.json({ geo, ip }) | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/next-codemod/transforms/__testfixtures__/next-request-geo-ip/has-imports.input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// @ts-nocheck | ||
import { type NextRequest, NextResponse } from "next/server"; | ||
import { geolocation, ipAddress, Geo } from "@vercel/functions"; | ||
|
||
export function GET(request: NextRequest) { | ||
const geo = request.geo as Geo | ||
const ip = request.ip | ||
return NextResponse.json({ geo, ip }) | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/next-codemod/transforms/__testfixtures__/next-request-geo-ip/has-imports.output.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// @ts-nocheck | ||
import { type NextRequest, NextResponse } from "next/server"; | ||
import { geolocation, ipAddress, Geo } from "@vercel/functions"; | ||
|
||
export function GET(request: NextRequest) { | ||
const geo = geolocation(request) as Geo | ||
const ip = ipAddress(request) | ||
return NextResponse.json({ geo, ip }) | ||
} |
10 changes: 10 additions & 0 deletions
10
...es/next-codemod/transforms/__testfixtures__/next-request-geo-ip/has-other-import.input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// @ts-nocheck | ||
import { type NextRequest, NextResponse } from "next/server"; | ||
import { waitUntil } from "@vercel/functions"; | ||
|
||
export function GET(request: NextRequest) { | ||
waitUntil(Promise.resolve()) | ||
const geo = request.geo as NextRequest['geo'] | ||
const ip = request.ip | ||
return NextResponse.json({ geo, ip }) | ||
} |
10 changes: 10 additions & 0 deletions
10
...s/next-codemod/transforms/__testfixtures__/next-request-geo-ip/has-other-import.output.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// @ts-nocheck | ||
import { type NextRequest, NextResponse } from "next/server"; | ||
import { waitUntil, geolocation, ipAddress, type Geo } from "@vercel/functions"; | ||
|
||
export function GET(request: NextRequest) { | ||
waitUntil(Promise.resolve()) | ||
const geo = geolocation(request) as Geo | ||
const ip = ipAddress(request) | ||
return NextResponse.json({ geo, ip }) | ||
} |
13 changes: 13 additions & 0 deletions
13
...es/next-codemod/transforms/__testfixtures__/next-request-geo-ip/multiple-imports.input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// @ts-nocheck | ||
import Link from "next/link"; | ||
// should added RIGHT BELOW the FIRST "next/server" import | ||
import type { NextRequest } from "next/server"; | ||
import Script from "next/script"; | ||
import { notFound } from "next/navigation"; | ||
import { NextResponse } from "next/server"; | ||
|
||
export function GET(request: NextRequest) { | ||
const geo = request.geo | ||
const ip = request.ip | ||
return NextResponse.json({ geo, ip }) | ||
} |
14 changes: 14 additions & 0 deletions
14
...s/next-codemod/transforms/__testfixtures__/next-request-geo-ip/multiple-imports.output.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// @ts-nocheck | ||
import Link from "next/link"; | ||
// should added RIGHT BELOW the FIRST "next/server" import | ||
import type { NextRequest } from "next/server"; | ||
import { geolocation, ipAddress } from "@vercel/functions"; | ||
import Script from "next/script"; | ||
import { notFound } from "next/navigation"; | ||
import { NextResponse } from "next/server"; | ||
|
||
export function GET(request: NextRequest) { | ||
const geo = geolocation(request) | ||
const ip = ipAddress(request) | ||
return NextResponse.json({ geo, ip }) | ||
} |
11 changes: 11 additions & 0 deletions
11
...xt-codemod/transforms/__testfixtures__/next-request-geo-ip/replace-type-geo-only.input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// @ts-nocheck | ||
import { type NextRequest, NextResponse } from "next/server"; | ||
|
||
const geo = { | ||
city: 'London', | ||
country: 'United Kingdom', | ||
latitude: 51.5074, | ||
longitude: -0.1278, | ||
region: 'England', | ||
timezone: 'Europe/London', | ||
} satisfies NextRequest['geo'] |
13 changes: 13 additions & 0 deletions
13
...t-codemod/transforms/__testfixtures__/next-request-geo-ip/replace-type-geo-only.output.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// @ts-nocheck | ||
import { type NextRequest, NextResponse } from "next/server"; | ||
|
||
import type { Geo } from "@vercel/functions"; | ||
|
||
const geo = { | ||
city: 'London', | ||
country: 'United Kingdom', | ||
latitude: 51.5074, | ||
longitude: -0.1278, | ||
region: 'England', | ||
timezone: 'Europe/London', | ||
} satisfies Geo |
4 changes: 4 additions & 0 deletions
4
...ext-codemod/transforms/__testfixtures__/next-request-geo-ip/replace-type-ip-only.input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// @ts-nocheck | ||
import { type NextRequest, NextResponse } from "next/server"; | ||
|
||
const ip = '127.0.0.1' as NextRequest['ip'] |
Oops, something went wrong.