Skip to content

Commit d108b2b

Browse files
committed
šŸ› Set a default status code before executing endpoint handler to prevent the overwrite of explicit status code
1 parent 5ffc1f2 commit d108b2b

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

ā€Žstarter/api/rest/src/view/rest/util/openapi.util.tsā€Ž

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,19 +129,21 @@ const asyncHandler =
129129
<TRes>(fn: ApiHandler<TRes>, operationId?: OperationIds): express.Handler =>
130130
async (req, res, next) => {
131131
try {
132-
const result = await fn(req.context, req, res)
133-
134-
if (operationId && !res.headersSent) {
132+
if (operationId) {
135133
const metadata = operationPaths[operationId]
136134
if (metadata?.successStatus) {
137135
res.status(metadata.successStatus)
138136
}
139137
}
140138

141-
if (result != undefined) {
142-
res.json(result)
143-
} else {
144-
res.end()
139+
const result = await fn(req.context, req, res)
140+
141+
if (!res.headersSent) {
142+
if (result != undefined) {
143+
res.json(result)
144+
} else {
145+
res.end()
146+
}
145147
}
146148
} catch (error: unknown) {
147149
next(error)

0 commit comments

Comments
Ā (0)