1+ import { decodeLink , CEconItemPreviewDataBlock } from '@csfloat/cs2-inspect-serializer' ;
12import { SimpleHandler } from './main' ;
23import { RequestType } from './types' ;
34
@@ -47,6 +48,7 @@ export interface ItemInfo {
4748export interface FetchInspectInfoRequest {
4849 link : string ;
4950 listPrice ?: number ;
51+ marketHashName ?: string ;
5052}
5153
5254export interface FetchInspectInfoResponse {
@@ -56,16 +58,73 @@ export interface FetchInspectInfoResponse {
5658
5759export const FetchInspectInfo = new SimpleHandler < FetchInspectInfoRequest , FetchInspectInfoResponse > (
5860 RequestType . FETCH_INSPECT_INFO ,
59- ( req ) => {
60- const apiUrl = `https://api.csfloat.com/?url=${ req . link } &minimal=true${ req . listPrice ? '&listPrice=' + req . listPrice : '' } ` ;
61- return fetch ( apiUrl ) . then ( ( resp ) => {
62- return resp . json ( ) . then ( ( json : FetchInspectInfoResponse ) => {
63- if ( resp . ok ) {
64- return json ;
65- } else {
66- throw Error ( json . error ) ;
67- }
68- } ) as Promise < FetchInspectInfoResponse > ;
69- } ) ;
61+ async ( req ) => {
62+ const itemMetadata = parseMarketHashName ( req . marketHashName ) ;
63+ let decoded : CEconItemPreviewDataBlock ;
64+ try {
65+ decoded = decodeLink ( req . link ) ;
66+ } catch ( error ) {
67+ throw new Error ( 'Failed to decode inspect link' ) ;
68+ }
69+
70+ return {
71+ iteminfo : {
72+ stickers : decoded . stickers . map ( ( sticker ) => ( {
73+ slot : sticker . slot ?? 0 ,
74+ stickerId : sticker . stickerId ?? 0 ,
75+ wear : sticker . wear ,
76+ } ) ) ,
77+ keychains : decoded . keychains . map ( ( keychain ) => ( {
78+ slot : keychain . slot ?? 0 ,
79+ stickerId : keychain . stickerId ?? 0 ,
80+ wear : keychain . wear ,
81+ pattern : keychain . pattern ?? 0 ,
82+ } ) ) ,
83+ itemid : decoded . itemid ?. toString ( ) ?? '' ,
84+ defindex : decoded . defindex ?? 0 ,
85+ paintindex : decoded . paintindex ?? 0 ,
86+ rarity : decoded . rarity ?? 0 ,
87+ quality : decoded . quality ?? 0 ,
88+ paintseed : decoded . paintseed ?? 0 ,
89+ inventory : decoded . inventory ?? 0 ,
90+ origin : decoded . origin ?? 0 ,
91+ s : '' ,
92+ a : '' ,
93+ d : '' ,
94+ m : '' ,
95+ floatvalue : decoded . paintwear ?? 0 ,
96+ imageurl : '' ,
97+ min : 0 ,
98+ max : 1 ,
99+ weapon_type : itemMetadata ?. weaponType ,
100+ item_name : itemMetadata ?. itemName ,
101+ wear_name : itemMetadata ?. wearName ,
102+ full_item_name : req . marketHashName ,
103+ } ,
104+ } ;
70105 }
71106) ;
107+
108+ interface ParsedMarketHashName {
109+ weaponType ?: string ;
110+ itemName ?: string ;
111+ wearName ?: string ;
112+ }
113+
114+ function parseMarketHashName ( marketHashName ?: string ) : ParsedMarketHashName | undefined {
115+ if ( ! marketHashName ) {
116+ return ;
117+ }
118+
119+ const match = / ^ ( .* ?) \| ( .* ?) (?: \( ( [ ^ ) ] + ) \) ) ? $ / . exec ( marketHashName ) ;
120+ if ( ! match ) {
121+ return ;
122+ }
123+
124+ const [ , weaponType , itemName , wearName ] = match ;
125+ return {
126+ weaponType,
127+ itemName,
128+ wearName,
129+ } ;
130+ }
0 commit comments