Skip to content

Commit

Permalink
fix(types)!: increase strictness of optional types
Browse files Browse the repository at this point in the history
  • Loading branch information
serhalp committed Feb 28, 2025
1 parent 62d0ded commit e339cc6
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 24 deletions.
2 changes: 1 addition & 1 deletion packages/headers-parser/src/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const getFileHeaders = async function (headersFiles: string[]) {
return concatResults(resultsArrays)
}

const getConfigHeaders = async function (netlifyConfigPath?: undefined | string) {
const getConfigHeaders = async function (netlifyConfigPath?: string) {
if (netlifyConfigPath === undefined) {
return splitResults([])
}
Expand Down
2 changes: 1 addition & 1 deletion packages/headers-parser/src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function parseHeaderObject(
}

// Normalize and validate the `for` field
const normalizePath = function (rawPath?: undefined | string): string {
const normalizePath = function (rawPath?: string): string {
if (rawPath === undefined) {
throw new TypeError('Missing "for" field')
}
Expand Down
20 changes: 16 additions & 4 deletions packages/headers-parser/tests/all.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,29 @@ test.each([
{ for: '/path', values: { test: 'two' } },
],
],
['invalid_mixed', { headersFiles: ['success'], configHeaders: {} }, [{ for: '/path', values: { test: 'one' } }]],
['invalid_mixed', { headersFiles: ['success'], configHeaders: [] }, [{ for: '/path', values: { test: 'one' } }]],
])(`Parses netlify.toml and _headers | %s`, async (_, input, output) => {
const { headers } = await parseHeaders(input)
const { headers } = await parseHeaders({
headersFiles: undefined,
netlifyConfigPath: undefined,
configHeaders: undefined,
minimal: true,
...input,
})
expect(headers).toStrictEqual(output)
})

test.each([
['invalid_config_headers_array', { configHeaders: {} }, /must be an array/],
['invalid_mixed', { headersFiles: ['simple'], configHeaders: {} }, /must be an array/],
])(`Validate syntax errors | %s`, async (_, input, errorMessage) => {
const { errors } = await parseHeaders(input)
])(`Validates syntax errors | %s`, async (_, input, errorMessage) => {
// @ts-expect-error -- Intentional runtime test of invalid input for some reason
const { errors } = await parseHeaders({
headersFiles: undefined,
netlifyConfigPath: undefined,
minimal: true,
...input,
})
expect(errors).not.toHaveLength(0)
expect(errors.some((error) => errorMessage.test(error.message))).toBeTruthy()
})
4 changes: 3 additions & 1 deletion packages/headers-parser/tests/for-regexp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ test.each([
['double_slash_multiple', '//a//b//', /^\/a\/b\/?$/iu],
])(`Add forRegExp when minimal is false | %s`, async (_, forPath, forRegExp) => {
const { headers } = await parseHeaders({
headersFiles: undefined,
netlifyConfigPath: undefined,
configHeaders: [{ for: forPath, values: { test: 'one' } }],
minimal: undefined,
minimal: false,
})
expect(headers).toStrictEqual([{ for: forPath.trim(), forRegExp, values: { test: 'one' } }])
})
14 changes: 6 additions & 8 deletions packages/headers-parser/tests/helpers/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,18 @@ export const parseHeaders = async function ({
headersFiles,
netlifyConfigPath,
configHeaders,
...input
minimal,
}: {
headersFiles?: undefined | string[]
netlifyConfigPath?: undefined | string
configHeaders?: undefined | MinimalHeader[]
minimal?: undefined | boolean
headersFiles: undefined | string[]
netlifyConfigPath: undefined | string
configHeaders: undefined | MinimalHeader[]
minimal: boolean
}) {
return await parseAllHeaders({
...(headersFiles && { headersFiles: headersFiles.map(addFileFixtureDir) }),
...(netlifyConfigPath && { netlifyConfigPath: addConfigFixtureDir(netlifyConfigPath) }),
configHeaders,
// Default `minimal` to `true` but still allows passing `undefined` to
// test the default value of that option
minimal: 'minimal' in input ? input.minimal : true,
minimal,
})
}

Expand Down
18 changes: 14 additions & 4 deletions packages/headers-parser/tests/line-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ test.each([
['trim_name', { headersFiles: ['trim_name'] }, [{ for: '/path', values: { test: 'one' } }]],
['trim_value', { headersFiles: ['trim_value'] }, [{ for: '/path', values: { test: 'one' } }]],
['multiple_values', { headersFiles: ['multiple_values'] }, [{ for: '/path', values: { test: 'one, two' } }]],
])(`Parses _headers | %s`, async (_, input, output) => {
const { headers } = await parseHeaders(input)
])(`Parses _headers | %s`, async (_, { headersFiles }, output) => {
const { headers } = await parseHeaders({
headersFiles,
netlifyConfigPath: undefined,
configHeaders: undefined,
minimal: true,
})
expect(headers).toStrictEqual(output)
})

Expand All @@ -24,8 +29,13 @@ test.each([
['invalid_value_name', { headersFiles: ['invalid_value_name'] }, /Missing header name/],
['invalid_value_string', { headersFiles: ['invalid_value_string'] }, /Missing header value/],
['invalid_for_order', { headersFiles: ['invalid_for_order'] }, /Path should come before/],
])(`Validate syntax errors | %s`, async (_, input, errorMessage) => {
const { errors } = await parseHeaders(input)
])(`Validate syntax errors | %s`, async (_, { headersFiles }, errorMessage) => {
const { errors } = await parseHeaders({
headersFiles,
netlifyConfigPath: undefined,
configHeaders: undefined,
minimal: true,
})
expect(errors).not.toHaveLength(0)
expect(errors.some((error) => errorMessage.test(error.message))).toBeTruthy()
})
2 changes: 2 additions & 0 deletions packages/headers-parser/tests/merge.bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import { parseHeaders } from './helpers/main.js'
bench('Merges large _headers file with config headers', async () => {
const input = {
headersFiles: ['large_headers_file'],
netlifyConfigPath: undefined,
configHeaders: [
{ for: '/base/some-1', values: { test: 'some-1' } },
{ for: '/unique-header', values: { test: 'unique-value' } },
],
minimal: true,
}
const { headers, errors } = await parseHeaders(input)
expect(errors).toHaveLength(0)
Expand Down
8 changes: 7 additions & 1 deletion packages/headers-parser/tests/merge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ test.each([
],
],
])(`Merges _headers with netlify.toml headers | %s`, async (_, input, output) => {
const { headers } = await parseHeaders(input)
const { headers } = await parseHeaders({
headersFiles: undefined,
configHeaders: undefined,
netlifyConfigPath: undefined,
minimal: true,
...input,
})
expect(headers).toStrictEqual(output)
})
18 changes: 14 additions & 4 deletions packages/headers-parser/tests/netlify-config-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ test.each([
['values_undefined', { netlifyConfigPath: 'values_undefined' }, []],
['value_array', { netlifyConfigPath: 'value_array' }, [{ for: '/path', values: { test: 'one, two' } }]],
['for_path_no_slash', { netlifyConfigPath: 'for_path_no_slash' }, [{ for: 'path', values: { test: 'one' } }]],
])(`Parses netlify.toml headers | %s`, async (_, input, output) => {
const { headers } = await parseHeaders(input)
])(`Parses netlify.toml headers | %s`, async (_, { netlifyConfigPath }, output) => {
const { headers } = await parseHeaders({
headersFiles: undefined,
netlifyConfigPath,
configHeaders: undefined,
minimal: true,
})
expect(headers).toStrictEqual(output)
})

Expand All @@ -29,8 +34,13 @@ test.each([
['invalid_value_name', { netlifyConfigPath: 'invalid_value_name' }, /Empty header name/],
['invalid_value_string', { netlifyConfigPath: 'invalid_value_string' }, /must be a string/],
['invalid_value_array', { netlifyConfigPath: 'invalid_value_array' }, /must be a string/],
])(`Validate syntax errors | %s`, async (_, input, errorMessage) => {
const { errors } = await parseHeaders(input)
])(`Validate syntax errors | %s`, async (_, { netlifyConfigPath }, errorMessage) => {
const { errors } = await parseHeaders({
headersFiles: undefined,
netlifyConfigPath,
configHeaders: undefined,
minimal: true,
})
expect(errors).not.toHaveLength(0)
expect(errors.some((error) => errorMessage.test(error.message))).toBeTruthy()
})

0 comments on commit e339cc6

Please sign in to comment.