Skip to content

Commit d30206b

Browse files
committed
Adds Fetch Steam Powered Inventory Handler
1 parent f68a462 commit d30206b

3 files changed

Lines changed: 125 additions & 0 deletions

File tree

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

src/lib/bridge/handlers/handlers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {FetchSlimTrades} from './fetch_slim_trades';
3636
import {NotaryProve} from './notary_prove';
3737
import {FetchNotaryMeta} from './fetch_notary_meta';
3838
import {FetchNotaryToken} from './fetch_notary_token';
39+
import {FetchSteamPoweredInventory} from './fetch_steam_inventory';
3940

4041
export const HANDLERS_MAP: {[key in RequestType]: RequestHandler<any, any>} = {
4142
[RequestType.EXECUTE_SCRIPT_ON_PAGE]: ExecuteScriptOnPage,
@@ -74,4 +75,5 @@ export const HANDLERS_MAP: {[key in RequestType]: RequestHandler<any, any>} = {
7475
[RequestType.NOTARY_PROVE]: NotaryProve,
7576
[RequestType.FETCH_NOTARY_META]: FetchNotaryMeta,
7677
[RequestType.FETCH_NOTARY_TOKEN]: FetchNotaryToken,
78+
[RequestType.FETCH_STEAM_POWERED_INVENTORY]: FetchSteamPoweredInventory,
7779
};

src/lib/bridge/handlers/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ export enum RequestType {
3535
NOTARY_PROVE = 33,
3636
FETCH_NOTARY_META = 34,
3737
FETCH_NOTARY_TOKEN = 35,
38+
FETCH_STEAM_POWERED_INVENTORY = 36,
3839
}

0 commit comments

Comments
 (0)