import {useEntityManager} from "~/composables/data/useEntityManager"; import ApiResource from "~/models/ApiResource"; import {AssociativeArray, Collection} from "~/types/data"; import {AsyncData} from "#app"; import {ComputedRef, Ref} from "vue"; interface useEntityFetchReturnType { fetch: (model: typeof ApiResource, id: number) => AsyncData, fetchCollection: (model: typeof ApiResource, parent?: ApiResource | null, query?: Ref) => AsyncData // @ts-ignore getRef: (model: typeof T, id: Ref) => ComputedRef } // TODO: améliorer le typage des fonctions sur le modèle de getRef export const useEntityFetch = (lazy: boolean = false): useEntityFetchReturnType => { const { em } = useEntityManager() const fetch = (model: typeof ApiResource, id: number) => useAsyncData( model.entity + '_' + id, () => em.fetch(model, id, true), { lazy } ) const fetchCollection = (model: typeof ApiResource, parent: ApiResource | null = null, query: Ref = ref(null)) => useAsyncData( model.entity + '_many', () => em.fetchCollection(model, parent, query.value), { lazy } ) // @ts-ignore const getRef = (model: typeof T, id: Ref): ComputedRef => { return computed(() => (id.value ? em.find(model, id.value) as T : null)) } //@ts-ignore return { fetch, fetchCollection, getRef } }