Files
CoHaiSe-CLOUD/frontend/src/services/apiClient.ts
T
Esiah 68d4d5dea0
PR — Ouverture automatique / create-pr (push) Successful in 3s
refactor: restructure frontend and backend by domain services
Split the monolithic backend (lib/, routes/, access.ts, storage.ts, media.ts)
into a layered structure inspired by JeanXXIII-CRM:
- config/: appConfig, db
- middleware/: auth (session, roles, cookies)
- models/: shared types
- routes/: *Routes.ts (thin HTTP handlers)
- services/: *Service.ts (business logic, one per domain)
- utils/: errors, http, migrate, ratelimit, validators, service bootstrap

Frontend likewise: services/*Service.ts + apiClient, contexts/, types/, utils/format.
Route files now carry the "Routes" suffix; service files are named
authService.ts, ratelimit.ts, etc. and live in their own files, frontend and
backend separated.

Updated docs and READMEs to reflect the new layout. Typecheck and builds pass.
2026-11-13 09:45:21 +01:00

83 lines
2.7 KiB
TypeScript

export class ApiError extends Error {
readonly status: number;
constructor(status: number, message: string) {
super(message);
this.name = "ApiError";
this.status = status;
}
}
export const BASE = "/api/cloud";
/** Au-dela de ce seuil, l'envoi passe par des sessions morcelees. */
export const LARGE_UPLOAD_THRESHOLD = 90 * 1024 * 1024;
const messageOf = (body: unknown, fallback: string): string =>
(body as { error?: { message?: string } } | null)?.error?.message ?? fallback;
export const request = async <T>(path: string, init?: RequestInit): Promise<T> => {
let response: Response;
try {
response = await fetch(`${BASE}${path}`, {
credentials: "same-origin",
...init,
headers: {
...(init?.body ? { "content-type": "application/json" } : {}),
...init?.headers,
},
});
} catch {
throw new ApiError(0, "Serveur injoignable. Verifiez votre connexion.");
}
const body: unknown = await response.json().catch(() => null);
if (!response.ok) throw new ApiError(response.status, messageOf(body, "Une erreur est survenue."));
return body as T;
};
export const get = <T>(path: string): Promise<T> => request<T>(path);
export const post = <T>(path: string, payload?: unknown): Promise<T> =>
request<T>(path, {
method: "POST",
...(payload === undefined ? {} : { body: JSON.stringify(payload) }),
});
export const patch = <T>(path: string, payload: unknown): Promise<T> =>
request<T>(path, { method: "PATCH", body: JSON.stringify(payload) });
export const put = <T>(path: string, payload?: unknown): Promise<T> =>
request<T>(path, {
method: "PUT",
...(payload === undefined ? {} : { body: JSON.stringify(payload) }),
});
export const del = <T>(path: string): Promise<T> => request<T>(path, { method: "DELETE" });
export const query = (params: Record<string, string | number | null | undefined>): string => {
const search = new URLSearchParams();
for (const [key, value] of Object.entries(params)) {
if (value !== null && value !== undefined && value !== "") search.set(key, String(value));
}
const suffix = search.toString();
return suffix ? `?${suffix}` : "";
};
export const putBinary = async <T>(path: string, body: BodyInit, type: string): Promise<T> => {
let response: Response;
try {
response = await fetch(`${BASE}${path}`, {
method: "PUT",
credentials: "same-origin",
headers: type ? { "content-type": type } : {},
body,
});
} catch {
throw new ApiError(0, "Serveur injoignable. Verifiez votre connexion.");
}
const parsed: unknown = await response.json().catch(() => null);
if (!response.ok) throw new ApiError(response.status, messageOf(parsed, "L'envoi a echoue."));
return parsed as T;
};