Skip to content

Commit 8b0a0eb

Browse files
authored
サーバーのコードをリファクタリング (#64)
* refactor: 共通の設定を main.ts へ * refactor: エラーハンドリングを共通化 * refactor: Prisma でのデータ取得を修正 * fix: middleware のレスポンスは乗らないので回避
1 parent 0fce565 commit 8b0a0eb

File tree

4 files changed

+263
-293
lines changed

4 files changed

+263
-293
lines changed

client/src/pages/Home.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ export default function HomePage() {
3131
let errorMessage = "イベントの取得に失敗しました。";
3232
try {
3333
const data = await res.json();
34-
if (data && typeof data.message === "string" && data.message.trim()) {
35-
errorMessage = data.message.trim();
34+
const err = data as unknown as { message: string }; // Middleware のレスポンスは Hono RPC の型に乗らない
35+
if (typeof err.message === "string" && err.message.trim()) {
36+
errorMessage = err.message.trim();
3637
}
3738
} catch (_) {
3839
// レスポンスがJSONでない場合は無視

client/src/pages/Project.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export default function ProjectPage() {
9393
projectName: string;
9494
} | null>(null);
9595

96+
// TODO: グローバルにしないと、delete の際は遷移を伴うので表示されない
9697
const [toast, setToast] = useState<{
9798
message: string;
9899
variant: "success" | "error";
@@ -215,9 +216,8 @@ export default function ProjectPage() {
215216
projectName: name,
216217
});
217218
} else {
218-
const { message } = await res.json();
219219
setToast({
220-
message,
220+
message: "イベントの作成に失敗しました。",
221221
variant: "error",
222222
});
223223
setTimeout(() => setToast(null), 3000);
@@ -604,6 +604,7 @@ export default function ProjectPage() {
604604
if (!res.ok) {
605605
throw new Error("削除に失敗しました。");
606606
}
607+
// TODO: トーストをグローバルにする
607608
navigate("/home");
608609
setToast({
609610
message: "イベントを削除しました。",

server/src/main.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,23 @@ import { PrismaClient } from "@prisma/client";
33
import dotenv from "dotenv";
44
import { Hono } from "hono";
55
import { cors } from "hono/cors";
6+
import { customAlphabet } from "nanoid";
67
import { browserIdMiddleware } from "./middleware/browserId.js";
78
import projectsRoutes from "./routes/projects.js";
89

910
dotenv.config();
1011

12+
/**
13+
* ハイフン・アンダースコアを含まない Nano ID 形式。
14+
*/
15+
export const nanoid = customAlphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 21);
16+
1117
export const prisma = new PrismaClient();
12-
const port = process.env.PORT || 3000;
18+
19+
const port = Number(process.env.PORT) || 3000;
1320
const allowedOrigins = process.env.CORS_ALLOW_ORIGINS?.split(",") || [];
1421

15-
type AppVariables = {
22+
export type AppVariables = {
1623
browserId: string;
1724
};
1825

@@ -28,12 +35,16 @@ const app = new Hono<{ Variables: AppVariables }>()
2835
.get("/", (c) => {
2936
return c.json({ message: "Hello! イツヒマ?" });
3037
})
31-
.route("/projects", projectsRoutes);
38+
.route("/projects", projectsRoutes)
39+
.onError((err, c) => {
40+
console.error(err);
41+
return c.json({ message: "Internal Server Error" }, 500);
42+
});
3243

3344
serve(
3445
{
3546
fetch: app.fetch,
36-
port: Number(port),
47+
port,
3748
hostname: "0.0.0.0",
3849
},
3950
() => {

0 commit comments

Comments
 (0)