add option to use sveltekit's fetch for seeding db
This commit is contained in:
@@ -36,14 +36,16 @@ function ensureBrowser() {
|
||||
*
|
||||
* @param opts.version Bump to force users to refresh from the shipped snapshot
|
||||
* @param opts.force If true, reseed even if already seeded for this version
|
||||
* @param opts.fetch Optional fetch implementation (prefer the one from load)
|
||||
*/
|
||||
export async function ensureSeeded(
|
||||
opts: { version?: number; force?: boolean } = {},
|
||||
opts: { version?: number; force?: boolean; fetch?: typeof fetch } = {},
|
||||
): Promise<void> {
|
||||
ensureBrowser();
|
||||
|
||||
const version = opts.version ?? AMQ_DB_SEED_VERSION;
|
||||
const key = seededStorageKey(version);
|
||||
const fetcher = opts.fetch ?? fetch;
|
||||
|
||||
if (!opts.force && localStorage.getItem(key) === "1") return;
|
||||
|
||||
@@ -56,7 +58,7 @@ export async function ensureSeeded(
|
||||
|
||||
const url = asset(SEED_ASSET_PATH);
|
||||
|
||||
const res = await fetch(url, { cache: "no-cache" });
|
||||
const res = await fetcher(url, { cache: "no-cache" });
|
||||
if (!res.ok) {
|
||||
throw new Error(
|
||||
`Failed to fetch seed DB from ${url}: ${res.status} ${res.statusText}`,
|
||||
|
||||
@@ -83,7 +83,7 @@ export const load: PageLoad = async ({ url, fetch, depends }) => {
|
||||
}
|
||||
|
||||
// Browser path: seed then query local DB for songs by MAL ids
|
||||
await ensureSeeded();
|
||||
await ensureSeeded({ fetch });
|
||||
|
||||
const malIds = malResponse.data.map((e) => e.node.id);
|
||||
const songRows = await getSongsForMalAnimeIds(db, malIds);
|
||||
|
||||
Reference in New Issue
Block a user