Skip to content

Commit 13d30ce

Browse files
committed
tighten up types for server action metadata
1 parent 5c5aa6c commit 13d30ce

File tree

2 files changed

+30
-21
lines changed

2 files changed

+30
-21
lines changed

Diff for: packages/next/src/server/app-render/action-handler.ts

-6
Original file line numberDiff line numberDiff line change
@@ -870,12 +870,6 @@ export async function handleAction({
870870
console.error(err)
871871
return { type: 'not-found' }
872872
}
873-
if (actionId === null) {
874-
// `getActionModIdOrError` checks this, but typescript doesn't know that.
875-
throw new InvariantError(
876-
'Expected actionId to be defined for a fetch action'
877-
)
878-
}
879873

880874
// The temporary reference set is used for parsing the arguments and in the catch handler,
881875
// so we want to create it before any of the decoding logic has a chance to throw.

Diff for: packages/next/src/server/lib/server-action-request-meta.ts

+30-15
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,25 @@ import type { BaseNextRequest } from '../base-http'
33
import type { NextRequest } from '../web/exports'
44
import { ACTION_HEADER } from '../../client/components/app-router-headers'
55

6+
export type ServerActionRequestMetadata =
7+
| {
8+
isFetchAction: true
9+
actionId: string
10+
isURLEncodedAction: boolean
11+
isMultipartAction: boolean
12+
isPotentialServerAction: true
13+
}
14+
| {
15+
isFetchAction: false
16+
actionId: null
17+
isURLEncodedAction: boolean
18+
isMultipartAction: boolean
19+
isPotentialServerAction: boolean
20+
}
21+
622
export function getServerActionRequestMetadata(
723
req: IncomingMessage | BaseNextRequest | NextRequest
8-
): {
9-
actionId: string | null
10-
isURLEncodedAction: boolean
11-
isMultipartAction: boolean
12-
isFetchAction: boolean
13-
isPotentialServerAction: boolean
14-
} {
24+
): ServerActionRequestMetadata {
1525
let actionId: string | null
1626
let contentType: string | null
1727

@@ -29,21 +39,26 @@ export function getServerActionRequestMetadata(
2939
const isMultipartAction = Boolean(
3040
req.method === 'POST' && contentType?.startsWith('multipart/form-data')
3141
)
32-
const isFetchAction = Boolean(
33-
actionId !== undefined &&
34-
typeof actionId === 'string' &&
35-
req.method === 'POST'
36-
)
42+
if (actionId !== null && req.method === 'POST') {
43+
return {
44+
isFetchAction: true,
45+
actionId,
46+
isMultipartAction,
47+
isURLEncodedAction,
48+
isPotentialServerAction: true,
49+
}
50+
}
3751

3852
const isPotentialServerAction = Boolean(
39-
isFetchAction || isURLEncodedAction || isMultipartAction
53+
isURLEncodedAction || isMultipartAction
4054
)
4155

4256
return {
43-
actionId,
57+
isFetchAction: false,
58+
// it may technically be non-null, but there's no use for it outside a fetch action.
59+
actionId: null,
4460
isURLEncodedAction,
4561
isMultipartAction,
46-
isFetchAction,
4762
isPotentialServerAction,
4863
}
4964
}

0 commit comments

Comments
 (0)