Back to packages list

Vals using @hono/swagger-ui

Description from the NPM package:
A middleware for using SwaggerUI in Hono
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
import { swaggerUI } from "npm:@hono/swagger-ui";
import { z } from "npm:@hono/zod-openapi";
import { createRoute, OpenAPIHono } from "npm:@hono/zod-openapi";
import { html } from "npm:hono/html";
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(html`Try going to <a href="/ui">/ui</a>`));
app.openapi(route, (c) => {
const { id } = c.req.valid("param");
return c.json({
id,
age: 20,
name: "Ultra-man",
});
});
// The OpenAPI documentation will be available at /doc
app.doc("/doc", {
openapi: "3.0.0",
info: {
version: "1.0.0",
title: "My API",
},
});
app.get("/ui", swaggerUI({ url: "/doc" }));
export default app.fetch;
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;
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
import { swaggerUI } from "npm:@hono/swagger-ui";
import { z } from "npm:@hono/zod-openapi";
import { createRoute, OpenAPIHono } from "npm:@hono/zod-openapi";
import { html } from "npm:hono/html";
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(html`Try going to <a href="/ui">/ui</a>`));
app.openapi(route, (c) => {
const { id } = c.req.valid("param");
return c.json({
id,
age: 20,
name: "Ultra-man",
});
});
// The OpenAPI documentation will be available at /doc
app.doc("/doc", {
openapi: "3.0.0",
info: {
version: "1.0.0",
title: "My API",
},
});
app.get("/ui", swaggerUI({ url: "/doc" }));
export default app.fetch;
1
Next