diff --git a/src/client.ts b/src/client.ts index 8999113..7e821a5 100644 --- a/src/client.ts +++ b/src/client.ts @@ -2,62 +2,70 @@ import * as axios from 'axios'; import { Query } from './interfaces/query'; export class Client { - private readonly POKEMONTCG_API_BASE_URL: string = 'https://api.pokemontcg.io'; - private readonly POKEMONTCG_API_VERSION: string = '2'; - private readonly POKEMONTCG_API_URL: string = `${this.POKEMONTCG_API_BASE_URL}/v${this.POKEMONTCG_API_VERSION}`; - private readonly POKEMONTCG_API_KEY?: string = process.env.POKEMONTCG_API_KEY; + private readonly POKEMONTCG_API_BASE_URL: string = + 'https://api.pokemontcg.io'; + private readonly POKEMONTCG_API_VERSION: string = '2'; + private readonly POKEMONTCG_API_URL: string = `${this.POKEMONTCG_API_BASE_URL}/v${this.POKEMONTCG_API_VERSION}`; + private readonly POKEMONTCG_API_KEY?: string = + process.env.POKEMONTCG_API_KEY; - private static instance: Client; + private static instance: Client; - private constructor() {} + private constructor() {} - public static getInstance(): Client { - if (!Client.instance) { - Client.instance = new Client(); - } + public static getInstance(): Client { + if (!Client.instance) { + Client.instance = new Client(); + } - return Client.instance; - } - - async get(resource: string, params?: Query[] | string): Promise { - let url: string = `${this.POKEMONTCG_API_URL}/${resource}`; - let headers = { - 'Content-Type': 'application/json' + return Client.instance; } - if (this.POKEMONTCG_API_KEY) { - headers['X-Api-Key'] = this.POKEMONTCG_API_KEY; + async get(resource: string, params?: Query[] | string): Promise { + let url = `${this.POKEMONTCG_API_URL}/${resource}`; + const headers = { + 'Content-Type': 'application/json', + }; + + if (this.POKEMONTCG_API_KEY) { + headers['X-Api-Key'] = this.POKEMONTCG_API_KEY; + } + + const config: axios.AxiosRequestConfig = { + headers, + }; + + if (typeof params === 'string') { + if ( + params.toLowerCase().includes('page') || + params.toLowerCase().includes('order') + ) + url += `?${params}`; + else url += `/${params}`; + } else if (params) url += `?q=${this.paramsToQuery(params)}`; + + return axios.default + .get(url, config) + .then((response) => { + return response.data[Object.keys(response.data)[0]]; + }) + .catch((error) => Promise.reject(error)); } - const config: axios.AxiosRequestConfig = { - headers - }; + private paramsToQuery(params: Query[]): string { + let query = ''; + const paramsLength: number = params.length; - if (typeof params === 'string') { - if (params.toLowerCase().includes('page') || params.toLowerCase().includes('order')) url += `?${params}`; - else url += `/${params}`; + params.map((q: Query, i: number) => { + if (paramsLength === i + 1) { + query += `${q.name}:${encodeURIComponent(q.value.toString())}`; + } else { + query += `${q.name}:${encodeURIComponent( + q.value.toString() + )}`.concat('&'); + } + }); + + return query; } - else if (params) url += `?q=${this.paramsToQuery(params)}`; - - return axios.default.get(url, config) - .then(response => { - return response.data[Object.keys(response.data)[0]]; - }) - .catch(error => Promise.reject(error)); - } - - private paramsToQuery(params: Query[]): string { - let query: string = ''; - const paramsLength: number = params.length; - - params.map((q: Query, i: number) => { - if (paramsLength === i + 1) { - query += `${q.name}:${encodeURIComponent(q.value.toString())}`; - } else { - query += `${q.name}:${encodeURIComponent(q.value.toString())}`.concat('&'); - } - }); - - return query; - } -} \ No newline at end of file +} diff --git a/src/enums/parameter.ts b/src/enums/parameter.ts index ecbe202..83ab418 100644 --- a/src/enums/parameter.ts +++ b/src/enums/parameter.ts @@ -1,6 +1,6 @@ export enum Parameter { - Query = "q", - Page = "page", - PageSize = "pageSize", - Order = "orderBy" -} \ No newline at end of file + Query = 'q', + Page = 'page', + PageSize = 'pageSize', + Order = 'orderBy', +} diff --git a/src/enums/rarity.ts b/src/enums/rarity.ts index 19f5c7e..b26e4b3 100644 --- a/src/enums/rarity.ts +++ b/src/enums/rarity.ts @@ -1,25 +1,25 @@ export enum Rarity { - AmazingRare = "Amazing Rare", - Common = "Common", - Legend = "LEGEND", - Promo = "Promo", - Rare = "Rare", - RareAce = "Rare ACE", - RareBreak = "Rare BREAK", - RareHolo = "Rare Holo", - RareHoloEX = "Rare Holo EX", - RareHoloGX = "Rare Holo GX", - RareHoloLVX = "Rare Holo LV.X", - RareHoloStar = "Rare Holo Star", - RareHoloV = "Rare Holo V", - RareHoloVMAX = "Rare Holo VMAX", - RarePrime = "Rare Prime", - RarePrimeStar = "Rare Prism Star", - RareRainbow = "Rare Rainbow", - RareSecret = "Rare Secret", - RareShining = "Rare Shining", - RareShiny = "Rare Shiny", - RareShinyGX = "Rare Shiny GX", - RareUltra = "Rare Ultra", - Uncommon = "Uncommon", -} \ No newline at end of file + AmazingRare = 'Amazing Rare', + Common = 'Common', + Legend = 'LEGEND', + Promo = 'Promo', + Rare = 'Rare', + RareAce = 'Rare ACE', + RareBreak = 'Rare BREAK', + RareHolo = 'Rare Holo', + RareHoloEX = 'Rare Holo EX', + RareHoloGX = 'Rare Holo GX', + RareHoloLVX = 'Rare Holo LV.X', + RareHoloStar = 'Rare Holo Star', + RareHoloV = 'Rare Holo V', + RareHoloVMAX = 'Rare Holo VMAX', + RarePrime = 'Rare Prime', + RarePrimeStar = 'Rare Prism Star', + RareRainbow = 'Rare Rainbow', + RareSecret = 'Rare Secret', + RareShining = 'Rare Shining', + RareShiny = 'Rare Shiny', + RareShinyGX = 'Rare Shiny GX', + RareUltra = 'Rare Ultra', + Uncommon = 'Uncommon', +} diff --git a/src/enums/subtype.ts b/src/enums/subtype.ts index 42e4918..8f78cc4 100644 --- a/src/enums/subtype.ts +++ b/src/enums/subtype.ts @@ -1,25 +1,25 @@ export enum Subtype { - Break = "BREAK", - Baby = "Baby", - Basic = "Basic", - EX = "EX", - GX = "GX", - GoldenrodGameCorner = "Goldenrod Game Corner", - Item = "Item", - Legend = "LEGEND", - LevelUp = "Level-Up", - Mega = "MEGA", - PokemonTool = "Pokémon Tool", - PokemonToolF = "Pokémon Tool F", - Restored = "Restored", + Break = 'BREAK', + Baby = 'Baby', + Basic = 'Basic', + EX = 'EX', + GX = 'GX', + GoldenrodGameCorner = 'Goldenrod Game Corner', + Item = 'Item', + Legend = 'LEGEND', + LevelUp = 'Level-Up', + Mega = 'MEGA', + PokemonTool = 'Pokémon Tool', + PokemonToolF = 'Pokémon Tool F', + Restored = 'Restored', RocketsSecretMachine = "Rocket's Secret Machine", - Special = "Special", - Stadium = "Stadium", - StageOne = "Stage 1", - StageTwo = "Stage 2", - Supporter = "Supporter", - TagTeam = "TAG TEAM", - TechnicalMachine = "Technical Machine", - V = "V", - VMax = "VMAX" -} \ No newline at end of file + Special = 'Special', + Stadium = 'Stadium', + StageOne = 'Stage 1', + StageTwo = 'Stage 2', + Supporter = 'Supporter', + TagTeam = 'TAG TEAM', + TechnicalMachine = 'Technical Machine', + V = 'V', + VMax = 'VMAX', +} diff --git a/src/enums/supertype.ts b/src/enums/supertype.ts index 60bfbf0..e9b1d2a 100644 --- a/src/enums/supertype.ts +++ b/src/enums/supertype.ts @@ -1,5 +1,5 @@ export enum Supertype { - Energy = "Energy", - Pokemon = "Pokémon", - Trainer = "Trainer" -} \ No newline at end of file + Energy = 'Energy', + Pokemon = 'Pokémon', + Trainer = 'Trainer', +} diff --git a/src/enums/type.ts b/src/enums/type.ts index 3c730d7..4fff658 100644 --- a/src/enums/type.ts +++ b/src/enums/type.ts @@ -1,13 +1,13 @@ export enum Type { - Colorless = "Colorless", - Darkness = "Darkness", - Dragon = "Dragon", - Fairy = "Fairy", - Fighting = "Fighting", - Fire = "Fire", - Grass = "Grass", - Lightening = "Lightning", - Metal = "Metal", - Psychic = "Psychic", - Water = "Water" -} \ No newline at end of file + Colorless = 'Colorless', + Darkness = 'Darkness', + Dragon = 'Dragon', + Fairy = 'Fairy', + Fighting = 'Fighting', + Fire = 'Fire', + Grass = 'Grass', + Lightening = 'Lightning', + Metal = 'Metal', + Psychic = 'Psychic', + Water = 'Water', +} diff --git a/src/interfaces/ability.ts b/src/interfaces/ability.ts index c0372bc..d71b3d5 100644 --- a/src/interfaces/ability.ts +++ b/src/interfaces/ability.ts @@ -1,5 +1,5 @@ export interface Ability { - name: string; - text: string; - type: string; -} \ No newline at end of file + name: string; + text: string; + type: string; +} diff --git a/src/interfaces/ancientTrait.ts b/src/interfaces/ancientTrait.ts index 2114559..a122632 100644 --- a/src/interfaces/ancientTrait.ts +++ b/src/interfaces/ancientTrait.ts @@ -1,4 +1,4 @@ export interface AncientTrait { - name: string; - text: string; -} \ No newline at end of file + name: string; + text: string; +} diff --git a/src/interfaces/attack.ts b/src/interfaces/attack.ts index 2939687..47e1da3 100644 --- a/src/interfaces/attack.ts +++ b/src/interfaces/attack.ts @@ -1,7 +1,7 @@ export interface Attack { - convertedEnergyCost: number; - cost: string[]; - damage: string; - name: string; - text: string; -} \ No newline at end of file + convertedEnergyCost: number; + cost: string[]; + damage: string; + name: string; + text: string; +} diff --git a/src/interfaces/card.ts b/src/interfaces/card.ts index adb5e59..32b3353 100644 --- a/src/interfaces/card.ts +++ b/src/interfaces/card.ts @@ -13,29 +13,29 @@ import { Subtype } from '../enums/subtype'; import { Rarity } from '../enums/rarity'; export interface Card { - id: string; - name: string; - supertype: Supertype; - subtypes: Subtype[]; - hp?: string; - types?: Type[]; - evolesFrom?: string; - evolvesTo?: string[]; - rules?: string[]; - ancientTrait?: AncientTrait; - abilities?: Ability[]; - attacks?: Attack[]; - weaknesses?: Weakness[]; - resistances?: Resistance[]; - retreatCost?: string[]; - convertedRetreatCost?: number; - set: Set; - number: string; - artist?: string; - rarity: Rarity; - flavorText?: string; - nationalPokedexNumbers?: number[]; - legalities: ILegality; - images: CardImage; - tcgplayer?: TCGPlayer; -} \ No newline at end of file + id: string; + name: string; + supertype: Supertype; + subtypes: Subtype[]; + hp?: string; + types?: Type[]; + evolesFrom?: string; + evolvesTo?: string[]; + rules?: string[]; + ancientTrait?: AncientTrait; + abilities?: Ability[]; + attacks?: Attack[]; + weaknesses?: Weakness[]; + resistances?: Resistance[]; + retreatCost?: string[]; + convertedRetreatCost?: number; + set: Set; + number: string; + artist?: string; + rarity: Rarity; + flavorText?: string; + nationalPokedexNumbers?: number[]; + legalities: ILegality; + images: CardImage; + tcgplayer?: TCGPlayer; +} diff --git a/src/interfaces/image.ts b/src/interfaces/image.ts index 0de66a0..af10c5b 100644 --- a/src/interfaces/image.ts +++ b/src/interfaces/image.ts @@ -1,9 +1,9 @@ export interface SetImage { - symbol: string; - logo: string; + symbol: string; + logo: string; } export interface CardImage { - small: string; - large: string; -} \ No newline at end of file + small: string; + large: string; +} diff --git a/src/interfaces/legality.ts b/src/interfaces/legality.ts index 25a4dc4..f4f101f 100644 --- a/src/interfaces/legality.ts +++ b/src/interfaces/legality.ts @@ -1,10 +1,10 @@ export enum Legality { - Legal = 'Legal', - Banned = 'Banned', + Legal = 'Legal', + Banned = 'Banned', } export interface ILegality { - expanded?: Legality; - standard?: Legality; - unlimited?: Legality; -} \ No newline at end of file + expanded?: Legality; + standard?: Legality; + unlimited?: Legality; +} diff --git a/src/interfaces/query.ts b/src/interfaces/query.ts index deef363..3e17644 100644 --- a/src/interfaces/query.ts +++ b/src/interfaces/query.ts @@ -1,4 +1,4 @@ export interface Query { - name: string; - value: string | number; -} \ No newline at end of file + name: string; + value: string | number; +} diff --git a/src/interfaces/set.ts b/src/interfaces/set.ts index b95d1d1..67a03a1 100644 --- a/src/interfaces/set.ts +++ b/src/interfaces/set.ts @@ -1,15 +1,15 @@ -import { SetImage } from "./image"; -import { ILegality } from "./legality"; +import { SetImage } from './image'; +import { ILegality } from './legality'; export interface Set { - id: string; - name: string; - series: string; - printedTotal: number; - total: number; - legalities: ILegality; - ptcgoCode: string; - releaseDate: string; - updatedAt: string; - images: SetImage; -} \ No newline at end of file + id: string; + name: string; + series: string; + printedTotal: number; + total: number; + legalities: ILegality; + ptcgoCode: string; + releaseDate: string; + updatedAt: string; + images: SetImage; +} diff --git a/src/interfaces/stats.ts b/src/interfaces/stats.ts index 6fa9381..cbf6e41 100644 --- a/src/interfaces/stats.ts +++ b/src/interfaces/stats.ts @@ -5,4 +5,4 @@ interface Stats { export interface Resistance extends Stats {} -export interface Weakness extends Stats {} \ No newline at end of file +export interface Weakness extends Stats {} diff --git a/src/interfaces/tcgplayer.ts b/src/interfaces/tcgplayer.ts index 0bffa19..b2a3dba 100644 --- a/src/interfaces/tcgplayer.ts +++ b/src/interfaces/tcgplayer.ts @@ -1,17 +1,17 @@ export interface TCGPlayer { - url: string; - updatedAt: string; - prices: { - normal?: Price; - holofoil?: Price; - reverseHolofoil?: Price; - } + url: string; + updatedAt: string; + prices: { + normal?: Price; + holofoil?: Price; + reverseHolofoil?: Price; + }; } export interface Price { - low: number | null; - mid: number | null; - high: number | null; - market: number | null; - directLow: number | null; -} \ No newline at end of file + low: number | null; + mid: number | null; + high: number | null; + market: number | null; + directLow: number | null; +} diff --git a/src/sdk.ts b/src/sdk.ts index b3a98bb..96eaafd 100644 --- a/src/sdk.ts +++ b/src/sdk.ts @@ -5,6 +5,13 @@ export * from './interfaces/card'; export * from './interfaces/query'; export * from './interfaces/stats'; -// services +// Enums +export * from './enums/type'; +export * from './enums/supertype'; +export * from './enums/subtype'; +export * from './enums/rarity'; +export * from './enums/parameter'; + +// Services export * from './services/cardService'; -export * from './services/setService'; \ No newline at end of file +export * from './services/setService'; diff --git a/src/services/cardService.ts b/src/services/cardService.ts index f5abeab..6e70f29 100644 --- a/src/services/cardService.ts +++ b/src/services/cardService.ts @@ -1,10 +1,10 @@ -import { Query } from "../interfaces/query"; -import { Card } from "../interfaces/card"; +import { Query } from '../interfaces/query'; +import { Card } from '../interfaces/card'; import { Type } from '../enums/type'; import { Supertype } from '../enums/supertype'; import { Subtype } from '../enums/subtype'; import { Rarity } from '../enums/rarity'; -import { Client } from "../client"; +import { Client } from '../client'; export async function findCardByID(id: string): Promise { const client: Client = Client.getInstance(); @@ -19,7 +19,7 @@ export async function findCardsByQueries(params: Query[]): Promise { } export async function getAllCards(): Promise { - const param: string = 'pageSize:250' + const param = 'pageSize:250'; const client: Client = Client.getInstance(); const response: Card[] = await client.get('cards', param); @@ -52,4 +52,4 @@ export async function getRarities(): Promise { const response: Rarity[] = await client.get('rarities'); return response; -} \ No newline at end of file +} diff --git a/src/services/setService.ts b/src/services/setService.ts index c4fbfa7..1de196e 100644 --- a/src/services/setService.ts +++ b/src/services/setService.ts @@ -1,6 +1,6 @@ -import { Query } from "../interfaces/query"; -import { Set } from "../interfaces/set"; -import { Client } from "../client"; +import { Query } from '../interfaces/query'; +import { Set } from '../interfaces/set'; +import { Client } from '../client'; export async function findSetByID(id: string): Promise { const client: Client = Client.getInstance(); @@ -15,7 +15,7 @@ export async function findSetsByQueries(params: Query[]): Promise { } export async function getAllSets(): Promise { - const param: string = 'pageSize:250' + const param = 'pageSize:250'; const client: Client = Client.getInstance(); const response: Set[] = await client.get('sets', param); diff --git a/test/cardService.test.ts b/test/cardService.test.ts index c1dec81..d148372 100644 --- a/test/cardService.test.ts +++ b/test/cardService.test.ts @@ -28,28 +28,28 @@ describe('Card Service', () => { }); it('should get a list of card supertypes', async () => { - const expected: string[] = Object.values(Supertype); + const expected: Supertype[] = Object.values(Supertype); const result: Supertype[] = await getSupertypes(); expect(expected.sort()).toEqual(result.sort()); }); it('should get a list of card subtypes', async () => { - const expected: string[] = Object.values(Subtype); + const expected: Subtype[] = Object.values(Subtype); const result: Subtype[] = await getSubtypes(); expect(expected.sort()).toEqual(result.sort()); }); it('should get a list of card rarities', async () => { - const expected: string[] = Object.values(Rarity); + const expected: Rarity[] = Object.values(Rarity); const result: Rarity[] = await getRarities(); expect(expected.sort()).toEqual(result.sort()); }); it('should get a list of card types', async () => { - const expected: string[] = Object.values(Type); + const expected: Type[] = Object.values(Type); const result: Type[] = await getTypes(); expect(expected.sort()).toEqual(result.sort());