SPB Git

spb/trouve-ka Public

Trouve-KA — moteur de recherche web indépendant, Québec-first. Crawler distribué, index OpenSearch, ranking bilingue, galerie d'images. En prod : www.trouve-ka.com

Python 76.8% TypeScript 15.7% SQL 3.9% Shell 1.4% CSS 1.3% Dockerfile 0.7%
1.2 KB · 40 lines typescript
Raw Blame History
1/**2 * Trouve-KA — client API admin (jeton X-Admin-Token, gestion 401)3 * Author: Simon-Pierre Boucher4 * Contact: contact@spboucher.ai5 */67/** Clé sessionStorage où le jeton admin est conservé (session seulement). */8export const ADMIN_TOKEN_KEY = "trouveka-admin-token";910/** Levée quand le backend répond 401/403 : le jeton doit être redemandé. */11export class UnauthorizedError extends Error {12  constructor() {13    super("Jeton d'administration invalide ou expiré");14    this.name = "UnauthorizedError";15  }16}1718/**19 * Fetch authentifié vers l'API admin (chemins relatifs `/api/admin/...`).20 * Ajoute le header `X-Admin-Token` et convertit 401/403 en UnauthorizedError.21 */22export async function adminFetch<T>(23  path: string,24  token: string,25  init?: RequestInit,26): Promise<T> {27  const headers: Record<string, string> = {28    "X-Admin-Token": token,29    ...(init?.body ? { "Content-Type": "application/json" } : {}),30  };31  const res = await fetch(path, { ...init, headers });32  if (res.status === 401 || res.status === 403) {33    throw new UnauthorizedError();34  }35  if (!res.ok) {36    throw new Error(`Erreur ${res.status}`);37  }38  return (await res.json()) as T;39}40