diff --git a/scripts/preload-tcgplayer.ts b/scripts/preload-tcgplayer.ts index 3725828..a72a5c4 100644 --- a/scripts/preload-tcgplayer.ts +++ b/scripts/preload-tcgplayer.ts @@ -55,14 +55,32 @@ const syncProductLine = async ( "sort": {}, }; - const response = await fetch('https://mp-search-api.tcgplayer.com/v1/search/request?q=&isList=false', { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify(d), - }); - - if (!response.ok) { - throw new Error(`TCGPlayer search request failed: ${response.status} ${response.statusText}`); + let response: Response | null = null; + let lastError: unknown = null; + for (let attempt = 1; attempt <= 3; attempt++) { + try { + const r = await fetch('https://mp-search-api.tcgplayer.com/v1/search/request?q=&isList=false', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(d), + }); + if (r.ok) { + response = r; + break; + } + lastError = new Error(`TCGPlayer search request failed: ${r.status} ${r.statusText}`); + } catch (err) { + lastError = err; + } + if (attempt < 3) { + log(` retry ${attempt}/2 for search request in 5s...`); + await helper.Sleep(5000); + } + } + if (!response) { + throw lastError instanceof Error + ? lastError + : new Error(`TCGPlayer search request failed: ${String(lastError)}`); } const data = await response.json();