feat: add types and init middleware

This commit is contained in:
nafees nazik
2024-07-09 21:56:25 +05:30
parent aaaf62d771
commit 1765a03a57
2 changed files with 29 additions and 19 deletions
+19 -19
View File
@@ -1,29 +1,27 @@
import { env } from "@/env";
import { handleError } from "@/server/api/error";
import { handleError, handleZodError } from "@/server/api/error";
import type { TPrisma } from "@/server/db";
import { swaggerUI } from "@hono/swagger-ui";
import { OpenAPIHono } from "@hono/zod-openapi";
import type { Context as GenericContext, MiddlewareHandler } from "hono";
export type HonoEnv = {
Bindings: {
NODE_ENV: (typeof env)["NODE_ENV"];
};
Variables: {
db: TPrisma;
};
};
/**
* Initializes and configures the public API.
*
* This function sets up the public API using OpenAPIHono, including error handling,
* OpenAPI documentation, and the Swagger UI.
*
* @returns {OpenAPIHono} The configured API instance.
*/
export function PublicAPI() {
const api = new OpenAPIHono().basePath("/api");
const api = new OpenAPIHono<HonoEnv>({
defaultHook: handleZodError,
}).basePath("/api");
api.onError(handleError);
/**
* Provides the OpenAPI schema for the API.
*
* @route GET /v1/schema
* @group Documentation
* @description This route returns the OpenAPI schema for the Captable, Inc. API.
* @returns {object} 200 - The OpenAPI schema
* @returns {Error} default - Unexpected error
*/
api.doc("/v1/schema", () => ({
openapi: "3.0.0",
info: {
@@ -43,3 +41,5 @@ export function PublicAPI() {
}
export type PublicAPI = ReturnType<typeof PublicAPI>;
export type Context = GenericContext<HonoEnv>;
export type Middleware = MiddlewareHandler<HonoEnv>;
+10
View File
@@ -0,0 +1,10 @@
import { db } from "@/server/db";
import type { Middleware } from "../hono";
export function initMiddleware(): Middleware {
return async (c, next) => {
c.set("db", db);
await next();
};
}