Skip to content

Commit

Permalink
fix: enforce type imports (#600)
Browse files Browse the repository at this point in the history
  • Loading branch information
prescottprue authored Nov 20, 2024
1 parent cfd9066 commit 53c55a3
Show file tree
Hide file tree
Showing 42 changed files with 245 additions and 2,034 deletions.
5 changes: 0 additions & 5 deletions .eslintignore

This file was deleted.

49 changes: 0 additions & 49 deletions .eslintrc.js

This file was deleted.

49 changes: 49 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
"vcs": {
"enabled": false,
"clientKind": "git",
"useIgnoreFile": false
},
"files": {
"ignoreUnknown": false,
"ignore": [".yarn", "node_modules", "coverage", "esm", "dist"]
},
"formatter": {
"enabled": true,
"indentStyle": "space"
},
"organizeImports": {
"enabled": true
},
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
},
"javascript": {
"formatter": {
"quoteStyle": "single"
}
},
"overrides": [
{
"include": ["**/package.json"],
"formatter": {
"lineWidth": 1
}
},
{
"include": ["src/**/*.test.ts", "src/**/__mocks__/**"],
"linter": {
"rules": {
"suspicious": {
"noImplicitAnyLet": "off",
"noExplicitAny": "off"
}
}
}
}
]
}
21 changes: 7 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,24 @@
"esm"
],
"scripts": {
"lint": "eslint . --ignore-pattern='!.eslintrc.js' --ext .ts,.js --max-warnings 0",
"lint": "biome lint",
"types:check": "tsc --noEmit",
"test": "vitest",
"test:cov": "yarn test --coverage",
"build": "yarn build:lib && yarn build:esm",
"build:lib": "rimraf dist && tsc -m commonjs",
"build:esm": "rimraf esm && tsc -m ESNext --outDir esm",
"prepare": "husky",
"format": "prettier --write .",
"format:check": "prettier --check ."
"format": "biome format --write",
"format:check": "biome format"
},
"devDependencies": {
"@biomejs/biome": "1.9.4",
"@commitlint/cli": "19.6.0",
"@commitlint/config-conventional": "19.6.0",
"@side/commitlint-config": "1.1.0",
"@side/eslint-config-base": "2.3.1",
"@side/prettier-config": "1.1.0",
"@types/node": "20.17.6",
"@typescript-eslint/eslint-plugin": "8.15.0",
"@typescript-eslint/parser": "8.15.0",
"@vitest/coverage-v8": "2.1.5",
"eslint": "8.57.1",
"eslint-config-prettier": "9.1.0",
"husky": "9.1.7",
"lint-staged": "15.2.10",
"prettier": "3.3.3",
Expand All @@ -58,11 +53,9 @@
},
"prettier": "@side/prettier-config",
"lint-staged": {
"*.{js,jsx,ts,tsx}": [
"eslint --fix --max-warnings 0",
"prettier --write"
],
"*.{json,md,yaml,yml,graphql}": "prettier --write"
"*.{json,md,yaml,yml,js,ts}": [
"biome check --no-errors-on-unmatched --write"
]
},
"packageManager": "yarn@4.5.1"
}
28 changes: 15 additions & 13 deletions src/__tests__/directives/validation.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it, expect } from 'vitest';
import { describe, expect, it } from 'vitest';
import { directiveValidation } from '../../directives/validation';

type FastifySettingPlaceholder = Record<string, string[] | boolean | number>;
Expand Down Expand Up @@ -32,7 +32,7 @@ describe('./src/directives/validation.ts', () => {
'ONE',
),
).toThrowErrorMatchingInlineSnapshot(
`[Error: Header-Name only allows one selection. You can only specify one option for this header.]`,
'[Error: Header-Name only allows one selection. You can only specify one option for this header.]',
);
});

Expand All @@ -46,7 +46,7 @@ describe('./src/directives/validation.ts', () => {
'ONE',
),
).toThrowErrorMatchingInlineSnapshot(
`[Error: Header-Name.testDirective array must contain non-empty strings.]`,
'[Error: Header-Name.testDirective array must contain non-empty strings.]',
);
});

Expand All @@ -60,7 +60,7 @@ describe('./src/directives/validation.ts', () => {
'ONE',
),
).toThrowErrorMatchingInlineSnapshot(
`[Error: Header-Name.testDirective array must contain non-empty strings.]`,
'[Error: Header-Name.testDirective array must contain non-empty strings.]',
);
});

