You've already forked tf2wikipricing
refactor: priceUsingPricesTF now expects an SKU string
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user