Add formatted and linted files
This commit is contained in:
106
src/client.ts
106
src/client.ts
@ -2,62 +2,70 @@ import * as axios from 'axios';
|
||||
import { Query } 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;
|
||||
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;
|
||||
|
||||
private static instance: Client;
|
||||
private static instance: Client;
|
||||
|
||||
private constructor() {}
|
||||
private constructor() {}
|
||||
|
||||
public static getInstance(): Client {
|
||||
if (!Client.instance) {
|
||||
Client.instance = new Client();
|
||||
}
|
||||
public static getInstance(): Client {
|
||||
if (!Client.instance) {
|
||||
Client.instance = new Client();
|
||||
}
|
||||
|
||||
return Client.instance;
|
||||
}
|
||||
|
||||
async get<T>(resource: string, params?: Query[] | string): Promise<T> {
|
||||
let url: string = `${this.POKEMONTCG_API_URL}/${resource}`;
|
||||
let headers = {
|
||||
'Content-Type': 'application/json'
|
||||
return Client.instance;
|
||||
}
|
||||
|
||||
if (this.POKEMONTCG_API_KEY) {
|
||||
headers['X-Api-Key'] = this.POKEMONTCG_API_KEY;
|
||||
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));
|
||||
}
|
||||
|
||||
const config: axios.AxiosRequestConfig = {
|
||||
headers
|
||||
};
|
||||
private paramsToQuery(params: Query[]): string {
|
||||
let query = '';
|
||||
const paramsLength: number = params.length;
|
||||
|
||||
if (typeof params === 'string') {
|
||||
if (params.toLowerCase().includes('page') || params.toLowerCase().includes('order')) url += `?${params}`;
|
||||
else url += `/${params}`;
|
||||
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;
|
||||
}
|
||||
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: string = '';
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user