1
0

chore: 强化代码质量与风格检查体系

ESLint 升级到 recommended-type-checked + stylistic-type-checked,
引入 perfectionist 导入排序和 import 插件导入验证。

Prettier 显式声明全部格式化参数,消除跨环境差异。
TypeScript 启用 noUnusedLocals 和 noPropertyAccessFromIndexSignature。
完善 ignore 列表,排除 .agents/、bun.lock、data/ 等。
引入 husky + lint-staged(pre-commit)+ commitlint(commit-msg)。
更新 DEVELOPMENT.md 代码质量章节。
修复所有新增规则检测到的类型和风格违规。
This commit is contained in:
2026-05-12 18:44:59 +08:00
parent ce8baae3d1
commit a5cf6065c2
83 changed files with 2654 additions and 1824 deletions

View File

@@ -1,6 +1,10 @@
import type { ApiErrorResponse, CheckFailure, CheckResult, HealthResponse, RuntimeMode } from "../shared/api";
import type { StoredCheckResult } from "./checker/types";
export function allowsGetHead(method: string): boolean {
return method === "GET" || method === "HEAD";
}
export function createApiError(error: string, status: number): ApiErrorResponse {
return { error, status };
}
@@ -16,9 +20,23 @@ export function createHeaders(mode: RuntimeMode, init: HeadersInit): Headers {
return headers;
}
export function createHealthResponse(): HealthResponse {
return {
ok: true,
service: "dial-server",
timestamp: new Date().toISOString(),
};
}
export function formatDuration(ms: number): string {
if (ms >= 60000 && ms % 60000 === 0) return `${ms / 60000}m`;
if (ms >= 1000 && ms % 1000 === 0) return `${ms / 1000}s`;
return `${ms}ms`;
}
export function jsonResponse(
body: unknown,
options: { method?: string; mode: RuntimeMode; status?: number; headers?: HeadersInit },
options: { headers?: HeadersInit; method?: string; mode: RuntimeMode; status?: number },
): Response {
const headers = createHeaders(options.mode, {
"Content-Type": "application/json; charset=utf-8",
@@ -27,29 +45,11 @@ export function jsonResponse(
const responseBody = options.method === "HEAD" ? null : JSON.stringify(body);
return new Response(responseBody, {
status: options.status,
headers,
status: options.status,
});
}
export function methodNotAllowedResponse(allow: string[], mode: RuntimeMode): Response {
return jsonResponse(createApiError("Method not allowed", 405), {
mode,
status: 405,
headers: { Allow: allow.join(", ") },
});
}
export function allowsGetHead(method: string): boolean {
return method === "GET" || method === "HEAD";
}
export function formatDuration(ms: number): string {
if (ms >= 60000 && ms % 60000 === 0) return `${ms / 60000}m`;
if (ms >= 1000 && ms % 1000 === 0) return `${ms / 1000}s`;
return `${ms}ms`;
}
export function mapCheckResult(row: StoredCheckResult): CheckResult {
let failure: CheckFailure | null = null;
if (row.failure) {
@@ -62,18 +62,18 @@ export function mapCheckResult(row: StoredCheckResult): CheckResult {
}
return {
timestamp: row.timestamp,
matched: row.matched === 1,
durationMs: row.duration_ms,
statusDetail: row.status_detail,
failure,
matched: row.matched === 1,
statusDetail: row.status_detail,
timestamp: row.timestamp,
};
}
export function createHealthResponse(): HealthResponse {
return {
ok: true,
service: "dial-server",
timestamp: new Date().toISOString(),
};
export function methodNotAllowedResponse(allow: string[], mode: RuntimeMode): Response {
return jsonResponse(createApiError("Method not allowed", 405), {
headers: { Allow: allow.join(", ") },
mode,
status: 405,
});
}