import * as axios from 'axios'; import { PokemonTCG } from './sdk'; import { IQuery } from './interfaces/query'; export class Client { static apiUrl: string = `${PokemonTCG.API_URL}/v${PokemonTCG.version}`; static get(resource: string, params?: IQuery[] | string): Promise { let url: string = `${this.apiUrl}/${resource}`; let config: axios.AxiosRequestConfig = { headers: { 'Content-Type': 'application/json' } }; if(typeof params === 'string') url += `/${params}`; else url += `?${this.paramsToQuery(params)}`; return axios.default.get(url, config) .then(response => { return response.data[Object.keys(response.data)[0]]; }) } private static paramsToQuery(params?: IQuery[]): string { let query: string = ''; if (params) { params.map((q: IQuery) => { query += `${q.name}=${encodeURIComponent(q.value.toString())}`.concat('&'); }); } return query; } }