From 278f43aa0e60b071c416d47f23e99b2a4111fa64 Mon Sep 17 00:00:00 2001 From: nour-borgi Date: Tue, 27 Jun 2023 11:23:56 +0100 Subject: [PATCH 1/2] Return primary response and request body fix --- src/middleware/router.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/middleware/router.js b/src/middleware/router.js index 75c43152..241ea739 100644 --- a/src/middleware/router.js +++ b/src/middleware/router.js @@ -61,6 +61,11 @@ function setKoaResponse(ctx, response) { } } + // Save response body of the primary route + ctx.resBody = Buffer.isBuffer(response.body) + ? response.body.toString() + : response.body + for (const key in response.headers) { const value = response.headers[key] switch (key.toLowerCase()) { @@ -488,6 +493,11 @@ function sendRequest(ctx, route, options) { } } + if (route.primary) { + // Save request body of the primary route + ctx.reqBody = Buffer.isBuffer(ctx.body) ? ctx.body.toString() : ctx.body + } + if (response instanceof Error) { orchestration.error = { message: response.message, @@ -657,7 +667,8 @@ function sendKafkaRequest(ctx, route) { method: ctx.request.method, path: ctx.request.url, headers: ctx.request.headers, - body: ctx.body && ctx.body.toString() + requestBody: ctx.reqBody ?? '', + responseBody: ctx.resBody ?? '' } return producer From 76892afea2e880fcab2abf28b19270a1568b366d Mon Sep 17 00:00:00 2001 From: Ryan Crichton Date: Fri, 13 Sep 2024 14:50:50 +0200 Subject: [PATCH 2/2] Fix implementation of kafka routing: * Add more response details * Ensure request body is available when kafka route is primary * Ensure backwards compatible properties are used --- src/middleware/router.js | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/middleware/router.js b/src/middleware/router.js index b6cc42f7..944897e3 100644 --- a/src/middleware/router.js +++ b/src/middleware/router.js @@ -61,10 +61,14 @@ function setKoaResponse(ctx, response) { } } - // Save response body of the primary route - ctx.resBody = Buffer.isBuffer(response.body) - ? response.body.toString() - : response.body + // Save response details of the primary route + ctx.resDetails = { + status: response.status, + headers: response.headers, + body: Buffer.isBuffer(response.body) + ? response.body.toString() + : response.body + } for (const key in response.headers) { const value = response.headers[key] @@ -299,6 +303,10 @@ function sendRequestToRoutes(ctx, routes, next) { if (route.primary) { ctx.primaryRoute = route + + // Save request body of the primary route + ctx.reqBody = Buffer.isBuffer(ctx.body) ? ctx.body.toString() : ctx.body + promise = sendRequest(ctx, route, options) .then(response => { logger.info(`executing primary route : ${route.name}`) @@ -493,11 +501,6 @@ function sendRequest(ctx, route, options) { } } - if (route.primary) { - // Save request body of the primary route - ctx.reqBody = Buffer.isBuffer(ctx.body) ? ctx.body.toString() : ctx.body - } - if (response instanceof Error) { orchestration.error = { message: response.message, @@ -668,8 +671,8 @@ function sendKafkaRequest(ctx, route) { path: ctx.request.url, pattern: channel.urlPattern, headers: ctx.request.headers, - requestBody: ctx.reqBody ?? '', - responseBody: ctx.resBody ?? '' + body: ctx.reqBody ?? '', + response: ctx.resDetails ?? {} } return producer