Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include x-request-id as header response #95

Closed
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
7 changes: 7 additions & 0 deletions src/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,13 @@ export class Response extends Macroable {
this.safeStatus(204)
}

/**
*
*/
if (this.#config.generateRequestId) {
this.header('X-Request-Id', this.request.headers['x-request-id'])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the X-Request-Id should be set if its available in the request headers, regardless of whether it is enabled within the AdonisJS app or not.

This is because, the frontend might set the header, the backend app never generates it and re-uses the existing header and in that case, we still want the id to exist in the response.

}

const statusCode = this.response.statusCode

/**
Expand Down
2 changes: 2 additions & 0 deletions src/types/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export type ResponseConfig = {
* Options to set cookies
*/
cookie: Partial<CookieOptions>

generateRequestId?: boolean
}

/**
Expand Down
5 changes: 4 additions & 1 deletion tests/server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,7 @@ test.group('Server | force content negotiation', () => {
.merge({
app,
config: {
generateRequestId: true,
useAsyncLocalStorage: true,
},
})
Expand Down Expand Up @@ -1021,12 +1022,14 @@ test.group('Server | force content negotiation', () => {
'Http context is not available outside of an HTTP request'
)

const { body } = await supertest(httpServer).get('/').expect(200)
const { body, headers } = await supertest(httpServer).get('/').expect(200)
assert.deepEqual(body, {
enabled: true,
get: true,
getOrFail: true,
})

assert.exists(headers['x-request-id'])
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was just a quick check, I'll write a new test for the feature

})

test('run a callback outside the ALS context', async ({ assert }) => {
Expand Down