Skip to content

Commit 8ba033d

Browse files
authored
Merge pull request #1197 from jembi/primary-res-req-body-fix
Return primary response and request body fix
2 parents e686ddb + 76892af commit 8ba033d

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/middleware/router.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ function setKoaResponse(ctx, response) {
6161
}
6262
}
6363

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+
}
72+
6473
for (const key in response.headers) {
6574
const value = response.headers[key]
6675
switch (key.toLowerCase()) {
@@ -294,6 +303,10 @@ function sendRequestToRoutes(ctx, routes, next) {
294303

295304
if (route.primary) {
296305
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+
297310
promise = sendRequest(ctx, route, options)
298311
.then(response => {
299312
logger.info(`executing primary route : ${route.name}`)
@@ -658,7 +671,8 @@ function sendKafkaRequest(ctx, route) {
658671
path: ctx.request.url,
659672
pattern: channel.urlPattern,
660673
headers: ctx.request.headers,
661-
body: ctx.body && ctx.body.toString()
674+
body: ctx.reqBody ?? '',
675+
response: ctx.resDetails ?? {}
662676
}
663677

664678
return producer

0 commit comments

Comments
 (0)