openapi-ts-hono
v0.1.1
Published
Type-check Hono apps against OpenAPI paths generated by openapi-typescript
Readme
openapi-ts-hono
Type-check a Hono app against an OpenAPI paths type generated by openapi-typescript.
Installation
pnpm add hono
pnpm add -D openapi-ts-hono openapi-typescript typescriptUsage
import { Hono } from "hono";
import { defineApp } from "openapi-ts-hono";
// generated by openapi-typescript
import type { paths } from "./openapi-types";
const app = defineApp<paths>()(
new Hono().get("/users/:id", (c) => {
return c.json({ id: c.req.param("id"), name: "Alice" });
}),
);
// or
const defineAppWithPaths = defineApp<paths>();
const app = defineAppWithPaths(
new Hono().get("/users/:id", (c) => {
return c.json({ id: c.req.param("id"), name: "Alice" });
}),
);
// sub app
const userApp = defineApp<paths, "/users">()(
new Hono().get("/:id", (c) => {
return c.json({ id: c.req.param("id"), name: "Alice" });
}),
);
const routedApp = defineApp<paths>()(new Hono().route("/users", userApp));
export default app;If the app does not conform to the OpenAPI schema, TypeScript will raise a type error.
License
MIT License
