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:
16
app/shared/utils/apiHelpers.ts
Normal file
16
app/shared/utils/apiHelpers.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* Shared API response helpers.
|
||||
*
|
||||
* Extracted from 10+ composables/components that each had an identical local
|
||||
* copy of extractCollection (parsing hydra:member / member / data / array).
|
||||
*/
|
||||
|
||||
export function extractCollection<T = any>(payload: unknown): T[] {
|
||||
if (Array.isArray(payload)) return payload as T[]
|
||||
const p = payload as Record<string, unknown> | null
|
||||
if (Array.isArray(p?.member)) return p!.member as T[]
|
||||
if (Array.isArray(p?.['hydra:member'])) return p!['hydra:member'] as T[]
|
||||
if (Array.isArray(p?.items)) return p!.items as T[]
|
||||
if (Array.isArray(p?.data)) return p!.data as T[]
|
||||
return []
|
||||
}
|
||||
Reference in New Issue
Block a user