You've already forked tf2wikipricing
refactor: moved components to separate modules
`content.ts` is now half less than the size, exported functions can be unit tested
This commit is contained in:
81
src/content/priceService.ts
Normal file
81
src/content/priceService.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
import { defindex_key, storage_priceprefix } from "./config"
|
||||
import { priceUsingPricesTF } from "./pricing/pricestf"
|
||||
import { getStorageValue, setStorageValue } from "./storage"
|
||||
import { logDebug, log } from "./utils/log"
|
||||
|
||||
/** Pricing data for a given TF2 item. */
|
||||
export class ItemPriceData {
|
||||
/** Item SKU. */
|
||||
sku: string
|
||||
/** Date updated. */
|
||||
update: Date
|
||||
/** TTL in milliseconds. */
|
||||
ttl: number
|
||||
/** Price in keys. */
|
||||
keys: number
|
||||
/** Price in refined metal. */
|
||||
metal: number
|
||||
/** Steam Community Market price. */
|
||||
scmPrice: number
|
||||
|
||||
toString(): string {
|
||||
return `Price for ${this.sku}, fetched ${this.update} (expires ${this.update.getTime() + this.ttl})\n` +
|
||||
JSON.stringify({
|
||||
keys: this.keys,
|
||||
metal: this.metal,
|
||||
scmPrice: this.scmPrice
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export async function fetchKeyPrice(token: string) {
|
||||
return fetchPrice(token, defindex_key, 6, new Date(), 86400000)
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a price for a given SKU, using cached values if available.
|
||||
* @param token prices.tf access token.
|
||||
* @param update Date retrieved.
|
||||
* @param ttl Time to cache results in milliseconds. 30 minutes by default.
|
||||
*/
|
||||
export async function fetchPrice(token: string, defIndex: number, quality: number, update: Date = new Date(), ttl: number = 30 * 60 * 1000): Promise<ItemPriceData> {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
const sku = defIndex.toString() + ";" + quality.toString();
|
||||
var data: ItemPriceData | null
|
||||
|
||||
const cached = await getStorageValue(storage_priceprefix + sku, null)
|
||||
if (cached != null && 'keys' in cached && 'metal' in cached) {
|
||||
data = cached
|
||||
}
|
||||
|
||||
if (!data || data.sku != sku || 'update' in data && 'ttl' in data && Date.now() > (new Date(data.update).getTime() + data.ttl)) {
|
||||
logDebug(`Fetching price data for ${sku}`)
|
||||
if(!token) {
|
||||
reject(401)
|
||||
}
|
||||
data = new ItemPriceData()
|
||||
data.sku = sku
|
||||
data.update = update
|
||||
data.ttl = ttl
|
||||
|
||||
try {
|
||||
const response = await priceUsingPricesTF(token, defIndex, quality)
|
||||
if (response) {
|
||||
data.keys = response.keys
|
||||
data.metal = response.metal
|
||||
}
|
||||
} catch (error) {
|
||||
log(`Received ${error} error while pricing ${sku} using prices.tf`)
|
||||
reject(error)
|
||||
}
|
||||
|
||||
if ('metal' in data && 'keys' in data) {
|
||||
await setStorageValue(storage_priceprefix + sku, data)
|
||||
}
|
||||
} else {
|
||||
logDebug(`Using cached price data for ${sku}`)
|
||||
}
|
||||
resolve(data)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user