From 3b3b3372a7e9637cdfad1e28c8a4ef86792ef755 Mon Sep 17 00:00:00 2001 From: Naveen M K Date: Sat, 4 Nov 2023 11:03:09 +0530 Subject: [PATCH] Always return JSON as output for functions --- functions/src/index.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/functions/src/index.ts b/functions/src/index.ts index fb08de9..b60f037 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -4,7 +4,7 @@ const router = Router(); router.get("/api/get-article-hits", async ({query}, env) => { if (!query || !query.slug) - return new Response("slug is required", {status: 400}); + return Response.json({error: "slug is required"}, {status: 400}); // get the slug from the query string const slug = decodeURIComponent(query.slug); @@ -21,7 +21,7 @@ router.get("/api/get-article-hits", async ({query}, env) => { ) .bind(slug) .run(); - return new Response(((results[0]["count"] as number) + 1).toString()); + return Response.json({hits: (results[0]["count"] as number) + 1}); } // the slug is not in the database, add it @@ -31,7 +31,7 @@ router.get("/api/get-article-hits", async ({query}, env) => { .bind(slug) .run(); if (insertRes.results.length === 1) { - return new Response("1"); + return Response.json({hits: 1}); } });