From 6fdc6fd132a931a6601756190c24c058cffbcb7c Mon Sep 17 00:00:00 2001 From: xenticore Date: Tue, 29 Apr 2025 00:49:32 -0400 Subject: [PATCH] refactor: rewrite async promise as promise chain --- src/content/pricing/pricestf.ts | 86 ++++++++++++++------------------- 1 file changed, 36 insertions(+), 50 deletions(-) diff --git a/src/content/pricing/pricestf.ts b/src/content/pricing/pricestf.ts index ea843e0..55e56da 100644 --- a/src/content/pricing/pricestf.ts +++ b/src/content/pricing/pricestf.ts @@ -1,6 +1,6 @@ declare function GM_fetch(input: string | URL | globalThis.Request, init?: RequestInit): Promise import '../GM_fetch' -import { logDebug } from '../utils/log' +import { logDebug, logError } from '../utils/log' async function getPricesToken(): Promise { return new Promise((resolve, reject) => { @@ -42,62 +42,48 @@ async function priceUsingPricesTF(token: string, sku: string, retries: number = // prices.tf // https://api2.prices.tf/prices/${sku} // Authorization: Bearer ${token} - return new Promise(async (resolve, reject) => { - if (!token) { - reject(401) + return GM_fetch(`https://api2.prices.tf/prices/${encodeURIComponent(sku)}`, { + method: 'get', + headers: { + 'Accept': 'application/json', + 'Authorization': `Bearer ${token}`, } - let response = await GM_fetch(`https://api2.prices.tf/prices/${encodeURIComponent(sku)}`, { - method: 'get', - headers: new Headers({ - 'Accept': 'application/json', - 'Authorization': `Bearer ${token}` - }) - }) - if (response.status === 404 && sku.includes(';')) { + }) + .then(response => { + if (!response.ok) { + throw new Error(`HTTP error! Status: ${response.status}`); + } + if (response.status === 404 && sku.includes(';') && !sku.includes(';uncraftable')) { const quality: number = parseInt(sku.split(';')[1], 10); if(quality === 6) { // Try uncraftable variant if unique weapon - response = await GM_fetch(`https://api2.prices.tf/prices/${encodeURIComponent(sku + ';uncraftable')}`, { - method: 'get', - headers: new Headers({ - 'Accept': 'application/json', - 'Authorization': `Bearer ${token}` - }) - }) + return priceUsingPricesTF(token, sku + ';uncraftable', retries); } } - switch (response.status) { - case 200: - const json = await response.json() - resolve({ keys: json['sellKeys'], metal: json['sellHalfScrap'] / 18.0 }) - break; - case 404: - reject("Not found in prices.tf") - break; - case 429: - case 503: - // Happens if we send too many requests in a short period of time - // Retry after a few seconds - if(retries > 0) { - logDebug(`Cloudflare rate limit exceeded, trying again after 2 seconds, ${retries} retries left`) - await new Promise(resolve => setTimeout(resolve, 2000)); - try { - const retryResult = await priceUsingPricesTF(token, sku, retries - 1); - resolve(retryResult); - } catch (err) { - reject(err); - } - } else { - reject("Cloudflare rate limit exceeded, stopping") - } - break; - default: - // Something went wrong - logDebug(`Received ${response.status} error while pricing ${sku}`) - logDebug(`${JSON.stringify(response.headers)}`) - reject("Unknown error") - break; + if(response.status === 404) { + throw new Error(`Item not found: ${sku}`); } + if(response.status === 503) { + // Happens if we send too many requests in a short period of time + // Retry after a few seconds + if(retries > 0) { + logDebug(`Cloudflare rate limit exceeded, trying again after 2 seconds, ${retries} retries left`) + return priceUsingPricesTF(token, sku, retries - 1); + } else { + throw new Error(`Cloudflare rate limit exceeded, stopping`) + } + } + return response.json(); + }) + .then(data => { + const prices = new PricesResponse(); + prices.keys = data['sellKeys'] + prices.metal = data['sellHalfScrap'] / 18.0; + return prices; + }) + .catch(error => { + logError(`Failed to fetch prices from prices.tf for item ${sku}`) + throw error; }) }