refactor: 全面优化后端代码质量与架构
- app.ts 单体路由拆分为 routes/ + helpers + middleware + static 独立模块 - 类型去重:CheckFailure/CheckResult 以 shared/api.ts 为唯一源头,收紧 phase 联合类型 - es-toolkit 替换:isPlainObject/isNil/isEmptyObject/isEqual/isError/Semaphore/groupBy - Bun 内置 API:Object.fromEntries 替代手写 headersToRecord - bun:sqlite 规范:prepare() → query() 利用内置缓存,避免 N+1 查询 - 新增 getLatestChecksMap/allGetTargetStats 批量查询方法 - 新增 backend-code-quality/api-route-separation/batch-data-queries 规范 - 补充 openspec/config.yaml 后端开发规范与 DEVELOPMENT.md 后端开发指引
This commit is contained in:
@@ -1,14 +1,7 @@
|
||||
import type { CheckResult, ResolvedHttpTarget } from "./types";
|
||||
import { checkHttpExpect } from "./expect/http";
|
||||
import { errorFailure } from "./expect/failure";
|
||||
|
||||
function headersToRecord(headers: Headers): Record<string, string> {
|
||||
const result: Record<string, string> = {};
|
||||
headers.forEach((value, key) => {
|
||||
result[key] = value;
|
||||
});
|
||||
return result;
|
||||
}
|
||||
import { isError } from "es-toolkit";
|
||||
|
||||
export async function runHttpCheck(target: ResolvedHttpTarget): Promise<CheckResult> {
|
||||
const timestamp = new Date().toISOString();
|
||||
@@ -27,7 +20,7 @@ export async function runHttpCheck(target: ResolvedHttpTarget): Promise<CheckRes
|
||||
|
||||
const durationMs = Math.round(performance.now() - start);
|
||||
const statusCode = response.status;
|
||||
const responseHeaders = headersToRecord(response.headers);
|
||||
const responseHeaders = Object.fromEntries(response.headers);
|
||||
|
||||
const hasBodyRules = !!(target.expect?.body && target.expect.body.length > 0);
|
||||
|
||||
@@ -93,7 +86,7 @@ export async function runHttpCheck(target: ResolvedHttpTarget): Promise<CheckRes
|
||||
failure: errorFailure(
|
||||
"status",
|
||||
"request",
|
||||
isTimeout ? `请求超时 (${target.timeoutMs}ms)` : error instanceof Error ? error.message : String(error),
|
||||
isTimeout ? `请求超时 (${target.timeoutMs}ms)` : isError(error) ? error.message : String(error),
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user