diff --git a/app/src/api/api.ts b/app/src/api/api.ts index b6866ebb..fa706388 100644 --- a/app/src/api/api.ts +++ b/app/src/api/api.ts @@ -107,7 +107,9 @@ export default { asFormData = true, }: APIQuery): Promise { const cache = cachePath ? useCache(method, cachePath) : undefined - if (method === 'GET' && cache?.content) return cache.content + if (method === 'GET' && cache?.content.value !== undefined) { + return cache.content.value + } const { locale } = useSettings() const { startRequest, endRequest } = useRequests() @@ -149,6 +151,7 @@ export default { cache?.update(responseData) endRequest({ request, success: true }) + if (cache) return cache.content.value as T return responseData }, diff --git a/app/src/composables/data.ts b/app/src/composables/data.ts index 3db1b7ad..35d62753 100644 --- a/app/src/composables/data.ts +++ b/app/src/composables/data.ts @@ -2,7 +2,7 @@ import { createGlobalState } from '@vueuse/core' import { computed, reactive, ref, toValue, type MaybeRefOrGetter } from 'vue' import type { RequestMethod } from '@/api/api' -import { isObjectLiteral } from '@/helpers/commons' +import { isEmptyValue, isObjectLiteral } from '@/helpers/commons' import { stratify } from '@/helpers/data/tree' import type { Obj } from '@/types/commons' import type { @@ -215,29 +215,29 @@ export function useCache( method: RequestMethod, cachePath: StorePath, ) { - const [key, param] = cachePath.split('.') as + const [key, param] = cachePath.split(/\.(.*)/s) as | [StoreKeys, undefined] | [StoreKeysParam, string] const data = useData() // FIXME get global cache policy setting // Add noCache arg? not used - if (!(key in data)) { - throw new Error('Trying to get cache of inexistant data') - } - const d = data[key].value - let content = d as T - if (param) { - if (isObjectLiteral(d) && !Array.isArray(d)) { - content = d[param] as T - } else { - content = undefined as T - console.warn('Trying to get param on non object data') - } - } - return { - content, + content: computed(() => { + if (!(key in data)) { + throw new Error('Trying to get cache of inexistant data') + } + const d = data[key].value + if (param) { + if (isObjectLiteral(d) && !Array.isArray(d)) { + return d[param] as T + } else { + return undefined as T + console.warn('Trying to get param on non object data') + } + } + return (isEmptyValue(d) ? undefined : d) as T + }), update: (payload: T) => data.update(method, payload, key, param), } }