refactor(api): extract shared extractCollection helper (F2.1)

Create shared/utils/apiHelpers.ts with generic extractCollection<T>()
that handles hydra:member, member, items, data, and array formats.
Replace 7 local implementations in CRUD composables.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Matthieu
2026-02-09 11:13:20 +01:00
parent 78718b85ae
commit 86bb8af32d
7 changed files with 22 additions and 104 deletions

View File

@@ -1,6 +1,7 @@
import { ref } from 'vue'
import { useApi } from './useApi'
import { useToast } from './useToast'
import { extractCollection } from '~/shared/utils/apiHelpers'
export interface Constructeur {
id: string
@@ -52,23 +53,6 @@ const upsertConstructeurs = (items: Constructeur[] = []) => {
const getIndexedConstructeur = (id: string): Constructeur | null =>
constructeurs.value.find((item) => item && item.id === id) || null
const extractCollection = (payload: unknown): Constructeur[] => {
if (Array.isArray(payload)) {
return payload as Constructeur[]
}
const p = payload as Record<string, unknown> | null
if (Array.isArray(p?.member)) {
return p.member as Constructeur[]
}
if (Array.isArray(p?.['hydra:member'])) {
return p['hydra:member'] as Constructeur[]
}
if (Array.isArray(p?.data)) {
return p.data as Constructeur[]
}
return []
}
const pendingFetches = new Map<string, Promise<Constructeur | null>>()
export function useConstructeurs() {