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:
@ -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('&');
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user