Basic Working Product

The QueryBuilder is now being used to get data for each different class i.e. Card, Set, Type, SuperType and SubType
This commit is contained in:
Bradyn Glines
2018-01-20 16:45:17 -07:00
parent 92c2dea62f
commit 94a0da5aaa
17 changed files with 158 additions and 42 deletions

View File

@ -10,14 +10,36 @@ import { ISet } from './interfaces/set';
export class Client {
static apiUrl: string = `${PokemonTCG.API_URL}/v${PokemonTCG.version}`;
static get(resource: string, params?: IQuery[]): axios.AxiosPromise<any> {
static get(resource: string, params?: IQuery[]): Promise<any> {
let url: string = `${Client.apiUrl}/${resource}`;
let config: axios.AxiosRequestConfig = {
headers: {
'Content-Type': 'application/json'
}
};
return axios.default.get<any>(`${Client.apiUrl}/${resource}?${Client.paramsToQuery(params)}`, config);
// This is needed because the /sets endpoint doesn't take
// an id as a parameter so we need to append it to the url
url += this.checkForId(params);
return axios.default.get<any>(`${this.apiUrl}/${resource}?${this.paramsToQuery(params)}`, config)
.then(response => {
return response.data[Object.keys(response.data)[0]];
})
}
private static checkForId(params?: IQuery[]): string {
let url: string = '';
if (params) {
params.map(param => {
if (param.name === 'id') {
url = `/${param.value}`;
}
});
}
return url;
}
private static paramsToQuery(params?: IQuery[]): string {
@ -25,7 +47,7 @@ export class Client {
if (params !== null) {
params.map((q: IQuery) => {
query += `${q.name}=${encodeURI(q.value)}`.concat('&');
query += `${q.name}=${encodeURI(q.value.toString())}`.concat('&');
});
}