From c4fff9b37055287533f84b8659f1573d92d74c2d Mon Sep 17 00:00:00 2001 From: Avior Date: Fri, 8 Jan 2021 15:20:19 +0100 Subject: [PATCH] Let the enduser handle not found errors Signed-off-by: Avior --- Request.ts | 27 +++++++++++---------------- tcgdex.ts | 35 +++++++++++++++++++++++++---------- 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/Request.ts b/Request.ts index be58528..70bd8ed 100644 --- a/Request.ts +++ b/Request.ts @@ -23,7 +23,7 @@ export class Request { this.url = url } - public async get(): Promise { + public async get(): Promise { const now = new Date() if ( this.fetched && @@ -34,22 +34,17 @@ export class Request { } // Fetch Response - try { - const resp = await fetch(this.url, { - headers: { - "Content-Type": "text/plain" - } - }) - if (resp.status !== 200) { - throw new Error(`Error request ended with the code (${resp.status})`) + const resp = await fetch(this.url, { + headers: { + "Content-Type": "text/plain" } - const response = await resp.json() - this.response = response - this.fetched = now - return response - } catch (e) { - console.error(e) - throw new Error('An error occured') + }) + if (resp.status !== 200) { + return undefined } + const response = await resp.json() + this.response = response + this.fetched = now + return response } } diff --git a/tcgdex.ts b/tcgdex.ts index f304dc9..7d5691a 100644 --- a/tcgdex.ts +++ b/tcgdex.ts @@ -25,53 +25,68 @@ export default class TCGdex { return this.getBaseUrl() } - public async getCard(id: string|number, set: string): Promise; - public async getCard(id: string): Promise; - public async getCard(id: string|number, set?: string): Promise { + public async getCard(id: string|number, set: string): Promise + public async getCard(id: string): Promise + public async getCard(id: string|number, set?: string): Promise { const txt = set ? `sets/${set}` : "cards" const req = this.rwgr(`${this.gbu()}/${txt}/${id}/`) return req.get() } - public async getCards(set?: string): Promise> { + public async getCards(set?: string): Promise | undefined> { if (set) { const setSingle = await this.getSet(set) + if (!setSingle) { + return undefined + } 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() + if (!resp) { + return undefined + } return resp.list } - public async getSet(set: string, transformDate: false): Promise - public async getSet(set: string, transformDate?: true): Promise - public async getSet(set: string, transformDate?: boolean): Promise { + public async getSet(set: string, transformDate: false): Promise + public async getSet(set: string, transformDate?: true): Promise + public async getSet(set: string, transformDate?: boolean): Promise { const req = this.rwgr(`${this.gbu()}/sets/${set}/`) const resp = await req.get() + if (!resp) { + return undefined + } if (!transformDate) { return resp as SetSingleRaw } return Object.assign(resp, {releaseDate: new Date(resp.releaseDate)}) as SetSingle } - public async getExpansion(expansion: string): Promise { + public async getExpansion(expansion: string): Promise { const req = this.rwgr(`${this.gbu()}/expansions/${expansion}/`) return req.get() } - public async getExpansions(): Promise { + public async getExpansions(): Promise { const req = this.rwgr(`${this.gbu()}/expansions/`) return req.get() } - public async getSets(expansion?: string): Promise> { + public async getSets(expansion?: string): Promise | undefined> { if (expansion) { const expansionSingle = await this.getExpansion(expansion) + if (!expansionSingle) { + return undefined + } return expansionSingle.sets } const req = this.rwgr(`${this.gbu()}/sets/`) const list = await req.get() + if (!list) { + return undefined + } return list.list }