|
| 1 | +import {SimpleHandler} from './main'; |
| 2 | +import {RequestType} from './types'; |
| 3 | +import {getAccessToken} from '../../alarms/access_token'; |
| 4 | + |
| 5 | +export interface FetchSteamPoweredInventoryRequest { |
| 6 | + // steam_id needs to correspond to the logged in user |
| 7 | + steam_id: string; |
| 8 | + app_id: number; |
| 9 | + context_id: number; |
| 10 | + start_assetid?: string; |
| 11 | + count?: number; |
| 12 | + get_descriptions?: boolean; |
| 13 | + for_trade_offer_verification?: boolean; |
| 14 | + language?: string; |
| 15 | + get_asset_properties?: boolean; |
| 16 | +} |
| 17 | + |
| 18 | +export interface InventoryAsset { |
| 19 | + assetid: string; |
| 20 | + classid: string; |
| 21 | + instanceid: string; |
| 22 | +} |
| 23 | + |
| 24 | +export interface InventoryDescriptionItem { |
| 25 | + value: string; |
| 26 | + name: string; |
| 27 | +} |
| 28 | + |
| 29 | +export interface InventoryTag { |
| 30 | + category: string; |
| 31 | + internal_name: string; |
| 32 | + localized_category_name: string; |
| 33 | + localized_tag_name: string; |
| 34 | +} |
| 35 | + |
| 36 | +export interface AssetProperty { |
| 37 | + propertyid: number; |
| 38 | + int_value?: string; |
| 39 | + float_value?: string; |
| 40 | + string_value?: string; |
| 41 | +} |
| 42 | + |
| 43 | +export interface AssetPropertyWrapper { |
| 44 | + appid: number; |
| 45 | + asset_properties: AssetProperty[]; |
| 46 | + assetid: string; |
| 47 | + contextid: string; |
| 48 | +} |
| 49 | + |
| 50 | +export interface InventoryDescription { |
| 51 | + name: string; |
| 52 | + market_hash_name: string; |
| 53 | + icon_url: string; |
| 54 | + icon_url_large: string; |
| 55 | + tradable: boolean; |
| 56 | + type: string; |
| 57 | + name_color: string; |
| 58 | + commodity: boolean; |
| 59 | + inspect_link?: string; |
| 60 | + classid: string; |
| 61 | + instanceid: string; |
| 62 | + marketable: boolean; |
| 63 | + descriptions: InventoryDescriptionItem[]; |
| 64 | + tags: InventoryTag[]; |
| 65 | +} |
| 66 | + |
| 67 | +export interface FetchSteamPoweredInventoryResponse { |
| 68 | + assets: InventoryAsset[]; |
| 69 | + descriptions: InventoryDescription[]; |
| 70 | + asset_properties?: AssetPropertyWrapper[]; |
| 71 | + last_assetid?: string; |
| 72 | + total_inventory_count: number; |
| 73 | +} |
| 74 | + |
| 75 | +export const FetchSteamPoweredInventory = new SimpleHandler< |
| 76 | + FetchSteamPoweredInventoryRequest, |
| 77 | + FetchSteamPoweredInventoryResponse |
| 78 | +>(RequestType.FETCH_STEAM_POWERED_INVENTORY, async (req) => { |
| 79 | + const accessToken = await getAccessToken(req.steam_id); |
| 80 | + |
| 81 | + const params = new URLSearchParams({ |
| 82 | + access_token: accessToken.token, |
| 83 | + steamid: req.steam_id, |
| 84 | + appid: req.app_id.toString(), |
| 85 | + contextid: req.context_id.toString(), |
| 86 | + }); |
| 87 | + |
| 88 | + if (req.start_assetid) { |
| 89 | + params.set('start_assetid', req.start_assetid); |
| 90 | + } |
| 91 | + |
| 92 | + if (req.count) { |
| 93 | + params.set('count', req.count.toString()); |
| 94 | + } |
| 95 | + |
| 96 | + if (req.get_descriptions !== undefined) { |
| 97 | + params.set('get_descriptions', req.get_descriptions.toString()); |
| 98 | + } |
| 99 | + |
| 100 | + if (req.for_trade_offer_verification !== undefined) { |
| 101 | + params.set('for_trade_offer_verification', req.for_trade_offer_verification.toString()); |
| 102 | + } |
| 103 | + |
| 104 | + if (req.language) { |
| 105 | + params.set('language', req.language); |
| 106 | + } |
| 107 | + |
| 108 | + if (req.get_asset_properties !== undefined) { |
| 109 | + params.set('get_asset_properties', req.get_asset_properties.toString()); |
| 110 | + } |
| 111 | + |
| 112 | + const resp = await fetch( |
| 113 | + `https://api.steampowered.com/IEconService/GetInventoryItemsWithDescriptions/v1/?${params.toString()}` |
| 114 | + ); |
| 115 | + |
| 116 | + if (!resp.ok) { |
| 117 | + throw new Error(`Invalid response code: ${resp.status}`); |
| 118 | + } |
| 119 | + |
| 120 | + const data = await resp.json(); |
| 121 | + |
| 122 | + if (!data.response) { |
| 123 | + throw new Error('Invalid response from Steam API'); |
| 124 | + } |
| 125 | + |
| 126 | + return data.response as FetchSteamPoweredInventoryResponse; |
| 127 | +}); |
0 commit comments