Files
Jyotisha/frontend/src/lib/chart-view-client.ts
T
Jesse_ChenandClaude Opus 5.5 06d0c7dc37 Cold /chart, /people, /synastry read in one round and stop re-reading the shell's data (issue #127, BUG-1478)
- The HTML early-read script also runs on /chart, /people and /synastry:
  account, people list and the current person's sessions, plus on /chart the
  remembered person's chart-view (bound to that person, page deadline kept).
  The model list stays home-only.
- Account and people-list early reads hand one clone to the shell and one to
  the page, once each, in either order; later reloads go to the network.
- Synastry sends product-access, account, people and history together; access
  is still decided before anything about people paints.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 12:11:03 +08:00

59 lines
2.8 KiB
TypeScript

import { withBootFallback } from "./boot-early-reads.ts";
import { chartViewResponseSchema, type ChartViewResponse } from "./chart-view-contract.ts";
import type { ChartViewLayer } from "./chart-view-engine.ts";
import { chartViewFailureResponse } from "./chart-view-failure.ts";
export async function fetchChartView(input: {
layers?: readonly ChartViewLayer[];
signal?: AbortSignal;
subjectId?: string;
/** Transit layer day (YYYY-MM-DD); the server uses today when absent. */
transitDate?: string;
/** The HTML-sent first read of a cold `/chart` (issue #127), taken by the caller. */
early?: Promise<Response> | null;
}): Promise<{ httpStatus: number; body: ChartViewResponse | null }> {
const params = new URLSearchParams();
if (input.layers?.length) params.set("layers", input.layers.join(","));
if (input.subjectId) params.set("subject", input.subjectId);
if (input.transitDate) params.set("transitDate", input.transitDate);
const serialized = params.toString();
const query = serialized ? `?${serialized}` : "";
const fresh = () => fetch(`/api/chart-view${query}`, {
credentials: "same-origin",
headers: { Accept: "application/json" },
cache: "no-store",
signal: input.signal,
});
const response = await withBootFallback(input.early ? untilAborted(input.early, input.signal) : null, fresh);
// Authentication is known from the headers; a missing body must not keep
// a signed-out reader waiting for the JSON deadline.
if (response.status === 401) return { httpStatus: 401, body: null };
let json: unknown = null;
try {
json = await response.json();
} catch (error) {
// Aborting while reading the body is still a timeout, not malformed JSON.
if (input.signal?.aborted) throw input.signal.reason;
const errorName = error instanceof Error ? error.name : "Error";
console.warn("chart_view_client_bad_payload", { errorName, httpStatus: response.status });
return { httpStatus: response.status, body: chartViewFailureResponse("response_schema_failure") };
}
const parsed = chartViewResponseSchema.safeParse(json);
if (!parsed.success) {
console.warn("chart_view_client_schema_failed", { httpStatus: response.status });
return { httpStatus: response.status, body: chartViewFailureResponse("response_schema_failure") };
}
return { httpStatus: response.status, body: parsed.data };
}
/** The early read was sent without the page's deadline; the deadline still applies. */
function untilAborted(early: Promise<Response>, signal: AbortSignal | undefined): Promise<Response> {
if (!signal) return early;
if (signal.aborted) return Promise.reject(signal.reason);
return new Promise((resolve, reject) => {
const abort = () => reject(signal.reason);
signal.addEventListener("abort", abort, { once: true });
early.then(resolve, reject).finally(() => signal.removeEventListener("abort", abort));
});
}