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) const result = await fetchPrice(mockToken, mockDefIndex, mockQuality)
expect(priceUsingPricesTF).toHaveBeenCalledWith(mockToken, mockDefIndex, mockQuality) expect(priceUsingPricesTF).toHaveBeenCalledWith(mockToken, `${mockDefIndex};${mockQuality}`)
expect(setStorageValue).toHaveBeenCalled() expect(setStorageValue).toHaveBeenCalled()
expect(result.metal).not.toBe(mockCachedData.metal) expect(result.metal).not.toBe(mockCachedData.metal)
expect(result.metal).toBe(mockPriceResponse.metal) expect(result.metal).toBe(mockPriceResponse.metal)
@@ -86,7 +86,7 @@ describe('Price Service', () => {
const result = await fetchKeyPrice(mockToken) 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.keys).toBe(0) // A key cannot cost a key :P
expect(result.metal).toBe(mockKeyPriceResponse.metal) 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 data.ttl = ttl
try { try {
const response = await priceUsingPricesTF(token, defIndex, quality) const response = await priceUsingPricesTF(token, sku)
if (response) { if (response) {
data.keys = response.keys data.keys = response.keys
data.metal = response.metal 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. * and uses it to fetch the latest pricing data for the given item in keys and metal.
* *
* @example * @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") * console.log("Strange Brigade Helm price: ${price.keys} keys ${price.metal} metal")
* *
* @returns {Promise<PricesResponse>} Object containing 'keys' and 'metal' prices * @returns {Promise<PricesResponse>} Object containing 'keys' and 'metal' prices
* @throws When authentication fails or API returns non-200 status code * @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 // prices.tf
// https://api2.prices.tf/prices/${sku} // https://api2.prices.tf/prices/${sku}
// Authorization: Bearer ${token} // Authorization: Bearer ${token}
@@ -45,7 +45,6 @@ async function priceUsingPricesTF(token: string, defIndex: number, quality: numb
if (!token) { if (!token) {
reject(401) reject(401)
} }
const sku = defIndex + ";" + quality;
var response = await GM_fetch(`https://api2.prices.tf/prices/${encodeURIComponent(sku)}`, { var response = await GM_fetch(`https://api2.prices.tf/prices/${encodeURIComponent(sku)}`, {
method: 'get', method: 'get',
headers: new Headers({ headers: new Headers({
@@ -53,15 +52,18 @@ async function priceUsingPricesTF(token: string, defIndex: number, quality: numb
'Authorization': `Bearer ${token}` 'Authorization': `Bearer ${token}`
}) })
}) })
if (response.status === 404 && quality === 6) { if (response.status === 404 && sku.includes(';')) {
// Try uncraftable variant const quality: number = parseInt(sku.split(';')[1], 10);
response = await GM_fetch(`https://api2.prices.tf/prices/${encodeURIComponent(sku + ';uncraftable')}`, { if(quality === 6) {
method: 'get', // Try uncraftable variant if unique weapon
headers: new Headers({ response = await GM_fetch(`https://api2.prices.tf/prices/${encodeURIComponent(sku + ';uncraftable')}`, {
'Accept': 'application/json', method: 'get',
'Authorization': `Bearer ${token}` headers: new Headers({
'Accept': 'application/json',
'Authorization': `Bearer ${token}`
})
}) })
}) }
} }
if (response.status === 200) { if (response.status === 200) {
const json = await response.json() const json = await response.json()