Public
HTTP
Forked from nbbaier/honoZodSwaggerUi
stevekrouse-honozodswaggerui.web.val.run
December 6, 2023
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/** @jsxImportSource https://esm.sh/hono@3.9.2/jsx **/
import { swaggerUI } from "npm:@hono/swagger-ui";
import { z } from "npm:@hono/zod-openapi";
import { OpenAPIHono } from "npm:@hono/zod-openapi";
import { createRoute } from "npm:@hono/zod-openapi";
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");
const route = createRoute({
method: "get",
path: "/users/{id}",
request: {
params: ParamsSchema,
},
responses: {
200: {
content: {
"application/json": {
schema: UserSchema,
},
},
description: "Retrieve the user",
},
},
});
const app = new OpenAPIHono();
app.get("/", c =>
c.html(
<>
<h1>Hono Zod Example</h1>
<ul>
<li>
<a href="/openapi.json">OpenAPI Spec</a>
</li>
<li>
<a href="/swagger">Swagger UI</a>
</li>
<li>
Example API call:{" "}
<a href="/users/123">
<code>GET /users/123</code>
</a>
</li>
</ul>
</>,
));
app.openapi(route, (c) => {
const { id } = c.req.valid("param");
return c.json({
id,
age: 20,
name: "Ultra-man",
});
});
app.doc("/openapi.json", {
openapi: "3.0.0",
info: {
version: "1.0.0",
title: "My API",
},
});
app.get("/swagger", swaggerUI({ url: "/openapi.json" }));
export default app.fetch;
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
December 6, 2023