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