diff --git a/app/pages/charts.vue b/app/pages/charts.vue index 09c8eb1..186a27b 100644 --- a/app/pages/charts.vue +++ b/app/pages/charts.vue @@ -73,7 +73,7 @@ watch(isFetching, () => { }); const explanation = computed(() => { - return useExplanations(projectType.value, stat.value); + return useExplanations(projectType.value, stat.value); }); const sideBarOpen = ref(false); diff --git a/server/utils/storage.ts b/server/utils/storage.ts index dcf3481..daa1586 100644 --- a/server/utils/storage.ts +++ b/server/utils/storage.ts @@ -24,7 +24,15 @@ export const DB = { return getFromStorageCached("metadata", "latestDate"); }, async getNotCached(): Promise { - return getFromStorage("metadata", "latestDate"); + const data = await getFromStorage("metadata", "latestDate"); + + if (!data) { + return new Error( + `could not find latest date in storage - storage: "metadata" key: "latestDate"`, + ); + } + + return data; }, }, GlobalStats: { @@ -134,7 +142,22 @@ function getProjectStorageKey( return key; } -const getFromStorageCached = defineCachedFunction( +async function getFromStorageCached( + storageName: string, + key: string, +): Promise { + const data = await _getFromStorageCached(storageName, key); + + if (!data) { + return new Error( + `could not find value in storage - storage: "${storageName}" key: "${key}"`, + ); + } + + return data; +} + +const _getFromStorageCached = defineCachedFunction( async (storageName: string, key: string) => { return await getFromStorage(storageName, key); }, @@ -144,22 +167,37 @@ const getFromStorageCached = defineCachedFunction( swr: false, getKey: (storageName: string, key: string) => `${storageName}-${key}`, }, -) as (storageName: string, key: string) => Promise; +) as (storageName: string, key: string) => Promise; async function getFromStorage(storageName: string, key: string) { const storage = useStorage(storageName); const data = (await storage.getItem(key)) as t; if (!data) { - return new Error( - `could not find value in storage - storage: "${storageName}" key: "${key}"`, - ); + return null; } return data; } -const getFromStorageCachedBulk = defineCachedFunction( +async function getFromStorageCachedBulk( + storageName: string, + keys: string[], +): Promise<(T | Error)[]> { + const data = await _getFromStorageCachedBulk(storageName, keys); + + return data.map((item, index) => { + if (!item) { + return new Error( + `could not find value in storage - storage: "${storageName}" key: "${keys[index]}"`, + ); + } + + return item; + }); +} + +const _getFromStorageCachedBulk = defineCachedFunction( async (storageName: string, keys: string[]) => { return await getFromStorageBulk(storageName, keys); }, @@ -170,7 +208,7 @@ const getFromStorageCachedBulk = defineCachedFunction( getKey: (storageName: string, keys: string[]) => `${storageName}-${keys[0]}-${keys[keys.length - 1]}`, }, -) as (storageName: string, keys: string[]) => Promise; +) as (storageName: string, keys: string[]) => Promise<(T | null)[]>; async function getFromStorageBulk(storageName: string, keys: string[]) { const storage = useStorage(storageName); @@ -178,9 +216,7 @@ async function getFromStorageBulk(storageName: string, keys: string[]) { const mapped_data = data.map((data) => { if (!data.value) { - return new Error( - `could not find value in storage - storage: "${storageName}" key: "${data.key}"`, - ); + return null; } return data.value as t;