Initialize API

This commit is contained in:
KMKoushik
2024-04-09 09:50:56 +10:00
parent 377069978a
commit 4d0441791b
7 changed files with 169 additions and 1 deletions

View File

@@ -19,6 +19,9 @@
"@auth/prisma-adapter": "^1.4.0",
"@aws-sdk/client-sesv2": "^3.535.0",
"@aws-sdk/client-sns": "^3.540.0",
"@hono/node-server": "^1.9.1",
"@hono/swagger-ui": "^0.2.1",
"@hono/zod-openapi": "^0.10.0",
"@prisma/client": "^5.11.0",
"@t3-oss/env-nextjs": "^0.9.2",
"@tanstack/react-query": "^5.25.0",
@@ -28,6 +31,7 @@
"@trpc/server": "next",
"@unsend/ui": "workspace:*",
"date-fns": "^3.6.0",
"hono": "^4.2.2",
"install": "^0.13.0",
"lucide-react": "^0.359.0",
"next": "^14.1.3",

View File

@@ -0,0 +1,7 @@
import { handle } from "hono/vercel";
import { app } from "~/server/public-api";
export const GET = handle(app);
export const POST = handle(app);
export const PUT = handle(app);
export const DELETE = handle(app);

View File

@@ -0,0 +1,31 @@
import { DomainStatus } from "@prisma/client";
import { z } from "zod";
export const DomainStatusSchema = z.nativeEnum(DomainStatus);
export const DomainSchema = z.object({
id: z.number().openapi({ description: "The ID of the domain", example: 1 }),
name: z
.string()
.openapi({ description: "The name of the domain", example: "example.com" }),
teamId: z.number().openapi({ description: "The ID of the team", example: 1 }),
status: DomainStatusSchema,
region: z.string().default("us-east-1"),
clickTracking: z.boolean().default(false),
openTracking: z.boolean().default(false),
publicKey: z.string(),
dkimStatus: z.string().optional().nullish(),
spfDetails: z.string().optional().nullish(),
createdAt: z.string(),
updatedAt: z.string(),
});
export const CreateDomainSchema = DomainSchema.omit({
id: true,
createdAt: true,
updatedAt: true,
teamId: true,
publicKey: true,
dkimStatus: true,
spfDetails: true,
});

View 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;

View 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>;

View 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;