From 94a0da5aaaf36dca9fd873ca708eaa09074c8cd2 Mon Sep 17 00:00:00 2001 From: Bradyn Glines Date: Sat, 20 Jan 2018 16:45:17 -0700 Subject: [PATCH] Basic Working Product The QueryBuilder is now being used to get data for each different class i.e. Card, Set, Type, SuperType and SubType --- .gitignore | 3 ++- .npmignore | 2 ++ package.json | 6 ++++-- src/classes/card.ts | 24 +++++++++++++++++------- src/classes/set.ts | 25 ++++++++++++++++++------- src/classes/subType.ts | 10 ++++++++-- src/classes/superType.ts | 10 ++++++++-- src/client.ts | 28 +++++++++++++++++++++++++--- src/index.ts | 5 +++++ src/interfaces/card.ts | 5 ----- src/interfaces/query.ts | 2 +- src/interfaces/set.ts | 5 ----- src/interfaces/subType.ts | 3 +-- src/interfaces/superType.ts | 3 +-- src/playground/index.ts | 1 - src/queryBuilder.ts | 34 ++++++++++++++++++++++++++++++++-- yarn.lock | 34 ++++++++++++++++++++++++++++++++++ 17 files changed, 158 insertions(+), 42 deletions(-) create mode 100644 .npmignore create mode 100644 src/index.ts delete mode 100644 src/playground/index.ts create mode 100644 yarn.lock diff --git a/.gitignore b/.gitignore index b512c09..76add87 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -node_modules \ No newline at end of file +node_modules +dist \ No newline at end of file diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..3e00276 --- /dev/null +++ b/.npmignore @@ -0,0 +1,2 @@ +tsconfig.json +src \ No newline at end of file diff --git a/package.json b/package.json index 310e9d4..5745e02 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/src/classes/card.ts b/src/classes/card.ts index c368e5c..f4ea3e6 100644 --- a/src/classes/card.ts +++ b/src/classes/card.ts @@ -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(id); + static find(id: string): Promise { + return QueryBuilder.find(this, id) + .then(response => { + return response; + }); } - all(): ICard[] { - throw new Error("Method not implemented."); + static all(): Promise { + return QueryBuilder.all(this) + .then(response => { + return response; + }); } - where(args: object): ICard { - throw new Error("Method not implemented."); + static where(params: IQuery[]): Promise { + return QueryBuilder.where(this, params) + .then(response => { + return response; + }); } } \ No newline at end of file diff --git a/src/classes/set.ts b/src/classes/set.ts index ce76c9c..8f11fda 100644 --- a/src/classes/set.ts +++ b/src/classes/set.ts @@ -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 { + return QueryBuilder.find(this, id) + .then(response => { + return response; + }); } - all(): ISet[] { - throw new Error("Method not implemented."); + static all(): Promise { + return QueryBuilder.all(this) + .then(response => { + return response; + }); } - where(args: object): ISet { - throw new Error("Method not implemented."); + static where(params: IQuery[]): Promise { + return QueryBuilder.where(this, params) + .then(response => { + return response; + }); } } \ No newline at end of file diff --git a/src/classes/subType.ts b/src/classes/subType.ts index eb9d465..6774d6a 100644 --- a/src/classes/subType.ts +++ b/src/classes/subType.ts @@ -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 { + return QueryBuilder.all(this) + .then(response => { + return response; + }); } } \ No newline at end of file diff --git a/src/classes/superType.ts b/src/classes/superType.ts index 2c4a021..d59097d 100644 --- a/src/classes/superType.ts +++ b/src/classes/superType.ts @@ -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 { + return QueryBuilder.all(this) + .then(response => { + return response; + }); } } \ No newline at end of file diff --git a/src/client.ts b/src/client.ts index bab8b18..ac0404a 100644 --- a/src/client.ts +++ b/src/client.ts @@ -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 { + static get(resource: string, params?: IQuery[]): Promise { + let url: string = `${Client.apiUrl}/${resource}`; let config: axios.AxiosRequestConfig = { headers: { 'Content-Type': 'application/json' } }; - return axios.default.get(`${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(`${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('&'); }); } diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..de2b224 --- /dev/null +++ b/src/index.ts @@ -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'; \ No newline at end of file diff --git a/src/interfaces/card.ts b/src/interfaces/card.ts index 19b58bd..37a8e45 100644 --- a/src/interfaces/card.ts +++ b/src/interfaces/card.ts @@ -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; } \ No newline at end of file diff --git a/src/interfaces/query.ts b/src/interfaces/query.ts index a966da0..e2bd26d 100644 --- a/src/interfaces/query.ts +++ b/src/interfaces/query.ts @@ -1,4 +1,4 @@ export interface IQuery { name: string; - value: string; + value: string | number; } \ No newline at end of file diff --git a/src/interfaces/set.ts b/src/interfaces/set.ts index 3c6e57c..dd4f24e 100644 --- a/src/interfaces/set.ts +++ b/src/interfaces/set.ts @@ -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; } \ No newline at end of file diff --git a/src/interfaces/subType.ts b/src/interfaces/subType.ts index b96fac2..955696d 100644 --- a/src/interfaces/subType.ts +++ b/src/interfaces/subType.ts @@ -1,4 +1,3 @@ export interface ISubType { - resource(): string; - all(): ISubType[]; + types: string[]; } \ No newline at end of file diff --git a/src/interfaces/superType.ts b/src/interfaces/superType.ts index 5c12ae5..487f58f 100644 --- a/src/interfaces/superType.ts +++ b/src/interfaces/superType.ts @@ -1,4 +1,3 @@ export interface ISuperType { - resource(): string; - all(): ISuperType[]; + types: string[]; } \ No newline at end of file diff --git a/src/playground/index.ts b/src/playground/index.ts deleted file mode 100644 index f87827d..0000000 --- a/src/playground/index.ts +++ /dev/null @@ -1 +0,0 @@ -import {} \ No newline at end of file diff --git a/src/queryBuilder.ts b/src/queryBuilder.ts index 63e6b4c..d85ae8e 100644 --- a/src/queryBuilder.ts +++ b/src/queryBuilder.ts @@ -8,7 +8,37 @@ import { IQuery } from './interfaces/query'; import { AxiosResponse } from 'axios'; export class QueryBuilder { - static find(type: (new() => T), id: string): T { - Client.get(type.resource()) + static all(type: (new() => T)): Promise { + let t = new type(); + let params: IQuery[] = [{ + name: 'pageSize', + value: 1000 + }]; + + return this.returnResponse(t.resource(), params); + } + + static find(type: (new() => T), id: string): Promise { + let t = new type(); + let params: IQuery[] = [{ + name: 'id', + value: id + }]; + + return this.returnResponse(t.resource(), params); + } + + 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) + .then(response => { + return response; + }) + .catch(error => console.error(error)); } } \ No newline at end of file diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 0000000..2f8d0e6 --- /dev/null +++ b/yarn.lock @@ -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"