Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
3dbbcadc
Unverified
Commit
3dbbcadc
authored
Oct 04, 2024
by
Jiachi Liu
Committed by
GitHub
Oct 04, 2024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
codemod: handle the async api type (#70785)
parent
47497454
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
49 additions
and
0 deletions
+49
-0
async-api-23.input.tsx
...ext-async-request-api-dynamic-apis/async-api-23.input.tsx
+6
-0
async-api-23.output.tsx
...xt-async-request-api-dynamic-apis/async-api-23.output.tsx
+7
-0
next-async-dynamic-api.ts
...ransforms/lib/async-request-api/next-async-dynamic-api.ts
+36
-0
No files found.
packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-apis/async-api-23.input.tsx
0 → 100644
View file @
3dbbcadc
import
{
cookies
}
from
'next/headers'
export
type
Cookie
=
ReturnType
<
typeof
cookies
>
export
function
foo
(
c
:
ReturnType
<
typeof
cookies
>
)
{
return
c
}
packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-apis/async-api-23.output.tsx
0 → 100644
View file @
3dbbcadc
import
{
cookies
}
from
'next/headers'
export
type
Cookie
=
Awaited
<
ReturnType
<
typeof
cookies
>>
export
function
foo
(
c
:
Awaited
<
ReturnType
<
typeof
cookies
>>
)
{
return
c
}
packages/next-codemod/transforms/lib/async-request-api/next-async-dynamic-api.ts
View file @
3dbbcadc
...
...
@@ -205,6 +205,42 @@ export function transformDynamicAPI(
}
}
})
// Handle type usage of async API, e.g. `type Cookie = ReturnType<typeof cookies>`
// convert it to `type Cookie = Awaited<ReturnType<typeof cookies>>`
root
.
find
(
j
.
TSTypeReference
,
{
typeName
:
{
type
:
'Identifier'
,
name
:
'ReturnType'
,
},
})
.
forEach
((
path
)
=>
{
const
typeParam
=
path
.
node
.
typeParameters
?.
params
[
0
]
// Check if the ReturnType is for 'cookies'
if
(
typeParam
&&
j
.
TSTypeQuery
.
check
(
typeParam
)
&&
j
.
Identifier
.
check
(
typeParam
.
exprName
)
&&
typeParam
.
exprName
.
name
===
asyncRequestApiName
)
{
// Replace ReturnType<typeof cookies> with Awaited<ReturnType<typeof cookies>>
const
awaitedTypeReference
=
j
.
tsTypeReference
(
j
.
identifier
(
'Awaited'
),
j
.
tsTypeParameterInstantiation
([
j
.
tsTypeReference
(
j
.
identifier
(
'ReturnType'
),
j
.
tsTypeParameterInstantiation
([
typeParam
])
),
])
)
j
(
path
).
replaceWith
(
awaitedTypeReference
)
modified
=
true
}
})
}
const
isClientComponent
=
determineClientDirective
(
root
,
j
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment