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

3
.gitignore vendored
View File

@ -1 +1,2 @@
node_modules
node_modules
dist

2
.npmignore Normal file
View File

@ -0,0 +1,2 @@
tsconfig.json
src

View File

@ -2,7 +2,8 @@
"name": "pokemon-tcg-sdk-typescript",
"version": "0.0.1",
"description": "Typescript SDK for the PokemonTCG API (https://api.pokemontcg.io)",
"main": "index.js",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
@ -15,6 +16,7 @@
"author": "Bradyn Glines",
"license": "MIT",
"dependencies": {
"axios": "^0.17.1"
"axios": "^0.17.1",
"typescript": "^2.6.2"
}
}

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));
}
}

34
yarn.lock Normal file
View File

@ -0,0 +1,34 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
axios@^0.17.1:
version "0.17.1"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.17.1.tgz#2d8e3e5d0bdbd7327f91bc814f5c57660f81824d"
dependencies:
follow-redirects "^1.2.5"
is-buffer "^1.1.5"
debug@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261"
dependencies:
ms "2.0.0"
follow-redirects@^1.2.5:
version "1.3.0"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.3.0.tgz#f684871fc116d2e329fda55ef67687f4fabc905c"
dependencies:
debug "^3.1.0"
is-buffer@^1.1.5:
version "1.1.6"
resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-1.1.6.tgz#efaa2ea9daa0d7ab2ea13a97b2b8ad51fefbe8be"
ms@2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
typescript@^2.6.2:
version "2.6.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.6.2.tgz#3c5b6fd7f6de0914269027f03c0946758f7673a4"