Revert "Add v2 Migration"

This commit is contained in:
Tee Diang
2021-03-16 00:24:25 -04:00
committed by GitHub
parent 3b3b65e7d0
commit 76fa13fa12
38 changed files with 885 additions and 5920 deletions

View File

@ -1,71 +1,37 @@
import * as axios from 'axios';
import { Query } from './interfaces/query';
import { API_URL, API_VERSION } from './sdk';
import { IQuery } from './interfaces/query';
export class Client {
private readonly POKEMONTCG_API_BASE_URL: string =
'https://api.pokemontcg.io';
private readonly POKEMONTCG_API_VERSION: string = '2';
private readonly POKEMONTCG_API_URL: string = `${this.POKEMONTCG_API_BASE_URL}/v${this.POKEMONTCG_API_VERSION}`;
private readonly POKEMONTCG_API_KEY?: string =
process.env.POKEMONTCG_API_KEY;
static apiUrl: string = `${API_URL}/v${API_VERSION}`;
private static instance: Client;
static async get(resource: string, params?: IQuery[] | string): Promise<any> {
let url: string = `${this.apiUrl}/${resource}`;
const config: axios.AxiosRequestConfig = {
headers: {
'Content-Type': 'application/json'
}
};
private constructor() {}
if(typeof params === 'string') url += `/${params}`;
else url += `?${this.paramsToQuery(params)}`;
public static getInstance(): Client {
if (!Client.instance) {
Client.instance = new Client();
}
return axios.default.get<any>(url, config)
.then(response => {
return response.data[Object.keys(response.data)[0]];
})
.catch(error => Promise.reject(error));
}
return Client.instance;
private static paramsToQuery(params?: IQuery[]): string {
let query: string = '';
if (params) {
params.map((q: IQuery) => {
query += `${q.name}=${encodeURIComponent(q.value.toString())}`.concat('&');
});
}
async get<T>(resource: string, params?: Query[] | string): Promise<T> {
let url = `${this.POKEMONTCG_API_URL}/${resource}`;
const headers = {
'Content-Type': 'application/json',
};
if (this.POKEMONTCG_API_KEY) {
headers['X-Api-Key'] = this.POKEMONTCG_API_KEY;
}
const config: axios.AxiosRequestConfig = {
headers,
};
if (typeof params === 'string') {
if (
params.toLowerCase().includes('page') ||
params.toLowerCase().includes('order')
)
url += `?${params}`;
else url += `/${params}`;
} else if (params) url += `?q=${this.paramsToQuery(params)}`;
return axios.default
.get<T>(url, config)
.then((response) => {
return response.data[Object.keys(response.data)[0]];
})
.catch((error) => Promise.reject(error));
}
private paramsToQuery(params: Query[]): string {
let query = '';
const paramsLength: number = params.length;
params.map((q: Query, i: number) => {
if (paramsLength === i + 1) {
query += `${q.name}:${encodeURIComponent(q.value.toString())}`;
} else {
query += `${q.name}:${encodeURIComponent(
q.value.toString()
)}`.concat('&');
}
});
return query;
}
}
return query;
}
}