diff --git a/Request.ts b/Request.ts new file mode 100644 index 0000000..484fb1d --- /dev/null +++ b/Request.ts @@ -0,0 +1,51 @@ +import fetch from 'isomorphic-unfetch' + +export default class RequestWrapper { + private static cache: Array> = [] + + public static getRequest(url: string) { + let req = this.cache.find((req) => req.url === url) as Request|undefined + if (!req) { + req = new Request(url) + this.cache.push(req) + } + return req + } +} + +export class Request { + public static ttl = 1000 * 60 * 60 // 1 hour + private response?: T + private fetched?: Date + public url: string // url is public for quick url test + + public constructor(url: string) { + this.url = url + } + + public async get(): Promise { + const now = new Date() + if ( + this.fetched && + this.response && + now.getTime() - this.fetched.getTime() < Request.ttl + ) { + return this.response + } + + // Fetch Response + try { + const resp = await fetch(this.url) + if (resp.status !== 200) { + throw new Error(`Error request ended with the code (${resp.status})`) + } + const response = await resp.json() + this.response = response + this.fetched = now + return response + } catch (e) { + console.error(e) + throw new Error('An error occured') + } + } +} diff --git a/interfaces/Card.ts b/interfaces/Card.ts index 82ec575..708589f 100644 --- a/interfaces/Card.ts +++ b/interfaces/Card.ts @@ -41,40 +41,11 @@ export interface CardSingle { name: string effect: string } - cardTypes?: { - /** - * normal card without anything special - * - * - * @type {boolean} consider `undefined` to true - */ - normal?: boolean - /** - * Card which has a holographic background - * but not the picture - * - * @type {boolean} `undefined` === `true` - */ - reverse?: boolean - /** - * Card which has a hologaphic picture - * - * @type {boolean} `undefined` === `false` - */ - holo?: boolean - /** - * Card which can have a `1st ed` icon - * - * only the base expansion should received it - * - * @type {boolean} `undefined` === `false` - */ - firstEd?: boolean - } // Pokémon only hp?: number dexId?: number + lvl?: number type?: Array evolveFrom?: string evolveTo?: Array diff --git a/interfaces/Tag.ts b/interfaces/Tag.ts index ce3830e..db4a0b2 100644 --- a/interfaces/Tag.ts +++ b/interfaces/Tag.ts @@ -1,8 +1,19 @@ import { List } from "./General" import { CardSimple } from "./Card" - +/** + * Anum of "Tags" each card can contains + * + * @enum {number} + */ enum Tag { + /** + * basic pokémon + */ BASIC, + + /** + * Basic Energy + */ BASICENERGY, BREAK, EX, @@ -16,14 +27,44 @@ enum Tag { SP, SPECIAL, STADIUM, + /** + * Stage 1 pokémon + */ STAGE1, + + /** + * Stage 2 Pokémon + */ STAGE2, SUPPORTER, TAGTEAM, TECHNICALMACHINE, TOOL, + + /** + * V Pokémon + */ V, + + /** + * VMAX Pokémon + */ VMAX, + + /** + * The card is available with the holographic picture + */ + HASHOLO, + + /** + * Card can have a 1st badge + */ + HAS1ST, + + /** + * Card is full art (art is not in the frame) + */ + ISFULLART, } export default Tag diff --git a/tcgdex.ts b/tcgdex.ts index f052d2e..9a7e0b1 100644 --- a/tcgdex.ts +++ b/tcgdex.ts @@ -1,8 +1,8 @@ -import fetch from 'isomorphic-unfetch' import { Langs } from './interfaces/Langs' -import { SetSingle, SetRequest, SetSimple, SetList } from './interfaces/Set' +import { SetSingle, SetSimple, SetList } from './interfaces/Set' import { CardSingle, CardList, CardSimple } from './interfaces/Card' import { ExpansionSingle, ExpansionList } from './interfaces/Expansion' +import RequestWrapper from './Request' export default class TCGdex { public lang: Langs = "en" @@ -22,114 +22,49 @@ export default class TCGdex { public async getCard(id: string|number, set: string): Promise; public async getCard(id: string): Promise; public async getCard(id: string|number, set?: string): Promise { - try { - const txt = set ? `sets/${set}` : "cards" - const resp = await fetch(`${this.gbu()}/${txt}/${id}/`) - if (resp.status !== 200) throw new Error("Card not found") - try { - return await resp.json() - } catch (e) { - throw e - } - } catch (e) { - throw e - } + const txt = set ? `sets/${set}` : "cards" + const req = this.rwgr(`${this.gbu()}/${txt}/${id}/`) + return req.get() } public async getCards(set?: string): Promise> { if (set) { - try { - const setSingle = await this.getSet(set) - return setSingle.list - } catch (e) { - throw e - } - } - try { - console.warn("note: while it's possible to fetch every cards at once it's not recommended as it take much more time than any other requests") - const resp = await fetch(`${this.gbu()}/cards/`) - if (resp.status !== 200) { - throw new Error("Could not fetch cards") - } - try { - const t: CardList = await resp.json() - return t.list - } catch (e) { - throw e - } - } catch (e) { - throw e + const setSingle = await this.getSet(set) + return setSingle.list } + console.warn("note: while it's possible to fetch every cards at once it's not recommended as it take much more time than any other requests") + const req = this.rwgr(`${this.gbu()}/cards/`) + const resp = await req.get() + return resp.list } public async getSet(set: string): Promise { - try { - const resp = await fetch(`${this.gbu()}/sets/${set}/`) - console.log(resp.status) - if (resp.status !== 200) throw new Error("Set not found") - try { - const setTmp: SetRequest = await resp.json(); - return Object.assign(setTmp, {releaseDate: new Date(setTmp.releaseDate)}) as SetSingle - } catch (e) { - throw e - } - } catch (e) { - throw e - } + const req = this.rwgr(`${this.gbu()}/sets/${set}/`) + const resp = await req.get() + return Object.assign(resp, {releaseDate: new Date(resp.releaseDate)}) as SetSingle } public async getExpansion(expansion: string): Promise { - try { - const resp = await fetch(`${this.gbu()}/expansions/${expansion}/`) - if (resp.status !== 200) throw new Error("Expansion not found") - try { - return await resp.json() - } catch (e) { - throw e - } - } catch (e) { - throw e - } + const req = this.rwgr(`${this.gbu()}/expansions/${expansion}/`) + return req.get() } public async getExpansions(): Promise { - try { - const resp = await fetch(`${this.gbu()}/expansions/`) - if (resp.status !== 200) throw new Error("Could not fetch expansions") - try { - return await resp.json() - } catch (e) { - throw e - } - } catch (e) { - throw e - } + const req = this.rwgr(`${this.gbu()}/expansions/`) + return req.get() } public async getSets(expansion?: string): Promise> { if (expansion) { - try { - const expansionSingle = await this.getExpansion(expansion) - return expansionSingle.sets - } catch (e) { - throw e - } - } else { - try { - const resp = await fetch(`${this.gbu()}/sets/`) - if (resp.status !== 200) { - throw new Error("Could not fetch sets") - } - try { - const sets: SetList = await resp.json() - return sets.list - } catch (e) { - throw e - } - - } catch (e) { - throw e - } + const expansionSingle = await this.getExpansion(expansion) + return expansionSingle.sets } + const req = this.rwgr(`${this.gbu()}/sets/`) + const list = await req.get() + return list.list + } + + private rwgr(url: string) { + return RequestWrapper.getRequest(url) } }