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

Codemod for Async Request API #69572

Merged
merged 46 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
4b521da
Transform cookies for sync and async migratable cases
huozhi Sep 2, 2024
a49711b
rm if cond check
huozhi Sep 4, 2024
6722ee1
rm cond
huozhi Sep 5, 2024
ca294c4
skip client
huozhi Sep 10, 2024
6570c2e
wip params and search params
huozhi Sep 10, 2024
830d1ec
rename properties
huozhi Sep 11, 2024
99e74ac
transform search params
huozhi Sep 11, 2024
8c95ab6
reogrnaize, merge two transform
huozhi Sep 11, 2024
230ec3f
support typescript param and searchParam
huozhi Sep 12, 2024
948230d
rename to props
huozhi Sep 12, 2024
b929bea
insert imports
huozhi Sep 12, 2024
3785aa1
format
huozhi Sep 13, 2024
79ec53d
skip local api calls
huozhi Sep 13, 2024
3aa7855
convert recent export default to async if possible
huozhi Sep 13, 2024
5cea1fd
add hook test
huozhi Sep 13, 2024
66d3ec2
handle generateMetadata
huozhi Sep 17, 2024
53a36c1
handle props types defined somewhere else
huozhi Sep 18, 2024
cf48031
handle cases are not able to transform
huozhi Sep 18, 2024
ec7b2ac
do not apply if possible
huozhi Sep 18, 2024
c62981f
secure transform func
huozhi Sep 18, 2024
1295221
handle route.js transform
huozhi Sep 19, 2024
3402568
require entry file
huozhi Sep 19, 2024
f964b11
reorg files
huozhi Sep 19, 2024
21ae0bf
skip types
huozhi Sep 19, 2024
7c84196
avoid duplicates
huozhi Sep 19, 2024
a8ad7ba
fix cookies type name and fix missing await case
huozhi Sep 19, 2024
f2c4992
handle destruction
huozhi Sep 19, 2024
5c68f58
convert return type
huozhi Sep 19, 2024
b2a1bff
rename UnsafeUnwrapped
huozhi Sep 19, 2024
c4d446b
insert helper per function
huozhi Sep 19, 2024
e293623
add return type case
huozhi Sep 19, 2024
1b89229
transform the decl.func return type instead of path
huozhi Sep 19, 2024
3c61a1b
fix transform multi arg types
huozhi Sep 19, 2024
b97c1fb
avoid inserting duplicated types
huozhi Sep 19, 2024
e6d50c6
format
huozhi Sep 20, 2024
60c89bd
prettier input
huozhi Sep 20, 2024
089017f
update comment
huozhi Sep 20, 2024
db8f04b
prettier more
huozhi Sep 20, 2024
9a54831
move folder to include the assets in files
huozhi Sep 20, 2024
c9b1b60
reorganize tests
huozhi Sep 20, 2024
a06013d
fix await calls under arrow function
huozhi Sep 20, 2024
2676c4f
unique identifier
huozhi Sep 23, 2024
7a75a8e
rename tests
huozhi Sep 23, 2024
53f999f
handle import specifiers
huozhi Sep 23, 2024
daf9d4f
Merge branch 'canary' into 09-02-codemod_exotic_cookie
huozhi Sep 23, 2024
be4f882
add docs
huozhi Sep 23, 2024
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
4 changes: 4 additions & 0 deletions packages/next-codemod/bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ const TRANSFORMER_INQUIRER_CHOICES = [
name: 'built-in-next-font: Uninstall `@next/font` and transform imports to `next/font`',
value: 'built-in-next-font',
},
{
name: 'next-async-request-api: Transforms usage of Next.js async Request APIs',
value: 'next-async-request-api',
},
]

function expandFilePathsIfNeeded(filesBeforeExpansion) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { cookies } from 'next/headers'

export async function MyComponent() {
const name = cookies().get('name')
callback(name)
}

