useEntityFetch.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import type { AsyncData } from "#app";
  2. import type { ComputedRef, Ref } from "vue";
  3. import { v4 as uuid4 } from "uuid";
  4. import { useEntityManager } from "~/composables/data/useEntityManager";
  5. import ApiResource from "~/models/ApiResource";
  6. import type { AssociativeArray, Collection } from "~/types/data";
  7. interface useEntityFetchReturnType {
  8. fetch: (
  9. model: typeof ApiResource,
  10. id: number,
  11. ) => AsyncData<ApiResource, ApiResource | true>;
  12. fetchCollection: (
  13. model: typeof ApiResource,
  14. parent?: ApiResource | null,
  15. query?: Ref<AssociativeArray>,
  16. ) => AsyncData<Collection, any>;
  17. // @ts-ignore
  18. getRef: <T extends ApiResource>(
  19. model: typeof T,
  20. id: Ref<number | null>,
  21. ) => ComputedRef<null | T>;
  22. }
  23. // TODO: améliorer le typage des fonctions sur le modèle de getRef
  24. export const useEntityFetch = (
  25. lazy: boolean = false,
  26. ): useEntityFetchReturnType => {
  27. const { em } = useEntityManager();
  28. const fetch = (model: typeof ApiResource, id: number) =>
  29. useAsyncData(
  30. model.entity + "_" + id + "_" + uuid4(),
  31. () => em.fetch(model, id, true),
  32. { lazy },
  33. );
  34. const fetchCollection = (
  35. model: typeof ApiResource,
  36. parent: ApiResource | null = null,
  37. query: Ref<AssociativeArray | null> = ref(null),
  38. ) =>
  39. useAsyncData(
  40. model.entity + "_many_" + uuid4(),
  41. () => em.fetchCollection(model, parent, query.value ?? undefined),
  42. { lazy },
  43. );
  44. // @ts-ignore
  45. const getRef = <T extends ApiResource>(
  46. model: typeof T,
  47. id: Ref<number | null>,
  48. ): ComputedRef<T | null> => {
  49. return computed(() => (id.value ? (em.find(model, id.value) as T) : null));
  50. };
  51. // @ts-ignore
  52. return { fetch, fetchCollection, getRef };
  53. };