Renamed downloadAndStore to downloadCache

This commit is contained in:
2025-01-23 19:46:19 +01:00
parent 5202251ac7
commit 9744083dea
3 changed files with 27 additions and 22 deletions

View File

@ -1,3 +1,7 @@
import type { BaseStorage } from '@/storage/baseStorage'
import config from '@/application/config'
import type { HttpResponse } from '@/application/types'
export function uuidv4() {
return '10000000-1000-4000-8000-100000000000'.replace(/[018]/g, (c) => (+c ^ (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (+c / 4)))).toString(16))
}
@ -23,3 +27,18 @@ export function getDomain() {
return window.location.hostname.split('.').slice(-2).join('.')
}
export async function downloadCache<T extends { id: string }>(endpoint: string, storage: BaseStorage<T>) {
const request = await fetch(`${config.server_endpoint}/cache/${endpoint}`)
const response = (await request.json()) as HttpResponse<T[]>
if (!response.success) {
console.error(`Failed to download ${endpoint}:`, response.message)
return
}
const items = response.data ?? []
for (const item of items) {
await storage.add(item)
}
}