-
Notifications
You must be signed in to change notification settings - Fork 28k
/
Copy pathserver-action-request-meta.ts
55 lines (49 loc) · 1.54 KB
/
server-action-request-meta.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import type { IncomingMessage } from 'http'
import type { BaseNextRequest } from '../base-http'
import type { NextRequest } from '../web/exports'
import { ACTION } from '../../client/components/app-router-headers'
export function getServerActionRequestMetadata(
req: IncomingMessage | BaseNextRequest | NextRequest
): {
actionId: string | null
isURLEncodedAction: boolean
isMultipartAction: boolean
isFetchAction: boolean
isServerAction: boolean
} {
let actionId: string | null
let contentType: string | null
if (req.headers instanceof Headers) {
actionId = req.headers.get(ACTION.toLowerCase()) ?? null
contentType = req.headers.get('content-type')
} else {
actionId = (req.headers[ACTION.toLowerCase()] as string) ?? null
contentType = req.headers['content-type'] ?? null
}
const isURLEncodedAction = Boolean(
req.method === 'POST' && contentType === 'application/x-www-form-urlencoded'
)
const isMultipartAction = Boolean(
req.method === 'POST' && contentType?.startsWith('multipart/form-data')
)
const isFetchAction = Boolean(
actionId !== undefined &&
typeof actionId === 'string' &&
req.method === 'POST'
)
const isServerAction = Boolean(
isFetchAction || isURLEncodedAction || isMultipartAction
)
return {
actionId,
isURLEncodedAction,
isMultipartAction,
isFetchAction,
isServerAction,
}
}
export function getIsServerAction(
req: IncomingMessage | BaseNextRequest | NextRequest
): boolean {
return getServerActionRequestMetadata(req).isServerAction
}