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

@@ -62,7 +62,7 @@ describe('Price Service', () => {
const result = await fetchPrice(mockToken, mockDefIndex, mockQuality)
expect(priceUsingPricesTF).toHaveBeenCalledWith(mockToken, mockDefIndex, mockQuality)
expect(priceUsingPricesTF).toHaveBeenCalledWith(mockToken, `${mockDefIndex};${mockQuality}`)
expect(setStorageValue).toHaveBeenCalled()
expect(result.metal).not.toBe(mockCachedData.metal)
expect(result.metal).toBe(mockPriceResponse.metal)
@@ -86,7 +86,7 @@ describe('Price Service', () => {
const result = await fetchKeyPrice(mockToken)
expect(priceUsingPricesTF).toHaveBeenCalledWith(mockToken, defindex_key, 6)
expect(priceUsingPricesTF).toHaveBeenCalledWith(mockToken, `${defindex_key};6`)
expect(result.keys).toBe(0) // A key cannot cost a key :P
expect(result.metal).toBe(mockKeyPriceResponse.metal)
})

View File

@@ -60,7 +60,7 @@ export async function fetchPrice(token: string, defIndex: number, quality: numbe
data.ttl = ttl
try {
const response = await priceUsingPricesTF(token, defIndex, quality)
const response = await priceUsingPricesTF(token, sku)
if (response) {
data.keys = response.keys
data.metal = response.metal

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,8 +52,10 @@ async function priceUsingPricesTF(token: string, defIndex: number, quality: numb
'Authorization': `Bearer ${token}`
})
})
if (response.status === 404 && quality === 6) {
// Try uncraftable variant
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({
@@ -63,6 +64,7 @@ async function priceUsingPricesTF(token: string, defIndex: number, quality: numb
})
})
}
}
if (response.status === 200) {
const json = await response.json()
resolve({ keys: json['sellKeys'], metal: json['sellHalfScrap'] / 18.0 })