Skip to content

Commit 1503464

Browse files
committed
fix(express): fix express overriding 2xx status
changed the redirectRequest method so it returns the original status from WP not only the body of the response WP-2961 TICKET: WP-2961
1 parent da9401e commit 1503464

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

modules/express/src/clientRoutes.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,7 +1142,11 @@ function redirectRequest(bitgo: BitGo, method: string, url: string, req: express
11421142
if (req.params.enterpriseId) {
11431143
request.set('enterprise-id', req.params.enterpriseId);
11441144
}
1145-
return request.result();
1145+
1146+
return request.result().then((result) => {
1147+
const status = request.res?.statusCode || 200;
1148+
return { status, body: result };
1149+
});
11461150
}
11471151

11481152
// something has presumably gone wrong
@@ -1217,8 +1221,7 @@ function prepareBitGo(config: Config) {
12171221
next();
12181222
};
12191223
}
1220-
1221-
type RequestHandlerResponse = string | unknown | undefined;
1224+
type RequestHandlerResponse = string | unknown | undefined | { status: number; body: unknown };
12221225
interface RequestHandler extends express.RequestHandler<ParamsDictionary, any, RequestHandlerResponse> {
12231226
(req: express.Request, res: express.Response, next: express.NextFunction):
12241227
| RequestHandlerResponse
@@ -1265,7 +1268,12 @@ function promiseWrapper(promiseRequestHandler: RequestHandler) {
12651268
debug(`handle: ${req.method} ${req.originalUrl}`);
12661269
try {
12671270
const result = await promiseRequestHandler(req, res, next);
1268-
res.status(200).send(result);
1271+
if (typeof result === 'object' && result !== null && 'body' in result && 'status' in result) {
1272+
const { status, body } = result as { status: number; body: unknown };
1273+
res.status(status).send(body);
1274+
} else {
1275+
res.status(200).send(result);
1276+
}
12691277
} catch (e) {
12701278
handleRequestHandlerError(res, e);
12711279
}

0 commit comments

Comments
 (0)