refactor: priceUsingPricesTF now expects an SKU string

This commit is contained in:
xenticore
2025-03-28 23:31:04 -04:00
parent e6013ef61c
commit d1c5083425
3 changed files with 16 additions and 14 deletions

View File

@@ -31,13 +31,13 @@ class PricesResponse {
* and uses it to fetch the latest pricing data for the given item in keys and metal.
*
* @example
* const price = await priceUsingPricesTF(token, 105, 11);
* const price = await priceUsingPricesTF(token, '105;11');
* console.log("Strange Brigade Helm price: ${price.keys} keys ${price.metal} metal")
*
* @returns {Promise<PricesResponse>} Object containing 'keys' and 'metal' prices
* @throws When authentication fails or API returns non-200 status code
*/
async function priceUsingPricesTF(token: string, defIndex: number, quality: number): Promise<PricesResponse> {
async function priceUsingPricesTF(token: string, sku: string): Promise<PricesResponse> {
// prices.tf
// https://api2.prices.tf/prices/${sku}
// Authorization: Bearer ${token}
@@ -45,7 +45,6 @@ async function priceUsingPricesTF(token: string, defIndex: number, quality: numb
if (!token) {
reject(401)
}
const sku = defIndex + ";" + quality;
var response = await GM_fetch(`https://api2.prices.tf/prices/${encodeURIComponent(sku)}`, {
method: 'get',
headers: new Headers({
@@ -53,15 +52,18 @@ async function priceUsingPricesTF(token: string, defIndex: number, quality: numb
'Authorization': `Bearer ${token}`
})
})
if (response.status === 404 && quality === 6) {
// Try uncraftable variant
response = await GM_fetch(`https://api2.prices.tf/prices/${encodeURIComponent(sku + ';uncraftable')}`, {
method: 'get',
headers: new Headers({
'Accept': 'application/json',
'Authorization': `Bearer ${token}`
if (response.status === 404 && sku.includes(';')) {
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}`
})
})
})
}
}
if (response.status === 200) {
const json = await response.json()