/** Server-side SDK. Never bundle the integration key into a CAD's browser application. */ export interface CaptureInput { member_id?: string; external_unit_id?: string; pre_ms?: number; external_case_id?: string; external_actor_id?: string; } export interface CaptureResult { id: string; status: | "accepted" | "delivered" | "acknowledged" | "upload_queued" | "ready" | "failed" | "expired"; clip_id: string | null; requested_pre_ms: number; effective_pre_ms: number; expires_at: number; failure_code?: string | null; } export interface UnitInput { member_id?: string; roblox_user_id?: string; callsign?: string; badge_number?: string; department?: { external_id?: string; name?: string }; expected_revision?: number; } export interface ViewerInput { resource_type: "clip" | "preview" | "timeline"; origin: string; clip_id?: string; member_id?: string; external_unit_id?: string; external_case_id?: string; } export interface ViewerResult { session: { id: string; resource_type: ViewerInput["resource_type"]; expires_at: number; bootstrap_expires_at: number; }; embed_url: string; } export interface TimelineEventInput { external_event_id: string; type: string; occurred_at: number; note?: string; member_id?: string; external_unit_id?: string; } export interface ApiFailure { error: { code: string; message: string }; } export class AxionError extends Error { constructor( public readonly status: number, public readonly code: string, message: string, public readonly retryAfter: string | null, ) { super(message); this.name = "AxionError"; } } export class Axion { private readonly root: string; constructor( baseURL: string, private readonly key: string, private readonly transport: typeof fetch = fetch, ) { this.root = baseURL.replace(/\/$/, "") + "/api/external/v1"; } async request( method: string, path: string, input?: | CaptureInput | UnitInput | ViewerInput | TimelineEventInput | { clip_id: string; external_case_id: string }, idempotencyKey?: string, ): Promise { if (!path.startsWith("/") || path.startsWith("//")) throw new Error("Expected an API-relative path"); const headers = new Headers({ Authorization: `Bearer ${this.key}`, "Content-Type": "application/json", }); if (idempotencyKey) headers.set("Idempotency-Key", idempotencyKey); const options: RequestInit = { method, headers, redirect: "error", signal: AbortSignal.timeout(15_000), }; if (input) options.body = JSON.stringify(input); const response = await this.transport(this.root + path, options); if (!response.ok) { // SAFETY: Axion's error handler emits ApiFailure on failed API calls. const failure = (await response.json()) as ApiFailure; throw new AxionError( response.status, failure.error.code, failure.error.message, response.headers.get("retry-after"), ); } // SAFETY: T is the response contract for the selected endpoint; 204 has no response body. return (response.status === 204 ? undefined : await response.json()) as T; } capture(input: CaptureInput, key: string) { return this.request<{ request: CaptureResult }>( "POST", "/capture-requests", input, key, ); } captureResult(id: string) { return this.request<{ request: CaptureResult }>( "GET", `/capture-requests/${encodeURIComponent(id)}`, ); } viewer(input: ViewerInput, key: string) { return this.request("POST", "/viewer-sessions", input, key); } mapUnit(id: string, input: UnitInput, key: string) { return this.request( "PUT", `/unit-mappings/${encodeURIComponent(id)}`, input, key, ); } appendEvent(caseId: string, input: TimelineEventInput, key: string) { return this.request( "POST", `/cases/${encodeURIComponent(caseId)}/events`, input, key, ); } linkEvidence(clipId: string, caseId: string, key: string) { return this.request( "POST", "/evidence-links", { clip_id: clipId, external_case_id: caseId }, key, ); } }