UNPKG

zhonya

Version:

A simple and typed League of Legends API wrapper for Node.js

90 lines 3.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ZhonyaClient = void 0; const api_1 = require("./api"); const config_1 = require("./config"); const ZhonyaError_1 = require("./errors/ZhonyaError"); /** * Implementação do contexto interno * @private */ class ZhonyaContext { constructor(config) { this._initialized = false; this._config = { ...config }; } get config() { return this._config; } set initialized(value) { this._initialized = value; } checkInitialized() { if (!this._initialized) { throw new ZhonyaError_1.ZhonyaError("Zhonya client must be initialized before use. Use Zhonya.init() to create a client."); } } checkApiKey() { if (!this._config.riotApiKey) { throw new ZhonyaError_1.ZhonyaError("Riot API key is required for this method. Initialize the client with a valid API key."); } } } /** * Main client for the Zhonya */ class ZhonyaClient { /** * Initializes the Zhonya client and returns it ready to use * * @param riotApiKey - Riot Games API key (optional for some methods) * @param regionalRouting - Regional routing (default: americas) * @param platformRouting - Platform routing (default: br1) * @param language - Language for data returned (default: en_US) * @returns Zhonya client instance ready to use * * @example * ```typescript * import { ZhonyaClient } from "zhonya"; * * // Client with API key (full access to all methods) * const clientWithKey = ZhonyaClient.init({ * riotApiKey: process.env.RIOT_API_KEY, * regionalRouting: "americas", * platformRouting: "br1", * }); * * // Client without API key (limited access) * const clientWithoutKey = ZhonyaClient.init(); * // This will work * const champions = await clientWithoutKey.champion.getAll(); * // This will throw an error * const summoner = await clientWithoutKey.summoner.getByName("playerName"); * ``` * */ static init({ riotApiKey, regionalRouting = config_1.DEFAULT_REGIONAL_ROUTING, platformRouting = config_1.DEFAULT_PLATFORM_ROUTING, language = config_1.DEFAULT_LANGUAGE, } = {}) { const config = { riotApiKey, regionalRouting, platformRouting, language, }; const client = new ZhonyaClient(config); client.context.initialized = true; return client; } /** * Private constructor to prevent direct instantiation * Use Zhonya.init() instead */ constructor(config) { this.context = new ZhonyaContext(config); // Initialize APIs passando o contexto compartilhado this.champion = new api_1.ChampionAPI(this.context); this.summoner = new api_1.SummonerAPI(this.context); this.freeWeek = new api_1.FreeWeekAPI(this.context); } } exports.ZhonyaClient = ZhonyaClient; //# sourceMappingURL=client.js.map