@@ -3,15 +3,25 @@ import type { BaseNextRequest } from '../base-http'
3
3
import type { NextRequest } from '../web/exports'
4
4
import { ACTION_HEADER } from '../../client/components/app-router-headers'
5
5
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
+
6
22
export function getServerActionRequestMetadata (
7
23
req : IncomingMessage | BaseNextRequest | NextRequest
8
- ) : {
9
- actionId : string | null
10
- isURLEncodedAction : boolean
11
- isMultipartAction : boolean
12
- isFetchAction : boolean
13
- isPotentialServerAction : boolean
14
- } {
24
+ ) : ServerActionRequestMetadata {
15
25
let actionId : string | null
16
26
let contentType : string | null
17
27
@@ -29,21 +39,26 @@ export function getServerActionRequestMetadata(
29
39
const isMultipartAction = Boolean (
30
40
req . method === 'POST' && contentType ?. startsWith ( 'multipart/form-data' )
31
41
)
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
+ }
37
51
38
52
const isPotentialServerAction = Boolean (
39
- isFetchAction || isURLEncodedAction || isMultipartAction
53
+ isURLEncodedAction || isMultipartAction
40
54
)
41
55
42
56
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 ,
44
60
isURLEncodedAction,
45
61
isMultipartAction,
46
- isFetchAction,
47
62
isPotentialServerAction,
48
63
}
49
64
}
0 commit comments