Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | 1x 9x 9x 9x 9x 9x 1x 5x 9x 6x 1x 20x 5x 6x 6x 2x 4x 1x 3x 1x 2x 6x 6x 6x 6x 6x 2x 10x 10x 10x 10x 10x 2x 1x 1x 3x 3x 3x 1x 2x 8x 8x 8x 1x 8x 3x 2x 7x 5x 1x 4x 8x 4x 4x 1x 3x 3x 1x 2x 2x 5x 2x 2x 2x 2x | import type { ProblemDetail } from "./api-types";
import { getLocale, t, translated } from "./i18n";
export type { PageData, ProblemDetail, Row, User } from "./api-types";
export { date, display } from "./format";
export const NETWORK_ERROR_STATUS = 0;
export class ApiError extends Error {
constructor(
public status: number,
message: string,
public retryAfter: number | null = null,
public type: string | null = null,
) {
super(message);
this.name = "ApiError";
}
}
const fallbackMessages: Record<number, string> = translated({
[NETWORK_ERROR_STATUS]: "Le serveur est injoignable. Vérifiez votre connexion et réessayez.",
400: "La demande est invalide.",
401: "Votre session a expiré. Reconnectez-vous.",
403: "Votre rôle ne permet pas cette action.",
404: "L’élément demandé est introuvable.",
409: "Cette opération entre en conflit avec un élément existant.",
413: "La demande est trop volumineuse.",
429: "Trop de tentatives. Réessayez dans quelques instants.",
502: "Un service externe est momentanément indisponible. Réessayez dans quelques instants.",
503: "Le service est momentanément indisponible. Réessayez dans quelques instants.",
504: "Le serveur a mis trop de temps à répondre. Réessayez dans quelques instants.",
});
function fallbackMessage(status: number) {
return (
fallbackMessages[status] ??
(status >= 500
? t("Le serveur a rencontré une erreur. Réessayez plus tard.")
: t("La demande n’a pas pu aboutir."))
);
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
function asProblem(value: unknown): ProblemDetail {
if (!isRecord(value)) {
return {};
}
const text = (key: string) => (typeof value[key] === "string" ? value[key] : undefined);
return {
type: text("type"),
title: text("title"),
status: typeof value.status === "number" ? value.status : undefined,
detail: text("detail"),
instance: text("instance"),
};
}
function readableDetail(detail: string | undefined, status: number) {
const trimmed = detail?.trim();
if (!trimmed || trimmed.length > 500 || status === 500) {
return null;
}
if (/\n\s*at\s|(?:java|kotlin|org\.springframework|com\.stripe)\.|Exception\b/.test(trimmed)) {
return null;
}
if (
/^(No (static resource|endpoint|handler)|Request method|Required|Failed to|Content-Type)/i.test(
trimmed,
)
) {
return null;
}
return trimmed;
}
function retryAfterSeconds(response: Response) {
const value = Number(response.headers.get("retry-after"));
return Number.isFinite(value) && value > 0 ? value : null;
}
export async function errorFromResponse(response: Response): Promise<ApiError> {
const body: unknown = await response.json().catch(() => null);
const problem = asProblem(body);
return new ApiError(
response.status,
readableDetail(problem.detail, response.status) ?? fallbackMessage(response.status),
retryAfterSeconds(response),
problem.type ?? null,
);
}
function isAbort(error: unknown) {
return error instanceof DOMException && error.name === "AbortError";
}
async function send(input: string, init: RequestInit) {
const headers = new Headers(init.headers);
Eif (!headers.has("Accept-Language")) {
headers.set("Accept-Language", getLocale());
}
try {
return await fetch(input, { ...init, headers });
} catch (error) {
if (isAbort(error)) {
throw error;
}
throw new ApiError(NETWORK_ERROR_STATUS, fallbackMessage(NETWORK_ERROR_STATUS));
}
}
async function csrfHeader(signal: AbortSignal | null | undefined) {
const response = await send("/api/backend/api/v1/auth/csrf", {
cache: "no-store",
credentials: "same-origin",
signal,
});
const body: unknown = response.ok ? await response.json().catch(() => null) : null;
if (!isRecord(body) || typeof body.headerName !== "string" || typeof body.token !== "string") {
throw new ApiError(response.status, t("Impossible d’établir une session sécurisée."));
}
return { name: body.headerName, token: body.token };
}
async function request(path: string, options: RequestInit): Promise<Response> {
const method = options.method || "GET";
const headers = new Headers(options.headers);
if (options.body && !headers.has("Content-Type")) {
headers.set("Content-Type", "application/json");
}
if (!["GET", "HEAD"].includes(method)) {
const csrf = await csrfHeader(options.signal);
headers.set(csrf.name, csrf.token);
}
const response = await send(`/api/backend/api/v1/${path}`, {
...options,
headers,
cache: "no-store",
credentials: "same-origin",
});
if (!response.ok) {
throw await errorFromResponse(response);
}
return response;
}
export async function api<T>(path: string, options: RequestInit = {}): Promise<T> {
const response = await request(path, options);
const text = await response.text();
if (!text) {
return undefined as T;
}
try {
return JSON.parse(text) as T;
} catch {
throw new ApiError(response.status, t("La réponse du serveur est illisible."));
}
}
function filenameFrom(response: Response, fallback: string) {
const header = response.headers.get("content-disposition") ?? "";
const match = /filename\*?=(?:UTF-8'')?"?([^";]+)"?/i.exec(header);
return match ? decodeURIComponent(match[1]) : fallback;
}
export function saveBlob(blob: Blob, filename: string) {
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = filename;
document.body.append(link);
link.click();
link.remove();
setTimeout(() => URL.revokeObjectURL(url), 1000);
}
export async function download(path: string, filename: string) {
const response = await request(path, { method: "GET", headers: { Accept: "text/csv" } });
saveBlob(await response.blob(), filenameFrom(response, filename));
}
export async function upload<T>(
path: string,
body: BodyInit,
contentType: string,
method = "POST",
) {
return api<T>(path, { method, body, headers: { "Content-Type": contentType } });
}
export function query(parameters: Record<string, string | number | boolean | null | undefined>) {
const search = new URLSearchParams();
for (const [key, value] of Object.entries(parameters)) {
if (value !== null && value !== undefined && value !== "") {
search.set(key, String(value));
}
}
const text = search.toString();
return text ? `?${text}` : "";
}
export function errorMessage(error: unknown, fallback: string) {
return error instanceof Error && error.message ? error.message : fallback;
}
|