Skip to content

Commit

Permalink
feat: add response.onFinish method
Browse files Browse the repository at this point in the history
Closes: #82
  • Loading branch information
Harminder Virk authored and Harminder Virk committed Mar 29, 2024
1 parent bdd97c9 commit 647d400
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,17 @@ export class Response extends Macroable {
}
}

/**
* Listen for the event the response is written
* to the TCP socket.
*
* Under the hood the callback is registered with
* the "https://github.com/jshttp/on-finished" package
*/
onFinish(callback: (err: Error | null, response: ServerResponse) => void) {
onFinished(this.response, callback)
}

/**
* Writes headers with the Node.js res object using the
* response.setHeader method
Expand Down
14 changes: 14 additions & 0 deletions tests/response.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1510,4 +1510,18 @@ test.group('Response', (group) => {

await supertest(url).get('/')
})

test('listen for the event when response finishes', async ({ assert }) => {
assert.plan(1)

const { url } = await httpServer.create((req, res) => {
const response = new ResponseFactory().merge({ req, res, encryption, router }).create()
response.onFinish(() => {
assert.isTrue(true)
})
res.end()
})

await supertest(url).get('/').expect(200)
})
})

0 comments on commit 647d400

Please sign in to comment.