Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/middleware/with-response-object-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ export const withResponseObjectCheck: Middleware<
const rawResponse = await next(req, ctx)

if (typeof rawResponse === "object" && !(rawResponse instanceof Response)) {
const method = req.method?.toUpperCase() ?? "UNKNOWN"
const path = new URL(req.url).pathname
throw new Error(
"Use ctx.json({...}) instead of returning an object directly."
`Route ${method} ${path} returned a plain object. Use ctx.json({...}) instead of returning an object directly.`
)
}

Expand Down
1 change: 1 addition & 0 deletions tests/errors/do-not-allow-raw-json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ test("should throw an error when responding with raw JSON", async (t) => {
const { data } = await axios.get("/", {
validateStatus: () => true,
})
t.true(data.error.includes("Route GET /"))
t.true(
data.error.includes(
"Use ctx.json({...}) instead of returning an object directly"
Expand Down
Loading