Skip to content

Commit

Permalink
Always return JSON as output for functions
Browse files Browse the repository at this point in the history
  • Loading branch information
naveen521kk committed Nov 4, 2023
1 parent c2e4400 commit 3b3b337
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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});
}
});

Expand Down

0 comments on commit 3b3b337

Please sign in to comment.