UNPKG

steam-api-sdk

Version:

A Node.js wrapper for the Steam Web API, includes many tools for fetching and parsing data from the Steam Web API.

221 lines (210 loc) 7.68 kB
var __defProp = Object.defineProperty; var __defProps = Object.defineProperties; var __getOwnPropDescs = Object.getOwnPropertyDescriptors; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); var __async = (__this, __arguments, generator) => { return new Promise((resolve, reject) => { var fulfilled = (value) => { try { step(generator.next(value)); } catch (e) { reject(e); } }; var rejected = (value) => { try { step(generator.throw(value)); } catch (e) { reject(e); } }; var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); step((generator = generator.apply(__this, __arguments)).next()); }); }; // config.ts var config = { apiKey: process.env.STEAM_API_KEY || null, debugMode: process.env.DEBUG_MODE === "true" || false }; var config_default = config; // Functions/From32To64.ts import Big2 from "big.js"; // utils.ts import Big from "big.js"; var BASE_NUM = Big("76561197960265728"); // Functions/From32To64.ts var From32To64 = (steam32) => Big2(steam32).plus(BASE_NUM).toString(); var From32To64_default = From32To64; // Functions/From64To32.ts import Big3 from "big.js"; var From64To32 = (steam64) => Big3(steam64).minus(BASE_NUM).toString(); var From64To32_default = From64To32; // Functions/From64ToSteamID.ts import Big4 from "big.js"; var Steam64ToID = (steam64) => { if (!steam64 || typeof steam64 !== "string") return null; let v = BASE_NUM, w = Big4(steam64), y = w.mod(2).toString(); w = w.minus(y).minus(v); if (parseInt(w) < 1) return null; return [`STEAM_0:${y}:${w.div(2).toString()}`, `STEAM_1:${y}:${w.div(2).toString()}`]; }; var From64ToSteamID_default = Steam64ToID; // Functions/From64ToUser.ts import { takeRight, dropRight } from "lodash-es"; import axios from "axios"; function From64ToUser(steam64, delay = 100) { return __async(this, null, function* () { if (!steam64) throw new Error("Invalid Steam64 ID"); const fetchUsers = (ids) => __async(this, null, function* () { const data = yield SteamFetch(ids); if (!data.length) throw new Error("Invalid Steam64 ID"); const users = data.map((p) => __spreadProps(__spreadValues({}, p), { steamIds: From64ToSteamID_default(p.steamid) })).filter((u) => !!u); return users; }); if (Array.isArray(steam64)) { if (steam64.length > 100) { const allUsers = []; while (steam64.length > 0) { const users = takeRight(steam64, 100); steam64 = dropRight(steam64, 100); const fetchedUsers = yield fetchUsers(users); allUsers.push(...fetchedUsers); if (delay) yield new Promise((r) => setTimeout(r, delay)); } return allUsers; } else { return yield fetchUsers(steam64); } } else { const users = yield fetchUsers([steam64]); if (!users.length) throw new Error("Could not find user"); return users[0]; } }); } var SteamFetch = (users) => __async(void 0, null, function* () { var _a; let apiKey = null; if (typeof config_default.apiKey === "string") { if ((_a = config_default.apiKey) == null ? void 0 : _a.includes(",")) { const keys = config_default.apiKey.split(","); apiKey = keys[Math.floor(Math.random() * keys.length)]; } else { apiKey = config_default.apiKey; } } else if (Array.isArray(config_default.apiKey)) { apiKey = config_default.apiKey[Math.floor(Math.random() * config_default.apiKey.length)]; } if (!apiKey) throw new Error("No API key found"); const url = `http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2/?key=${apiKey}&format=json&steamids=${Array.isArray(users) ? users.join(",") : users}`; try { const { data } = yield axios.get(url); return data.response.players; } catch (error) { if (axios.isAxiosError(error)) { if (error.response) { const { status, statusText } = error.response; throw new Error(`Error ${status}: ${statusText} (apiKey: ${apiKey})`); } else if (error.request) { throw new Error("No response received from server (apiKey: ${apiKey})"); } else { throw new Error(`Request error: ${error.message} (apiKey: ${apiKey})`); } } else { throw new Error(`Unexpected error: ${error} (apiKey: ${apiKey})`); } } }); var From64ToUser_default = From64ToUser; // Functions/SteamIDToSteam64.ts var SteamIDTo64 = (steamid) => { if (!steamid || typeof steamid !== "string") return null; let split = steamid.split(":"), v = BASE_NUM, z = split[2], y = split[1]; if (z && y) return v.plus(parseInt(z) * 2).plus(y).toString(); return null; }; var SteamIDToSteam64_default = SteamIDTo64; // Functions/VanityURLTo64.ts import axios2 from "axios"; var VanityURLTo64 = (url) => __async(void 0, null, function* () { if (!url || typeof url !== "string") return null; if (!url.includes("steamcommunity.com/id/")) return null; const newUrl = url.replace("steamcommunity.com/id/", "").replace("https://", "").replace("http://", "").replace("/", ""); if (newUrl.length < 1) return null; const API = `http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key=${config_default.apiKey}&vanityurl=${newUrl}`; const { data: { response } } = yield axios2(API); const { success, steamid } = response; if (!success) return null; return steamid; }); var VanityURLTo64_default = VanityURLTo64; // Functions/GetSteamUser.ts var GetSteamUser = (identifier) => __async(void 0, null, function* () { if (!identifier) throw new Error("Invalid Identifier"); let steam64 = null; if (identifier.includes("steamcommunity.com/id/")) { steam64 = yield VanityURLTo64_default(identifier); if (!steam64) throw new Error("Invalid Custom URL"); } else if (identifier.includes("steamcommunity.com/profiles/")) { identifier = identifier.replace("steamcommunity.com/profiles/", "").replace("https://", "").replace("http://", "").replace("/", ""); steam64 = identifier; } else if (identifier && identifier.toString().startsWith("STEAM_")) { steam64 = SteamIDToSteam64_default(identifier); if (!steam64) throw new Error("Invalid SteamID"); } if (!identifier.startsWith("765") && !steam64) { console.log("ERROR", "Invalid SteamID64", identifier, steam64); throw new Error("Invalid SteamID64"); } const user = yield From64ToUser_default(steam64 || identifier); if (!user) throw new Error("Invalid SteamID64"); return user; }); var GetSteamUser_default = GetSteamUser; export { From32To64_default as From32To64, From64To32_default as From64To32, From64ToSteamID_default as From64ToSteamID, From64ToUser_default as From64ToUser, GetSteamUser_default as GetSteamUser, SteamIDToSteam64_default as SteamIDToSteam64, VanityURLTo64_default as VanityURLTo64, config_default as config };