From ab10ab709191fd2c99d02c77a58947b0180715a1 Mon Sep 17 00:00:00 2001 From: HorizonCode Date: Thu, 11 May 2023 11:53:28 +0200 Subject: [PATCH] add object returner support --- example/test.ts | 16 ++++++---------- mod.ts | 7 ++++++- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/example/test.ts b/example/test.ts index ec08912..487e82d 100644 --- a/example/test.ts +++ b/example/test.ts @@ -9,12 +9,12 @@ httpServer.error((req, _rep) => { code: Status.NotFound, message: "Route not found!", path: req.path, - url: req.url + url: req.url, }, null, 2, ); -}) +}); httpServer.add("GET", "/", (req, rep) => { rep.status(Status.Teapot) @@ -24,14 +24,10 @@ httpServer.add("GET", "/", (req, rep) => { console.log(req.cookie("working")); - return JSON.stringify( - { - code: Status.Teapot, - message: "Hello World!", - }, - null, - 2, - ); + return { + code: Status.Teapot, + message: "Hello World!", + }; }); httpServer.add("GET", "/api/user/:userId", (req, rep) => { diff --git a/mod.ts b/mod.ts index 3c90da7..ab0f084 100644 --- a/mod.ts +++ b/mod.ts @@ -117,10 +117,15 @@ export class HTTPServer { : undefined; if (route) { - const handler = await route.handler( + let handler = await route.handler( routeRequest, routeReply, ); + + if (typeof (handler) == "object") { + handler = JSON.stringify(handler, null, 2); + } + await requestEvent.respondWith( new Response(handler as string, { status: routeReply.statusCode,