-
Notifications
You must be signed in to change notification settings - Fork 366
/
headers.ts
58 lines (50 loc) · 2.15 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
55
56
57
58
import { parseAllHeaders } from 'netlify-headers-parser'
import { NETLIFYDEVERR, log } from './command-helpers.js'
/**
* Get the matching headers for `path` given a set of `rules`.
*
* @param {Object<string,Object<string,string[]>>!} headers
* The rules to use for matching.
*
* @param {string!} path
* The path to match against.
*
* @returns {Object<string,string[]>}
*/
// @ts-expect-error TS(7006) FIXME: Parameter 'headers' implicitly has an 'any' type.
export const headersForPath = function (headers, path) {
// @ts-expect-error TS(7031) FIXME: Binding element 'forRegExp' implicitly has an 'any... Remove this comment to see the full error message
const matchingHeaders = headers.filter(({ forRegExp }) => forRegExp.test(path)).map(getHeaderValues)
const headersRules = Object.assign({}, ...matchingHeaders)
return headersRules
}
// @ts-expect-error TS(7031) FIXME: Binding element 'values' implicitly has an 'any' t... Remove this comment to see the full error message
const getHeaderValues = function ({ values }) {
return values
}
// @ts-expect-error TS(7031) FIXME: Binding element 'configPath' implicitly has an 'an... Remove this comment to see the full error message
export const parseHeaders = async function ({ config, configPath, headersFiles }): Promise<Header[]> {
const { errors, headers } = await parseAllHeaders({
headersFiles,
netlifyConfigPath: configPath,
minimal: false,
configHeaders: config?.headers || [],
})
handleHeadersErrors(errors)
return headers
}
// @ts-expect-error TS(7006) FIXME: Parameter 'errors' implicitly has an 'any' type.
const handleHeadersErrors = function (errors) {
if (errors.length === 0) {
return
}
const errorMessage = errors.map(getErrorMessage).join('\n\n')
log(NETLIFYDEVERR, `Headers syntax errors:\n${errorMessage}`)
}
// @ts-expect-error TS(7031) FIXME: Binding element 'message' implicitly has an 'any' ... Remove this comment to see the full error message
const getErrorMessage = function ({ message }) {
return message
}
export const NFFunctionName = 'x-nf-function-name'
export const NFFunctionRoute = 'x-nf-function-route'
export const NFRequestID = 'x-nf-request-id'