Skip to content

Commit 2f47650

Browse files
committed
fix: Properly set ctx.response to a Response when returning a value
from handlers.
1 parent 4e55f4d commit 2f47650

3 files changed

Lines changed: 133 additions & 65 deletions

File tree

src/internal/__tests__/compile-fetch-function.test.ts

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,24 @@ describe("compileFetchFunction", () => {
3939
error instanceof utils.HttpError
4040
? error.status
4141
: utils.HttpStatus.InternalServerError;
42-
return (ctx.response = Response.json(utils.serializeErrorResponse(error), { status }));
42+
return (
43+
ctx.response = Response.json(
44+
utils.serializeErrorResponse(error),
45+
{ status, headers: ctx.set.headers },
46+
)
47+
);
4348
});
4449
} catch (error) {
4550
const status =
4651
error instanceof utils.HttpError
4752
? error.status
4853
: utils.HttpStatus.InternalServerError;
49-
return (ctx.response = Response.json(utils.serializeErrorResponse(error), { status }));
54+
return (
55+
ctx.response = Response.json(
56+
utils.serializeErrorResponse(error),
57+
{ status, headers: ctx.set.headers },
58+
)
59+
);
5060
}
5161
}"
5262
`);
@@ -97,14 +107,24 @@ describe("compileFetchFunction", () => {
97107
error instanceof utils.HttpError
98108
? error.status
99109
: utils.HttpStatus.InternalServerError;
100-
return (ctx.response = Response.json(utils.serializeErrorResponse(error), { status }));
110+
return (
111+
ctx.response = Response.json(
112+
utils.serializeErrorResponse(error),
113+
{ status, headers: ctx.set.headers },
114+
)
115+
);
101116
});
102117
} catch (error) {
103118
const status =
104119
error instanceof utils.HttpError
105120
? error.status
106121
: utils.HttpStatus.InternalServerError;
107-
return (ctx.response = Response.json(utils.serializeErrorResponse(error), { status }));
122+
return (
123+
ctx.response = Response.json(
124+
utils.serializeErrorResponse(error),
125+
{ status, headers: ctx.set.headers },
126+
)
127+
);
108128
}
109129
}"
110130
`);
@@ -147,7 +167,12 @@ describe("compileFetchFunction", () => {
147167
error instanceof utils.HttpError
148168
? error.status
149169
: utils.HttpStatus.InternalServerError;
150-
return (ctx.response = Response.json(utils.serializeErrorResponse(error), { status }));
170+
return (
171+
ctx.response = Response.json(
172+
utils.serializeErrorResponse(error),
173+
{ status, headers: ctx.set.headers },
174+
)
175+
);
151176
}).finally(() => {
152177
setTimeout(() => {
153178
utils.hooks.onGlobalAfterResponse[0].callback(ctx);
@@ -158,7 +183,12 @@ describe("compileFetchFunction", () => {
158183
error instanceof utils.HttpError
159184
? error.status
160185
: utils.HttpStatus.InternalServerError;
161-
return (ctx.response = Response.json(utils.serializeErrorResponse(error), { status }));
186+
return (
187+
ctx.response = Response.json(
188+
utils.serializeErrorResponse(error),
189+
{ status, headers: ctx.set.headers },
190+
)
191+
);
162192
} finally {
163193
setTimeout(() => {
164194
utils.hooks.onGlobalAfterResponse[0].callback(ctx);
@@ -209,7 +239,12 @@ describe("compileFetchFunction", () => {
209239
error instanceof utils.HttpError
210240
? error.status
211241
: utils.HttpStatus.InternalServerError;
212-
return (ctx.response = Response.json(utils.serializeErrorResponse(error), { status }));
242+
return (
243+
ctx.response = Response.json(
244+
utils.serializeErrorResponse(error),
245+
{ status, headers: ctx.set.headers },
246+
)
247+
);
213248
});
214249
} catch (error) {
215250
ctx.error = error;
@@ -219,7 +254,12 @@ describe("compileFetchFunction", () => {
219254
error instanceof utils.HttpError
220255
? error.status
221256
: utils.HttpStatus.InternalServerError;
222-
return (ctx.response = Response.json(utils.serializeErrorResponse(error), { status }));
257+
return (
258+
ctx.response = Response.json(
259+
utils.serializeErrorResponse(error),
260+
{ status, headers: ctx.set.headers },
261+
)
262+
);
223263
}
224264
}"
225265
`);

src/internal/__tests__/compile-route-handler.test.ts

Lines changed: 72 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,22 @@ describe("compileRouteHandler", () => {
4545
}
4646
4747
if (ctx.response == null) {
48-
return new Response(undefined, {
49-
status: ctx.set.status,
50-
headers: ctx.set.headers,
51-
})
48+
return (
49+
ctx.response = new Response(undefined, {
50+
status: ctx.set.status,
51+
headers: ctx.set.headers,
52+
})
53+
)
5254
}
5355
5456
const serialized = utils.smartSerialize(ctx.response);
5557
if (!ctx.set.headers["Content-Type"]) ctx.set.headers["Content-Type"] = serialized.contentType
56-
return new Response(serialized.value, {
57-
status: ctx.set.status,
58-
headers: ctx.set.headers,
59-
})
58+
return (
59+
ctx.response = new Response(serialized.value, {
60+
status: ctx.set.status,
61+
headers: ctx.set.headers,
62+
})
63+
)
6064
}"
6165
`,
6266
);
@@ -88,18 +92,22 @@ describe("compileRouteHandler", () => {
8892
}
8993
9094
if (ctx.response == null) {
91-
return new Response(undefined, {
92-
status: ctx.set.status,
93-
headers: ctx.set.headers,
94-
})
95+
return (
96+
ctx.response = new Response(undefined, {
97+
status: ctx.set.status,
98+
headers: ctx.set.headers,
99+
})
100+
)
95101
}
96102
97103
const serialized = utils.smartSerialize(ctx.response);
98104
if (!ctx.set.headers["Content-Type"]) ctx.set.headers["Content-Type"] = serialized.contentType
99-
return new Response(serialized.value, {
100-
status: ctx.set.status,
101-
headers: ctx.set.headers,
102-
})
105+
return (
106+
ctx.response = new Response(serialized.value, {
107+
status: ctx.set.status,
108+
headers: ctx.set.headers,
109+
})
110+
)
103111
}"
104112
`,
105113
);
@@ -137,18 +145,22 @@ describe("compileRouteHandler", () => {
137145
}
138146
139147
if (ctx.response == null) {
140-
return new Response(undefined, {
141-
status: ctx.set.status,
142-
headers: ctx.set.headers,
143-
})
148+
return (
149+
ctx.response = new Response(undefined, {
150+
status: ctx.set.status,
151+
headers: ctx.set.headers,
152+
})
153+
)
144154
}
145155
146156
const serialized = utils.smartSerialize(ctx.response);
147157
if (!ctx.set.headers["Content-Type"]) ctx.set.headers["Content-Type"] = serialized.contentType
148-
return new Response(serialized.value, {
149-
status: ctx.set.status,
150-
headers: ctx.set.headers,
151-
})
158+
return (
159+
ctx.response = new Response(serialized.value, {
160+
status: ctx.set.status,
161+
headers: ctx.set.headers,
162+
})
163+
)
152164
}"
153165
`);
154166
});
@@ -185,18 +197,22 @@ describe("compileRouteHandler", () => {
185197
}
186198
187199
if (ctx.response == null) {
188-
return new Response(undefined, {
189-
status: ctx.set.status,
190-
headers: ctx.set.headers,
191-
})
200+
return (
201+
ctx.response = new Response(undefined, {
202+
status: ctx.set.status,
203+
headers: ctx.set.headers,
204+
})
205+
)
192206
}
193207
194208
const serialized = utils.smartSerialize(ctx.response);
195209
if (!ctx.set.headers["Content-Type"]) ctx.set.headers["Content-Type"] = serialized.contentType
196-
return new Response(serialized.value, {
197-
status: ctx.set.status,
198-
headers: ctx.set.headers,
199-
})
210+
return (
211+
ctx.response = new Response(serialized.value, {
212+
status: ctx.set.status,
213+
headers: ctx.set.headers,
214+
})
215+
)
200216
}"
201217
`);
202218
});
@@ -230,18 +246,22 @@ describe("compileRouteHandler", () => {
230246
return onAfterHandleRes0;
231247
232248
if (ctx.response == null) {
233-
return new Response(undefined, {
234-
status: ctx.set.status,
235-
headers: ctx.set.headers,
236-
})
249+
return (
250+
ctx.response = new Response(undefined, {
251+
status: ctx.set.status,
252+
headers: ctx.set.headers,
253+
})
254+
)
237255
}
238256
239257
const serialized = utils.smartSerialize(ctx.response);
240258
if (!ctx.set.headers["Content-Type"]) ctx.set.headers["Content-Type"] = serialized.contentType
241-
return new Response(serialized.value, {
242-
status: ctx.set.status,
243-
headers: ctx.set.headers,
244-
})
259+
return (
260+
ctx.response = new Response(serialized.value, {
261+
status: ctx.set.status,
262+
headers: ctx.set.headers,
263+
})
264+
)
245265
}"
246266
`);
247267
});
@@ -275,18 +295,22 @@ describe("compileRouteHandler", () => {
275295
return onMapResponseRes0;
276296
277297
if (ctx.response == null) {
278-
return new Response(undefined, {
279-
status: ctx.set.status,
280-
headers: ctx.set.headers,
281-
})
298+
return (
299+
ctx.response = new Response(undefined, {
300+
status: ctx.set.status,
301+
headers: ctx.set.headers,
302+
})
303+
)
282304
}
283305
284306
const serialized = utils.smartSerialize(ctx.response);
285307
if (!ctx.set.headers["Content-Type"]) ctx.set.headers["Content-Type"] = serialized.contentType
286-
return new Response(serialized.value, {
287-
status: ctx.set.status,
288-
headers: ctx.set.headers,
289-
})
308+
return (
309+
ctx.response = new Response(serialized.value, {
310+
status: ctx.set.status,
311+
headers: ctx.set.headers,
312+
})
313+
)
290314
}"
291315
`);
292316
});

src/internal/compile-route-handler.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { MaybePromise } from "elysia";
2+
import { getMeta } from "../meta";
23
import type {
34
CompiledRouteHandler,
45
LifeCycleHookName,
@@ -15,7 +16,6 @@ import {
1516
validateInputSchema,
1617
validateOutputSchema,
1718
} from "./utils";
18-
import { getMeta } from "../meta";
1919

2020
export function compileRouteHandler(
2121
options: CompileOptions,
@@ -57,18 +57,22 @@ ${options.hooks.onAfterHandle?.length ? compileResponseModifierHookCall("onAfter
5757
${options.hooks.onMapResponse?.length ? compileResponseModifierHookCall("onMapResponse", options.hooks.onMapResponse.length) : ""}
5858
5959
if (ctx.response == null) {
60-
return new Response(undefined, {
61-
status: ctx.set.status,
62-
headers: ctx.set.headers,
63-
})
60+
return (
61+
ctx.response = new Response(undefined, {
62+
status: ctx.set.status,
63+
headers: ctx.set.headers,
64+
})
65+
)
6466
}
6567
6668
const serialized = utils.smartSerialize(ctx.response);
6769
if (!ctx.set.headers["Content-Type"]) ctx.set.headers["Content-Type"] = ${responseContentTypeMap ? "responseContentTypeMap[ctx.set.status] ??" : ""} serialized.contentType
68-
return new Response(serialized.value, {
69-
status: ctx.set.status,
70-
headers: ctx.set.headers,
71-
})
70+
return (
71+
ctx.response = new Response(serialized.value, {
72+
status: ctx.set.status,
73+
headers: ctx.set.headers,
74+
})
75+
)
7276
}
7377
//#sourceURL=${getSourceUrl(options)}
7478
`;

0 commit comments

Comments
 (0)