feat: add health check

Summary:

Test Plan:
This commit is contained in:
2024-05-14 22:30:52 -04:00
parent d34a1a4771
commit 6c771eaaad
5 changed files with 65 additions and 5 deletions

View File

@@ -4,11 +4,14 @@
"deploy": "wrangler deploy --minify src/index.ts"
},
"dependencies": {
"hono": "^4.3.6"
"@hono/zod-openapi": "^0.12.0",
"hono": "^4.3.6",
"zod": "^3.23.8"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20240403.0",
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
"@types/bun": "^1.1.2",
"prettier": "^3.2.5",
"prettier-plugin-toml": "^2.0.1",
"wrangler": "^3.47.0"

View File

@@ -0,0 +1,11 @@
import { describe, expect, it } from "bun:test";
import app from "~/index";
describe("Health Check", () => {
it("should return { success: true }", async () => {
const res = await app.request("/");
expect(await res.json()).toEqual({ success: true });
});
});

View File

@@ -0,0 +1,24 @@
import { OpenAPIHono, createRoute } from "@hono/zod-openapi";
import { SuccessResponse, SuccessResponseSchema } from "~/types/schema";
const app = new OpenAPIHono();
const route = createRoute({
method: "get",
path: "/",
responses: {
200: {
content: {
"application/json": {
schema: SuccessResponseSchema(),
},
},
description: "Retrieve the user",
},
},
});
app.openapi(route, (c) => c.json(SuccessResponse, 200));
export default app;

View File

@@ -1,9 +1,21 @@
import { Hono } from "hono";
import { OpenAPIHono } from "@hono/zod-openapi";
const app = new Hono();
const app = new OpenAPIHono();
app.get("/", (c) => {
return c.text("Hello Hono!");
app.route(
"/",
await import("~/controllers/health-check").then(
(controller) => controller.default,
),
);
// The OpenAPI documentation will be available at /doc
app.doc("/doc", {
openapi: "3.0.0",
info: {
version: "1.0.0",
title: "Aniplay API",
},
});
export default app;

10
src/types/schema.ts Normal file
View File

@@ -0,0 +1,10 @@
import { type ZodSchema, z } from "zod";
export const SuccessResponse = { success: true } as const;
export const SuccessResponseSchema = <T extends ZodSchema>(schema?: T) => {
if (!schema) {
return z.object({ success: z.literal(true) });
}
return z.object({ success: z.literal(true), result: schema });
};