/** * Schemas Hydra / API Platform 4. * * Important : API Platform 4 abandonne le prefixe `hydra:` dans les noms de * proprietes (compare a la version 3). Un GET /api/audit-logs renvoie : * { "@context": ..., "@id": ..., "@type": "...", * "member": [...], * "totalItems": 30, * "view": { "@id": ..., "@type": "...", "first": ..., "next": ..., ... } } * * En `application/json` (sans ld), API Platform retourne un simple tableau * plat sans ces metadonnees — on doit donc explicitement demander * `application/ld+json` (via l'option `headers: { Accept: ... }` de useApi) * pour avoir acces a la pagination. */ export interface HydraView { '@id'?: string '@type'?: string first?: string last?: string next?: string previous?: string } export interface HydraCollection { member: T[] totalItems: number view?: HydraView } export function extractHydraMembers(collection: HydraCollection): T[] { return collection.member ?? [] } /** * Une violation de contrainte API Platform (reponse 422). Le `propertyPath` * pointe le champ concerne, `message` est le libelle a afficher. */ export interface ApiViolation { propertyPath: string message: string } /** * Extrait les violations d'un payload d'erreur 422 d'API Platform 4. Supporte * les deux formats de negociation (`violations` ou `hydra:violations`) et * renvoie un tableau vide si le payload n'en contient pas d'exploitables. * * Utilise par useCategoryForm et tout futur composable de formulaire qui * doit mapper les violations serveur sur ses champs. */ export function extractApiViolations(data: unknown): ApiViolation[] { if (!data || typeof data !== 'object') return [] const record = data as Record const raw = record.violations ?? record['hydra:violations'] if (!Array.isArray(raw)) return [] const out: ApiViolation[] = [] for (const v of raw) { if (!v || typeof v !== 'object') continue const obj = v as Record out.push({ propertyPath: String(obj.propertyPath ?? ''), message: String(obj.message ?? ''), }) } return out } /** * Extrait un message d'erreur lisible depuis un payload Hydra / JSON * d'erreur API Platform. Essaie les champs courants dans l'ordre : * `hydra:description` → `detail` → `description` → `message` → `error` → * `title` → `hydra:title`. Renvoie '' si rien d'exploitable. * * Si `data` est une string, la renvoie telle quelle (cas des erreurs * Symfony en text/plain ou des messages bruts). */ export function extractApiErrorMessage(data: unknown): string { if (typeof data === 'string') return data if (!data || typeof data !== 'object') return '' const record = data as Record return ( (record['hydra:description'] as string) ?? (record.detail as string) ?? (record.description as string) ?? (record.message as string) ?? (record.error as string) ?? (record.title as string) ?? (record['hydra:title'] as string) ?? '' ) }