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:
@@ -2,6 +2,7 @@ import { ref } from 'vue'
|
||||
import { useApi } from './useApi'
|
||||
import { useToast } from './useToast'
|
||||
import { normalizeRelationIds } from '~/shared/apiRelations'
|
||||
import { extractCollection } from '~/shared/utils/apiHelpers'
|
||||
|
||||
export interface Document {
|
||||
id: string
|
||||
@@ -34,23 +35,6 @@ export interface DocumentResult {
|
||||
const documents = ref<Document[]>([])
|
||||
const loading = ref(false)
|
||||
|
||||
const extractCollection = (payload: unknown): Document[] => {
|
||||
if (Array.isArray(payload)) {
|
||||
return payload
|
||||
}
|
||||
const p = payload as Record<string, unknown> | null
|
||||
if (Array.isArray(p?.member)) {
|
||||
return p.member as Document[]
|
||||
}
|
||||
if (Array.isArray(p?.['hydra:member'])) {
|
||||
return p['hydra:member'] as Document[]
|
||||
}
|
||||
if (Array.isArray(p?.data)) {
|
||||
return p.data as Document[]
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
const fileToBase64 = (file: File): Promise<string> =>
|
||||
new Promise((resolve, reject) => {
|
||||
const reader = new FileReader()
|
||||
|
||||
Reference in New Issue
Block a user