refactor(前端): 拆掉 camelCase→snake_case 转换层,契约成为唯一真相
utils/legacy.ts 是迁移期的临时层:新后端一律 camelCase,而组件读的还是
旧 Django 的 snake_case,于是在 api 层做一次递归键名重写。它自己的注释就
写了「迁移完成后这一层应当整体拆掉」。现在拆了。
代价不只是那 96 处包装:每个响应都要递归遍历整个对象重写一遍键名,而且
utils/types.ts 和 packages/contract 是两份真相 —— 手抄的那份还抄歪了好几处。
做法是按域推进,每域都用 vue-tsc 相对基线做差,确认零新增错误后再往下走。
前端的类型现在一律以契约为准,只在必要处窄化(比如 languages/template 的键
窄化成 LANGUAGE),删掉的重复定义包括 WebsiteConfig、LoginSummary、
AchievementSummary、ProblemSet、Contest、User、Profile、AdminTag、
StuckProblem 等等,其中 ClassComparison 有两个组件各手抄了一份。
## 顺带修掉的真 bug
- 管理端公告列表的「可见」开关每次都 400:列表响应被契约 omit 掉了 content,
而更新接口要求 content 必填,toggleVisible 把列表行原样回传。而且是乐观
翻转、不 await 不 catch,管理员看到开关动了、实际没存也没有提示。
改成先 GET 整条再 PUT,加失败提示。
- 删有提交的题时只显示笼统的「删除失败」:前端还在 match 旧 Django 的英文
文案,而后端返回的是 problem-has-submissions + 中文。连同另外 8 处同类
匹配一起改成判错误码 —— 文案是后端随时能改的,match 文案改一个字就静默失效。
- SubmissionStatus.time_limit_exceeded 写成 `1 | 2`,TS 按位或算成 3,和
memory_limit_exceeded 撞了同一个值。后端 judge/status.ts 里这是分开的
两个码,按后端拆成 cpu_/real_ 两项。当前没有代码读这两个成员,但
CLAUDE.md 明确要求判题状态码三处同步。
- 流程图历史翻到没有提交的那一页会直接抛:契约里 submission 是 nullable,
被 any 掩盖成看起来非空。补了 null 分支。
## 契约里被逼出来的三处不诚实
- grade 写成 z.string(),但 averageGrade() 在没有可用数据时返回空串,
前端三张图表拿它查 Record<Grade,...> 会查出 undefined。按实际收紧成
z.enum([...,""]),四个查表点都补了「无评级」分支。
- difficulty 写成 z.string()。核对过生产库 dump:956 道题只有
Low/Mid/High 三个值(761/149/46)。收紧成枚举。
- topReaction 写成 z.string(),既对不上前端渲染的 {type,count},也对不上
旧后端 get_top_reactions 下发的形状。改成正确形状并注明当前恒传 null。
## 明确保留 snake_case 的 54 处
判题沙箱原始输出(cpu_time/exit_code/output_md5/compile_output)、
statistic_info 内容(err_info/time_cost/ast_results)、submission_info
JSONB(is_ac/ac_time/error_number,回滚时旧后端还要读)、SQL 判题引擎的
total_rows/order_sensitive/changed_tables、WebSocket 的 submission_id、
以及数据库选项键 enable_maxkb。每一处都在类型定义旁写了为什么不能改。
language 没有跟着收紧契约 —— 它是配置项、随时可能加语言,收紧会让新语言
在后端 parse 时直接抛。改在 api 边界一处窄化。
## 另外
- utils/http.ts 整个模块已是死代码(四处引用全是 import type),删除。
- profile 的 blog/github/school/major/language 五个字段全链路空转,没有
任何组件读,从契约到类型一并摘除(数据库列不动)。
- admin/account.ts 往 user_profile 塞的 totalScore 是 OI 模式遗留,表里
没这一列。Drizzle 按表定义拼列名会把它静默丢弃,所以没出过错,是死代码。
验证:vue-tsc 143 → 54 条且无新增,apps/api tsc、check:routes、web build
全通过;各域响应形状逐条打接口核对过。
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -9,28 +9,10 @@ export const registerRequestSchema = z.object({
|
||||
password: z.string().min(6).max(20),
|
||||
})
|
||||
|
||||
/**
|
||||
* 对齐旧后端 `account/serializers.py:125,127` 的 `serializers.URLField`:
|
||||
* 这两个字段会被前端渲染成可点击链接,放任自由字符串等于允许写入
|
||||
* `javascript:` 一类的伪协议。只放行 http/https,空串与 null 表示「清空」。
|
||||
*/
|
||||
const linkField = z
|
||||
.string()
|
||||
.max(256)
|
||||
.refine(
|
||||
(value) => value === "" || /^https?:\/\/\S+$/i.test(value),
|
||||
"必须是以 http:// 或 https:// 开头的网址",
|
||||
)
|
||||
|
||||
export const updateProfileRequestSchema = z.object({
|
||||
realName: z.string().max(32).nullable().optional(),
|
||||
avatar: z.string().max(256).optional(),
|
||||
blog: linkField.nullable().optional(),
|
||||
mood: z.string().max(256).nullable().optional(),
|
||||
github: linkField.nullable().optional(),
|
||||
school: z.string().max(64).nullable().optional(),
|
||||
major: z.string().max(64).nullable().optional(),
|
||||
language: z.string().max(32).nullable().optional(),
|
||||
})
|
||||
|
||||
export const metricsSchema = z.object({
|
||||
@@ -65,3 +47,7 @@ export const publicProfileSchema = userProfileSchema
|
||||
|
||||
export type RegisterRequest = z.infer<typeof registerRequestSchema>
|
||||
export type UpdateProfileRequest = z.infer<typeof updateProfileRequestSchema>
|
||||
export type ProblemRank = z.infer<typeof problemRankSchema>
|
||||
export type RankProfile = z.infer<typeof rankProfileSchema>
|
||||
export type UserRank = z.infer<typeof userRankSchema>
|
||||
export type ActivityRankItem = z.infer<typeof activityRankItemSchema>
|
||||
|
||||
@@ -32,20 +32,38 @@ export const pendingAchievementSchema = z.object({
|
||||
rarity: achievementRaritySchema,
|
||||
})
|
||||
|
||||
export const achievementRarityStatSchema = z.object({
|
||||
rarity: achievementRaritySchema,
|
||||
label: z.string(),
|
||||
total: z.number().int(),
|
||||
unlocked: z.number().int(),
|
||||
})
|
||||
|
||||
/**
|
||||
* WebSocket 推来的解锁通知。比 `/achievements/pending` 多一个 `kind` ——
|
||||
* 题单奖章和成就来自两张表、id 会重叠,前端靠它区分(见 events.ts 的 publishAchievementNotification)。
|
||||
*/
|
||||
export const achievementNotificationSchema = pendingAchievementSchema.extend({
|
||||
kind: z.enum(["achievement", "badge"]),
|
||||
})
|
||||
|
||||
export const achievementSummarySchema = z.object({
|
||||
username: z.string(),
|
||||
total: z.number().int(),
|
||||
unlocked: z.number().int(),
|
||||
percent: z.number(),
|
||||
rarity: z.array(z.object({
|
||||
rarity: achievementRaritySchema,
|
||||
label: z.string(),
|
||||
total: z.number().int(),
|
||||
unlocked: z.number().int(),
|
||||
})),
|
||||
rarity: z.array(achievementRarityStatSchema),
|
||||
recent: z.array(pendingAchievementSchema),
|
||||
})
|
||||
|
||||
export const markAchievementsReadSchema = z.object({
|
||||
ids: z.array(z.number().int()),
|
||||
})
|
||||
|
||||
export type Achievement = z.infer<typeof achievementSchema>
|
||||
export type AchievementList = z.infer<typeof achievementListSchema>
|
||||
export type PendingAchievement = z.infer<typeof pendingAchievementSchema>
|
||||
export type AchievementSummary = z.infer<typeof achievementSummarySchema>
|
||||
export type AchievementRarity = z.infer<typeof achievementRaritySchema>
|
||||
export type AchievementRarityStat = z.infer<typeof achievementRarityStatSchema>
|
||||
export type AchievementNotification = z.infer<typeof achievementNotificationSchema>
|
||||
|
||||
@@ -2,6 +2,7 @@ import { z } from "zod"
|
||||
|
||||
import { achievementRaritySchema } from "./achievement"
|
||||
import { paginatedSchema, sampleUserSchema } from "./common"
|
||||
import { problemDifficultySchema } from "./problem"
|
||||
|
||||
/**
|
||||
* 后台侧的契约。与 oj 侧分开放:同一张表在两侧下发的字段集通常不同
|
||||
@@ -497,12 +498,16 @@ export const adminProblemListItemSchema = z.object({
|
||||
createdBy: sampleUserSchema,
|
||||
visible: z.boolean(),
|
||||
createTime: z.string(),
|
||||
difficulty: z.string(),
|
||||
difficulty: problemDifficultySchema,
|
||||
tags: z.array(z.string()),
|
||||
hasAstRules: z.boolean(),
|
||||
allowFlowchart: z.boolean(),
|
||||
showFlowchart: z.boolean(),
|
||||
topReaction: z.string().nullable(),
|
||||
// 最高票评价 {type, count}。**当前后端恒传 null** —— 旧后端的
|
||||
// reaction/services.py:get_top_reactions 没有跟着迁过来,这一列现在是空的。
|
||||
topReaction: z
|
||||
.object({ type: z.string(), count: z.number().int() })
|
||||
.nullable(),
|
||||
})
|
||||
|
||||
export const adminProblemListSchema = paginatedSchema(adminProblemListItemSchema)
|
||||
@@ -526,7 +531,7 @@ export const adminProblemSchema = z.object({
|
||||
timeLimit: z.number().int(),
|
||||
memoryLimit: z.number().int(),
|
||||
visible: z.boolean(),
|
||||
difficulty: z.string(),
|
||||
difficulty: problemDifficultySchema,
|
||||
source: z.string().nullable(),
|
||||
submissionNumber: z.number().int(),
|
||||
acceptedNumber: z.number().int(),
|
||||
@@ -621,3 +626,33 @@ export const generateSqlTestCaseRequestSchema = z.object({
|
||||
})
|
||||
|
||||
export const generateSqlTestCaseResponseSchema = z.object({ sql: z.string() })
|
||||
|
||||
export type AdminAchievement = z.infer<typeof adminAchievementSchema>
|
||||
export type AchievementMetric = z.infer<typeof achievementMetricSchema>
|
||||
|
||||
export type AdminProblem = z.infer<typeof adminProblemSchema>
|
||||
export type AdminProblemListItem = z.infer<typeof adminProblemListItemSchema>
|
||||
export type AdminProblemList = z.infer<typeof adminProblemListSchema>
|
||||
export type CreateProblemRequest = z.infer<typeof createProblemRequestSchema>
|
||||
|
||||
export type AdminContest = z.infer<typeof adminContestSchema>
|
||||
export type AdminContestList = z.infer<typeof adminContestListSchema>
|
||||
export type AcmHelperItem = z.infer<typeof acmHelperItemSchema>
|
||||
export type AdminUser = z.infer<typeof adminUserSchema>
|
||||
export type AdminUserList = z.infer<typeof adminUserListSchema>
|
||||
export type JudgeServer = z.infer<typeof judgeServerSchema>
|
||||
export type DashboardInfo = z.infer<typeof dashboardInfoSchema>
|
||||
export type JudgeServerList = z.infer<typeof judgeServerListSchema>
|
||||
export type OrphanTestCase = z.infer<typeof orphanTestCaseSchema>
|
||||
|
||||
export type AdminAiReportListItem = z.infer<typeof adminAiReportListItemSchema>
|
||||
export type AdminAiReport = z.infer<typeof adminAiReportSchema>
|
||||
export type AdminAiReportList = z.infer<typeof adminAiReportListSchema>
|
||||
export type StuckProblem = z.infer<typeof stuckProblemSchema>
|
||||
export type AcTrend = z.infer<typeof acTrendSchema>
|
||||
|
||||
export type AdminTag = z.infer<typeof adminTagSchema>
|
||||
export type RenameTagResponse = z.infer<typeof renameTagResponseSchema>
|
||||
export type BatchProblemTagResponse = z.infer<typeof batchProblemTagResponseSchema>
|
||||
export type SqlTestCaseScript = z.infer<typeof sqlTestCaseScriptSchema>
|
||||
export type GenerateSqlTestCaseResponse = z.infer<typeof generateSqlTestCaseResponseSchema>
|
||||
|
||||
@@ -1,11 +1,17 @@
|
||||
import { z } from "zod"
|
||||
|
||||
/**
|
||||
* 评级。`grade()` 返回 S/A/B/C,`averageGrade()` 在没有可用数据时返回空串 ——
|
||||
* 空串是真会下发的值,别把它从这里去掉,前端要按「无评级」处理。
|
||||
*/
|
||||
export const gradeSchema = z.enum(["S", "A", "B", "C", ""])
|
||||
|
||||
export const durationDataSchema = z.object({
|
||||
unit: z.string(),
|
||||
index: z.number().int(),
|
||||
start: z.string(),
|
||||
end: z.string(),
|
||||
grade: z.string(),
|
||||
grade: gradeSchema,
|
||||
problemCount: z.number().int(),
|
||||
submissionCount: z.number().int(),
|
||||
})
|
||||
@@ -20,7 +26,7 @@ export const solvedProblemSchema = z.object({
|
||||
acTime: z.string(),
|
||||
rank: z.number().int().nullable(),
|
||||
acCount: z.number().int(),
|
||||
grade: z.string(),
|
||||
grade: gradeSchema,
|
||||
periodRank: z.number().int().nullable(),
|
||||
periodAcCount: z.number().int(),
|
||||
difficulty: z.string(),
|
||||
@@ -43,7 +49,7 @@ export const aiDetailSchema = z.object({
|
||||
end: z.string(),
|
||||
solved: z.array(solvedProblemSchema),
|
||||
flowcharts: z.array(flowchartSummarySchema),
|
||||
grade: z.string(),
|
||||
grade: gradeSchema,
|
||||
tags: z.record(z.string(), z.number().int()),
|
||||
difficulty: z.record(z.string(), z.number().int()),
|
||||
contestCount: z.number().int(),
|
||||
@@ -94,3 +100,12 @@ export const loginSummarySchema = z.object({
|
||||
analysis: z.string(),
|
||||
analysisError: z.string().optional(),
|
||||
})
|
||||
|
||||
export type Grade = z.infer<typeof gradeSchema>
|
||||
export type DurationData = z.infer<typeof durationDataSchema>
|
||||
export type SolvedProblem = z.infer<typeof solvedProblemSchema>
|
||||
export type FlowchartSummary = z.infer<typeof flowchartSummarySchema>
|
||||
export type AiDetail = z.infer<typeof aiDetailSchema>
|
||||
export type HeatmapItem = z.infer<typeof heatmapItemSchema>
|
||||
export type AiAnalysisRecord = z.infer<typeof aiAnalysisRecordSchema>
|
||||
export type LoginSummary = z.infer<typeof loginSummarySchema>
|
||||
|
||||
@@ -24,12 +24,7 @@ export const userProfileSchema = z.object({
|
||||
realName: z.string().nullable(),
|
||||
acmProblemsStatus: z.record(z.string(), z.unknown()),
|
||||
avatar: z.string(),
|
||||
blog: z.string().nullable(),
|
||||
mood: z.string().nullable(),
|
||||
github: z.string().nullable(),
|
||||
school: z.string().nullable(),
|
||||
major: z.string().nullable(),
|
||||
language: z.string().nullable(),
|
||||
acceptedNumber: z.number().int(),
|
||||
submissionNumber: z.number().int(),
|
||||
})
|
||||
|
||||
@@ -62,3 +62,8 @@ export const classComparisonResponseSchema = z.object({
|
||||
comparisons: z.array(classComparisonSchema),
|
||||
hasTimeRange: z.boolean(),
|
||||
})
|
||||
|
||||
export type ClassRankItem = z.infer<typeof classRankItemSchema>
|
||||
export type ClassUserRank = z.infer<typeof classUserRankSchema>
|
||||
export type ClassComparison = z.infer<typeof classComparisonSchema>
|
||||
export type ClassComparisonResponse = z.infer<typeof classComparisonResponseSchema>
|
||||
|
||||
@@ -72,3 +72,7 @@ export const exerciseSchema = z.object({
|
||||
data: z.record(z.string(), z.unknown()),
|
||||
order: z.number().int(),
|
||||
})
|
||||
|
||||
export type Message = z.infer<typeof messageSchema>
|
||||
export type MessageList = z.infer<typeof messageListSchema>
|
||||
export type Announcement = z.infer<typeof announcementSchema>
|
||||
|
||||
@@ -40,3 +40,8 @@ export const contestRankItemSchema = z.object({
|
||||
})
|
||||
|
||||
export const contestRankSchema = paginatedSchema(contestRankItemSchema)
|
||||
|
||||
export type Contest = z.infer<typeof contestSchema>
|
||||
export type ContestList = z.infer<typeof contestListSchema>
|
||||
export type ContestRankItem = z.infer<typeof contestRankItemSchema>
|
||||
export type ContestRank = z.infer<typeof contestRankSchema>
|
||||
|
||||
@@ -89,3 +89,10 @@ export const flowchartUpdateSchema = z.object({
|
||||
|
||||
export type FlowchartUpdate = z.infer<typeof flowchartUpdateSchema>
|
||||
export type FlowchartStatistics = z.infer<typeof flowchartStatisticsSchema>
|
||||
export type FlowchartSubmission = z.infer<typeof flowchartSubmissionSchema>
|
||||
export type FlowchartListItem = z.infer<typeof flowchartListItemSchema>
|
||||
export type FlowchartList = z.infer<typeof flowchartListSchema>
|
||||
export type FlowchartCurrent = z.infer<typeof flowchartCurrentSchema>
|
||||
export type FlowchartDetail = z.infer<typeof flowchartDetailSchema>
|
||||
export type CreateFlowchartResponse = z.infer<typeof createFlowchartResponseSchema>
|
||||
export type CreateFlowchartRequest = z.infer<typeof createFlowchartRequestSchema>
|
||||
|
||||
@@ -2,6 +2,12 @@ import { z } from "zod"
|
||||
|
||||
import { paginatedSchema, sampleUserSchema } from "./common"
|
||||
|
||||
/**
|
||||
* 题目难度。生产库 956 道题只有这三个值(旧 Django 的 Problem.difficulty choices
|
||||
* 也是这三个),前端的 DIFFICULTY 映射表按它建 —— 写成 z.string() 的话
|
||||
* 多出来的值会静默渲染成 undefined。
|
||||
*/
|
||||
export const problemDifficultySchema = z.enum(["Low", "Mid", "High"])
|
||||
export const problemDetailSchema = z.object({
|
||||
id: z.number().int(),
|
||||
_id: z.string(),
|
||||
@@ -22,7 +28,7 @@ export const problemDetailSchema = z.object({
|
||||
lastUpdateTime: z.string().nullable(),
|
||||
timeLimit: z.number().int(),
|
||||
memoryLimit: z.number().int(),
|
||||
difficulty: z.string(),
|
||||
difficulty: problemDifficultySchema,
|
||||
source: z.string().nullable(),
|
||||
prompt: z.string().nullable(),
|
||||
submissionNumber: z.number().int(),
|
||||
@@ -55,7 +61,7 @@ export const problemListItemSchema = z.object({
|
||||
title: z.string(),
|
||||
submissionNumber: z.number().int(),
|
||||
acceptedNumber: z.number().int(),
|
||||
difficulty: z.string(),
|
||||
difficulty: problemDifficultySchema,
|
||||
createdBy: sampleUserSchema,
|
||||
tags: z.array(z.string()),
|
||||
contestId: z.number().int().nullable(),
|
||||
@@ -84,3 +90,10 @@ export const yearlyAcSchema = z.object({
|
||||
accepted: z.number().int().nonnegative(),
|
||||
acRate: z.number(),
|
||||
})
|
||||
|
||||
export type ProblemDifficulty = z.infer<typeof problemDifficultySchema>
|
||||
export type ProblemListItem = z.infer<typeof problemListItemSchema>
|
||||
export type ProblemList = z.infer<typeof problemListSchema>
|
||||
export type Tag = z.infer<typeof tagSchema>
|
||||
export type ProblemAuthor = z.infer<typeof problemAuthorSchema>
|
||||
export type YearlyAc = z.infer<typeof yearlyAcSchema>
|
||||
|
||||
@@ -98,3 +98,12 @@ export const userBadgeSchema = z.object({
|
||||
earnedTime: z.string(),
|
||||
problemset: z.object({ id: z.number().int(), title: z.string() }),
|
||||
})
|
||||
|
||||
export type ProblemSet = z.infer<typeof problemSetSchema>
|
||||
export type ProblemSetList = z.infer<typeof problemSetListSchema>
|
||||
export type ProblemSetBadge = z.infer<typeof problemSetBadgeSchema>
|
||||
export type ProblemSetProblem = z.infer<typeof problemSetProblemSchema>
|
||||
export type ProblemSetProgress = z.infer<typeof problemSetProgressSchema>
|
||||
export type ProblemSetProgressList = z.infer<typeof problemSetProgressListSchema>
|
||||
export type UserBadge = z.infer<typeof userBadgeSchema>
|
||||
export type CompletedProblem = z.infer<typeof completedProblemSchema>
|
||||
|
||||
@@ -140,3 +140,8 @@ export type SubmissionStatisticsUser = z.infer<
|
||||
typeof submissionStatisticsUserSchema
|
||||
>
|
||||
export type UnacceptedStudent = z.infer<typeof unacceptedStudentSchema>
|
||||
|
||||
export type SubmissionListItem = z.infer<typeof submissionListItemSchema>
|
||||
export type SubmissionList = z.infer<typeof submissionListSchema>
|
||||
export type EmbeddedSubmission = z.infer<typeof embeddedSubmissionSchema>
|
||||
export type CreateSubmissionResponse = z.infer<typeof createSubmissionResponseSchema>
|
||||
|
||||
Reference in New Issue
Block a user