diff --git a/src/client.ts b/src/client.ts index 840a879..61c541e 100644 --- a/src/client.ts +++ b/src/client.ts @@ -5,7 +5,7 @@ import { IQuery } from './interfaces/query'; export class Client { static apiUrl: string = `${PokemonTCG.API_URL}/v${PokemonTCG.version}`; - static get(resource: string, params?: IQuery[]): Promise { + static get(resource: string, params?: IQuery[] | string): Promise { let url: string = `${this.apiUrl}/${resource}`; let config: axios.AxiosRequestConfig = { headers: { @@ -13,30 +13,15 @@ export class Client { } }; - // 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(resource, params); + if(typeof params === 'string') url += `/${params}`; + else url += `?${this.paramsToQuery(params)}`; - return axios.default.get(`${url}?${this.paramsToQuery(params)}`, config) + return axios.default.get(url, config) .then(response => { return response.data[Object.keys(response.data)[0]]; }) } - private static checkForId(resource: string, 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 { let query: string = ''; diff --git a/src/queryBuilder.ts b/src/queryBuilder.ts index ee8f353..09b65d7 100644 --- a/src/queryBuilder.ts +++ b/src/queryBuilder.ts @@ -5,33 +5,25 @@ import { IQuery } from './interfaces/query'; import { AxiosResponse } from 'axios'; export class QueryBuilder { - static all(type: (new() => T)): Promise { - let t = new type(); - let params: IQuery[] = [{ - name: 'pageSize', - value: 1000 - }]; + static all(type: (new () => T)): Promise { + let t = new type(); + let params: IQuery[] = [{ + name: 'pageSize', + value: 1000 + }]; - return this.returnResponse(t.resource(), params); - } + return Client.get(t.resource(), params).catch(error => console.error(error)); + } - static find(type: (new() => T), id: string): Promise { - let t = new type(); - let params: IQuery[] = [{ - name: 'id', - value: id - }]; + static find(type: (new () => T), id: string): Promise { + let t = new type(); - return this.returnResponse(t.resource(), params); - } + return Client.get(t.resource(), id).catch(error => console.error(error)); + } - static where(type: (new() => T), params: IQuery[]): Promise { - let t = new type(); + static where(type: (new () => T), params: IQuery[]): Promise { + let t = new type(); - return this.returnResponse(t.resource(), params); - } - - private static returnResponse(resource: string, params: IQuery[]): Promise { - return Client.get(resource, params).catch(error => console.error(error)); - } + return Client.get(t.resource(), params).catch(error => console.error(error)); + } } \ No newline at end of file