SPB Git

Repository Public

api.ts 4.7 KB 114 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : MacLustr Vision8 * File             : web/src/api.ts9 * Module           : web10 * Repository       : https://git.spboucher.ai/maclustr-vision11 * Project Website  : https://camera.spboucher.ai12 * Created          : 2026-11-3013 * Last Modified    : 2026-11-3014 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: admin@spboucher.ai23 * Technical Contact: dev@spboucher.ai24 * Security/Admin   : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License          : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142// Client de l'API privée. Le cookie de session est HttpOnly : rien n'est conservé côté navigateur.43export class ApiError extends Error {44  status: number;45  code: string;46  data: any;47  constructor(status: number, code: string, message: string, data: any) {48    super(message);49    this.status = status;50    this.code = code;51    this.data = data;52  }53}5455let onUnauthorized: (() => void) | null = null;56export function setUnauthorizedHandler(fn: () => void): void {57  onUnauthorized = fn;58}5960export async function api<T = any>(path: string, opts: { method?: string; body?: unknown; quiet401?: boolean } = {}): Promise<T> {61  const method = opts.method || 'GET';62  const headers: Record<string, string> = {};63  if (method !== 'GET') {64    headers['X-MLV'] = '1';65    headers['Content-Type'] = 'application/json';66  }67  let res: Response;68  try {69    res = await fetch(path, { method, headers, body: method !== 'GET' ? JSON.stringify(opts.body ?? {}) : undefined, credentials: 'same-origin', cache: 'no-store' });70  } catch {71    throw new ApiError(0, 'network', 'Serveur injoignable', null);72  }73  let data: any = null;74  try {75    data = await res.json();76  } catch {77    data = null;78  }79  if (!res.ok) {80    if (res.status === 401 && !opts.quiet401 && onUnauthorized) onUnauthorized();81    throw new ApiError(res.status, data?.error || 'error', data?.message || `Erreur ${res.status}`, data);82  }83  return data as T;84}8586export type CameraDto = {87  id: string;88  name: string;89  model: string | null;90  firmware: string | null;91  capabilities: { ptz: boolean; audio: boolean };92  profiles: { role: 'main' | 'sub'; width: number | null; height: number | null; fps: number | null; bitrateKbps: number | null }[];93  settings: { recordEnabled: boolean; recordAudio: boolean; liveAudio: boolean; ptzEnabled: boolean; schedule: { days: number[]; from: string; to: string }[] | null; motionEnabled?: boolean; motionSensitivity?: number; motionCooldownS?: number; motionIgnore?: number[] };94  status: {95    reachable: boolean;96    reachableSinceMs: number | null;97    lastCheckMs: number | null;98    recorderState: string;99    recorderRestarts: number;100    lastSegmentEndMs: number | null;101    recConfirmed: boolean;102    bitrateBps: number | null;103    lastError: string | null;104    lastMotionMs?: number | null;105    motionState?: string;106  };107};108109export type SegmentDto = { id: number; startMs: number; endMs: number; durationMs: number; sizeBytes: number; codec: string | null; width: number | null; height: number | null; hasAudio: boolean; state: string; protected: boolean; discontinuity: boolean };110export type RangeDto = { startMs: number; endMs: number };111export type EventDto = { id: number; tsMs: number; cameraId: string | null; type: string; severity: 'info' | 'warning' | 'critical'; message: string; data: any };112export type ExportDto = { id: string; cameraId: string; fromMs: number; toMs: number; state: string; progress: number; sizeBytes: number | null; fileName: string | null; error: string | null; createdMs: number; finishedMs: number | null; expiresMs: number | null };113export type MotionDto = { eventId: number; fromMs: number; toMs: number | null };114
Files main
Menu

Theme