From bd97d355f64e5e0f855fe5b5376585ac29284fcb Mon Sep 17 00:00:00 2001 From: Tony Khaov Date: Wed, 27 Nov 2024 00:45:09 +0100 Subject: [PATCH] feat: include x-request-id as headers --- src/response.ts | 7 +++++++ src/types/response.ts | 2 ++ tests/server.spec.ts | 5 ++++- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/response.ts b/src/response.ts index ac6b167..cfad63f 100644 --- a/src/response.ts +++ b/src/response.ts @@ -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']) + } + const statusCode = this.response.statusCode /** diff --git a/src/types/response.ts b/src/types/response.ts index d6a9b3d..b9af06d 100644 --- a/src/types/response.ts +++ b/src/types/response.ts @@ -53,6 +53,8 @@ export type ResponseConfig = { * Options to set cookies */ cookie: Partial + + generateRequestId?: boolean } /** diff --git a/tests/server.spec.ts b/tests/server.spec.ts index 8c4f5af..242727e 100644 --- a/tests/server.spec.ts +++ b/tests/server.spec.ts @@ -994,6 +994,7 @@ test.group('Server | force content negotiation', () => { .merge({ app, config: { + generateRequestId: true, useAsyncLocalStorage: true, }, }) @@ -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']) }) test('run a callback outside the ALS context', async ({ assert }) => {