Files
arboretum/packages/web/src/lib/api.ts
T
Johan LEROYandClaude Opus 4.8 6a95c9510e feat: groupes de travail (P5)
Ajoute la notion de groupe — collection nommée de repos (membership légère
persistée, many-to-many) — pour piloter plusieurs repos en simultané :

- DB : migration #5 (tables groups + group_repos, FK ON DELETE CASCADE).
- GroupManager (synchrone, EventEmitter) + routes REST /api/v1/groups ;
  ne persiste que la membership, worktrees/sessions filtrés par repoId côté client.
- Protocole : GroupSummary, DTOs, topic WS « groups », events group_update/removed
  (additifs, PROTOCOL_VERSION inchangé).
- Web : store groups, GroupsListView, GroupView (réutilise RepoSection),
  grille multi-terminaux (TerminalGrid/TerminalCell), action « feature cross-repo »
  (orchestration client, tolérante aux échecs partiels), routes + i18n EN/FR.
- Tests : group-manager.test.ts + extension protocol.test.ts ; acceptance-p5.mjs.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-18 10:03:19 +02:00

40 lines
1.3 KiB
TypeScript

// Mini client REST : JSON, cookies de session, erreurs API normalisées.
import type { ApiError as ApiErrorBody } from '@arboretum/shared';
export class ApiError extends Error {
constructor(
readonly status: number,
readonly code: string,
message: string,
) {
super(message);
this.name = 'ApiError';
}
}
async function request<T>(path: string, method: string, body?: unknown): Promise<T> {
const init: RequestInit = { method, credentials: 'same-origin' };
if (body !== undefined) {
init.headers = { 'content-type': 'application/json' };
init.body = JSON.stringify(body);
}
const res = await fetch(path, init);
if (!res.ok) {
let parsed: ApiErrorBody | null = null;
try {
parsed = (await res.json()) as ApiErrorBody;
} catch {
// réponse non-JSON : on retombe sur le statusText
}
throw new ApiError(res.status, parsed?.error.code ?? 'UNKNOWN', parsed?.error.message ?? res.statusText);
}
return (await res.json()) as T;
}
export const api = {
get: <T>(path: string): Promise<T> => request<T>(path, 'GET'),
post: <T>(path: string, body?: unknown): Promise<T> => request<T>(path, 'POST', body),
patch: <T>(path: string, body?: unknown): Promise<T> => request<T>(path, 'PATCH', body),
delete: <T>(path: string): Promise<T> => request<T>(path, 'DELETE'),
};