-
Notifications
You must be signed in to change notification settings - Fork 390
/
Copy pathheaders.ts
54 lines (46 loc) · 1.69 KB
/
headers.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
import { type Header, parseAllHeaders } from '@netlify/headers-parser'
import { NETLIFYDEVERR, type NormalizedCachedConfigConfig, log } from './command-helpers.js'
/**
* Get the matching headers for `path` given a set of `rules`.
*/
export const headersForPath = function (headers: Header[], path: string) {
const matchingHeaders = headers.filter(({ forRegExp }) => forRegExp.test(path)).map(getHeaderValues)
const headersRules = Object.assign({}, ...matchingHeaders)
return headersRules
}
const getHeaderValues = function ({ values }: Header) {
return values
}
export const parseHeaders = async function ({
config,
configPath,
headersFiles,
}: {
config: NormalizedCachedConfigConfig
configPath?: string | undefined
headersFiles?: string[] | undefined
}): Promise<Header[]> {
const { errors, headers } = await parseAllHeaders({
headersFiles,
netlifyConfigPath: configPath,
minimal: false,
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- XXX(serhalp): fixed in stacked PR.
configHeaders: config.headers ?? [],
})
handleHeadersErrors(errors)
// TODO(serhalp): Make `parseAllHeaders()` smart enough to conditionally return a refined type based on `minimal`
return headers as Header[]
}
const handleHeadersErrors = function (errors: Error[]) {
if (errors.length === 0) {
return
}
const errorMessage = errors.map(getErrorMessage).join('\n\n')
log(NETLIFYDEVERR, `Headers syntax errors:\n${errorMessage}`)
}
const getErrorMessage = function ({ message }: Error): string {
return message
}
export const NFFunctionName = 'x-nf-function-name'
export const NFFunctionRoute = 'x-nf-function-route'
export const NFRequestID = 'x-nf-request-id'