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

@ -6,6 +6,7 @@ import { ISet } from '../interfaces/set';
import { IType } from '../interfaces/type';
import { IAttack } from '../interfaces/attack';
import { QueryBuilder } from '../queryBuilder';
import { IQuery } from '../interfaces/query';
export class Card implements ICard {
id: number;
@ -34,19 +35,28 @@ export class Card implements ICard {
constructor() {}
static resource(): string {
resource(): string {
return 'cards';
}
find(id: string): Card {
return QueryBuilder.find<Card>(id);
static find(id: string): Promise<Card> {
return QueryBuilder.find(this, id)
.then(response => {
return response;
});
}
all(): ICard[] {
throw new Error("Method not implemented.");
static all(): Promise<Card[]> {
return QueryBuilder.all(this)
.then(response => {
return response;
});
}
where(args: object): ICard {
throw new Error("Method not implemented.");
static where(params: IQuery[]): Promise<Card[]> {
return QueryBuilder.where(this, params)
.then(response => {
return response;
});
}
}

View File

@ -1,4 +1,6 @@
import { ISet } from '../interfaces/set';
import { QueryBuilder } from '../queryBuilder';
import { IQuery } from '../interfaces/query';
export class Set implements ISet {
code: string;
@ -14,19 +16,28 @@ export class Set implements ISet {
constructor() {}
resource(): string {
return "sets";
return 'sets';
}
find(id: number): ISet {
throw new Error("Method not implemented.");
static find(id: string): Promise<Set> {
return QueryBuilder.find(this, id)
.then(response => {
return response;
});
}
all(): ISet[] {
throw new Error("Method not implemented.");
static all(): Promise<Set[]> {
return QueryBuilder.all(this)
.then(response => {
return response;
});
}
where(args: object): ISet {
throw new Error("Method not implemented.");
static where(params: IQuery[]): Promise<Set[]> {
return QueryBuilder.where(this, params)
.then(response => {
return response;
});
}
}

View File

@ -1,14 +1,20 @@
import { ISubType } from '../interfaces/subType';
import { QueryBuilder } from '../queryBuilder';
export class SubType implements ISubType {
types: string[];
constructor() {}
resource(): string {
return 'subtypes';
}
all(): ISubType[] {
throw new Error("Method not implemented.");
static all(): Promise<SubType[]> {
return QueryBuilder.all(this)
.then(response => {
return response;
});
}
}

View File

@ -1,13 +1,19 @@
import { ISuperType } from '../interfaces/superType';
import { QueryBuilder } from '../queryBuilder';
export class SuperType implements ISuperType {
types: string[];
constructor () {}
resource(): string {
return 'supertypes';
}
all(): ISuperType[] {
throw new Error("Method not implemented.");
static all(): Promise<SuperType[]> {
return QueryBuilder.all(this)
.then(response => {
return response;
});
}
}

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('&');
});
}

5
src/index.ts Normal file
View File

@ -0,0 +1,5 @@
export { Card } from './classes/card';
export { Set } from './classes/set';
export { Type } from './classes/type';
export { SuperType } from './classes/superType';
export { SubType } from './classes/subType';

View File

@ -29,9 +29,4 @@ export interface ICard {
nationalPokedexNumber: number;
ancientTrait: string;
evolvesFrom: string;
resource(): string;
find(id: number): ICard;
all(): ICard[];
where(args: object): ICard;
}

View File

@ -1,4 +1,4 @@
export interface IQuery {
name: string;
value: string;
value: string | number;
}

View File

@ -8,9 +8,4 @@ export interface ISet {
releaseDate: string;
symbolUrl: string;
ptcgoCode: string;
resource(): string;
find(id: number): ISet;
all(): ISet[];
where(args: object): ISet;
}

View File

@ -1,4 +1,3 @@
export interface ISubType {
resource(): string;
all(): ISubType[];
types: string[];
}

View File

@ -1,4 +1,3 @@
export interface ISuperType {
resource(): string;
all(): ISuperType[];
types: string[];
}

View File

@ -1 +0,0 @@
import {}

View File

@ -8,7 +8,37 @@ import { IQuery } from './interfaces/query';
import { AxiosResponse } from 'axios';
export class QueryBuilder {
static find<T>(type: (new() => T), id: string): T {
Client.get(type.resource())
static all<T extends Card | Set | Type | SuperType | SubType>(type: (new() => T)): Promise<T[]> {
let t = new type();
let params: IQuery[] = [{
name: 'pageSize',
value: 1000
}];
return this.returnResponse(t.resource(), params);
}
static find<T extends Card | Set | Type | SuperType | SubType>(type: (new() => T), id: string): Promise<T> {
let t = new type();
let params: IQuery[] = [{
name: 'id',
value: id
}];
return this.returnResponse(t.resource(), params);
}
static where<T extends Card | Set | Type | SuperType | SubType>(type: (new() => T), params: IQuery[]): Promise<T[]> {
let t = new type();
return this.returnResponse(t.resource(), params);
}
private static returnResponse(resource: string, params: IQuery[]): Promise<any> {
return Client.get(resource, params)
.then(response => {
return response;
})
.catch(error => console.error(error));
}
}