Skip to content

Commit cdf6b02

Browse files
authored
refactor: use chained route (#17)
1 parent d37c9a9 commit cdf6b02

File tree

3 files changed

+66
-69
lines changed

3 files changed

+66
-69
lines changed

src/main.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ import { logger } from "hono/logger";
44
import app from "./route/index.ts";
55
import api from "./route/api.ts";
66

7-
const server = new Hono();
8-
9-
server.use(logger());
10-
server.route("/", app);
11-
server.route("/api", api);
7+
const server = new Hono()
8+
.use(logger())
9+
.route("/", app)
10+
.route("/api", api);
1211

1312
Deno.serve(server.fetch);

src/route/api.ts

Lines changed: 48 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -14,59 +14,58 @@ import { Logger } from "../service/logger.ts";
1414

1515
const apiKeys = Deno.env.get("API_KEYS")?.split(",").filter(Boolean) ?? [];
1616

17-
const api = new Hono();
1817
const logger = new Logger();
1918

20-
api.use(async (c, next) => {
21-
const apiKey = c.req.header("X-API-Key");
19+
const api = new Hono()
20+
.use(async (c, next) => {
21+
const apiKey = c.req.header("X-API-Key");
2222

23-
if (!apiKey) {
24-
await logger.apiAccess(c, "Unauthorized", true, apiKey);
25-
throw new HTTPException(401, { message: "Unauthorized" });
26-
}
23+
if (!apiKey) {
24+
await logger.apiAccess(c, "Unauthorized", true, apiKey);
25+
throw new HTTPException(401, { message: "Unauthorized" });
26+
}
2727

28-
if (!apiKeys.includes(apiKey)) {
29-
await logger.apiAccess(c, "Forbidden", true, apiKey);
30-
throw new HTTPException(403, { message: "Forbidden" });
31-
}
28+
if (!apiKeys.includes(apiKey)) {
29+
await logger.apiAccess(c, "Forbidden", true, apiKey);
30+
throw new HTTPException(403, { message: "Forbidden" });
31+
}
3232

33-
await logger.apiAccess(c, "Accessed", false, apiKey);
34-
await next();
35-
});
36-
37-
api.get("/all", getAllItemsController);
38-
api.get("/items/:param", getItemController);
39-
api.put(
40-
"/items/:param",
41-
zValidator(
42-
"json",
43-
z.object({
44-
description: z.string().optional(),
45-
url: z.string().url(),
46-
count: z.number().optional(),
47-
}),
48-
),
49-
(c) => {
50-
const { description, url, count } = c.req.valid("json");
51-
return putItemController(c, description, url, count);
52-
},
53-
);
54-
api.patch(
55-
"/items/:param",
56-
zValidator(
57-
"json",
58-
z.object({
59-
description: z.string().optional(),
60-
url: z.string().url().optional(),
61-
count: z.number().optional(),
62-
unavailable: z.boolean().optional(),
63-
}),
64-
),
65-
(c) => {
66-
const { description, url, count, unavailable } = c.req.valid("json");
67-
return patchItemController(c, description, url, count, unavailable);
68-
},
69-
);
70-
api.delete("/items/:param", deleteItemController);
33+
await logger.apiAccess(c, "Accessed", false, apiKey);
34+
await next();
35+
})
36+
.get("/all", getAllItemsController)
37+
.get("/items/:param", getItemController)
38+
.put(
39+
"/items/:param",
40+
zValidator(
41+
"json",
42+
z.object({
43+
description: z.string().optional(),
44+
url: z.string().url(),
45+
count: z.number().optional(),
46+
}),
47+
),
48+
(c) => {
49+
const { description, url, count } = c.req.valid("json");
50+
return putItemController(c, description, url, count);
51+
},
52+
)
53+
.patch(
54+
"/items/:param",
55+
zValidator(
56+
"json",
57+
z.object({
58+
description: z.string().optional(),
59+
url: z.string().url().optional(),
60+
count: z.number().optional(),
61+
unavailable: z.boolean().optional(),
62+
}),
63+
),
64+
(c) => {
65+
const { description, url, count, unavailable } = c.req.valid("json");
66+
return patchItemController(c, description, url, count, unavailable);
67+
},
68+
)
69+
.delete("/items/:param", deleteItemController);
7170

7271
export default api;

src/route/index.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,23 @@ import { serveStatic } from "hono/deno";
44
import { Logger } from "../service/logger.ts";
55
import { home, redirect } from "../controller/index.ts";
66

7-
const app = new Hono();
87
const logger = new Logger();
98

10-
app.use(async (c, next) => {
11-
await next();
9+
const app = new Hono()
10+
.use(async (c, next) => {
11+
await next();
1212

13-
if (!c.req.path.startsWith("/api")) {
14-
// 200-299 or 300-399
15-
if (c.res.ok || (c.res.status >= 300 && c.res.status < 400)) {
16-
await logger.access(c, false);
17-
} else {
18-
await logger.access(c, true);
13+
if (!c.req.path.startsWith("/api")) {
14+
// 200-299 or 300-399
15+
if (c.res.ok || (c.res.status >= 300 && c.res.status < 400)) {
16+
await logger.access(c, false);
17+
} else {
18+
await logger.access(c, true);
19+
}
1920
}
20-
}
21-
});
22-
app.use("/robots.txt", serveStatic({ path: "public/robots.txt" }));
23-
24-
app.get("/", home);
25-
app.get("/:param", redirect);
21+
})
22+
.use("/robots.txt", serveStatic({ path: "public/robots.txt" }))
23+
.get("/", home)
24+
.get("/:param", redirect);
2625

2726
export default app;

0 commit comments

Comments
 (0)