Initialize API
This commit is contained in:
29
apps/web/src/server/public-api/get_domains.ts
Normal file
29
apps/web/src/server/public-api/get_domains.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { createRoute, z } from "@hono/zod-openapi";
|
||||
import { DomainSchema } from "~/lib/zod/domain-schema";
|
||||
import { PublicAPIApp } from "./hono";
|
||||
import { db } from "../db";
|
||||
|
||||
const route = createRoute({
|
||||
method: "get",
|
||||
path: "/domains",
|
||||
responses: {
|
||||
200: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: z.array(DomainSchema),
|
||||
},
|
||||
},
|
||||
description: "Retrieve the user",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
function getDomains(app: PublicAPIApp) {
|
||||
app.openapi(route, async (c) => {
|
||||
const domains = await db.domain.findMany({});
|
||||
|
||||
return c.json(domains);
|
||||
});
|
||||
}
|
||||
|
||||
export default getDomains;
|
7
apps/web/src/server/public-api/hono.ts
Normal file
7
apps/web/src/server/public-api/hono.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { OpenAPIHono } from "@hono/zod-openapi";
|
||||
|
||||
export function getApp() {
|
||||
return new OpenAPIHono().basePath("/api/v1");
|
||||
}
|
||||
|
||||
export type PublicAPIApp = ReturnType<typeof getApp>;
|
22
apps/web/src/server/public-api/index.ts
Normal file
22
apps/web/src/server/public-api/index.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { swaggerUI } from "@hono/swagger-ui";
|
||||
|
||||
import { getApp } from "./hono";
|
||||
import getDomains from "./get_domains";
|
||||
|
||||
export const app = getApp();
|
||||
|
||||
getDomains(app);
|
||||
|
||||
// The OpenAPI documentation will be available at /doc
|
||||
app.doc("/doc", {
|
||||
openapi: "3.0.0",
|
||||
info: {
|
||||
version: "1.0.0",
|
||||
title: "My API",
|
||||
},
|
||||
servers: [{ url: "/api/v1" }],
|
||||
});
|
||||
|
||||
app.get("/ui", swaggerUI({ url: "/api/v1/doc" }));
|
||||
|
||||
export default app;
|
Reference in New Issue
Block a user