Skip to content

Commit 647d400

Browse files
Harminder VirkHarminder Virk
Harminder Virk
authored and
Harminder Virk
committed
feat: add response.onFinish method
Closes: #82
1 parent bdd97c9 commit 647d400

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/response.ts

+11
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,17 @@ export class Response extends Macroable {
575575
}
576576
}
577577

578+
/**
579+
* Listen for the event the response is written
580+
* to the TCP socket.
581+
*
582+
* Under the hood the callback is registered with
583+
* the "https://github.com/jshttp/on-finished" package
584+
*/
585+
onFinish(callback: (err: Error | null, response: ServerResponse) => void) {
586+
onFinished(this.response, callback)
587+
}
588+
578589
/**
579590
* Writes headers with the Node.js res object using the
580591
* response.setHeader method

tests/response.spec.ts

+14
Original file line numberDiff line numberDiff line change
@@ -1510,4 +1510,18 @@ test.group('Response', (group) => {
15101510

15111511
await supertest(url).get('/')
15121512
})
1513+
1514+
test('listen for the event when response finishes', async ({ assert }) => {
1515+
assert.plan(1)
1516+
1517+
const { url } = await httpServer.create((req, res) => {
1518+
const response = new ResponseFactory().merge({ req, res, encryption, router }).create()
1519+
response.onFinish(() => {
1520+
assert.isTrue(true)
1521+
})
1522+
res.end()
1523+
})
1524+
1525+
await supertest(url).get('/').expect(200)
1526+
})
15131527
})

0 commit comments

Comments
 (0)