import { defineStore } from "pinia"; import { ref } from "vue"; import { apiClient } from "@/client"; import { APIProperties } from "@/type/api"; export const useStoreAPI = defineStore("api", () => { // State const _response = ref(null); const loading = ref(false); const ongoingFetch = ref | null>(null); // Actions async function doFetch(): Promise { loading.value = true; const url = "/api"; const resp = (await apiClient.JSONGet(url)) as APIProperties; return resp; } async function get(): Promise { if (_response.value) { return _response.value; } if (ongoingFetch.value !== null) { return ongoingFetch.value; } ongoingFetch.value = doFetch().finally(() => { ongoingFetch.value = null; }); return ongoingFetch.value; } return { // Actions get, }; });