feat(infra): 落地 M01 基础框架与本机开发环境
交付 FastAPI/Celery/Vue 薄壳、FORCE RLS、Compose 回环与 review 修复证据,关闭 M01 DONE_LOCAL。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
8b2a809f06
commit
8fb16f4a4a
97 files changed
+8059
-22
No files matched your search
@@ -0,0 +1,93 @@
|
||||
/**
|
||||
* Shared JSON API client.
|
||||
* Parses the company success/error envelope and throws BusinessError on failure.
|
||||
*/
|
||||
|
||||
export type ApiSuccess<T> = {
|
||||
success: true;
|
||||
data: T;
|
||||
request_id: string;
|
||||
};
|
||||
|
||||
export type ApiFailure = {
|
||||
success: false;
|
||||
error: {
|
||||
code: number;
|
||||
message: string;
|
||||
details?: Record<string, unknown>;
|
||||
retryable?: boolean;
|
||||
permission_node?: string;
|
||||
};
|
||||
request_id: string;
|
||||
};
|
||||
|
||||
export class BusinessError extends Error {
|
||||
readonly code: number;
|
||||
readonly details: Record<string, unknown>;
|
||||
readonly status: number;
|
||||
readonly requestId: string;
|
||||
readonly retryable: boolean;
|
||||
|
||||
constructor(args: {
|
||||
code: number;
|
||||
message: string;
|
||||
details?: Record<string, unknown>;
|
||||
status: number;
|
||||
requestId: string;
|
||||
retryable?: boolean;
|
||||
}) {
|
||||
super(args.message);
|
||||
this.name = "BusinessError";
|
||||
this.code = args.code;
|
||||
this.details = args.details ?? {};
|
||||
this.status = args.status;
|
||||
this.requestId = args.requestId;
|
||||
this.retryable = args.retryable ?? false;
|
||||
}
|
||||
}
|
||||
|
||||
function requestIdFrom(response: Response): string {
|
||||
return response.headers.get("X-Request-Id") ?? "unknown";
|
||||
}
|
||||
|
||||
export async function requestJson<T>(
|
||||
input: string,
|
||||
init: RequestInit = {},
|
||||
): Promise<T> {
|
||||
const headers = new Headers(init.headers);
|
||||
if (!headers.has("Accept")) {
|
||||
headers.set("Accept", "application/json");
|
||||
}
|
||||
const response = await fetch(input, { ...init, headers });
|
||||
let body: ApiSuccess<T> | ApiFailure;
|
||||
try {
|
||||
body = (await response.json()) as ApiSuccess<T> | ApiFailure;
|
||||
} catch {
|
||||
throw new BusinessError({
|
||||
code: 50000,
|
||||
message: "Invalid API envelope",
|
||||
status: response.status,
|
||||
requestId: requestIdFrom(response),
|
||||
retryable: true,
|
||||
});
|
||||
}
|
||||
if (!body || typeof body !== "object" || !("success" in body)) {
|
||||
throw new BusinessError({
|
||||
code: 50000,
|
||||
message: "Invalid API envelope",
|
||||
status: response.status,
|
||||
requestId: requestIdFrom(response),
|
||||
});
|
||||
}
|
||||
if (body.success) {
|
||||
return body.data;
|
||||
}
|
||||
throw new BusinessError({
|
||||
code: body.error.code,
|
||||
message: body.error.message,
|
||||
details: body.error.details,
|
||||
status: response.status,
|
||||
requestId: body.request_id,
|
||||
retryable: body.error.retryable,
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user