@barbarbar338/bhapi
Version:
Brawlhalla API wrapper for NodeJS and web
175 lines • 7.03 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getGloryFromSteamURL = exports.getGloryFromSteamID = exports.getRankedBySteamURL = exports.getRankedBySteamID = exports.getStatsBySteamURL = exports.getStatsBySteamID = exports.getBHIDFromSteamURL = exports.getBHIDFromSteamID = exports.getSteamDataBySteamID = exports.getSteamDataByURL = void 0;
const tslib_1 = require("tslib");
const axios_1 = tslib_1.__importDefault(require("axios"));
const xml2js_1 = require("xml2js");
const brawlhalla_1 = require("./brawlhalla");
const client_1 = require("./client");
const types_1 = require("./types");
const steamURLRegex = /^https?:\/\/steamcommunity\.com\/(id|profiles)\/[a-zA-Z0-9_-]+\/?$/;
const validateURL = (url) => steamURLRegex.test(url);
const getSteamData = async (url) => {
if (!validateURL(url))
throw new types_1.BHAPIError("Not a valid Steam profile URL: " + url, {
code: "InvalidSteamURL",
status: 400,
details: `The provided URL "${url}" does not match the expected Steam profile format.`,
});
const xml = await axios_1.default
.get(url, {
params: { xml: 1 },
})
.then((r) => {
if (r.status > 199 && r.status < 300)
return r.data;
else
throw new types_1.BHAPIError("Not a valid Steam profile URL: " + url, {
code: "InvalidSteamURL",
status: 400,
details: `The provided URL "${url}" does not match the expected Steam profile format.`,
});
})
.catch(() => {
throw new types_1.BHAPIError("Not a valid Steam profile URL: " + url, {
code: "InvalidSteamURL",
status: 400,
details: `The provided URL "${url}" does not match the expected Steam profile format.`,
});
});
const result = (await (0, xml2js_1.parseStringPromise)(xml).catch(() => {
throw new types_1.BHAPIError("Not a valid Steam profile URL: " + url, {
code: "InvalidSteamURL",
status: 400,
details: `The provided URL "${url}" does not match the expected Steam profile format.`,
});
}));
if (result.profile && result.profile.steamID64)
return {
name: result.profile.steamID[0],
steam_id: result.profile.steamID64[0],
steam_url: "https://steamcommunity.com/profiles/" +
result.profile.steamID64[0],
};
else
throw new types_1.BHAPIError("Not a valid Steam profile URL: " + url, {
code: "InvalidSteamURL",
status: 400,
details: `The provided URL "${url}" does not match the expected Steam profile format.`,
});
};
/**
* Get Steam data by profile URL
*
* @param {string} steamProfileURL Steam profile URL
* @returns {Promise<SteamData>} Promise resolving to the Steam data
*/
const getSteamDataByURL = async (steamProfileURL) => getSteamData(steamProfileURL);
exports.getSteamDataByURL = getSteamDataByURL;
/**
* Get Steam data by Steam ID
*
* @param {string} steamID Steam ID
* @returns {Promise<SteamData>} Promise resolving to the Steam data
*/
const getSteamDataBySteamID = async (steamID) => getSteamData("https://steamcommunity.com/profiles/" + steamID);
exports.getSteamDataBySteamID = getSteamDataBySteamID;
/**
* Get Brawlhalla ID from Steam ID
*
* @param {string} steamID Steam ID
* @returns {Promise<number>} Promise resolving to the Brawlhalla ID
*/
const getBHIDFromSteamID = async (steamID) => {
const { data } = await client_1.request.get("search", {
params: {
steamid: steamID,
},
});
if (data.length === 0)
throw new types_1.BHAPIError("No player found with this Steam ID: " + steamID, {
code: "PlayerNotFound",
status: 404,
details: `No player found with Steam ID "${steamID}"`,
});
return data[0].brawlhalla_id;
};
exports.getBHIDFromSteamID = getBHIDFromSteamID;
/**
* Get Brawlhalla ID from Steam profile URL
*
* @param {string} steamProfileURL Steam profile URL
* @returns {Promise<number>} Promise resolving to the Brawlhalla ID
*/
const getBHIDFromSteamURL = async (steamProfileURL) => {
const steamData = await (0, exports.getSteamDataByURL)(steamProfileURL);
return (0, exports.getBHIDFromSteamID)(steamData.steam_id);
};
exports.getBHIDFromSteamURL = getBHIDFromSteamURL;
/**
* Get Brawlhalla stats by Steam ID
*
* @param {string} steamID Steam ID
* @returns {Promise<BrawlhallaStats>} Promise resolving to the Brawlhalla stats
*/
const getStatsBySteamID = async (steamID) => {
const brawlhallaId = await (0, exports.getBHIDFromSteamID)(steamID);
return (0, brawlhalla_1.getStatsByBHID)(brawlhallaId);
};
exports.getStatsBySteamID = getStatsBySteamID;
/**
* Get Brawlhalla stats by Steam profile URL
*
* @param {string} steamProfileURL Steam profile URL
* @returns {Promise<BrawlhallaStats>} Promise resolving to the Brawlhalla stats
*/
const getStatsBySteamURL = async (steamProfileURL) => {
const brawlhallaId = await (0, exports.getBHIDFromSteamURL)(steamProfileURL);
return (0, brawlhalla_1.getStatsByBHID)(brawlhallaId);
};
exports.getStatsBySteamURL = getStatsBySteamURL;
/**
* Get Brawlhalla ranked data by Steam ID
*
* @param {string} steamID Steam ID
* @returns {Promise<BrawlhallaRankedData>} Promise resolving to the Brawlhalla ranked data
*/
const getRankedBySteamID = async (steamID) => {
const brawlhallaId = await (0, exports.getBHIDFromSteamID)(steamID);
return (0, brawlhalla_1.getRankedByBHID)(brawlhallaId);
};
exports.getRankedBySteamID = getRankedBySteamID;
/**
* Get Brawlhalla ranked data by Steam profile URL
*
* @param {string} steamProfileURL Steam profile URL
* @returns {Promise<BrawlhallaRankedData>} Promise resolving to the Brawlhalla ranked data
*/
const getRankedBySteamURL = async (steamProfileURL) => {
const brawlhallaId = await (0, exports.getBHIDFromSteamURL)(steamProfileURL);
return (0, brawlhalla_1.getRankedByBHID)(brawlhallaId);
};
exports.getRankedBySteamURL = getRankedBySteamURL;
/**
* Get Brawlhalla glory by Steam ID
*
* @param {string} steamID Steam ID
* @returns {Promise<number>} Promise resolving to the Brawlhalla glory
*/
const getGloryFromSteamID = async (steamID) => {
const brawlhallaId = await (0, exports.getBHIDFromSteamID)(steamID);
return (0, brawlhalla_1.getGloryByBHID)(brawlhallaId);
};
exports.getGloryFromSteamID = getGloryFromSteamID;
/**
* Get Brawlhalla glory by Steam profile URL
*
* @param {string} steamProfileURL Steam profile URL
* @returns {Promise<number>} Promise resolving to the Brawlhalla glory
*/
const getGloryFromSteamURL = async (steamProfileURL) => {
const brawlhallaId = await (0, exports.getBHIDFromSteamURL)(steamProfileURL);
return (0, brawlhalla_1.getGloryByBHID)(brawlhallaId);
};
exports.getGloryFromSteamURL = getGloryFromSteamURL;
//# sourceMappingURL=steam.js.map