From 92c2dea62f8acd8123c8d891789630178e182248 Mon Sep 17 00:00:00 2001 From: Bradyn Glines Date: Thu, 18 Jan 2018 17:19:41 -0700 Subject: [PATCH] Initial commit. Classes and interfaces in place. The QueryBuilder generics are still a work in progress --- .gitignore | 1 + package-lock.json | 43 ++++++++++++++++++++++++++ package.json | 20 ++++++++++++ src/classes/ability.ts | 9 ++++++ src/classes/ancientAbility.ts | 8 +++++ src/classes/attack.ts | 11 +++++++ src/classes/card.ts | 52 ++++++++++++++++++++++++++++++++ src/classes/set.ts | 32 ++++++++++++++++++++ src/classes/subType.ts | 14 +++++++++ src/classes/superType.ts | 13 ++++++++ src/classes/type.ts | 16 ++++++++++ src/client.ts | 34 +++++++++++++++++++++ src/interfaces/ability.ts | 5 +++ src/interfaces/ancientAbility.ts | 4 +++ src/interfaces/attack.ts | 7 +++++ src/interfaces/card.ts | 37 +++++++++++++++++++++++ src/interfaces/query.ts | 4 +++ src/interfaces/set.ts | 16 ++++++++++ src/interfaces/subType.ts | 4 +++ src/interfaces/superType.ts | 4 +++ src/interfaces/type.ts | 7 +++++ src/playground/index.ts | 1 + src/queryBuilder.ts | 14 +++++++++ src/sdk.ts | 4 +++ tsconfig.json | 11 +++++++ 25 files changed, 371 insertions(+) create mode 100644 .gitignore create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 src/classes/ability.ts create mode 100644 src/classes/ancientAbility.ts create mode 100644 src/classes/attack.ts create mode 100644 src/classes/card.ts create mode 100644 src/classes/set.ts create mode 100644 src/classes/subType.ts create mode 100644 src/classes/superType.ts create mode 100644 src/classes/type.ts create mode 100644 src/client.ts create mode 100644 src/interfaces/ability.ts create mode 100644 src/interfaces/ancientAbility.ts create mode 100644 src/interfaces/attack.ts create mode 100644 src/interfaces/card.ts create mode 100644 src/interfaces/query.ts create mode 100644 src/interfaces/set.ts create mode 100644 src/interfaces/subType.ts create mode 100644 src/interfaces/superType.ts create mode 100644 src/interfaces/type.ts create mode 100644 src/playground/index.ts create mode 100644 src/queryBuilder.ts create mode 100644 src/sdk.ts create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..d57c317 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,43 @@ +{ + "name": "pokemon-tcg-sdk-typescript", + "version": "0.0.1", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "axios": { + "version": "0.17.1", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.17.1.tgz", + "integrity": "sha1-LY4+XQvb1zJ/kbyBT1xXZg+Bgk0=", + "requires": { + "follow-redirects": "1.3.0", + "is-buffer": "1.1.6" + } + }, + "debug": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", + "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", + "requires": { + "ms": "2.0.0" + } + }, + "follow-redirects": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.3.0.tgz", + "integrity": "sha1-9oSHH8EW0uMp/aVe9naH9Pq8kFw=", + "requires": { + "debug": "3.1.0" + } + }, + "is-buffer": { + "version": "1.1.6", + "resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-1.1.6.tgz", + "integrity": "sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w==" + }, + "ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..310e9d4 --- /dev/null +++ b/package.json @@ -0,0 +1,20 @@ +{ + "name": "pokemon-tcg-sdk-typescript", + "version": "0.0.1", + "description": "Typescript SDK for the PokemonTCG API (https://api.pokemontcg.io)", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [ + "PokemonTCG", + "Typescript", + "API", + "SDK" + ], + "author": "Bradyn Glines", + "license": "MIT", + "dependencies": { + "axios": "^0.17.1" + } +} diff --git a/src/classes/ability.ts b/src/classes/ability.ts new file mode 100644 index 0000000..19fecac --- /dev/null +++ b/src/classes/ability.ts @@ -0,0 +1,9 @@ +import { IAbility } from '../interfaces/ability'; + +export class Ability implements IAbility { + name: string; + text: string; + type: string; + + constructor() {} +} \ No newline at end of file diff --git a/src/classes/ancientAbility.ts b/src/classes/ancientAbility.ts new file mode 100644 index 0000000..4017e3d --- /dev/null +++ b/src/classes/ancientAbility.ts @@ -0,0 +1,8 @@ +import { IAncientAbility } from '../interfaces/ancientAbility'; + +export class AncientAbility implements IAncientAbility { + name: string; + text: string; + + constructor() {} +} \ No newline at end of file diff --git a/src/classes/attack.ts b/src/classes/attack.ts new file mode 100644 index 0000000..c37acd5 --- /dev/null +++ b/src/classes/attack.ts @@ -0,0 +1,11 @@ +import { IAttack } from '../interfaces/attack'; + +export class Attack implements IAttack { + cost: string; + name: string; + text: string; + damage: string; + convertedEnergyCost: string; + + constructor() {} +} \ No newline at end of file diff --git a/src/classes/card.ts b/src/classes/card.ts new file mode 100644 index 0000000..c368e5c --- /dev/null +++ b/src/classes/card.ts @@ -0,0 +1,52 @@ +import { ICard } from '../interfaces/card'; +import { ISubType } from '../interfaces/subType'; +import { ISuperType } from '../interfaces/superType'; +import { IAbility } from '../interfaces/ability'; +import { ISet } from '../interfaces/set'; +import { IType } from '../interfaces/type'; +import { IAttack } from '../interfaces/attack'; +import { QueryBuilder } from '../queryBuilder'; + +export class Card implements ICard { + id: number; + name: string; + imageUrl: string; + imageUrlHighRes: string; + subType: ISubType; + superType: ISuperType; + ability: IAbility; + hp: number; + number: number; + artist: string; + rarity: string; + series: string; + set: ISet; + setCode: string; + retreatCost: string; + text: string; + types: IType[]; + attacks: IAttack[]; + weaknesses: string[]; + resistances: string[]; + nationalPokedexNumber: number; + ancientTrait: string; + evolvesFrom: string; + + constructor() {} + + static resource(): string { + return 'cards'; + } + + find(id: string): Card { + return QueryBuilder.find(id); + } + + all(): ICard[] { + throw new Error("Method not implemented."); + } + + where(args: object): ICard { + throw new Error("Method not implemented."); + } +} \ No newline at end of file diff --git a/src/classes/set.ts b/src/classes/set.ts new file mode 100644 index 0000000..ce76c9c --- /dev/null +++ b/src/classes/set.ts @@ -0,0 +1,32 @@ +import { ISet } from '../interfaces/set'; + +export class Set implements ISet { + code: string; + name: string; + series: string; + totalCards: number; + standardLegal: boolean; + expandedLegal: boolean; + releaseDate: string; + symbolUrl: string; + ptcgoCode: string; + + constructor() {} + + resource(): string { + return "sets"; + } + + find(id: number): ISet { + throw new Error("Method not implemented."); + } + + all(): ISet[] { + throw new Error("Method not implemented."); + } + + where(args: object): ISet { + throw new Error("Method not implemented."); + } + +} \ No newline at end of file diff --git a/src/classes/subType.ts b/src/classes/subType.ts new file mode 100644 index 0000000..eb9d465 --- /dev/null +++ b/src/classes/subType.ts @@ -0,0 +1,14 @@ +import { ISubType } from '../interfaces/subType'; + +export class SubType implements ISubType { + constructor() {} + + resource(): string { + return 'subtypes'; + } + + all(): ISubType[] { + throw new Error("Method not implemented."); + } + +} \ No newline at end of file diff --git a/src/classes/superType.ts b/src/classes/superType.ts new file mode 100644 index 0000000..2c4a021 --- /dev/null +++ b/src/classes/superType.ts @@ -0,0 +1,13 @@ +import { ISuperType } from '../interfaces/superType'; + +export class SuperType implements ISuperType { + constructor () {} + + resource(): string { + return 'supertypes'; + } + + all(): ISuperType[] { + throw new Error("Method not implemented."); + } +} \ No newline at end of file diff --git a/src/classes/type.ts b/src/classes/type.ts new file mode 100644 index 0000000..4938713 --- /dev/null +++ b/src/classes/type.ts @@ -0,0 +1,16 @@ +import { IType } from '../interfaces/type'; + +export class Type implements IType { + type: string; + value: string; + + constructor() {} + + resource(): string { + return 'types'; + } + + all(): IType[] { + throw new Error("Method not implemented."); + } +} \ No newline at end of file diff --git a/src/client.ts b/src/client.ts new file mode 100644 index 0000000..bab8b18 --- /dev/null +++ b/src/client.ts @@ -0,0 +1,34 @@ +import * as axios from 'axios'; +import { PokemonTCG } from './sdk'; +import { IQuery } from './interfaces/query'; +import { ICard } from './interfaces/card'; +import { IType } from './interfaces/type'; +import { ISuperType } from './interfaces/superType'; +import { ISubType } from './interfaces/subType'; +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 { + let config: axios.AxiosRequestConfig = { + headers: { + 'Content-Type': 'application/json' + } + }; + + return axios.default.get(`${Client.apiUrl}/${resource}?${Client.paramsToQuery(params)}`, config); + } + + private static paramsToQuery(params?: IQuery[]): string { + let query: string = ''; + + if (params !== null) { + params.map((q: IQuery) => { + query += `${q.name}=${encodeURI(q.value)}`.concat('&'); + }); + } + + return query; + } +} \ No newline at end of file diff --git a/src/interfaces/ability.ts b/src/interfaces/ability.ts new file mode 100644 index 0000000..321ba8b --- /dev/null +++ b/src/interfaces/ability.ts @@ -0,0 +1,5 @@ +export interface IAbility { + name: string; + text: string; + type: string; +} \ No newline at end of file diff --git a/src/interfaces/ancientAbility.ts b/src/interfaces/ancientAbility.ts new file mode 100644 index 0000000..9e98422 --- /dev/null +++ b/src/interfaces/ancientAbility.ts @@ -0,0 +1,4 @@ +export interface IAncientAbility { + name: string; + text: string; +} \ No newline at end of file diff --git a/src/interfaces/attack.ts b/src/interfaces/attack.ts new file mode 100644 index 0000000..3312c6f --- /dev/null +++ b/src/interfaces/attack.ts @@ -0,0 +1,7 @@ +export interface IAttack { + cost: string; + name: string; + text: string; + damage: string; + convertedEnergyCost: string; +} \ No newline at end of file diff --git a/src/interfaces/card.ts b/src/interfaces/card.ts new file mode 100644 index 0000000..19b58bd --- /dev/null +++ b/src/interfaces/card.ts @@ -0,0 +1,37 @@ +import { IAbility } from '../interfaces/ability'; +import { IAttack } from '../interfaces/attack'; +import { ISet } from '../interfaces/set'; +import { ISubType } from '../interfaces/subType'; +import { ISuperType } from '../interfaces/superType'; +import { IType } from '../interfaces/type'; + +export interface ICard { + id: number; + name: string; + imageUrl: string; + imageUrlHighRes: string; + subType: ISubType; + superType: ISuperType; + ability: IAbility; + hp: number; + number: number; + artist: string; + rarity: string; + series: string; + set: ISet; + setCode: string; + retreatCost: string; + text: string; + types: IType[]; + attacks: IAttack[]; + weaknesses: string[]; + resistances: string[]; + 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 new file mode 100644 index 0000000..a966da0 --- /dev/null +++ b/src/interfaces/query.ts @@ -0,0 +1,4 @@ +export interface IQuery { + name: string; + value: string; +} \ No newline at end of file diff --git a/src/interfaces/set.ts b/src/interfaces/set.ts new file mode 100644 index 0000000..3c6e57c --- /dev/null +++ b/src/interfaces/set.ts @@ -0,0 +1,16 @@ +export interface ISet { + code: string; + name: string; + series: string; + totalCards: number; + standardLegal: boolean; + expandedLegal: boolean; + 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 new file mode 100644 index 0000000..b96fac2 --- /dev/null +++ b/src/interfaces/subType.ts @@ -0,0 +1,4 @@ +export interface ISubType { + resource(): string; + all(): ISubType[]; +} \ No newline at end of file diff --git a/src/interfaces/superType.ts b/src/interfaces/superType.ts new file mode 100644 index 0000000..5c12ae5 --- /dev/null +++ b/src/interfaces/superType.ts @@ -0,0 +1,4 @@ +export interface ISuperType { + resource(): string; + all(): ISuperType[]; +} \ No newline at end of file diff --git a/src/interfaces/type.ts b/src/interfaces/type.ts new file mode 100644 index 0000000..f7d012d --- /dev/null +++ b/src/interfaces/type.ts @@ -0,0 +1,7 @@ +export interface IType { + type: string; + value: string; + + resource(): string; + all(): IType[]; +} \ No newline at end of file diff --git a/src/playground/index.ts b/src/playground/index.ts new file mode 100644 index 0000000..f87827d --- /dev/null +++ b/src/playground/index.ts @@ -0,0 +1 @@ +import {} \ No newline at end of file diff --git a/src/queryBuilder.ts b/src/queryBuilder.ts new file mode 100644 index 0000000..63e6b4c --- /dev/null +++ b/src/queryBuilder.ts @@ -0,0 +1,14 @@ +import { Client } from './client'; +import { Card } from "./classes/card"; +import { Set } from "./classes/set"; +import { Type } from "./classes/type"; +import { SubType } from "./classes/subType"; +import { SuperType } from "./classes/superType"; +import { IQuery } from './interfaces/query'; +import { AxiosResponse } from 'axios'; + +export class QueryBuilder { + static find(type: (new() => T), id: string): T { + Client.get(type.resource()) + } +} \ No newline at end of file diff --git a/src/sdk.ts b/src/sdk.ts new file mode 100644 index 0000000..12159d1 --- /dev/null +++ b/src/sdk.ts @@ -0,0 +1,4 @@ +export namespace PokemonTCG { + export const API_URL: string = 'https://api.pokemontcg.io'; + export const version: string = '1'; +} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..e1b03ff --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es5", + "declaration": true, + "outDir": "./dist" + }, + "include": [ + "src/**/*" + ] +} \ No newline at end of file