import {z} from 'zod' export type CurrencyModel = 'rub' | 'uah' | 'kzt' | 'byn' | 'usd' | 'eur' | 'gbp' | 'cny' | 'try' | 'jpy' | 'brl'; export type GetApiSteamValueArgs = {link: string, app_id?: (730 | 578080 | 753 | 570 | 440 | 252490 | 304930 | 232090 | 322330) | undefined, currency?: CurrencyModel | undefined, ignore_cache?: boolean | undefined}; export type GetApiSteamValueOptions = {baseUrl?: string | undefined, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const respSystemInfo = z.object({rate_limit: z.object({limit: z.number().int(), remaining: z.number().int(), reset: z.number().int(), bucket: z.string().optional()}), visitor_id: z.number().int(), time: z.number().int(), log_id: z.number().int()}); export const getApiSteamValueResponse = z.object({query: z.string().optional(), data: z.object({items: z.object({"0": z.object({classid: z.string(), tradable: z.number().int(), marketable: z.number().int(), image_url: z.string(), title: z.string(), price: z.number(), count: z.number().int(), type: z.string(), market_hash_name: z.string(), fraudwarnings: z.unknown(), stickers: z.object({stickerCount: z.number().int(), count: z.number().int(), images: z.array(z.string()), title: z.string()})}).optional()}), steam_id: z.string(), appId: z.number().int(), appTitle: z.string(), totalValue: z.number(), itemCount: z.number().int(), marketableItemCount: z.number().int(), currency: z.string(), currencyIcon: z.string(), language: z.string(), time: z.number().int()}).optional(), appId: z.number().int().optional(), system_info: respSystemInfo.optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Get Steam Inventory Value * * GET /steam-value */ export const getApiSteamValue = async (args: GetApiSteamValueArgs, options?: GetApiSteamValueOptions) => { const {baseUrl = "https://api.lzt.market", fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.link != null) query.append("link", String(args.link)) if (args.app_id != null) query.append("app_id", String(args.app_id)) if (args.currency != null) query.append("currency", String(args.currency)) if (args.ignore_cache != null) query.append("ignore_cache", String(args.ignore_cache)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/steam-value${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiSteamValueResponse, await res.json()) };