Expand All @@ -74,7 +74,7 @@ describe('./src/directives/validation.ts', () => {
'ONE',
),
).toThrowErrorMatchingInlineSnapshot(
`[Error: Header-Name.testDirective must be set to a number greater than 0 and less than infinite.]`,
'[Error: Header-Name.testDirective must be set to a number greater than 0 and less than infinite.]',
);
});

Expand All @@ -84,11 +84,13 @@ describe('./src/directives/validation.ts', () => {
});
expect(() =>
headerValidation.checkForValidity(
{ testDirective: Infinity } as FastifySettingPlaceholder,
{
testDirective: Number.POSITIVE_INFINITY,
} as FastifySettingPlaceholder,
'ONE',
),
).toThrowErrorMatchingInlineSnapshot(
`[Error: Header-Name.testDirective must be set to a number greater than 0 and less than infinite.]`,
'[Error: Header-Name.testDirective must be set to a number greater than 0 and less than infinite.]',
);
});

Expand All @@ -102,7 +104,7 @@ describe('./src/directives/validation.ts', () => {
'ONE',
),
).toThrowErrorMatchingInlineSnapshot(
`[Error: Header-Name.testDirective value of directive is invalid. Directive value cannot contain ;|,: ";|"]`,
'[Error: Header-Name.testDirective value of directive is invalid. Directive value cannot contain ;|,: ";|"]',
);
});
});
Expand All @@ -112,22 +114,22 @@ describe('./src/directives/validation.ts', () => {
expect(() =>
directiveValidation(undefined as any, { allowedDirectives: [] }),
).toThrowErrorMatchingInlineSnapshot(
`[Error: headerName can only be a non-empty string]`,
'[Error: headerName can only be a non-empty string]',
);
});
it('fails when the header name is not a string', () => {
expect(() =>
directiveValidation({} as any, { allowedDirectives: [] }),
).toThrowErrorMatchingInlineSnapshot(
`[Error: headerName can only be a non-empty string]`,
'[Error: headerName can only be a non-empty string]',
);
});

it('fails when the header name is an empty string', () => {
expect(() =>
directiveValidation('', { allowedDirectives: [] }),
).toThrowErrorMatchingInlineSnapshot(
`[Error: headerName can only be a non-empty string]`,
'[Error: headerName can only be a non-empty string]',
);
});

Expand All @@ -137,7 +139,7 @@ describe('./src/directives/validation.ts', () => {
allowedDirectives: 'incorrect-format',
} as any),
).toThrowErrorMatchingInlineSnapshot(
`[Error: Allowable directives for Header-Name must be an array. incorrect-format is invalid.]`,
'[Error: Allowable directives for Header-Name must be an array. incorrect-format is invalid.]',
);
});

Expand All @@ -154,7 +156,7 @@ describe('./src/directives/validation.ts', () => {
'ONE',
),
).toThrowErrorMatchingInlineSnapshot(
`[Error: Header-Name does not support the "not-allowed". It is not in the specification.]`,
'[Error: Header-Name does not support the "not-allowed". It is not in the specification.]',
);
});
});
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/headers/cross-origin-opener-policy.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it, expect } from 'vitest';
import { describe, expect, it } from 'vitest';
import { fortifyHeaders } from '../..';

describe('Cross-Origin-Opener-Policy Tests', () => {
Expand Down Expand Up @@ -72,7 +72,7 @@ describe('Cross-Origin-Opener-Policy Tests', () => {
{ useDefaults: false },
),
).toThrowErrorMatchingInlineSnapshot(
`[Error: Cross-Origin-Opener-Policy only allows one selection. You can only specify one option for this header.]`,
'[Error: Cross-Origin-Opener-Policy only allows one selection. You can only specify one option for this header.]',
);
});
});
4 changes: 2 additions & 2 deletions src/__tests__/headers/cross-origin-resource-policy.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it, expect } from 'vitest';
import { describe, expect, it } from 'vitest';
import { fortifyHeaders } from '../..';

