Skip to content

Commit c1c12af

Browse files
committed
feat: add types
1 parent a491fef commit c1c12af

File tree

6 files changed

+57
-3
lines changed

6 files changed

+57
-3
lines changed

.github/workflows/ci.yml

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

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

HISTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ unreleased
22
==================
33

44
* Breaking Change: Node.js 18 is the minimum supported version
5+
* Add types
56

67
2.4.1 / 2022-02-22
78
==================

index.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference types="node" />
2+
3+
import { IncomingMessage, ServerResponse } from "node:http";
4+
5+
declare function onFinished<T extends IncomingMessage | ServerResponse>(
6+
msg: T,
7+
listener: (err: Error | null | undefiend, msg: T) => void
8+
): T;
9+
10+
declare namespace onFinished {
11+
function isFinished(msg: IncomingMessage | ServerResponse): boolean;
12+
}
13+
14+
export = onFinished;

package.json

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,39 @@
88
],
99
"license": "MIT",
1010
"repository": "jshttp/on-finished",
11+
"type": "commonjs",
12+
"main": "index.js",
13+
"types": "index.d.ts",
1114
"devDependencies": {
15+
"@arethetypeswrong/cli": "^0.18.2",
16+
"@tsconfig/node18": "^18.2.4",
17+
"@types/node": "^18.19.120",
1218
"eslint": "^8.57.1",
1319
"eslint-config-standard": "^14.1.1",
1420
"eslint-plugin-import": "^2.31.0",
1521
"eslint-plugin-markdown": "^2.2.1",
1622
"eslint-plugin-node": "^11.1.0",
1723
"eslint-plugin-promise": "^7.2.1",
1824
"eslint-plugin-standard": "^4.1.0",
25+
"expect-type": "^1.2.1",
1926
"mocha": "^11.7.0",
20-
"nyc": "^17.1.0"
27+
"nyc": "^17.1.0",
28+
"typescript": "^5.8.3"
2129
},
2230
"engines": {
2331
"node": ">=18"
2432
},
2533
"files": [
2634
"HISTORY.md",
2735
"LICENSE",
28-
"index.js"
36+
"index.js",
37+
"index.d.ts"
2938
],
3039
"scripts": {
3140
"lint": "eslint .",
3241
"test": "mocha --reporter spec --check-leaks test/",
3342
"test-ci": "nyc --reporter=lcovonly --reporter=text npm test",
34-
"test-cov": "nyc --reporter=html --reporter=text npm test"
43+
"test-cov": "nyc --reporter=html --reporter=text npm test",
44+
"test-types": "tsc --noEmit && attw --pack"
3545
}
3646
}

test/types.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { IncomingMessage, ServerResponse, createServer } from "node:http";
2+
import { expectTypeOf } from "expect-type";
3+
import onFinished, { isFinished } from "..";
4+
5+
createServer((req, res) => {
6+
onFinished(req, (err, req) => {
7+
expectTypeOf(err).toEqualTypeOf<Error | null | undefined>();
8+
expectTypeOf(req).toEqualTypeOf<IncomingMessage>();
9+
});
10+
11+
onFinished(res, (err, res) => {
12+
expectTypeOf(err).toEqualTypeOf<Error | null | undefined>();
13+
expectTypeOf(res).toEqualTypeOf<
14+
ServerResponse<IncomingMessage> & {
15+
req: IncomingMessage;
16+
}
17+
>();
18+
});
19+
20+
expectTypeOf(isFinished(req)).toEqualTypeOf<boolean>();
21+
expectTypeOf(isFinished(res)).toEqualTypeOf<boolean>();
22+
});

tsconfig.json

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

0 commit comments

Comments
 (0)