Skip to content

Commit 55857bb

Browse files
feat: add support for EF header property in netlify.toml (#6558)
* feat: add support for EF `header` property in `netlify.toml` * chore: update snapshot
1 parent 9103da6 commit 55857bb

File tree

6 files changed

+225
-1
lines changed

6 files changed

+225
-1
lines changed

packages/config/src/edge_functions.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,17 @@ const methodValues = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS']
55

66
const isMethod = (value: unknown) => typeof value === 'string' && methodValues.includes(value.toUpperCase())
77

8+
const isValidHeaderValue = (value: unknown) => typeof value === 'boolean' || typeof value === 'string'
9+
const isValidHeaders = (value: unknown) =>
10+
typeof value === 'object' && value !== null && !Array.isArray(value) && Object.values(value).every(isValidHeaderValue)
11+
812
export const validations = [
913
{
1014
property: 'edge_functions.*',
11-
...validProperties(['path', 'excludedPath', 'pattern', 'excludedPattern', 'function', 'cache', 'method'], []),
15+
...validProperties(
16+
['path', 'excludedPath', 'pattern', 'excludedPattern', 'function', 'cache', 'method', 'header'],
17+
[],
18+
),
1219
example: () => ({ edge_functions: [{ path: '/hello', function: 'hello' }] }),
1320
},
1421
{
@@ -87,4 +94,22 @@ export const validations = [
8794
message: `must be one of or array of: ${methodValues.join(', ')}`,
8895
example: () => ({ edge_functions: [{ method: ['PUT', 'DELETE'], path: '/hello', function: 'hello' }] }),
8996
},
97+
{
98+
property: 'edge_functions.*.header',
99+
check: isValidHeaders,
100+
message: 'must be an object with string keys and boolean or string values.',
101+
example: () => ({
102+
edge_functions: [
103+
{
104+
path: '/hello',
105+
function: 'hello',
106+
header: {
107+
'x-must-be-present': true,
108+
'x-must-not-be-present': false,
109+
'x-must-match-value': '^(value1|value2)$',
110+
},
111+
},
112+
],
113+
}),
114+
},
90115
]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[[edge_functions]]
2+
function = "function2"
3+
path = "/function2"
4+
5+
[edge_functions.header]
6+
x-bar = "some-value"
7+
x-foo = true
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[[edge_functions]]
2+
function = "function2"
3+
path = "/function2"
4+
5+
[edge_functions.header]
6+
x-bar = [true, false]
7+
x-foo = true

packages/config/tests/validate/snapshots/tests.js.md

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,6 +1514,7 @@ Generated by [AVA](https://avajs.dev).
15141514
- function␊
15151515
- cache␊
15161516
- method␊
1517+
- header␊
15171518
15181519
Invalid syntax␊
15191520
@@ -1832,3 +1833,177 @@ Generated by [AVA](https://avajs.dev).
18321833
]␊
18331834
path = "/external/path"␊
18341835
function = "hello"`
1836+
1837+
## edge_functions.any.header: allowed values
1838+
1839+
> Snapshot 1
1840+
1841+
`{␊
1842+
"accounts": [],␊
1843+
"branch": "branch",␊
1844+
"buildDir": "packages/config/tests/validate/fixtures/edge_functions_header_allowed",␊
1845+
"config": {␊
1846+
"build": {␊
1847+
"environment": {},␊
1848+
"processing": {␊
1849+
"css": {},␊
1850+
"html": {},␊
1851+
"images": {},␊
1852+
"js": {}␊
1853+
},␊
1854+
"publish": "packages/config/tests/validate/fixtures/edge_functions_header_allowed",␊
1855+
"publishOrigin": "default",␊
1856+
"services": {}␊
1857+
},␊
1858+
"edge_functions": [␊
1859+
{␊
1860+
"function": "function2",␊
1861+
"header": {␊
1862+
"x-bar": "some-value",␊
1863+
"x-foo": true␊
1864+
},␊
1865+
"path": "/external/path"␊
1866+
}␊
1867+
],␊
1868+
"functions": {␊
1869+
"*": {}␊
1870+
},␊
1871+
"headers": [],␊
1872+
"plugins": [],␊
1873+
"redirects": []␊
1874+
},␊
1875+
"configPath": "packages/config/tests/validate/fixtures/edge_functions_header_allowed/netlify.toml",␊
1876+
"context": "production",␊
1877+
"env": {␊
1878+
"BRANCH": {␊
1879+
"sources": [␊
1880+
"general"␊
1881+
],␊
1882+
"value": "branch"␊
1883+
},␊
1884+
"BUILD_ID": {␊
1885+
"sources": [␊
1886+
"general"␊
1887+
],␊
1888+
"value": "0"␊
1889+
},␊
1890+
"CACHED_COMMIT_REF": {␊
1891+
"sources": [␊
1892+
"general"␊
1893+
],␊
1894+
"value": "HEXADECIMAL_ID"␊
1895+
},␊
1896+
"COMMIT_REF": {␊
1897+
"sources": [␊
1898+
"general"␊
1899+
],␊
1900+
"value": "HEXADECIMAL_ID"␊
1901+
},␊
1902+
"CONTEXT": {␊
1903+
"sources": [␊
1904+
"general"␊
1905+
],␊
1906+
"value": "production"␊
1907+
},␊
1908+
"DEPLOY_ID": {␊
1909+
"sources": [␊
1910+
"general"␊
1911+
],␊
1912+
"value": "0"␊
1913+
},␊
1914+
"DEPLOY_PRIME_URL": {␊
1915+
"sources": [␊
1916+
"general"␊
1917+
],␊
1918+
"value": "https://branch--site-name.netlify.app"␊
1919+
},␊
1920+
"DEPLOY_URL": {␊
1921+
"sources": [␊
1922+
"general"␊
1923+
],␊
1924+
"value": "https://0--site-name.netlify.app"␊
1925+
},␊
1926+
"GATSBY_TELEMETRY_DISABLED": {␊
1927+
"sources": [␊
1928+
"general"␊
1929+
],␊
1930+
"value": "1"␊
1931+
},␊
1932+
"HEAD": {␊
1933+
"sources": [␊
1934+
"general"␊
1935+
],␊
1936+
"value": "branch"␊
1937+
},␊
1938+
"LANG": {␊
1939+
"sources": [␊
1940+
"general"␊
1941+
],␊
1942+
"value": "en_US.UTF-8"␊
1943+
},␊
1944+
"LANGUAGE": {␊
1945+
"sources": [␊
1946+
"general"␊
1947+
],␊
1948+
"value": "en_US:en"␊
1949+
},␊
1950+
"LC_ALL": {␊
1951+
"sources": [␊
1952+
"general"␊
1953+
],␊
1954+
"value": "en_US.UTF-8"␊
1955+
},␊
1956+
"NETLIFY_LOCAL": {␊
1957+
"sources": [␊
1958+
"general"␊
1959+
],␊
1960+
"value": "true"␊
1961+
},␊
1962+
"NEXT_TELEMETRY_DISABLED": {␊
1963+
"sources": [␊
1964+
"general"␊
1965+
],␊
1966+
"value": "1"␊
1967+
},␊
1968+
"PULL_REQUEST": {␊
1969+
"sources": [␊
1970+
"general"␊
1971+
],␊
1972+
"value": "false"␊
1973+
}␊
1974+
},␊
1975+
"headersPath": "packages/config/tests/validate/fixtures/edge_functions_header_allowed/_headers",␊
1976+
"integrations": [],␊
1977+
"redirectsPath": "packages/config/tests/validate/fixtures/edge_functions_header_allowed/_redirects",␊
1978+
"repositoryRoot": "packages/config/tests/validate/fixtures/edge_functions_header_allowed",␊
1979+
"siteInfo": {}␊
1980+
}`
1981+
1982+
## edge_functions.any.header: disallowed values
1983+
1984+
> Snapshot 1
1985+
1986+
`When resolving config file packages/config/tests/validate/fixtures/edge_functions_header_disallowed/netlify.toml:␊
1987+
Configuration property edge_functions[0].header must be an object with string keys and boolean or string values.␊
1988+
1989+
Invalid syntax␊
1990+
1991+
[[edge_functions]]␊
1992+
1993+
[edge_functions.header]␊
1994+
x-bar = [␊
1995+
true,␊
1996+
false␊
1997+
]␊
1998+
x-foo = true␊
1999+
2000+
Valid syntax␊
2001+
2002+
[[edge_functions]]␊
2003+
path = "/external/path"␊
2004+
function = "hello"␊
2005+
2006+
[edge_functions.header]␊
2007+
x-must-be-present = true␊
2008+
x-must-not-be-present = false␊
2009+
x-must-match-value = "^(value1|value2)$"`
Binary file not shown.

packages/config/tests/validate/tests.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,3 +320,13 @@ test('edge_functions.any.method: disallowed values', async (t) => {
320320
const output = await new Fixture('./fixtures/edge_functions_method_disallowed').runWithConfig()
321321
t.snapshot(normalizeOutput(output))
322322
})
323+
324+
test('edge_functions.any.header: allowed values', async (t) => {
325+
const output = await new Fixture('./fixtures/edge_functions_header_allowed').runWithConfig()
326+
t.snapshot(normalizeOutput(output))
327+
})
328+
329+
test('edge_functions.any.header: disallowed values', async (t) => {
330+
const output = await new Fixture('./fixtures/edge_functions_header_disallowed').runWithConfig()
331+
t.snapshot(normalizeOutput(output))
332+
})

0 commit comments

Comments
 (0)