diff --git a/__tests__/priceService.test.ts b/__tests__/priceService.test.ts index b9c421d..580bd12 100644 --- a/__tests__/priceService.test.ts +++ b/__tests__/priceService.test.ts @@ -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) }) diff --git a/src/content/priceService.ts b/src/content/priceService.ts index fd40e0d..767ebb0 100644 --- a/src/content/priceService.ts +++ b/src/content/priceService.ts @@ -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 diff --git a/src/content/pricing/pricestf.ts b/src/content/pricing/pricestf.ts index cdb273b..bf56fa0 100644 --- a/src/content/pricing/pricestf.ts +++ b/src/content/pricing/pricestf.ts @@ -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} 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 { +async function priceUsingPricesTF(token: string, sku: string): Promise { // 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()