From f1946e9171001bcce932756a8869b1e88c674c94 Mon Sep 17 00:00:00 2001 From: Avior Date: Thu, 17 Oct 2024 22:33:45 +0200 Subject: [PATCH] feat: Add random endpoints to the SDK Signed-off-by: Avior --- __tests__/basic.test.js | 10 ++++++++++ src/interfaces.d.ts | 6 +++--- src/tcgdex.ts | 33 +++++++++++++++++++++++++-------- src/utils.ts | 2 +- 4 files changed, 39 insertions(+), 12 deletions(-) diff --git a/__tests__/basic.test.js b/__tests__/basic.test.js index e755419..6c2f402 100644 --- a/__tests__/basic.test.js +++ b/__tests__/basic.test.js @@ -138,3 +138,13 @@ for (const endpoint of endpoints) { }) } + + +test(`random card/set/serie`, async () => { + const tcgdex = new TCGdex('en') + TCGdex.fetch = fetch + + expect((await tcgdex.random.card())).toBeTruthy() + expect((await tcgdex.random.set())).toBeTruthy() + expect((await tcgdex.random.serie())).toBeTruthy() +}) diff --git a/src/interfaces.d.ts b/src/interfaces.d.ts index 0cd2ac1..6cecb0d 100644 --- a/src/interfaces.d.ts +++ b/src/interfaces.d.ts @@ -316,6 +316,6 @@ export type Quality = 'low' | 'high' export type Extension = 'jpg' | 'webp' | 'png' export type Endpoints = 'cards' | 'categories' | 'dex-ids' | 'energy-types' | - 'hp' | 'illustrators' | 'rarities' | 'regulation-marks' | - 'retreats' | 'series' | 'sets' | 'stages' | 'suffixes' | - 'trainer-types' | 'types' | 'variants' + 'hp' | 'illustrators' | 'rarities' | 'regulation-marks' | + 'retreats' | 'series' | 'sets' | 'stages' | 'suffixes' | + 'trainer-types' | 'types' | 'variants' | 'random' diff --git a/src/tcgdex.ts b/src/tcgdex.ts index e207503..b8fcc47 100644 --- a/src/tcgdex.ts +++ b/src/tcgdex.ts @@ -17,6 +17,7 @@ import type { } from './interfaces' import CardModel from './models/Card' import CardResumeModel from './models/CardResume' +import Model from './models/Model' import SerieModel from './models/Serie' import SerieResume from './models/SerieResume' import SetModel from './models/Set' @@ -48,6 +49,22 @@ export default class TCGdex { */ public cacheTTL = 60 * 60 + // random card/set/serie endpoints + public readonly random = { + card: async (): Promise => { + const res = await this.fetch('random', 'card') + return Model.build(new CardModel(this), res) + }, + set: async (): Promise => { + const res = await this.fetch('random', 'set') + return Model.build(new SetModel(this), res) + }, + serie: async (): Promise => { + const res = await this.fetch('random', 'serie') + return Model.build(new SerieModel(this), res) + } + } + public readonly card = new Endpoint(this, CardModel, CardResumeModel, 'cards') public readonly set = new Endpoint(this, SetModel, SetResumeModel, 'sets') public readonly serie = new Endpoint(this, SerieModel, SerieResume, 'series') @@ -226,6 +243,13 @@ export default class TCGdex { */ public async fetch(...endpoint: ['sets', string]): Promise + /** + * Fetch a random element + * @param endpoint_0 'random' + * @param endpoint_1 {'set' | 'card' | 'serie'} the type of random element you want to get + */ + public async fetch(...endpoint: ['random', 'set' | 'card' | 'serie']): Promise + /** * Fetch every sets * @param endpoint_0 'sets' @@ -310,14 +334,7 @@ export default class TCGdex { // handle the Search Params if (searchParams) { - path += '?' - for (let idx = 0; idx < searchParams.length; idx++) { - const param = searchParams[idx] - if (idx !== 0) { - path += '&' - } - path += `${this.encode(param.key)}=${this.encode(param.value)}` - } + path += '?' + searchParams.map((it) => `${this.encode(it.key)}=${this.encode(it.value)}`).join('&') } // return with the endpoint and all the shit diff --git a/src/utils.ts b/src/utils.ts index f95b0df..4f3689d 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -16,5 +16,5 @@ export const ENDPOINTS: Array = [ 'cards', 'categories', 'dex-ids', 'energy-types', 'hp', 'illustrators', 'rarities', 'regulation-marks', 'retreats', 'series', 'sets', 'stages', 'suffixes', - 'trainer-types', 'types', 'variants' + 'trainer-types', 'types', 'variants', 'random' ] as const