Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/// <reference types="node" />

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;
16 changes: 13 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"author": "Douglas Christopher Wilson <[email protected]>",
"license": "MIT",
"repository": "pillarjs/finalhandler",
"type": "commonjs",
"main": "index.js",
"types": "index.d.ts",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"types": "index.d.ts",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe types would be resolved automatically even without this entry.
Just because of the index.* naming

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least in my PR attw resolves them without types in package.json

"dependencies": {
"debug": "^4.4.0",
"encodeurl": "^2.0.0",
Expand All @@ -14,21 +17,27 @@
"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",
"eslint-plugin-markdown": "2.2.1",
"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"
Comment on lines +39 to +40
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"index.js",
"index.d.ts"
"index.js"

],
"engines": {
"node": ">= 0.8"
Expand All @@ -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"
}
}
46 changes: 46 additions & 0 deletions test/types.ts
Original file line number Diff line number Diff line change
@@ -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<IncomingMessage>();
expectTypeOf(res).toEqualTypeOf<ServerResponse>();
},
};

expectTypeOf(options.env).toEqualTypeOf<string | undefined>();

// 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));
});
4 changes: 4 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "@tsconfig/node18/tsconfig.json",
"include": ["index.d.ts", "test/types.ts"]
}
Loading