Add formatted and linted files
This commit is contained in:
106
src/client.ts
106
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<T>(resource: string, params?: Query[] | string): Promise<T> {
|
||||
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<T>(resource: string, params?: Query[] | string): Promise<T> {
|
||||
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<T>(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<T>(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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
export enum Parameter {
|
||||
Query = "q",
|
||||
Page = "page",
|
||||
PageSize = "pageSize",
|
||||
Order = "orderBy"
|
||||
}
|
||||
Query = 'q',
|
||||
Page = 'page',
|
||||
PageSize = 'pageSize',
|
||||
Order = 'orderBy',
|
||||
}
|
||||
|
||||
@ -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",
|
||||
}
|
||||
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',
|
||||
}
|
||||
|
||||
@ -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"
|
||||
}
|
||||
Special = 'Special',
|
||||
Stadium = 'Stadium',
|
||||
StageOne = 'Stage 1',
|
||||
StageTwo = 'Stage 2',
|
||||
Supporter = 'Supporter',
|
||||
TagTeam = 'TAG TEAM',
|
||||
TechnicalMachine = 'Technical Machine',
|
||||
V = 'V',
|
||||
VMax = 'VMAX',
|
||||
}
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
export enum Supertype {
|
||||
Energy = "Energy",
|
||||
Pokemon = "Pokémon",
|
||||
Trainer = "Trainer"
|
||||
}
|
||||
Energy = 'Energy',
|
||||
Pokemon = 'Pokémon',
|
||||
Trainer = 'Trainer',
|
||||
}
|
||||
|
||||
@ -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"
|
||||
}
|
||||
Colorless = 'Colorless',
|
||||
Darkness = 'Darkness',
|
||||
Dragon = 'Dragon',
|
||||
Fairy = 'Fairy',
|
||||
Fighting = 'Fighting',
|
||||
Fire = 'Fire',
|
||||
Grass = 'Grass',
|
||||
Lightening = 'Lightning',
|
||||
Metal = 'Metal',
|
||||
Psychic = 'Psychic',
|
||||
Water = 'Water',
|
||||
}
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
export interface Ability {
|
||||
name: string;
|
||||
text: string;
|
||||
type: string;
|
||||
}
|
||||
name: string;
|
||||
text: string;
|
||||
type: string;
|
||||
}
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
export interface AncientTrait {
|
||||
name: string;
|
||||
text: string;
|
||||
}
|
||||
name: string;
|
||||
text: string;
|
||||
}
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
export interface Attack {
|
||||
convertedEnergyCost: number;
|
||||
cost: string[];
|
||||
damage: string;
|
||||
name: string;
|
||||
text: string;
|
||||
}
|
||||
convertedEnergyCost: number;
|
||||
cost: string[];
|
||||
damage: string;
|
||||
name: string;
|
||||
text: string;
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
export interface SetImage {
|
||||
symbol: string;
|
||||
logo: string;
|
||||
symbol: string;
|
||||
logo: string;
|
||||
}
|
||||
|
||||
export interface CardImage {
|
||||
small: string;
|
||||
large: string;
|
||||
}
|
||||
small: string;
|
||||
large: string;
|
||||
}
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
export enum Legality {
|
||||
Legal = 'Legal',
|
||||
Banned = 'Banned',
|
||||
Legal = 'Legal',
|
||||
Banned = 'Banned',
|
||||
}
|
||||
|
||||
export interface ILegality {
|
||||
expanded?: Legality;
|
||||
standard?: Legality;
|
||||
unlimited?: Legality;
|
||||
}
|
||||
expanded?: Legality;
|
||||
standard?: Legality;
|
||||
unlimited?: Legality;
|
||||
}
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
export interface Query {
|
||||
name: string;
|
||||
value: string | number;
|
||||
}
|
||||
name: string;
|
||||
value: string | number;
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
id: string;
|
||||
name: string;
|
||||
series: string;
|
||||
printedTotal: number;
|
||||
total: number;
|
||||
legalities: ILegality;
|
||||
ptcgoCode: string;
|
||||
releaseDate: string;
|
||||
updatedAt: string;
|
||||
images: SetImage;
|
||||
}
|
||||
|
||||
@ -5,4 +5,4 @@ interface Stats {
|
||||
|
||||
export interface Resistance extends Stats {}
|
||||
|
||||
export interface Weakness extends Stats {}
|
||||
export interface Weakness extends Stats {}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
low: number | null;
|
||||
mid: number | null;
|
||||
high: number | null;
|
||||
market: number | null;
|
||||
directLow: number | null;
|
||||
}
|
||||
|
||||
11
src/sdk.ts
11
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';
|
||||
export * from './services/setService';
|
||||
|
||||
@ -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<Card> {
|
||||
const client: Client = Client.getInstance();
|
||||
@ -19,7 +19,7 @@ export async function findCardsByQueries(params: Query[]): Promise<Card[]> {
|
||||
}
|
||||
|
||||
export async function getAllCards(): Promise<Card[]> {
|
||||
const param: string = 'pageSize:250'
|
||||
const param = 'pageSize:250';
|
||||
|
||||
const client: Client = Client.getInstance();
|
||||
const response: Card[] = await client.get<Card[]>('cards', param);
|
||||
@ -52,4 +52,4 @@ export async function getRarities(): Promise<Rarity[]> {
|
||||
|
||||
const response: Rarity[] = await client.get<Rarity[]>('rarities');
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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<Set> {
|
||||
const client: Client = Client.getInstance();
|
||||
@ -15,7 +15,7 @@ export async function findSetsByQueries(params: Query[]): Promise<Set[]> {
|
||||
}
|
||||
|
||||
export async function getAllSets(): Promise<Set[]> {
|
||||
const param: string = 'pageSize:250'
|
||||
const param = 'pageSize:250';
|
||||
|
||||
const client: Client = Client.getInstance();
|
||||
const response: Set[] = await client.get<Set[]>('sets', param);
|
||||
|
||||
Reference in New Issue
Block a user