describe('Cross-Origin-Resource-Policy Tests', () => {
Expand Down Expand Up @@ -72,7 +72,7 @@ describe('Cross-Origin-Resource-Policy Tests', () => {
{ useDefaults: false },
),
).toThrowErrorMatchingInlineSnapshot(
`[Error: Cross-Origin-Resource-Policy only allows one selection. You can only specify one option for this header.]`,
'[Error: Cross-Origin-Resource-Policy only allows one selection. You can only specify one option for this header.]',
);
});
});
4 changes: 2 additions & 2 deletions src/__tests__/headers/x-dns-prefetch-control.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it, expect } from 'vitest';
import { describe, expect, it } from 'vitest';
import { fortifyHeaders } from '../..';

describe('X-DNS-Prefetch-Control Tests', () => {
Expand Down Expand Up @@ -57,7 +57,7 @@ describe('X-DNS-Prefetch-Control Tests', () => {
{ useDefaults: false },
),
).toThrowErrorMatchingInlineSnapshot(
`[Error: X-Dns-Prefetch-Control only allows one selection. You can only specify one option for this header.]`,
'[Error: X-Dns-Prefetch-Control only allows one selection. You can only specify one option for this header.]',
);
});
});
4 changes: 2 additions & 2 deletions src/__tests__/headers/x-frame-options.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it, expect } from 'vitest';
import { describe, expect, it } from 'vitest';
import { fortifyHeaders } from '../..';

describe('X-Frame-Options Tests', () => {
Expand Down Expand Up @@ -72,7 +72,7 @@ describe('X-Frame-Options Tests', () => {
{ useDefaults: false },
),
).toThrowErrorMatchingInlineSnapshot(
`[Error: X-Frame-Options only allows one selection. You can only specify one option for this header.]`,
'[Error: X-Frame-Options only allows one selection. You can only specify one option for this header.]',
);
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it, expect } from 'vitest';
import { describe, expect, it } from 'vitest';
import { fortifyHeaders } from '../..';

describe('X-Permitted-Cross-Domain-Policies Tests', () => {
Expand Down Expand Up @@ -87,7 +87,7 @@ describe('X-Permitted-Cross-Domain-Policies Tests', () => {
{ useDefaults: false },
),
).toThrowErrorMatchingInlineSnapshot(
`[Error: X-Permitted-Cross-Domain-Policies only allows one selection. You can only specify one option for this header.]`,
'[Error: X-Permitted-Cross-Domain-Policies only allows one selection. You can only specify one option for this header.]',
);
});
});
4 changes: 2 additions & 2 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it, expect } from 'vitest';
import { describe, expect, it } from 'vitest';
import { fortifyHeaders } from '..';

describe('fortify-core entrypoint tests', () => {
Expand Down Expand Up @@ -108,7 +108,7 @@ describe('fortify-core entrypoint tests', () => {
}),
),
).toThrowErrorMatchingInlineSnapshot(
`[Error: unknownHeader is not a supported header]`,
'[Error: unknownHeader is not a supported header]',
);
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/directives/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FortifyHeader } from '../headers/types';
import type { FortifyHeader } from '../headers/types';

/**
* Applies a given set of default properties to header setting of T
Expand Down
5 changes: 3 additions & 2 deletions src/directives/validation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FortifyHeader } from '../headers/types';
import type { FortifyHeader } from '../headers/types';
import { camelcaseToKebab } from './normalize';
import { SelectionType, ValidationSettings } from './types';
import type { SelectionType, ValidationSettings } from './types';

/**
* Checks a string directiveValue for invalid characters
Expand Down Expand Up @@ -132,6 +132,7 @@ export function directiveValidation(
}

if (Array.isArray(directiveToken)) {
// biome-ignore lint/complexity/noForEach: Small list
directiveToken.forEach((token) => {
const isInvalid = containsInvalidCharacters(token);
if (isInvalid) {
Expand Down
8 changes: 6 additions & 2 deletions src/fortifyHeaders.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { FortifyHeaders, FortifySettings, GenerationOptions } from './types';
import { toHeaderCasing } from './directives/normalize';
import { getAllHeaders } from './headers';
import { FortifyHeader, HeaderFunction } from './headers/types';
import type { FortifyHeader, HeaderFunction } from './headers/types';
import type {
FortifyHeaders,
FortifySettings,
GenerationOptions,
} from './types';

/**
* Builds out a configuration that will generate the defaults. Defaults are generated
Expand Down
Loading

0 comments on commit 53c55a3

Please sign in to comment.