From b52ea90f2a318d8a04523c664ecdf21dbc8e6175 Mon Sep 17 00:00:00 2001 From: Phillip9587 Date: Wed, 26 Mar 2025 21:25:14 +0100 Subject: [PATCH 1/2] feat: add types --- .github/workflows/ci.yml | 3 +++ index.d.ts | 20 +++++++++++++++++ package.json | 16 +++++++++++--- test/types.ts | 46 ++++++++++++++++++++++++++++++++++++++++ tsconfig.json | 4 ++++ 5 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 index.d.ts create mode 100644 test/types.ts create mode 100644 tsconfig.json diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8414227..2e9ac29 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -31,6 +31,9 @@ jobs: - name: Lint code run: npm run lint + - name: Check types + run: npm run test-types + test: name: Test - Node.js ${{ matrix.node-version }} runs-on: ubuntu-latest diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..48db119 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,20 @@ +/// + +import { IncomingMessage, ServerResponse } from "node:http"; + +declare function finalhandler( + req: IncomingMessage, + res: ServerResponse, + options?: finalhandler.Options +): (err?: any) => void; + +declare namespace finalhandler { + interface Options { + env?: string | undefined; + onerror?: + | ((err: any, req: IncomingMessage, res: ServerResponse) => void) + | undefined; + } +} + +export = finalhandler; diff --git a/package.json b/package.json index 992b306..0a63344 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,9 @@ "author": "Douglas Christopher Wilson ", "license": "MIT", "repository": "pillarjs/finalhandler", + "type": "commonjs", + "main": "index.js", + "types": "types/index.d.ts", "dependencies": { "debug": "^4.4.0", "encodeurl": "^2.0.0", @@ -14,6 +17,9 @@ "statuses": "^2.0.1" }, "devDependencies": { + "@arethetypeswrong/cli": "^0.17.4", + "@tsconfig/node18": "^18.2.4", + "@types/node": "^22.13.10", "eslint": "7.32.0", "eslint-config-standard": "14.1.1", "eslint-plugin-import": "2.26.0", @@ -21,14 +27,17 @@ "eslint-plugin-node": "11.1.0", "eslint-plugin-promise": "5.2.0", "eslint-plugin-standard": "4.1.0", + "expect-type": "^1.2.0", "mocha": "^11.0.1", "nyc": "^17.1.0", - "supertest": "^7.0.0" + "supertest": "^7.0.0", + "typescript": "^5.8.2" }, "files": [ "LICENSE", "HISTORY.md", - "index.js" + "index.js", + "index.d.ts" ], "engines": { "node": ">= 0.8" @@ -38,6 +47,7 @@ "test": "mocha --reporter spec --check-leaks test/", "test-ci": "nyc --reporter=lcovonly --reporter=text npm test", "test-cov": "nyc --reporter=html --reporter=text npm test", - "test-inspect": "mocha --reporter spec --inspect --inspect-brk test/" + "test-inspect": "mocha --reporter spec --inspect --inspect-brk test/", + "test-types": "tsc --noEmit && attw --pack" } } diff --git a/test/types.ts b/test/types.ts new file mode 100644 index 0000000..11461fa --- /dev/null +++ b/test/types.ts @@ -0,0 +1,46 @@ +import { IncomingMessage, ServerResponse, createServer } from "node:http"; +import { expectTypeOf } from "expect-type"; +import finalhandler, { Options } from ".."; + +const req = {} as IncomingMessage; +const res = {} as ServerResponse; + +const options: Options = { + env: "anEnv", + onerror: (err, req, res) => { + expectTypeOf(err).toBeAny(); + expectTypeOf(req).toEqualTypeOf(); + expectTypeOf(res).toEqualTypeOf(); + }, +}; + +expectTypeOf(options.env).toEqualTypeOf(); + +// finalhandler without options +{ + const result = finalhandler(req, res); + expectTypeOf(result).toBeFunction(); + expectTypeOf(result).parameters.toEqualTypeOf<[any?]>(); + expectTypeOf(result).returns.toBeVoid(); + expectTypeOf(result).toBeCallableWith(new Error()); +} + +// finalhandler with options +{ + const result = finalhandler(req, res, options); + expectTypeOf(result).toBeFunction(); + expectTypeOf(result).parameters.toEqualTypeOf<[any?]>(); + expectTypeOf(result).returns.toBeVoid(); + expectTypeOf(result).toBeCallableWith(new Error()); +} + +// serve-static-like request handler +declare function requestHandler( + request: IncomingMessage, + response: ServerResponse, + next: (err?: any) => void +): any; + +createServer((req, res) => { + requestHandler(req, res, finalhandler(req, res)); +}); diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..30c279c --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,4 @@ +{ + "extends": "@tsconfig/node18/tsconfig.json", + "include": ["index.d.ts", "test/types.ts"] +} From 2432dca889070f89747eb425b181895623c7f9a1 Mon Sep 17 00:00:00 2001 From: Phillip Barta Date: Thu, 27 Mar 2025 08:04:24 +0100 Subject: [PATCH 2/2] correct types field Co-authored-by: Anna Bocharova --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0a63344..858a03b 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "repository": "pillarjs/finalhandler", "type": "commonjs", "main": "index.js", - "types": "types/index.d.ts", + "types": "index.d.ts", "dependencies": { "debug": "^4.4.0", "encodeurl": "^2.0.0",