Skip to content

Commit

Permalink
CustomHeaderSecurityHeaderSecurity with constraints (#2358)
Browse files Browse the repository at this point in the history
Renaming due to #2337
  • Loading branch information
RobinTail authored Jan 31, 2025
1 parent 691c428 commit cda3609
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 24 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## Version 23

### v23.0.0

- Publicly exposed interface `CustomHeaderSecurity` is renamed to `HeaderSecurity`;
- Restrictions have been placed between header names in the middleware `security` declaration and in `input` properties.

## Version 22

### v22.4.1
Expand Down
2 changes: 1 addition & 1 deletion coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export type {
BasicSecurity,
BearerSecurity,
CookieSecurity,
CustomHeaderSecurity,
HeaderSecurity,
InputSecurity,
OAuth2Security,
OpenIdSecurity,
Expand Down
19 changes: 15 additions & 4 deletions src/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import {
} from "@typescript-eslint/utils";

interface Queries {
placeholder: TSESTree.Identifier;
headerSecurity: TSESTree.Identifier;
}

type Listener = keyof Queries;

const queries: Record<Listener, string> = {
placeholder: `${NT.Identifier}`,
headerSecurity: `${NT.Identifier}[name='CustomHeaderSecurity']`,
};

const listen = <
Expand All @@ -33,10 +33,21 @@ const v23 = ESLintUtils.RuleCreator.withoutDocs({
type: "problem",
fixable: "code",
schema: [],
messages: {},
messages: {
change: "change {{ subject }} from {{ from }} to {{ to }}",
},
},
defaultOptions: [],
create: () => listen({ placeholder: () => {} }),
create: (ctx) =>
listen({
headerSecurity: (node) =>
ctx.report({
node,
messageId: "change",
data: { subject: "interface", from: node.name, to: "HeaderSecurity" },
fix: (fixer) => fixer.replaceText(node, "HeaderSecurity"),
}),
}),
});

/**
Expand Down
7 changes: 3 additions & 4 deletions src/security.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ export interface InputSecurity<K extends string> {
name: K;
}

/** @todo add constraints similar to InputSecurity, rename to just HeaderSecurity */
export interface CustomHeaderSecurity {
export interface HeaderSecurity<K extends string> {
type: "header";
name: string;
name: K;
}

export interface CookieSecurity {
Expand Down Expand Up @@ -90,7 +89,7 @@ export type Security<K extends string = string, S extends string = string> =
| BasicSecurity
| BearerSecurity
| InputSecurity<K>
| CustomHeaderSecurity
| HeaderSecurity<K>
| CookieSecurity
| OpenIdSecurity
| OAuth2Security<S>;
2 changes: 1 addition & 1 deletion tests/compat/migration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { readFile } from "node:fs/promises";
describe("Migration", () => {
test("should fix the import", async () => {
const fixed = await readFile("./sample.ts", "utf-8");
expect(fixed).toBe(`placeholder\n`);
expect(fixed).toBe(`const test: HeaderSecurity = {};\n`);
});
});
2 changes: 1 addition & 1 deletion tests/compat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"scripts": {
"preinstall": "rm -rf node_modules",
"pretest": "echo 'placeholder' > sample.ts",
"pretest": "echo 'const test: CustomHeaderSecurity = {};' > sample.ts",
"test": "eslint --fix && vitest --run && rm sample.ts"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,7 @@ exports[`Documentation helpers > depictSecurity() > should depict OAuth2 Securit
]
`;

exports[`Documentation helpers > depictSecurity() > should handle Basic, Bearer and CustomHeader Securities 1`] = `
exports[`Documentation helpers > depictSecurity() > should handle Basic, Bearer and Header Securities 1`] = `
[
[
{
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/__snapshots__/migration.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ exports[`Migration > should consist of one rule being the major version of the p
"defaultOptions": [],
"meta": {
"fixable": "code",
"messages": {},
"messages": {
"change": "change {{ subject }} from {{ from }} to {{ to }}",
},
"schema": [],
"type": "problem",
},
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/documentation-helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@ describe("Documentation helpers", () => {
});

describe("depictSecurity()", () => {
test("should handle Basic, Bearer and CustomHeader Securities", () => {
test("should handle Basic, Bearer and Header Securities", () => {
expect(
depictSecurity([
[{ type: "basic" }, { type: "bearer" }],
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
BearerSecurity,
CommonConfig,
CookieSecurity,
CustomHeaderSecurity,
HeaderSecurity,
Depicter,
FlatObject,
IOSchema,
Expand Down Expand Up @@ -74,7 +74,7 @@ describe("Index Entrypoint", () => {
expectTypeOf<{
type: "header";
name: "some";
}>().toMatchTypeOf<CustomHeaderSecurity>();
}>().toMatchTypeOf<HeaderSecurity<string>>();
expectTypeOf<{ type: "input"; name: "some" }>().toMatchTypeOf<
InputSecurity<string>
>();
Expand Down
25 changes: 18 additions & 7 deletions tests/unit/migration.spec.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
import { RuleTester } from "@typescript-eslint/rule-tester";
import migration from "../../src/migration";
// import parser from "@typescript-eslint/parser";
import parser from "@typescript-eslint/parser";
import { version } from "../../package.json";

RuleTester.afterAll = afterAll;
RuleTester.describe = describe;
RuleTester.it = it;

/*
const tester = new RuleTester({
languageOptions: { parser },
});
*/

describe("Migration", () => {
test("should consist of one rule being the major version of the package", () => {
expect(migration.rules).toHaveProperty(`v${version.split(".")[0]}`);
expect(migration).toMatchSnapshot();
});

/*
tester.run("v23", migration.rules.v23, {
valid: [],
invalid: [],
valid: [`import { HeaderSecurity } from "express-zod-api";`],
invalid: [
{
code: `const security: CustomHeaderSecurity = {};`,
output: `const security: HeaderSecurity = {};`,
errors: [
{
messageId: "change",
data: {
subject: "interface",
from: "CustomHeaderSecurity",
to: "HeaderSecurity",
},
},
],
},
],
});
*/
});

0 comments on commit cda3609

Please sign in to comment.