SPB Git

Repository Public

client.ts 2.9 KB 60 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : ORIX App Universe8 * File             : src/components/admin/client.ts9 * Module           : components10 * Repository       : https://git.spboucher.ai/orix-spboucher-ai11 * Project Website  : https://www.orixlab.dev12 * Created          : 2026-11-2113 * Last Modified    : 2026-11-2114 *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: support@spboucher.ai23 * Development      : dev@spboucher.ai24 * Support          : support@spboucher.ai25 * Security/Admin   : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License          : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243"use client";4445export class ApiClientError extends Error { constructor(public code: string, message: string, public issues?: string[]) { super(message); } }4647/** Same-origin JSON call carrying the session's CSRF token (read from the <meta> the admin layout renders). */48export async function api<T = unknown>(path: string, init: { method?: string; body?: unknown; form?: FormData } = {}): Promise<T> {49  const csrf = document.querySelector<HTMLMetaElement>('meta[name="orix-csrf"]')?.content ?? "";50  const res = await fetch(path, {51    method: init.method ?? (init.body !== undefined || init.form ? "POST" : "GET"), credentials: "same-origin",52    headers: { "x-csrf-token": csrf, ...(init.form ? {} : { "Content-Type": "application/json" }) },53    body: init.form ?? (init.body !== undefined ? JSON.stringify(init.body) : undefined),54  }).catch(() => { throw new ApiClientError("network", "Cannot reach the server"); });55  const data = await res.json().catch(() => ({}));56  if (!res.ok) throw new ApiClientError(data?.error?.code ?? "error", data?.error?.message ?? `HTTP ${res.status}`, data?.error?.issues);57  return data as T;58}59export const errorText = (e: unknown) => (e instanceof ApiClientError ? [e.message, ...(e.issues ?? [])].join(" — ") : e instanceof Error ? e.message : "Unexpected error");60
Files main
Menu

Theme