Skip to content

Commit bc5e35a

Browse files
authored
fix(types)!: fix @netlify/headers-parser types (#6104)
* fix(types): fix @netlify/headers-parser types These types were very weak, containing plentiful `any`s, both explicit and implicit, as well as some incorrect inferred types. * chore(tsconfig): simplify tsconfig strict config We should be opting *into* strict mode and only opting *out* of some flags incrementally while we fix errors. This commit: - flips the global `strict` on - removes values being set to the default via the above - stops disabling flags that obscured no errors (or very few, which I then fixed) - moves a few flag disablings to the specific packages that require it - explictly configures strict flags for already rather strict packages * fix(types): mark netlify.toml [[headers]].values required * style: add empty line after imports * refactor: improve variable name * docs: add inline comment explain funky type * refactor: remove incorrect extraneous property from type * fix(parseAllHeaders)!: mark `minimal` as required All callers already pass in a non-nil `boolean`, so this is only technically breaking. * fix(types)!: increase strictness of optional types * fix(types): add missing PollingStrategy.name type * feat(types): export Category, PollingStrategy
1 parent 2aa50a6 commit bc5e35a

31 files changed

+235
-117
lines changed

packages/build-info/src/frameworks/framework.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ export enum Accuracy {
2020
}
2121

2222
export type PollingStrategy = {
23-
name
23+
// TODO(serhalp) Define an enum
24+
name: string
2425
}
2526

2627
/** Information on how it was detected and how accurate the detection is */

packages/build-info/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export * from './file-system.js'
22
export * from './logger.js'
3-
export { DetectedFramework, FrameworkInfo } from './frameworks/framework.js'
3+
export type { Category, DetectedFramework, FrameworkInfo, PollingStrategy } from './frameworks/framework.js'
44
export * from './get-framework.js'
55
export * from './project.js'
66
export * from './settings/get-build-settings.js'

packages/build-info/src/settings/netlify-toml.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ export type RequestHeaders = {
324324
*/
325325
export type Headers = {
326326
for: For
327-
values?: Values
327+
values: Values
328328
}
329329
/**
330330
* Define the actual headers.

packages/build/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"extends": "../../tsconfig.base.json",
33
"compilerOptions": {
4-
"outDir": "lib" /* Specify an output folder for all emitted files. */
4+
"outDir": "lib" /* Specify an output folder for all emitted files. */,
5+
"strictBindCallApply": false /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
56
},
67
"include": ["src/**/*.js", "src/**/*.ts"],
78
"exclude": ["tests/**"]

packages/config/src/headers.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,11 @@ export const getHeadersPath = function ({ build: { publish } }) {
1212
const HEADERS_FILENAME = '_headers'
1313

1414
// Add `config.headers`
15-
export const addHeaders = async function ({
16-
config: { headers: configHeaders, ...config },
17-
headersPath,
18-
logs,
19-
featureFlags,
20-
}) {
15+
export const addHeaders = async function ({ config: { headers: configHeaders, ...config }, headersPath, logs }) {
2116
const { headers, errors } = await parseAllHeaders({
2217
headersFiles: [headersPath],
2318
configHeaders,
2419
minimal: true,
25-
featureFlags,
2620
})
2721
warnHeadersParsing(logs, errors)
2822
warnHeadersCaseSensitivity(logs, headers)

packages/config/src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ const getFullConfig = async function ({
288288
base: baseA,
289289
} = await resolveFiles({ packagePath, config: configA, repositoryRoot, base, baseRelDir })
290290
const headersPath = getHeadersPath(configB)
291-
const configC = await addHeaders({ config: configB, headersPath, logs, featureFlags })
291+
const configC = await addHeaders({ config: configB, headersPath, logs })
292292
const redirectsPath = getRedirectsPath(configC)
293293
const configD = await addRedirects({ config: configC, redirectsPath, logs, featureFlags })
294294
return { configPath, config: configD, buildDir, base: baseA, redirectsPath, headersPath }

packages/config/src/mutations/update.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export const updateConfig = async function (
3333
const inlineConfig = applyMutations({}, configMutations)
3434
const normalizedInlineConfig = ensureConfigPriority(inlineConfig, context, branch)
3535
const updatedConfig = await mergeWithConfig(normalizedInlineConfig, configPath)
36-
const configWithHeaders = await addHeaders({ config: updatedConfig, headersPath, logs, featureFlags })
36+
const configWithHeaders = await addHeaders({ config: updatedConfig, headersPath, logs })
3737
const finalConfig = await addRedirects({ config: configWithHeaders, redirectsPath, logs, featureFlags })
3838
const simplifiedConfig = simplifyConfig(finalConfig)
3939

packages/headers-parser/src/all.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,22 @@ import { mergeHeaders } from './merge.js'
33
import { parseConfigHeaders } from './netlify_config_parser.js'
44
import { normalizeHeaders } from './normalize.js'
55
import { splitResults, concatResults } from './results.js'
6+
import type { Header, MinimalHeader } from './types.js'
7+
8+
export type { Header, MinimalHeader }
69

710
// Parse all headers from `netlify.toml` and `_headers` file, then normalize
811
// and validate those.
912
export const parseAllHeaders = async function ({
1013
headersFiles = [],
1114
netlifyConfigPath,
1215
configHeaders = [],
13-
minimal = false,
16+
minimal,
17+
}: {
18+
headersFiles: undefined | string[]
19+
netlifyConfigPath?: undefined | string
20+
configHeaders: undefined | MinimalHeader[]
21+
minimal: boolean
1422
}) {
1523
const [
1624
{ headers: fileHeaders, errors: fileParseErrors },
@@ -37,12 +45,12 @@ export const parseAllHeaders = async function ({
3745
return { headers, errors }
3846
}
3947

40-
const getFileHeaders = async function (headersFiles) {
48+
const getFileHeaders = async function (headersFiles: string[]) {
4149
const resultsArrays = await Promise.all(headersFiles.map(parseFileHeaders))
4250
return concatResults(resultsArrays)
4351
}
4452

45-
const getConfigHeaders = async function (netlifyConfigPath) {
53+
const getConfigHeaders = async function (netlifyConfigPath?: string) {
4654
if (netlifyConfigPath === undefined) {
4755
return splitResults([])
4856
}

packages/headers-parser/src/for_regexp.js renamed to packages/headers-parser/src/for_regexp.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import escapeStringRegExp from 'escape-string-regexp'
22

33
// Retrieve `forRegExp` which is a `RegExp` used to match the `for` path
4-
export const getForRegExp = function (forPath) {
4+
export const getForRegExp = function (forPath: string): RegExp {
55
const pattern = forPath.split('/').map(trimString).filter(Boolean).map(getPartRegExp).join('/')
66
return new RegExp(`^/${pattern}/?$`, 'iu')
77
}
88

9-
const trimString = function (part) {
9+
const trimString = function (part: string): string {
1010
return part.trimEnd()
1111
}
1212

13-
const getPartRegExp = function (part) {
13+
const getPartRegExp = function (part: string): string {
1414
// Placeholder like `/segment/:placeholder/test`
1515
// Matches everything up to a /
1616
if (part.startsWith(':')) {

packages/headers-parser/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { parseAllHeaders } from './all.js'
1+
export { parseAllHeaders, type Header, type MinimalHeader } from './all.js'

0 commit comments

Comments
 (0)