import { BASE_URL, EApiActions } from '../../ressources/comon'; import { singleton } from 'tsyringe'; import axios from 'axios'; import { EApiErrors, RequestErrors } from '../../ressources/errors'; @singleton() export class Query { private apiKey: string | null; async setApiKey(apiKey: string) { this.apiKey = apiKey; } makeCall( action: EApiActions, query?: Record ): any { query = query || {}; return new Promise((resolve, reject) => { if (!this.apiKey) return reject(new Error(RequestErrors.MissingApiKey)); const params = new URLSearchParams({ api_key: this.apiKey, action: EApiActions[action], ...query, }); axios .get(BASE_URL, { params, }) .then((result) => { if (typeof result.data == 'string' && EApiErrors[result.data]) return reject(new Error(EApiErrors[result.data])); resolve(result.data); }) .catch((error) => reject(error)); }); } }