UNPKG

blox-api

Version:

Roblox web API wrapper for Node.js

111 lines 4.17 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RobloxClient = void 0; const auth_1 = require("../lib/auth"); const keyv_1 = __importDefault(require("keyv")); const keyv_file_1 = __importDefault(require("keyv-file")); const path = __importStar(require("path")); const user_1 = require("../lib/user"); const GroupReference_1 = require("./GroupReference"); class RobloxClient { /** * Create a new [[RobloxClient]] * * @param options Configuration options for a [[RobloxClient]] */ constructor(options) { options = options || { userId: "default" }; if (!options.userId) { options.userId = "default"; } this.options = options; this.cacheKeyv = new keyv_1.default(options.cacheUri); this.cacheKeyv.on("error", (err) => console.error("Cache connection error: " + err)); if (options.storageUri) { this.storageKeyv = new keyv_1.default(options.storageUri); this.storageKeyv.on("error", (err) => console.error("Persistent storage connection error: " + err)); } else { this.storageKeyv = new keyv_1.default({ store: new keyv_file_1.default({ filename: path.join(process.cwd(), options.storageFileName || "roblox.json"), }), }); } } /** * Authenticate the account to access secured endpoints * * @param cookie .ROBLOSECURITY cookie for the account */ async login(cookie) { const userId = this.options.userId; let savedSessionIsValid = false; const savedSession = await this.storageKeyv.get(userId); if (savedSession) { try { this.session = await auth_1.refreshSessionFromCookie(savedSession.cookie); savedSessionIsValid = true; } catch (err) { console.warn("Saved session is invalid, trying with provided cookie"); this.storageKeyv.delete(userId); } if (savedSessionIsValid) { this.storageKeyv.set(userId, this.session); return; } } try { this.session = await auth_1.refreshSessionFromCookie(cookie); this.storageKeyv.set(userId, this.session); } catch (err) { throw (err); } } /** * Returns information about the currently logged-in user */ async getCurrentUserInfo() { if (!this.session) { throw new Error("You must be logged in to use getCurrentUserInfo()"); } return user_1.getUserFromSession(this.session); } /** * Returns a [[GroupReference]] to the provided group ID * * @param groupId The ID of the group */ getGroupFromId(groupId) { if (!this.session) { throw new Error("The RobloxClient must be logged in to create a GroupReference"); } return new GroupReference_1.GroupReference(groupId, this.session); } } exports.RobloxClient = RobloxClient; //# sourceMappingURL=RobloxClient.js.map