-
Notifications
You must be signed in to change notification settings - Fork 366
/
redirects.ts
66 lines (60 loc) · 2.54 KB
/
redirects.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
59
60
61
62
63
64
65
66
import { parseAllRedirects } from 'netlify-redirect-parser'
import { NETLIFYDEVERR, log } from './command-helpers.js'
// Parse, normalize and validate all redirects from `_redirects` files
// and `netlify.toml`
// @ts-expect-error TS(7031) FIXME: Binding element 'configPath' implicitly has an 'an... Remove this comment to see the full error message
export const parseRedirects = async function ({ config, configPath, redirectsFiles }) {
const { errors, redirects } = await parseAllRedirects({
redirectsFiles,
netlifyConfigPath: configPath,
minimal: false,
configRedirects: config?.redirects || [],
})
handleRedirectParsingErrors(errors)
// @ts-expect-error TS(2345) FIXME: Argument of type '({ conditions: { country, langua... Remove this comment to see the full error message
return redirects.map(normalizeRedirect)
}
// @ts-expect-error TS(7006) FIXME: Parameter 'errors' implicitly has an 'any' type.
const handleRedirectParsingErrors = function (errors) {
if (errors.length === 0) {
return
}
const errorMessage = errors.map(getErrorMessage).join('\n\n')
log(NETLIFYDEVERR, `Redirects 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
}
// `netlify-redirector` does not handle the same shape as the backend:
// - `from` is called `origin`
// - `query` is called `params`
// - `conditions.role|country|language` are capitalized
const normalizeRedirect = function ({
// @ts-expect-error TS(7031) FIXME: Binding element 'country' implicitly has an 'any' ... Remove this comment to see the full error message
conditions: { country, language, role, ...conditions },
// @ts-expect-error TS(7031) FIXME: Binding element 'from' implicitly has an 'any' typ... Remove this comment to see the full error message
from,
// @ts-expect-error TS(7031) FIXME: Binding element 'query' implicitly has an 'any' ty... Remove this comment to see the full error message
query,
// @ts-expect-error TS(7031) FIXME: Binding element 'signed' implicitly has an 'any' t... Remove this comment to see the full error message
signed,
...redirect
}) {
return {
...redirect,
origin: from,
params: query,
conditions: {
...conditions,
...(role && { Role: role }),
...(country && { Country: country }),
...(language && { Language: language }),
},
...(signed && {
sign: {
jwt_secret: signed,
},
}),
}
}