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 @@ -29,6 +29,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
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ unreleased

* Breaking Change: Node.js 18 is the minimum supported version
* Validate that passed `listener` is a function
* Add types

2.4.1 / 2022-02-22
==================
Expand Down
14 changes: 14 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// <reference types="node" />

import { IncomingMessage, ServerResponse } from "node:http";

declare function onFinished<T extends IncomingMessage | ServerResponse>(
msg: T,
listener: (err: Error | null | undefined, msg: T) => void
): T;

declare namespace onFinished {
function isFinished(msg: IncomingMessage | ServerResponse): boolean | undefined;
}

export = onFinished;
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function onFinished (msg, listener) {
* Determine if message is already finished.
*
* @param {object} msg
* @return {boolean}
* @return {boolean | undefined}
* @public
*/

Expand Down
16 changes: 13 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,39 @@
],
"license": "MIT",
"repository": "jshttp/on-finished",
"type": "commonjs",
"main": "index.js",
"types": "index.d.ts",
"devDependencies": {
"@arethetypeswrong/cli": "^0.18.2",
"@tsconfig/node18": "^18.2.4",
"@types/node": "^18.19.122",
"eslint": "^8.57.1",
"eslint-config-standard": "^14.1.1",
"eslint-plugin-import": "^2.31.0",
"eslint-plugin-markdown": "^2.2.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^7.2.1",
"eslint-plugin-standard": "^4.1.0",
"expect-type": "^1.2.2",
"mocha": "^11.7.0",
"nyc": "^17.1.0"
"nyc": "^17.1.0",
"typescript": "^5.9.2"
},
"engines": {
"node": ">=18"
},
"files": [
"HISTORY.md",
"LICENSE",
"index.js"
"index.js",
"index.d.ts"
],
"scripts": {
"lint": "eslint .",
"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-cov": "nyc --reporter=html --reporter=text npm test",
"test-types": "tsc --noEmit && attw --pack"
}
}
18 changes: 18 additions & 0 deletions test/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { IncomingMessage, ServerResponse, createServer } from "node:http";
import { expectTypeOf } from "expect-type";
import onFinished, { isFinished } from "..";

createServer((req, res) => {
onFinished(req, (err, req) => {
expectTypeOf(err).toEqualTypeOf<Error | null | undefined>();
expectTypeOf(req).toEqualTypeOf<IncomingMessage>();
});

onFinished(res, (err, res) => {
expectTypeOf(err).toEqualTypeOf<Error | null | undefined>();
expectTypeOf(res).toExtend<ServerResponse>();
});

expectTypeOf(isFinished(req)).toEqualTypeOf<boolean | undefined>();
expectTypeOf(isFinished(res)).toEqualTypeOf<boolean | undefined>();
});
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