Files
watchman/web/apps/console/src/queries/preview.ts
T
unknownandCursor 21cc721fcc feat(queries): 完成 M04 查询定义与版本激活
交付不可变查询版本、保存即生效与激活(D26),并带控制台列表筛选排序与试跑预览;补 RLS 下 current_version_no 回填迁移。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-18 18:32:24 +08:00

64 lines
2.4 KiB
TypeScript

/**
* Maps a preview response (or a refused call) to the panel the console shows.
* Boundary: pure mapping, no I/O. The outcome is only ever held in memory for display; the
* console never stores sample rows. Server message text is not shown, only the status and
* failure kind pick the wording.
*/
import type { PreviewFailure, PreviewIllegalType, PreviewOutcome, PreviewRows } from "./types";
import { isDisabledRejection } from "./errors";
export const FAILURE_KINDS = [
"timeout",
"unreachable",
"auth_failed",
"rejected",
"sql_error",
"limit_exceeded",
"validation",
"engine_readonly_unsupported",
] as const;
export type PreviewView =
| { kind: "none" }
| { kind: "running" }
| { kind: "success"; outcome: PreviewRows }
| { kind: "empty"; outcome: PreviewRows }
| { kind: "illegal_type"; outcome: PreviewIllegalType }
| { kind: "failure"; failureKind: string; outcome: PreviewFailure }
/** scope "query": the definition is disabled (HTTP 409); "datasource": its source is disabled. */
| { kind: "disabled"; scope: "query" | "datasource" }
| { kind: "circuit_open"; until: string | null }
| { kind: "error"; text: string };
/** Connection-class failures count against the datasource circuit (M03); the others do not. */
export function isConnectionKind(kind: string): boolean {
return kind === "unreachable" || kind === "auth_failed" || kind === "rejected";
}
export function viewFromOutcome(outcome: PreviewOutcome): PreviewView {
if (outcome.status === "illegal_type") {
return { kind: "illegal_type", outcome };
}
if (outcome.status !== "failure") {
return outcome.status === "success" ? { kind: "success", outcome } : { kind: "empty", outcome };
}
const kind = outcome.failure_kind ?? "unknown";
if (kind === "disabled") {
return { kind: "disabled", scope: "datasource" };
}
if (kind === "circuit_open") {
const until = outcome.details?.circuit_open_until;
return { kind: "circuit_open", until: typeof until === "string" ? until : null };
}
return { kind: "failure", failureKind: kind, outcome };
}
/** A refused preview call: 409 disabled gets its own panel; anything else is a plain error. */
export function viewFromError(error: unknown, describe: (error: unknown) => string): PreviewView {
if (isDisabledRejection(error)) {
return { kind: "disabled", scope: "query" };
}
return { kind: "error", text: describe(error) };
}