INNER CODE UNIT · JavaScript
api
rootSunc/CNEquity · frontend/src/main.js:108
async function api(path, options) {
const sep = path.includes("?") ? "&" : "?";
const url = TOKEN ? `${path}${sep}token=${encodeURIComponent(TOKEN)}` : path;
const res = await fetch(url, options);
if (!res.ok) {
// Surface what the server said. A 422 here is usually a real contract
// message ("requires as_of= for point-in-time queries"), and showing the
// status code instead throws away the one useful part.
let detail = `${path} → ${res.status}`;
try {
const body = await res.json();
if (body?.detail) detail = typeof body.detail === "string" ? body.detail : JSON.stringify(body.detail);
} catch {
/* not JSON, keep the status line */
}
throw new Error(detail);
}
return res.json();