UNPKG

sprcl

Version:

Simple API client for Clash of Clans, Clash Royale, and Brawl Stars

45 lines (38 loc) 1.54 kB
const path = require('path'); const fs = require("fs") const { Connector } = require('./connector'); class Client { constructor(apiToken, game = 'clashofclans') { this.connector = new Connector(apiToken, game); this.game = game; const basePath = `./${game}`; this.Player = require(path.join(__dirname, basePath, 'player')).Player; this.Clan = require(path.join(__dirname, basePath, 'clan')).Clan; if (game === 'brawlstars') { this.Club = this.Clan; } } async getPlayer(tag) { tag = this.argumentToString(`getPlayer(${tag})`, tag, 'tag'); const data = await this.connector.request(['players', tag]); if (data?.reason) return data; return this.Player.fromJSON(data); } async getClan(tag) { tag = this.argumentToString(`getClan(${tag})`, tag, 'tag'); const endpoint = this.game === 'brawlstars' ? 'clubs' : 'clans'; const data = await this.connector.request([endpoint, tag]); if (data?.reason) return data; return this.Clan.fromJSON(data); } async getClub(tag) { return this.getClan(tag); } argumentToString(caller, object, key) { if (!object) throw new Error(`[!] ${caller}: Empty argument`); if (typeof object === 'string') return object; if (typeof object === 'object' && object[key]) return object[key]; throw new Error(`[!] ${caller}: Invalid object or missing key "${key}"`); } } module.exports = { Client };