/* Define our schemas */ // fun fact: the @ means zod-openapi is a scoped package instead of a submodule of hono import { z } from "npm:@hono/zod-openapi"; import { bearerAuth } from "npm:hono/bearer-auth"; const ParamsSchema = z.object({ id: z .string() .min(3) .openapi({ param: { name: "id", in: "path", }, example: "1212121", }), }); const UserSchema = z .object({ id: z.string().openapi({ example: "123", }), name: z.string().openapi({ example: "John Doe", }), age: z.number().openapi({ example: 42, }), }) .openapi("User"); /* Create a route */ import { createRoute } from "npm:@hono/zod-openapi"; const route = createRoute({ method: "get", security: [ { Bearer: [], }, ], path: "/users/{id}", request: { params: ParamsSchema, }, responses: { 200: { content: { "application/json": { schema: UserSchema, }, }, description: "Retrieve the user", }, }, }); /* App setup */ import { OpenAPIHono } from "npm:@hono/zod-openapi"; const app = new OpenAPIHono(); // https://github.com/honojs/middleware/tree/main/packages/zod-openapi#how-to-setup-authorization // https://hono.dev/docs/middleware/builtin/bearer-auth const token = "hunter2"; app.use("/users/*", bearerAuth({ token })); app.openAPIRegistry.registerComponent("securitySchemes", "Bearer", { type: "http", scheme: "bearer", }); app.openapi(route, (c) => { const { id } = c.req.valid("param"); return c.json({ id, age: 21, name: "pleb", }); }); // OpenAPI Spec app.doc("/openapi.json", { openapi: "3.0.0", info: { version: "1.0.0", title: "My API", }, }); /* OpenAPI Reference */ import { apiReference } from "npm:@scalar/hono-api-reference"; app.get( "/reference", apiReference({ pageTitle: "Scalar/Hono API Reference", theme: "elysiajs", spec: { url: "/openapi.json" }, }), ); Deno.serve(app.fetch);