Skip to content

Commit

Permalink
feat: add x-request-id to the response (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmartel authored Nov 28, 2024
1 parent 182dbb9 commit e1da87e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,13 @@ export class Response extends Macroable {
return
}

/*
* ----------------------------------------
* SET X-REQUEST-ID HEADER
* ----------------------------------------
*/
this.setRequestId()

/*
* ----------------------------------------
* SET CONTENT-LENGTH HEADER
Expand Down Expand Up @@ -779,6 +786,18 @@ export class Response extends Macroable {
return this
}

/**
* Set X-Request-Id header by copying the header value from the request if it exists.
*
*/
setRequestId(): this {
const requestId = this.request.headers['x-request-id']
if (requestId) {
this.header('X-Request-Id', requestId)
}
return this
}

/**
* Returns a boolean telling if the new response etag evaluates same
* as the request header `if-none-match`. In case of `true`, the
Expand Down
12 changes: 12 additions & 0 deletions tests/response.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ test.group('Response', (group) => {
})
})

test('set x-request-id header', async () => {
const { url } = await httpServer.create((req, res) => {
req.headers['x-request-id'] = '20241127'
const response = new ResponseFactory().merge({ req, res, encryption, router }).create()

response.send('<p> hello </p>')
response.finish()
})

await supertest(url).get('/').expect(200).expect('x-request-id', '20241127')
})

test('get merged from http res object', async ({ assert }) => {
const { url } = await httpServer.create((req, res) => {
res.setHeader('content-type', 'application/json')
Expand Down

0 comments on commit e1da87e

Please sign in to comment.