Skip to content

Commit

Permalink
fix route 2nd arg transform
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Oct 1, 2024
1 parent a2b6b8d commit 686867c
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function GET(req, { params }: any) {
call(params.foo)
}

export async function DELETE(req, { params }: any) {
call(params.foo)
}

export async function PATCH(req, context: any) {
call(context.params.foo)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export async function GET(req, props: any) {
const params = await props.params;
call(params.foo)
}

export async function DELETE(req, props: any) {
const params = await props.params;
call(params.foo)
}

export async function PATCH(req, context: any) {
call((await context.params).foo)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
wrapParentheseIfNeeded,
type FunctionScope,
insertCommentOnce,
TARGET_ROUTE_EXPORTS,
} from './utils'

const PAGE_PROPS = 'props'
Expand Down Expand Up @@ -274,17 +275,23 @@ export function transformDynamicProps(
// target properties mapping, only contains `params` and `searchParams`
const propertiesMap = new Map<string, any>()
let allProperties: ObjectPattern['properties'] = []

const isRoute = !isDefaultExport && TARGET_ROUTE_EXPORTS.has(functionName)
// generateMetadata API has 2 params
if (functionName === 'generateMetadata') {
if (params.length > 2 || params.length === 0) return
} else if (isRoute) {
if (params.length !== 2) return
} else {
// Page/Layout/Route handlers have 1 param
if (params.length !== 1) return
}
const propsIdentifier = generateUniqueIdentifier(PAGE_PROPS, path, j)

const currentParam = params[0]
const propsArgumentIndex = isRoute ? 1 : 0
console.log('propsArgumentIndex', propsArgumentIndex)

const currentParam = params[propsArgumentIndex]
if (!currentParam) return

// Argument destructuring case
if (currentParam.type === 'ObjectPattern') {
Expand Down Expand Up @@ -319,7 +326,7 @@ export function transformDynamicProps(
modifyTypes(currentParam.typeAnnotation, propsIdentifier, root, j)

// Override the first param to `props`
params[0] = propsIdentifier
params[propsArgumentIndex] = propsIdentifier

modified = true
modifiedPropArgument = true
Expand Down
11 changes: 11 additions & 0 deletions packages/next-codemod/transforms/lib/async-request-api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,20 @@ export type FunctionScope =
| FunctionExpression
| ArrowFunctionExpression

export const TARGET_ROUTE_EXPORTS = new Set([
'GET',
'POST',
'PUT',
'PATCH',
'DELETE',
'OPTIONS',
'HEAD',
])

export const TARGET_NAMED_EXPORTS = new Set([
// For page and layout
'generateMetadata',
...TARGET_ROUTE_EXPORTS,
])

export const TARGET_PROP_NAMES = new Set(['params', 'searchParams'])
Expand Down

0 comments on commit 686867c

Please sign in to comment.