|
| 1 | +import { serve } from "@hono/node-server"; |
| 2 | +import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi"; |
| 3 | +import { openApiUIReference } from "@openapi-ui/hono-openapi-ui"; |
| 4 | + |
| 5 | +const app = new OpenAPIHono(); |
| 6 | + |
| 7 | +// Example route |
| 8 | +app.openapi( |
| 9 | + createRoute({ |
| 10 | + method: "get", |
| 11 | + path: "/hello-world", |
| 12 | + description: "Respond a message", |
| 13 | + tags: ["basic example"], |
| 14 | + responses: { |
| 15 | + 200: { |
| 16 | + description: "OK", |
| 17 | + content: { |
| 18 | + "application/json": { |
| 19 | + schema: z.object({ |
| 20 | + message: z.string(), |
| 21 | + }), |
| 22 | + }, |
| 23 | + }, |
| 24 | + }, |
| 25 | + }, |
| 26 | + }), |
| 27 | + (c) => { |
| 28 | + return c.json({ |
| 29 | + message: "hello", |
| 30 | + }); |
| 31 | + }, |
| 32 | +); |
| 33 | + |
| 34 | +app.openapi( |
| 35 | + createRoute({ |
| 36 | + name: "Get all posts", |
| 37 | + method: "get", |
| 38 | + path: "/posts", |
| 39 | + description: "Returns all posts", |
| 40 | + tags: ["posts"], |
| 41 | + responses: { |
| 42 | + 200: { |
| 43 | + description: "OK", |
| 44 | + content: { |
| 45 | + "application/json": { |
| 46 | + schema: z.object({ |
| 47 | + posts: z.array( |
| 48 | + z.object({ |
| 49 | + id: z.number().default(123), |
| 50 | + title: z.string(), |
| 51 | + body: z.string(), |
| 52 | + }), |
| 53 | + ), |
| 54 | + }), |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + }), |
| 60 | + (c) => { |
| 61 | + return c.json({ |
| 62 | + posts: [ |
| 63 | + { |
| 64 | + id: 123, |
| 65 | + title: "My Blog Post Title", |
| 66 | + body: "My Blog Post Body", |
| 67 | + }, |
| 68 | + ], |
| 69 | + }); |
| 70 | + }, |
| 71 | +); |
| 72 | + |
| 73 | +app.openapi( |
| 74 | + createRoute({ |
| 75 | + name: "Create post", |
| 76 | + method: "post", |
| 77 | + path: "/posts", |
| 78 | + description: "Create a new post", |
| 79 | + tags: ["posts"], |
| 80 | + request: { |
| 81 | + body: { |
| 82 | + content: { |
| 83 | + "application/json": { |
| 84 | + schema: z.object({ |
| 85 | + title: z.string(), |
| 86 | + body: z.string(), |
| 87 | + }), |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + }, |
| 92 | + responses: { |
| 93 | + 200: { |
| 94 | + description: "OK", |
| 95 | + content: { |
| 96 | + "application/json": { |
| 97 | + schema: z.object({ |
| 98 | + id: z.number().default(123), |
| 99 | + title: z.string(), |
| 100 | + body: z.string(), |
| 101 | + }), |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + }), |
| 107 | + (c) => { |
| 108 | + return c.json({ |
| 109 | + id: 123, |
| 110 | + title: "My Blog Post Title", |
| 111 | + body: "My Blog Post Body", |
| 112 | + }); |
| 113 | + }, |
| 114 | +); |
| 115 | + |
| 116 | +app.openapi( |
| 117 | + createRoute({ |
| 118 | + name: "Delete Post", |
| 119 | + method: "delete", |
| 120 | + path: "/posts/{id}", |
| 121 | + description: "Delete a post", |
| 122 | + tags: ["posts"], |
| 123 | + request: { |
| 124 | + params: z.object({ |
| 125 | + id: z.number().default(123), |
| 126 | + }), |
| 127 | + }, |
| 128 | + responses: { |
| 129 | + 200: { |
| 130 | + description: "OK", |
| 131 | + content: { |
| 132 | + "application/json": { |
| 133 | + schema: z.object({ |
| 134 | + status: z.string().default("OK"), |
| 135 | + message: z.string().default("Post deleted"), |
| 136 | + }), |
| 137 | + }, |
| 138 | + }, |
| 139 | + }, |
| 140 | + 404: { |
| 141 | + description: "Not Found", |
| 142 | + content: { |
| 143 | + "application/json": { |
| 144 | + schema: z.object({ |
| 145 | + status: z.string().default("ERROR"), |
| 146 | + message: z.string().default("Post not found"), |
| 147 | + }), |
| 148 | + }, |
| 149 | + }, |
| 150 | + }, |
| 151 | + }, |
| 152 | + }), |
| 153 | + (c) => { |
| 154 | + return c.json({ |
| 155 | + status: "OK", |
| 156 | + message: "Post deleted", |
| 157 | + }); |
| 158 | + }, |
| 159 | +); |
| 160 | + |
| 161 | +const docsPath = "/openapi"; |
| 162 | +const specPath = "/openapi.json"; |
| 163 | + |
| 164 | +app.doc(specPath, { |
| 165 | + info: { |
| 166 | + title: "Example API", |
| 167 | + description: "Example API description", |
| 168 | + version: "v1", |
| 169 | + }, |
| 170 | + openapi: "3.0.0", |
| 171 | +}); |
| 172 | + |
| 173 | +app.use( |
| 174 | + docsPath, |
| 175 | + openApiUIReference({ |
| 176 | + title: "openapi docs", |
| 177 | + description: "openapi docs description", |
| 178 | + specPath: specPath, |
| 179 | + }), |
| 180 | +); |
| 181 | + |
| 182 | +const PORT = Number(process.env.PORT) || 8004; |
| 183 | +const HOST = process.env.HOST || "0.0.0.0"; |
| 184 | +serve( |
| 185 | + { |
| 186 | + fetch: app.fetch, |
| 187 | + port: PORT, |
| 188 | + hostname: HOST, |
| 189 | + }, |
| 190 | + () => { |
| 191 | + console.log(`✅ Hono Middleware listening on http://127.0.0.1:${PORT}${docsPath}`); |
| 192 | + }, |
| 193 | +); |
0 commit comments