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>
|
declare function GM_fetch(input: string | URL | globalThis.Request, init?: RequestInit): Promise<Response>
|
||||||
import '../GM_fetch'
|
import '../GM_fetch'
|
||||||
import { logDebug } from '../utils/log'
|
import { logDebug, logError } from '../utils/log'
|
||||||
|
|
||||||
async function getPricesToken(): Promise<string> {
|
async function getPricesToken(): Promise<string> {
|
||||||
return new Promise<string>((resolve, reject) => {
|
return new Promise<string>((resolve, reject) => {
|
||||||
@@ -42,62 +42,48 @@ async function priceUsingPricesTF(token: string, sku: string, retries: number =
|
|||||||
// prices.tf
|
// prices.tf
|
||||||
// https://api2.prices.tf/prices/${sku}
|
// https://api2.prices.tf/prices/${sku}
|
||||||
// Authorization: Bearer ${token}
|
// Authorization: Bearer ${token}
|
||||||
return new Promise(async (resolve, reject) => {
|
return GM_fetch(`https://api2.prices.tf/prices/${encodeURIComponent(sku)}`, {
|
||||||
if (!token) {
|
method: 'get',
|
||||||
reject(401)
|
headers: {
|
||||||
|
'Accept': 'application/json',
|
||||||
|
'Authorization': `Bearer ${token}`,
|
||||||
}
|
}
|
||||||
let response = await GM_fetch(`https://api2.prices.tf/prices/${encodeURIComponent(sku)}`, {
|
})
|
||||||
method: 'get',
|
.then(response => {
|
||||||
headers: new Headers({
|
if (!response.ok) {
|
||||||
'Accept': 'application/json',
|
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||||
'Authorization': `Bearer ${token}`
|
}
|
||||||
})
|
if (response.status === 404 && sku.includes(';') && !sku.includes(';uncraftable')) {
|
||||||
})
|
|
||||||
if (response.status === 404 && sku.includes(';')) {
|
|
||||||
const quality: number = parseInt(sku.split(';')[1], 10);
|
const quality: number = parseInt(sku.split(';')[1], 10);
|
||||||
if(quality === 6) {
|
if(quality === 6) {
|
||||||
// Try uncraftable variant if unique weapon
|
// Try uncraftable variant if unique weapon
|
||||||
response = await GM_fetch(`https://api2.prices.tf/prices/${encodeURIComponent(sku + ';uncraftable')}`, {
|
return priceUsingPricesTF(token, sku + ';uncraftable', retries);
|
||||||
method: 'get',
|
|
||||||
headers: new Headers({
|
|
||||||
'Accept': 'application/json',
|
|
||||||
'Authorization': `Bearer ${token}`
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
switch (response.status) {
|
if(response.status === 404) {
|
||||||
case 200:
|
throw new Error(`Item not found: ${sku}`);
|
||||||
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 === 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;
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user