UNPKG

osrs-json-hiscores

Version:

The Old School Runescape API wrapper that does more!

122 lines (121 loc) 4.92 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.validateRSN = exports.httpGet = exports.rsnFromElement = exports.numberFromElement = exports.getActivityPageURL = exports.getSkillPageURL = exports.getPlayerTableURL = exports.getStatsURL = void 0; var axios_1 = require("axios"); var ua = require("useragent-generator"); var constants_1 = require("./constants"); /** * Will generate a stats URL for the official OSRS API. * * @param gamemode Gamemode to fetch ranks for. * @param rsn Username of the player. * @param json If the JSON endpoint is desired instead of CSV. * @returns Encoded stats URL. */ var getStatsURL = function (gamemode, rsn, json) { if (json === void 0) { json = false; } return "".concat(constants_1.GAMEMODE_URL[gamemode]).concat(json ? constants_1.JSON_STATS_URL : constants_1.STATS_URL).concat(encodeURIComponent(rsn)); }; exports.getStatsURL = getStatsURL; /** * Will generate a player table URL for the official OSRS hiscores website. * * @param gamemode Gamemode to fetch ranks for. * @param rsn Username of the player. * @returns Encoded player table URL. */ var getPlayerTableURL = function (gamemode, rsn) { return "".concat(constants_1.GAMEMODE_URL[gamemode]).concat(constants_1.SCORES_URL, "table=0&user=").concat(encodeURIComponent(rsn)); }; exports.getPlayerTableURL = getPlayerTableURL; /** * Will generate a skill table URL for the official OSRS hiscores website. * * @param gamemode Gamemode to fetch ranks for. * @param skill Skill to fetch ranks for. * @param page Page number. * @returns */ var getSkillPageURL = function (gamemode, skill, page) { return "".concat(constants_1.GAMEMODE_URL[gamemode]).concat(constants_1.SCORES_URL, "table=").concat(constants_1.SKILLS.indexOf(skill), "&page=").concat(page); }; exports.getSkillPageURL = getSkillPageURL; /** * Will generate an activity table URL for the official OSRS hiscores website. * * @param gamemode Gamemode to fetch ranks for. * @param activity Activity or boss to fetch ranks for. * @param page Page number. * @returns */ var getActivityPageURL = function (gamemode, activity, page) { return "".concat(constants_1.GAMEMODE_URL[gamemode]).concat(constants_1.SCORES_URL, "category_type=1&table=").concat(constants_1.ACTIVITIES.indexOf(activity), "&page=").concat(page); }; exports.getActivityPageURL = getActivityPageURL; /** * Extracts a number from an OSRS hiscores table cell element. * * @param el OSRS hiscores table cell element. * @returns Number parsed from cell text. */ var numberFromElement = function (el) { var _a; var innerHTML = (el !== null && el !== void 0 ? el : {}).innerHTML; var number = (_a = innerHTML === null || innerHTML === void 0 ? void 0 : innerHTML.replace(/[\n|,]/g, '')) !== null && _a !== void 0 ? _a : '-1'; return parseInt(number, 10); }; exports.numberFromElement = numberFromElement; /** * Extracts a RSN from an OSRS hiscores table cell element. * * @param el OSRS hiscores table cell element. * @returns RSN parsed from cell text. */ var rsnFromElement = function (el) { var _a; var innerHTML = (el !== null && el !== void 0 ? el : {}).innerHTML; return (_a = innerHTML === null || innerHTML === void 0 ? void 0 : innerHTML.replace(/\uFFFD/g, ' ')) !== null && _a !== void 0 ? _a : ''; }; exports.rsnFromElement = rsnFromElement; /** * Will run an Axios `GET` request against a given URL after injecting a `User-Agent` header. * * @param url URL to run a `GET` request against. * @returns Axios response. */ var httpGet = function (url, config) { if (config === void 0) { config = {}; } return axios_1.default.get(url, __assign({ headers: { // without User-Agent header requests may be rejected by DDoS protection mechanism 'User-Agent': ua.firefox(80) } }, config)); }; exports.httpGet = httpGet; /** * Validates that a provided RSN has the same username restrictions as Jagex. * @param rsn Username to validate. * @throws Error if the RSN fails validation. */ var validateRSN = function (rsn) { if (typeof rsn !== 'string') { throw new constants_1.InvalidRSNError('RSN must be a string'); } else if (!/^[a-zA-Z0-9 _-]+$/.test(rsn)) { throw new constants_1.InvalidRSNError('RSN contains invalid character'); } else if (rsn.length > 12 || rsn.length < 1) { throw new constants_1.InvalidRSNError('RSN must be between 1 and 12 characters'); } }; exports.validateRSN = validateRSN;