Skip to content

Commit c6fed0c

Browse files
committed
feat: add types
1 parent 3b591e5 commit c6fed0c

File tree

5 files changed

+76
-3
lines changed

5 files changed

+76
-3
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ jobs:
3131
- name: Lint code
3232
run: npm run lint
3333

34+
- name: Check types
35+
run: npm run test-types
36+
3437
test:
3538
name: Test - Node.js ${{ matrix.node-version }}
3639
runs-on: ubuntu-latest

package.json

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
"author": "Douglas Christopher Wilson <[email protected]>",
66
"license": "MIT",
77
"repository": "pillarjs/finalhandler",
8+
"type": "commonjs",
9+
"main": "index.js",
10+
"types": "types/index.d.ts",
811
"dependencies": {
912
"debug": "^4.4.0",
1013
"encodeurl": "^2.0.0",
@@ -14,6 +17,9 @@
1417
"statuses": "^2.0.1"
1518
},
1619
"devDependencies": {
20+
"@arethetypeswrong/cli": "^0.17.4",
21+
"@tsconfig/node18": "^18.2.4",
22+
"@types/node": "^22.13.10",
1723
"eslint": "7.32.0",
1824
"eslint-config-standard": "14.1.1",
1925
"eslint-plugin-import": "2.26.0",
@@ -23,12 +29,15 @@
2329
"eslint-plugin-standard": "4.1.0",
2430
"mocha": "^11.0.1",
2531
"nyc": "^17.1.0",
26-
"supertest": "^7.0.0"
32+
"supertest": "^7.0.0",
33+
"tsd": "^0.31.2",
34+
"typescript": "^5.8.2"
2735
},
2836
"files": [
2937
"LICENSE",
3038
"HISTORY.md",
31-
"index.js"
39+
"index.js",
40+
"types/index.d.ts"
3241
],
3342
"engines": {
3443
"node": ">= 0.8"
@@ -38,6 +47,7 @@
3847
"test": "mocha --reporter spec --check-leaks test/",
3948
"test-ci": "nyc --reporter=lcovonly --reporter=text npm test",
4049
"test-cov": "nyc --reporter=html --reporter=text npm test",
41-
"test-inspect": "mocha --reporter spec --inspect --inspect-brk test/"
50+
"test-inspect": "mocha --reporter spec --inspect --inspect-brk test/",
51+
"test-types": "tsd && attw --pack"
4252
}
4353
}

tsconfig.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "@tsconfig/node18/tsconfig.json",
3+
"compilerOptions": {
4+
"noEmit": true
5+
},
6+
"include": ["types/index.d.ts", "types/index.test-d.ts"]
7+
}

types/index.d.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/// <reference types="node" />
2+
3+
import { IncomingMessage, ServerResponse } from "node:http";
4+
5+
declare function finalhandler(
6+
req: IncomingMessage,
7+
res: ServerResponse,
8+
options?: finalhandler.Options
9+
): (err?: any) => void;
10+
11+
declare namespace finalhandler {
12+
interface Options {
13+
env?: string | undefined;
14+
onerror?:
15+
| ((err: any, req: IncomingMessage, res: ServerResponse) => void)
16+
| undefined;
17+
}
18+
}
19+
20+
export = finalhandler;

types/index.test-d.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { IncomingMessage, ServerResponse, createServer } from "node:http";
2+
import { expectType } from "tsd";
3+
import finalhandler, { Options } from "..";
4+
5+
const req: IncomingMessage = {} as IncomingMessage;
6+
const res: ServerResponse = {} as ServerResponse;
7+
8+
const options: Options = {
9+
env: "anEnv",
10+
onerror: (err: any, req: IncomingMessage, res: ServerResponse): any => {
11+
return;
12+
},
13+
};
14+
15+
{
16+
const result = finalhandler(req, res);
17+
expectType<(err?: any) => void>(result);
18+
}
19+
{
20+
const result = finalhandler(req, res, options);
21+
expectType<(err?: any) => void>(result);
22+
}
23+
24+
// serve-static-like request handler
25+
declare function requestHandler(
26+
request: IncomingMessage,
27+
response: ServerResponse,
28+
next: () => void
29+
): any;
30+
31+
createServer((req, res) => {
32+
requestHandler(req, res, finalhandler(req, res));
33+
});

0 commit comments

Comments
 (0)