Skip to content

Commit 76892af

Browse files
committed
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
1 parent 2af9ffd commit 76892af

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

src/middleware/router.js

+14-11
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,14 @@ function setKoaResponse(ctx, response) {
6161
}
6262
}
6363

64-
// Save response body of the primary route
65-
ctx.resBody = Buffer.isBuffer(response.body)
66-
? response.body.toString()
67-
: response.body
64+
// Save response details of the primary route
65+
ctx.resDetails = {
66+
status: response.status,
67+
headers: response.headers,
68+
body: Buffer.isBuffer(response.body)
69+
? response.body.toString()
70+
: response.body
71+
}
6872

6973
for (const key in response.headers) {
7074
const value = response.headers[key]
@@ -299,6 +303,10 @@ function sendRequestToRoutes(ctx, routes, next) {
299303

300304
if (route.primary) {
301305
ctx.primaryRoute = route
306+
307+
// Save request body of the primary route
308+
ctx.reqBody = Buffer.isBuffer(ctx.body) ? ctx.body.toString() : ctx.body
309+
302310
promise = sendRequest(ctx, route, options)
303311
.then(response => {
304312
logger.info(`executing primary route : ${route.name}`)
@@ -493,11 +501,6 @@ function sendRequest(ctx, route, options) {
493501
}
494502
}
495503

496-
if (route.primary) {
497-
// Save request body of the primary route
498-
ctx.reqBody = Buffer.isBuffer(ctx.body) ? ctx.body.toString() : ctx.body
499-
}
500-
501504
if (response instanceof Error) {
502505
orchestration.error = {
503506
message: response.message,
@@ -668,8 +671,8 @@ function sendKafkaRequest(ctx, route) {
668671
path: ctx.request.url,
669672
pattern: channel.urlPattern,
670673
headers: ctx.request.headers,
671-
requestBody: ctx.reqBody ?? '',
672-
responseBody: ctx.resBody ?? ''
674+
body: ctx.reqBody ?? '',
675+
response: ctx.resDetails ?? {}
673676
}
674677

675678
return producer

0 commit comments

Comments
 (0)