import * as axios from 'axios'; import { API_URL, API_VERSION } from './sdk'; import { IQuery } from './interfaces/query'; export class Client { static apiUrl: string = `${API_URL}/v${API_VERSION}`; static async get(resource: string, params?: IQuery[] | string): Promise { let url: string = `${this.apiUrl}/${resource}`; const POKEMONTCG_API_KEY = process.env.POKEMONTCG_API_KEY; const config: axios.AxiosRequestConfig = { headers: { 'Content-Type': 'application/json' } }; if (POKEMONTCG_API_KEY) { config.headers['X-Api-Key'] = POKEMONTCG_API_KEY; } 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]]; }) .catch(error => Promise.reject(error)); } 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; } }