function callback(name: any) {
console.log(name)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { cookies } from 'next/headers'

export async function MyComponent() {
const name = (await cookies()).get('name')
callback(name)
}

function callback(name: any) {
console.log(name)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { draftMode } from 'next/headers'

export async function MyComponent() {
draftMode().enable()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { draftMode } from 'next/headers'

export async function MyComponent() {
(await draftMode()).enable()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// If it's sync default export, convert to async and await the function call
import { draftMode } from 'next/headers'

export default function MyComponent() {
draftMode().enable()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// If it's sync default export, convert to async and await the function call
import { draftMode } from 'next/headers'

export default async function MyComponent() {
(await draftMode()).enable()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// If it's already awaited, don't wrap it again
huozhi marked this conversation as resolved.
Show resolved Hide resolved
import { draftMode } from 'next/headers'

export default async function MyComponent() {
draftMode().enable()
}

export async function MyComponent2() {
draftMode().enable()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// If it's already awaited, don't wrap it again
import { draftMode } from 'next/headers'

export default async function MyComponent() {
(await draftMode()).enable()
}

export async function MyComponent2() {
(await draftMode()).enable()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { draftMode } from 'next/headers'

export function MyComponent2() {
draftMode().enable()
}

export function useDraftModeEnabled() {
draftMode().enable()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { use } from "react";
import { draftMode, type UnsafeUnwrappedDraftMode } from 'next/headers';

export function MyComponent2() {
(draftMode() as unknown as UnsafeUnwrappedDraftMode).enable()
}

export function useDraftModeEnabled() {
use(draftMode()).enable()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react'
import { draftMode } from 'next/headers'

export default function Page() {
return (
<button
disabled={draftMode().isEnabled}
>
Enable Draft Mode
</button>
)
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from 'react'
import { draftMode } from 'next/headers'

export default async function Page() {
return (
(<button
disabled={(await draftMode()).isEnabled}
>Enable Draft Mode
</button>)
);
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { draftMode, type UnsafeUnwrappedDraftMode } from 'next/headers';

export function MyComponent2() {
draftMode().enable()
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { draftMode, type UnsafeUnwrappedDraftMode } from 'next/headers';

export function MyComponent2() {
(draftMode() as unknown as UnsafeUnwrappedDraftMode).enable()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { headers } from 'next/headers';

export function GET(): Response {
headers()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { headers } from 'next/headers';

export async function GET(): Promise<Response> {
await headers()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export default function Page(
{
params: { slug },
searchParams: { search },
} : {
params: { slug: string }
searchParams: any
}
): JSX.Element {
slug
search
huozhi marked this conversation as resolved.
Show resolved Hide resolved

return <div>Page</div>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export default async function Page(
props: {
params: Promise<{ slug: string }>
searchParams: Promise<any>
}
): Promise<JSX.Element> {
const searchParams = await props.searchParams;

const {
search
} = searchParams;

const params = await props.params;

const {
slug
} = params;

slug
search

return <div>Page</div>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { headers } from 'next/headers'

export function MyComp() {
return headers()
}

export function MyComp2() {
return headers()
}

export function MyComp3() {
return headers()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { headers, type UnsafeUnwrappedHeaders } from 'next/headers';

export function MyComp() {
return (headers() as unknown as UnsafeUnwrappedHeaders);
}

export function MyComp2() {
return (headers() as unknown as UnsafeUnwrappedHeaders);
}

export function MyComp3() {
return (headers() as unknown as UnsafeUnwrappedHeaders);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { cookies } from "next/headers";

async function MyComponent() {
callSomething(cookies());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { cookies } from "next/headers";

async function MyComponent() {
callSomething(await cookies());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { headers } from "next/headers";

async function MyComponent() {
callSomething(headers());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { headers } from "next/headers";

async function MyComponent() {
callSomething(await headers());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from 'react'
import {
headers,
cookies,
draftMode,
} from 'next/headers'

export function MyDraftComponent() {
if (draftMode().isEnabled) {
return null
}
huozhi marked this conversation as resolved.
Show resolved Hide resolved

return <p>page</p>
}

export function MyCookiesComponent() {
const c = cookies()
return c.get('name')
}

export function MyHeadersComponent() {
const h = headers()
return (
<p>{h.get('x-foo')}</p>
)
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react'
import {
headers,
cookies,
draftMode,
type UnsafeUnwrappedCookies,
type UnsafeUnwrappedHeaders,
type UnsafeUnwrappedDraftMode,
} from 'next/headers';

export function MyDraftComponent() {
if ((draftMode() as unknown as UnsafeUnwrappedDraftMode).isEnabled) {
return null
}

return <p>page</p>
}

export function MyCookiesComponent() {
const c = (cookies() as unknown as UnsafeUnwrappedCookies)
return c.get('name')
}

export function MyHeadersComponent() {
const h = (headers() as unknown as UnsafeUnwrappedHeaders)
return (
<p>{h.get('x-foo')}</p>
)
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from 'react'
import {
headers,
cookies,
draftMode,
} from 'next/headers'

export function MyDraftComponent() {
if (draftMode().isEnabled) {
return null
}

return <p>page</p>
}

export function MyCookiesComponent() {
const c = cookies()
return c.get('name')
}

export function MyHeadersComponent() {
const h = headers()
return (
<p>{h.get('x-foo')}</p>
)
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react'
import {
headers,
cookies,
draftMode,
} from 'next/headers'

export function MyDraftComponent() {
if (/* TODO: await this async call and propagate the async correctly */
huozhi marked this conversation as resolved.
Show resolved Hide resolved
draftMode().isEnabled) {
return null
}

return <p>page</p>
}

export function MyCookiesComponent() {
const c = /* TODO: await this async call and propagate the async correctly */
cookies()
return c.get('name')
}

export function MyHeadersComponent() {
const h = /* TODO: await this async call and propagate the async correctly */
headers()
return (
<p>{h.get('x-foo')}</p>
)
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { cookies } from "next/headers";

export default async function Page() {
callSomething(cookies());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { cookies } from "next/headers";

export default async function Page() {
callSomething(await cookies());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { headers } from 'next/headers'

export const GET = function () {
headers()
}

export function POST() {
headers()
}

export async function DELETE() {
headers()
}
Loading
Loading