Skip to content

Commit

Permalink
Merge pull request #1197 from jembi/primary-res-req-body-fix
Browse files Browse the repository at this point in the history
Return primary response and request body fix
  • Loading branch information
rcrichton authored Sep 16, 2024
2 parents e686ddb + 76892af commit 8ba033d
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/middleware/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ function setKoaResponse(ctx, response) {
}
}

// 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]
switch (key.toLowerCase()) {
Expand Down Expand Up @@ -294,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}`)
Expand Down Expand Up @@ -658,7 +671,8 @@ function sendKafkaRequest(ctx, route) {
path: ctx.request.url,
pattern: channel.urlPattern,
headers: ctx.request.headers,
body: ctx.body && ctx.body.toString()
body: ctx.reqBody ?? '',
response: ctx.resDetails ?? {}
}

return producer
Expand Down

0 comments on commit 8ba033d

Please sign in to comment.