Skip to content

Commit 6286cf9

Browse files
committed
refactor(api): type jwt payload variables across endpoints
1 parent 5c8009b commit 6286cf9

File tree

11 files changed

+64
-45
lines changed

11 files changed

+64
-45
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,4 @@ Thumbs.db # Windows
131131
# Learn more https://docs.github.com/en/get-started/getting-started-with-git/ignoring-files
132132

133133
/mobile/service-account-key.json
134+
.alma-snapshots

backend/src/api/agents.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Hono } from "hono";
2+
import { JwtPayload } from "../types";
23
import { Bindings } from "../models/db";
34
import { Agent } from "../models/agent";
45
import {
@@ -15,12 +16,12 @@ import {
1516

1617
const agents = new Hono<{
1718
Bindings: Bindings;
18-
Variables: { agent: Agent; jwtPayload: any };
19+
Variables: { agent: Agent; jwtPayload: JwtPayload };
1920
}>();
2021

2122
// 获取所有客户端
2223
agents.get("/", async (c) => {
23-
const payload = c.get("jwtPayload");
24+
const payload = c.get("jwtPayload") as JwtPayload;
2425
const result = await getAgents(payload.id);
2526

2627
return c.json(
@@ -36,7 +37,7 @@ agents.get("/", async (c) => {
3637
// 更新客户端信息
3738
agents.put("/:id", async (c) => {
3839
const agentId = Number(c.req.param("id"));
39-
const payload = c.get("jwtPayload");
40+
const payload = c.get("jwtPayload") as JwtPayload;
4041
const updateData = await c.req.json();
4142

4243
const result = await updateAgentService(c.env.DB, agentId, updateData);
@@ -55,7 +56,7 @@ agents.put("/:id", async (c) => {
5556
agents.delete("/:id", async (c) => {
5657
try {
5758
const agentId = Number(c.req.param("id"));
58-
const payload = c.get("jwtPayload"); // 获取用户信息
59+
const payload = c.get("jwtPayload") as JwtPayload; // 获取用户信息
5960

6061
await deleteAgentService(agentId, payload.id); // 传入 userId
6162

backend/src/api/auth.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Hono } from "hono";
2+
import { JwtPayload } from "../types";
23
import { jwtMiddleware } from "../middlewares";
34
import {
45
loginUser,
@@ -9,7 +10,7 @@ import { Bindings } from "../models/db";
910
import { db } from "../config";
1011

1112

12-
const auth = new Hono<{ Bindings: Bindings }>();
13+
const auth = new Hono<{ Bindings: Bindings; Variables: { jwtPayload: JwtPayload } }>();
1314

1415
// 注册路由
1516
auth.post("/register", async (c) => {
@@ -91,7 +92,7 @@ auth.use("/me", jwtMiddleware);
9192

9293
auth.get("/me", async (c) => {
9394
try {
94-
const payload = c.get("jwtPayload");
95+
const payload = c.get("jwtPayload") as JwtPayload;
9596

9697
// 调用 AuthService 的获取当前用户方法
9798
const result = await getCurrentUser(c.env, payload.id);

backend/src/api/dashboard.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { Hono } from "hono";
2+
import { JwtPayload } from "../types";
23
import { Bindings } from "../models/db";
34
import { getDashboardData } from "../services/DashboardService";
4-
export const dashboard = new Hono<{ Bindings: Bindings }>();
5+
export const dashboard = new Hono<{ Bindings: Bindings; Variables: { jwtPayload: JwtPayload } }>();
56

67
// 获取仪表盘数据
78
dashboard.get("/", async (c) => {
8-
const payload = c.get("jwtPayload");
9+
const payload = c.get("jwtPayload") as JwtPayload;
910
const result = await getDashboardData(payload.id);
1011
return c.json(
1112
{

backend/src/api/monitors.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { Hono } from "hono";
2+
import { JwtPayload } from "../types";
23
import { Bindings } from "../models/db";
34
import * as MonitorService from "../services/MonitorService";
45

5-
const monitors = new Hono<{ Bindings: Bindings }>();
6+
const monitors = new Hono<{ Bindings: Bindings; Variables: { jwtPayload: JwtPayload } }>();
67

78
// 获取所有监控
89
monitors.get("/", async (c) => {
9-
const payload = c.get("jwtPayload");
10+
const payload = c.get("jwtPayload") as JwtPayload;
1011

1112
// 调用服务层获取监控列表
1213
const result = await MonitorService.getAllMonitors(payload.id);
@@ -22,7 +23,7 @@ monitors.get("/", async (c) => {
2223

2324
// 获取所有监控的每日统计数据
2425
monitors.get("/daily", async (c) => {
25-
const payload = c.get("jwtPayload");
26+
const payload = c.get("jwtPayload") as JwtPayload;
2627
// 调用服务层获取所有监控的每日统计数据
2728
const result = await MonitorService.getAllMonitorDailyStats(payload.id);
2829

@@ -35,7 +36,7 @@ monitors.get("/daily", async (c) => {
3536

3637
// 创建监控
3738
monitors.post("/", async (c) => {
38-
const payload = c.get("jwtPayload");
39+
const payload = c.get("jwtPayload") as JwtPayload;
3940
const data = await c.req.json();
4041

4142
// 调用服务层创建监控
@@ -53,7 +54,7 @@ monitors.post("/", async (c) => {
5354

5455
// 获取所有监控状态历史
5556
monitors.get("/history", async (c) => {
56-
const payload = c.get("jwtPayload");
57+
const payload = c.get("jwtPayload") as JwtPayload;
5758
// 调用服务层获取监控历史
5859
const result = await MonitorService.getAllMonitorStatusHistory(payload.id);
5960

@@ -69,7 +70,7 @@ monitors.get("/history", async (c) => {
6970
// 获取单个监控
7071
monitors.get("/:id", async (c) => {
7172
const id = parseInt(c.req.param("id"));
72-
const payload = c.get("jwtPayload");
73+
const payload = c.get("jwtPayload") as JwtPayload;
7374

7475
// 调用服务层获取监控详情
7576
const result = await MonitorService.getMonitorById(id, payload.id, payload.role);
@@ -88,7 +89,7 @@ monitors.get("/:id", async (c) => {
8889
monitors.put("/:id", async (c) => {
8990
const id = parseInt(c.req.param("id"));
9091
const data = await c.req.json();
91-
const payload = c.get("jwtPayload"); // 获取用户信息
92+
const payload = c.get("jwtPayload") as JwtPayload; // 获取用户信息
9293

9394
// 调用服务层更新监控,并传入用户信息以进行权限验证
9495
const result = await MonitorService.updateMonitor(id, data, payload.id, payload.role);
@@ -106,7 +107,7 @@ monitors.put("/:id", async (c) => {
106107
// 删除监控
107108
monitors.delete("/:id", async (c) => {
108109
const id = parseInt(c.req.param("id"));
109-
const payload = c.get("jwtPayload");
110+
const payload = c.get("jwtPayload") as JwtPayload;
110111

111112
// 调用服务层删除监控,并传入用户信息以进行权限验证
112113
const result = await MonitorService.deleteMonitor(id, payload.id, payload.role);
@@ -123,7 +124,7 @@ monitors.delete("/:id", async (c) => {
123124
// 获取单个监控状态历史
124125
monitors.get("/:id/history", async (c) => {
125126
const id = parseInt(c.req.param("id"));
126-
const payload = c.get("jwtPayload");
127+
const payload = c.get("jwtPayload") as JwtPayload;
127128

128129
// 调用服务层获取监控历史
129130
const result = await MonitorService.getMonitorStatusHistoryById(
@@ -145,7 +146,7 @@ monitors.get("/:id/history", async (c) => {
145146
// 获取单个监控的每日统计数据
146147
monitors.get("/:id/daily", async (c) => {
147148
const id = parseInt(c.req.param("id"));
148-
const payload = c.get("jwtPayload");
149+
const payload = c.get("jwtPayload") as JwtPayload;
149150

150151
// 调用服务层获取每日统计数据
151152
const result = await MonitorService.getMonitorDailyStats(id, payload.id, payload.role);
@@ -160,7 +161,7 @@ monitors.get("/:id/daily", async (c) => {
160161
// 手动检查单个监控
161162
monitors.post("/:id/check", async (c) => {
162163
const id = parseInt(c.req.param("id"));
163-
const payload = c.get("jwtPayload");
164+
const payload = c.get("jwtPayload") as JwtPayload;
164165

165166
// 调用服务层手动检查监控,并传入用户信息以进行权限验证
166167
const result = await MonitorService.manualCheckMonitor(id, payload.id, payload.role);

backend/src/api/notifications.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { Hono } from "hono";
2+
import { JwtPayload } from "../types";
23
import { z } from "zod";
34
import { Bindings } from "../models/db";
45
import * as NotificationService from "../services/NotificationService";
56

6-
const notifications = new Hono<{ Bindings: Bindings; Variables: { jwtPayload: any } }>();
7+
const notifications = new Hono<{ Bindings: Bindings; Variables: { jwtPayload: JwtPayload } }>();
78

89
// 获取通知配置
910
notifications.get("/", async (c) => {
1011
try {
11-
const userId = c.get("jwtPayload").id;
12+
const userId = (c.get("jwtPayload") as JwtPayload).id;
1213
const config = await NotificationService.getNotificationConfig(userId);
1314

1415
return c.json({
@@ -31,7 +32,7 @@ notifications.get("/", async (c) => {
3132
// 获取通知渠道列表
3233
notifications.get("/channels", async (c) => {
3334
try {
34-
const userId = c.get("jwtPayload").id;
35+
const userId = (c.get("jwtPayload") as JwtPayload).id;
3536
const channels = await NotificationService.getNotificationChannels(userId);
3637

3738
return c.json({
@@ -55,7 +56,7 @@ notifications.get("/channels", async (c) => {
5556
notifications.get("/channels/:id", async (c) => {
5657
try {
5758
const id = parseInt(c.req.param("id"));
58-
const userId = c.get("jwtPayload").id;
59+
const userId = (c.get("jwtPayload") as JwtPayload).id;
5960

6061
if (isNaN(id)) {
6162
return c.json(
@@ -99,7 +100,7 @@ notifications.get("/channels/:id", async (c) => {
99100
// 创建通知渠道
100101
notifications.post("/channels", async (c) => {
101102
try {
102-
const userId = c.get("jwtPayload").id;
103+
const userId = (c.get("jwtPayload") as JwtPayload).id;
103104
const body = await c.req.json();
104105

105106
// 验证请求数据
@@ -160,7 +161,7 @@ notifications.post("/channels", async (c) => {
160161
notifications.put("/channels/:id", async (c) => {
161162
try {
162163
const id = parseInt(c.req.param("id"));
163-
const userId = c.get("jwtPayload").id;
164+
const userId = (c.get("jwtPayload") as JwtPayload).id;
164165

165166
if (isNaN(id)) {
166167
return c.json(
@@ -225,7 +226,7 @@ notifications.put("/channels/:id", async (c) => {
225226
notifications.delete("/channels/:id", async (c) => {
226227
try {
227228
const id = parseInt(c.req.param("id"));
228-
const userId = c.get("jwtPayload").id;
229+
const userId = (c.get("jwtPayload") as JwtPayload).id;
229230

230231
if (isNaN(id)) {
231232
return c.json(
@@ -270,7 +271,7 @@ notifications.delete("/channels/:id", async (c) => {
270271
// 获取通知模板列表
271272
notifications.get("/templates", async (c) => {
272273
try {
273-
const userId = c.get("jwtPayload").id;
274+
const userId = (c.get("jwtPayload") as JwtPayload).id;
274275
const templates = await NotificationService.getNotificationTemplates(userId);
275276

276277
return c.json({
@@ -294,7 +295,7 @@ notifications.get("/templates", async (c) => {
294295
notifications.get("/templates/:id", async (c) => {
295296
try {
296297
const id = parseInt(c.req.param("id"));
297-
const userId = c.get("jwtPayload").id; // 获取 userId
298+
const userId = (c.get("jwtPayload") as JwtPayload).id; // 获取 userId
298299

299300
if (isNaN(id)) {
300301
return c.json(
@@ -338,7 +339,7 @@ notifications.get("/templates/:id", async (c) => {
338339
// 创建通知模板
339340
notifications.post("/templates", async (c) => {
340341
try {
341-
const userId = c.get("jwtPayload").id;
342+
const userId = (c.get("jwtPayload") as JwtPayload).id;
342343
const body = await c.req.json();
343344

344345
// 验证请求数据
@@ -402,7 +403,7 @@ notifications.post("/templates", async (c) => {
402403
notifications.put("/templates/:id", async (c) => {
403404
try {
404405
const id = parseInt(c.req.param("id"));
405-
const userId = c.get("jwtPayload").id; // 获取 userId
406+
const userId = (c.get("jwtPayload") as JwtPayload).id; // 获取 userId
406407

407408
if (isNaN(id)) {
408409
return c.json(
@@ -465,7 +466,7 @@ notifications.put("/templates/:id", async (c) => {
465466
notifications.delete("/templates/:id", async (c) => {
466467
try {
467468
const id = parseInt(c.req.param("id"));
468-
const userId = c.get("jwtPayload").id; // 获取 userId
469+
const userId = (c.get("jwtPayload") as JwtPayload).id; // 获取 userId
469470

470471
if (isNaN(id)) {
471472
return c.json(
@@ -510,7 +511,7 @@ notifications.delete("/templates/:id", async (c) => {
510511
// 保存通知设置
511512
notifications.post("/settings", async (c) => {
512513
try {
513-
const userId = c.get("jwtPayload").id;
514+
const userId = (c.get("jwtPayload") as JwtPayload).id;
514515
const body = await c.req.json();
515516

516517
const schema = z.object({

backend/src/api/settings.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { Hono } from "hono";
2+
import { JwtPayload } from "../types";
23
import { Bindings } from "../models/db";
34
import * as SettingsService from "../services/SettingsService";
45
import { jwtMiddleware } from "../middlewares";
56

6-
const settings = new Hono<{ Bindings: Bindings; Variables: { jwtPayload: any } }>();
7+
const settings = new Hono<{ Bindings: Bindings; Variables: { jwtPayload: JwtPayload } }>();
78

89
// 公开端点,用于检查是否允许注册
910
settings.get("/allow_new_user_registration", async (c) => {
@@ -16,7 +17,7 @@ settings.use("/*", jwtMiddleware);
1617

1718
// 获取所有设置
1819
settings.get("/", async (c) => {
19-
const payload = c.get("jwtPayload");
20+
const payload = c.get("jwtPayload") as JwtPayload;
2021
if (payload.role !== 'admin') {
2122
return c.json({ success: false, message: '无权访问' }, 403);
2223
}
@@ -26,7 +27,7 @@ settings.get("/", async (c) => {
2627

2728
// 更新新用户注册设置
2829
settings.put("/allow_new_user_registration", async (c) => {
29-
const payload = c.get("jwtPayload");
30+
const payload = c.get("jwtPayload") as JwtPayload;
3031
if (payload.role !== 'admin') {
3132
return c.json({ success: false, message: '无权操作' }, 403);
3233
}

backend/src/api/status.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Hono } from "hono";
2+
import { JwtPayload } from "../types";
23
import { Bindings } from "../models/db";
34
import {
45
getStatusPageConfig,
@@ -17,11 +18,11 @@ interface StatusPageConfig {
1718
}
1819

1920
// 创建API路由
20-
const status = new Hono<{ Bindings: Bindings }>();
21+
const status = new Hono<{ Bindings: Bindings; Variables: { jwtPayload: JwtPayload } }>();
2122

2223
// 获取状态页配置(管理员)
2324
status.get("/config", async (c) => {
24-
const payload = c.get("jwtPayload");
25+
const payload = c.get("jwtPayload") as JwtPayload;
2526
const userId = payload.id;
2627

2728
try {
@@ -35,7 +36,7 @@ status.get("/config", async (c) => {
3536

3637
// 保存状态页配置
3738
status.post("/config", async (c) => {
38-
const payload = c.get("jwtPayload");
39+
const payload = c.get("jwtPayload") as JwtPayload;
3940
const userId = payload.id;
4041
const data = (await c.req.json()) as StatusPageConfig;
4142

0 commit comments

Comments
 (0)