import RequestWrapper from './Request' import { Serie, Set, Card, CardResume, SerieList, SetList, SupportedLanguages } from './interfaces' export default class TCGdex { public static defaultLang: SupportedLanguages = "en" public constructor(public lang?: SupportedLanguages) {} public getLang() { return this.lang || TCGdex.defaultLang } private getBaseUrl() { return `https://api.tcgdex.net/v2/${this.getLang()}` } public async fetchCard(id: string | number, set?: string): Promise { const path = set ? ['sets', set] : ['cards'] return this.rwgr(...path, id).get() } public async fetchCards(set?: string): Promise | undefined> { if (set) { const setSingle = await this.fetchSet(set) if (!setSingle) { return undefined } return setSingle.cards } const req = this.rwgr>('cards') const resp = await req.get() if (!resp) { return undefined } return resp } public async fetchSet(set: string): Promise { const req = this.rwgr('sets', set) const resp = await req.get() if (!resp) { return undefined } return resp } public async fetchSerie(expansion: string): Promise { const req = this.rwgr('series', expansion) return req.get() } public async fetchSeries(): Promise { const req = this.rwgr('series') return req.get() } public async fetchSets(expansion?: string): Promise { if (expansion) { const expansionSingle = await this.fetchSerie(expansion) if (!expansionSingle) { return undefined } return expansionSingle.sets } const req = this.rwgr('sets') const list = await req.get() if (!list) { return undefined } return list } private rwgr(...url: Array) { return RequestWrapper.getRequest(`${this.getBaseUrl()}/${url.map((v) => encodeURI( // Normalize URL v.toString().replace('?', '%3F').normalize('NFC').replace(/["'\u0300-\u036f]/g, "") )).join('/')}`) } }