feat: add health check
Summary: Test Plan:
This commit is contained in:
11
src/controllers/health-check/index.spec.ts
Normal file
11
src/controllers/health-check/index.spec.ts
Normal 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 });
|
||||
});
|
||||
});
|
||||
24
src/controllers/health-check/index.ts
Normal file
24
src/controllers/health-check/index.ts
Normal 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;
|
||||
20
src/index.ts
20
src/index.ts
@@ -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
10
src/types/schema.ts
Normal 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 });
|
||||
};
|
||||
Reference in New Issue
Block a user