You've already forked tf2wikipricing
refactor: rewrite async promise as promise chain
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
declare function GM_fetch(input: string | URL | globalThis.Request, init?: RequestInit): Promise<Response>
|
||||
import '../GM_fetch'
|
||||
import { logDebug } from '../utils/log'
|
||||
import { logDebug, logError } from '../utils/log'
|
||||
|
||||
async function getPricesToken(): Promise<string> {
|
||||
return new Promise<string>((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)
|
||||
}
|
||||
let response = await GM_fetch(`https://api2.prices.tf/prices/${encodeURIComponent(sku)}`, {
|
||||
return GM_fetch(`https://api2.prices.tf/prices/${encodeURIComponent(sku)}`, {
|
||||
method: 'get',
|
||||
headers: new Headers({
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'Authorization': `Bearer ${token}`
|
||||
'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:
|
||||
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`)
|
||||
await new Promise(resolve => setTimeout(resolve, 2000));
|
||||
try {
|
||||
const retryResult = await priceUsingPricesTF(token, sku, retries - 1);
|
||||
resolve(retryResult);
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
}
|
||||
return priceUsingPricesTF(token, sku, retries - 1);
|
||||
} else {
|
||||
reject("Cloudflare rate limit exceeded, stopping")
|
||||
throw new Error(`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;
|
||||
}
|
||||
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;
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user