Init Main project
This commit is contained in:
commit
9e581ae67f
220 changed files with 253987 additions and 0 deletions
170
src/client/api.ts
Normal file
170
src/client/api.ts
Normal file
|
|
@ -0,0 +1,170 @@
|
|||
// Client HTTP API wrapper, token/settings localStorage helpers -> (this fails half the time but oh well), and SSE stream connection.
|
||||
import type {
|
||||
LockedDesignItemInfo,
|
||||
RocketDesign,
|
||||
SavedRocketBlueprint,
|
||||
} from "../shared/types.js";
|
||||
|
||||
export interface ClientSettings {
|
||||
quality: "low" | "medium" | "high";
|
||||
sound: boolean;
|
||||
notifications: boolean;
|
||||
dynamicTheme: boolean;
|
||||
colorMode: "dark" | "light";
|
||||
reducedEffects: boolean;
|
||||
}
|
||||
|
||||
const TOKEN_KEY = "spacegaem_proto_token";
|
||||
const SETTINGS_KEY = "spacegaem_proto_settings";
|
||||
|
||||
export function getToken(): string | null {
|
||||
return localStorage.getItem(TOKEN_KEY);
|
||||
}
|
||||
|
||||
export function setToken(t: string | null): void {
|
||||
if (t) localStorage.setItem(TOKEN_KEY, t);
|
||||
else localStorage.removeItem(TOKEN_KEY);
|
||||
}
|
||||
|
||||
export function getSettings(): ClientSettings {
|
||||
try {
|
||||
const raw = JSON.parse(
|
||||
localStorage.getItem(SETTINGS_KEY) ?? "{}",
|
||||
) as Partial<ClientSettings>;
|
||||
return {
|
||||
quality: raw.quality ?? "high",
|
||||
sound: raw.sound ?? true,
|
||||
notifications: raw.notifications ?? true,
|
||||
dynamicTheme: raw.dynamicTheme ?? true,
|
||||
colorMode: raw.colorMode === "light" ? "light" : "dark",
|
||||
reducedEffects: raw.reducedEffects ?? false,
|
||||
};
|
||||
} catch {
|
||||
return {
|
||||
quality: "high",
|
||||
sound: true,
|
||||
notifications: true,
|
||||
dynamicTheme: true,
|
||||
colorMode: "dark",
|
||||
reducedEffects: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export function saveSettings(s: ClientSettings): void {
|
||||
localStorage.setItem(SETTINGS_KEY, JSON.stringify(s));
|
||||
document.body.dataset.dynamicTheme = s.dynamicTheme ? "on" : "off";
|
||||
document.documentElement.dataset.colorMode = s.colorMode;
|
||||
document.documentElement.classList.toggle(
|
||||
"reduced-effects",
|
||||
!!s.reducedEffects,
|
||||
);
|
||||
}
|
||||
|
||||
async function request<T = any>(
|
||||
method: string,
|
||||
url: string,
|
||||
body?: unknown,
|
||||
auth = true,
|
||||
): Promise<T> {
|
||||
const headers: Record<string, string> = {};
|
||||
if (body !== undefined) headers["Content-Type"] = "application/json";
|
||||
const token = getToken();
|
||||
if (auth && token) headers.Authorization = `Bearer ${token}`;
|
||||
const res = await fetch(url, {
|
||||
method,
|
||||
headers,
|
||||
body: body !== undefined ? JSON.stringify(body) : undefined,
|
||||
});
|
||||
const text = await res.text();
|
||||
let data: any = null;
|
||||
try {
|
||||
data = text ? JSON.parse(text) : null;
|
||||
} catch {
|
||||
data = { error: text };
|
||||
}
|
||||
if (!res.ok) {
|
||||
throw new Error(data?.error ?? `HTTP ${res.status}`);
|
||||
}
|
||||
return data as T;
|
||||
}
|
||||
|
||||
export const api = {
|
||||
get: <T = any>(url: string, auth = true) =>
|
||||
request<T>("GET", url, undefined, auth),
|
||||
post: <T = any>(url: string, body: unknown, auth = true) =>
|
||||
request<T>("POST", url, body, auth),
|
||||
delete: <T = any>(url: string, auth = true) =>
|
||||
request<T>("DELETE", url, undefined, auth),
|
||||
getMlStatus: () => request<any>("GET", "/api/ml-status", undefined, false),
|
||||
toggleMlBridge: (enabled?: boolean) =>
|
||||
request<any>("POST", "/api/ml-toggle", { enabled }, true),
|
||||
getDesigns: () =>
|
||||
request<{
|
||||
ok: boolean;
|
||||
myDesigns: SavedRocketBlueprint[];
|
||||
communityDesigns: SavedRocketBlueprint[];
|
||||
}>("GET", "/api/agency/designs", undefined, true),
|
||||
saveDesignBlueprint: (payload: {
|
||||
id?: string;
|
||||
name: string;
|
||||
design: RocketDesign;
|
||||
notes?: string;
|
||||
isPublic?: boolean;
|
||||
targetHint?: string;
|
||||
}) =>
|
||||
request<{
|
||||
ok: boolean;
|
||||
blueprint: SavedRocketBlueprint;
|
||||
shareCode: string;
|
||||
myDesigns: SavedRocketBlueprint[];
|
||||
communityDesigns: SavedRocketBlueprint[];
|
||||
}>("POST", "/api/agency/designs", payload, true),
|
||||
deleteDesignBlueprint: (id: string) =>
|
||||
request<{
|
||||
ok: boolean;
|
||||
myDesigns: SavedRocketBlueprint[];
|
||||
communityDesigns: SavedRocketBlueprint[];
|
||||
}>(
|
||||
"DELETE",
|
||||
`/api/agency/designs/${encodeURIComponent(id)}`,
|
||||
undefined,
|
||||
true,
|
||||
),
|
||||
importDesignBlueprint: (payload: {
|
||||
codeOrJson: string;
|
||||
name?: string;
|
||||
saveToHangar?: boolean;
|
||||
}) =>
|
||||
request<{
|
||||
ok: boolean;
|
||||
design: RocketDesign;
|
||||
shareCode: string;
|
||||
lockedItems: LockedDesignItemInfo[];
|
||||
summary: {
|
||||
totalDv: number;
|
||||
totalCost: number;
|
||||
stageCount: number;
|
||||
designTier: string;
|
||||
};
|
||||
blueprint?: SavedRocketBlueprint;
|
||||
myDesigns?: SavedRocketBlueprint[];
|
||||
}>("POST", "/api/agency/designs/import", payload, true),
|
||||
};
|
||||
|
||||
export interface LiveEvent {
|
||||
type: string;
|
||||
data?: Record<string, any>;
|
||||
at?: number;
|
||||
}
|
||||
|
||||
export function connectEvents(onEvent: (ev: LiveEvent) => void): EventSource {
|
||||
const es = new EventSource("/api/events");
|
||||
es.onmessage = (msg) => {
|
||||
try {
|
||||
const parsed = JSON.parse(msg.data) as LiveEvent;
|
||||
onEvent(parsed);
|
||||
} catch {}
|
||||
};
|
||||
return es;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue