Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve dependency #38

Open
wants to merge 2 commits into
base: v2.0.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ src
tsconfig.json
dist/coverage
*.tgz
test/
jest.config.js
.gitignore
node_modules/
9,867 changes: 5,151 additions & 4,716 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@
],
"license": "MIT",
"dependencies": {
"axios": "0.21.1"
"axios": "^1.7.7"
},
"devDependencies": {
"@types/jest": "26.0.20",
"@typescript-eslint/eslint-plugin": "4.17.0",
"@typescript-eslint/parser": "4.17.0",
"eslint": "7.22.0",
"eslint-config-prettier": "8.1.0",
"eslint-plugin-prettier": "3.3.1",
"jest": "26.6.3",
"jest": "^29.7.0",
"prettier": "2.2.1",
"ts-jest": "26.5.3",
"ts-jest": "^29.2.5",
"ts-node": "4.1.0",
"typescript": "4.1.5"
"typescript": "^4.3.0",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"eslint": "^8.0.0"
}
}
36 changes: 19 additions & 17 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
import * as axios from 'axios';
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
import { Parameter } from './interfaces/parameter';

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_URL: string;
private readonly POKEMONTCG_API_KEY?: string =
process.env.POKEMONTCG_API_KEY;

private static instance: Client;

private constructor() {}
private constructor() {
this.POKEMONTCG_API_URL = `${this.POKEMONTCG_API_BASE_URL}/v${this.POKEMONTCG_API_VERSION}`;
}

public static getInstance(): Client {
if (!Client.instance) {
Client.instance = new Client();
}

return Client.instance;
}

async get<T>(resource: string, params?: Parameter | string): Promise<T> {
let url = `${this.POKEMONTCG_API_URL}/${resource}`;
const headers = {
const headers: Record<string, string> = {
'Content-Type': 'application/json',
};

if (this.POKEMONTCG_API_KEY) {
headers['X-Api-Key'] = this.POKEMONTCG_API_KEY;
}

const config: axios.AxiosRequestConfig = {
const config: AxiosRequestConfig = {
headers,
};

Expand All @@ -41,24 +42,25 @@ export class Client {
url += `?${this.stringify(params)}`;
}

return axios.default
.get<T>(url, config)
.then((response) => {
return response.data[Object.keys(response.data)[0]];
})
.catch((error) => Promise.reject(error));
try {
const response: AxiosResponse<{ data: T }> = await axios.get(
url,
config
);
return response.data.data;
} catch (error) {
return Promise.reject(error);
}
}

private stringify(params: Parameter): string {
const queryString = Object.keys(params)
return Object.entries(params)
.map(
(key: string) =>
([key, value]) =>
`${encodeURIComponent(key)}=${encodeURIComponent(
params[key]
String(value)
)}`
)
.join('&');

return queryString;
}
}
14 changes: 13 additions & 1 deletion src/enums/rarity.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
export enum Rarity {
AceSpecRare = 'ACE SPEC Rare',
AmazingRare = 'Amazing Rare',
ClassicCollection = 'Classic Collection',
Common = 'Common',
DoubleRare = 'Double Rare',
HyperRare = 'Hyper Rare',
IllustrationRare = 'Illustration Rare',
Legend = 'LEGEND',
Promo = 'Promo',
RadiantRare = 'Radiant Rare',
Rare = 'Rare',
RareAce = 'Rare ACE',
RareBreak = 'Rare BREAK',
Expand All @@ -13,13 +19,19 @@ export enum Rarity {
RareHoloStar = 'Rare Holo Star',
RareHoloV = 'Rare Holo V',
RareHoloVMAX = 'Rare Holo VMAX',
RareHoloVSTAR = 'Rare Holo VSTAR',
RarePrime = 'Rare Prime',
RarePrimeStar = 'Rare Prism Star',
RarePrismStar = 'Rare Prism Star',
RareRainbow = 'Rare Rainbow',
RareSecret = 'Rare Secret',
RareShining = 'Rare Shining',
RareShiny = 'Rare Shiny',
RareShinyGX = 'Rare Shiny GX',
RareUltra = 'Rare Ultra',
ShinyRare = 'Shiny Rare',
ShinyUltraRare = 'Shiny Ultra Rare',
SpecialIllustrationRare = 'Special Illustration Rare',
TrainerGalleryRareHolo = 'Trainer Gallery Rare Holo',
UltraRare = 'Ultra Rare',
Uncommon = 'Uncommon',
}
22 changes: 19 additions & 3 deletions src/enums/subtype.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
export enum Subtype {
AceSpec = 'ACE SPEC',
Ancient = 'Ancient',
Break = 'BREAK',
Baby = 'Baby',
Basic = 'Basic',
EX = 'EX',
Eternamax = 'Eternamax',
FusionStrike = 'Fusion Strike',
Future = 'Future',
GX = 'GX',
GoldenrodGameCorner = 'Goldenrod Game Corner',
Item = 'Item',
Expand All @@ -11,17 +16,28 @@ export enum Subtype {
Mega = 'MEGA',
PokemonTool = 'Pokémon Tool',
PokemonToolF = 'Pokémon Tool F',
Prime = 'Prime',
PrismStar = 'Prism Star',
Radiant = 'Radiant',
RapidStrike = 'Rapid Strike',
Restored = 'Restored',
RocketsSecretMachine = "Rocket's Secret Machine",
SP = 'SP',
SingleStrike = 'Single Strike',
Special = 'Special',
Stadium = 'Stadium',
StageOne = 'Stage 1',
StageTwo = 'Stage 2',
Star = 'Star',
Supporter = 'Supporter',
TagTeam = 'TAG TEAM',
TeamPlasma = "Team Plasma",
TechnicalMachine = 'Technical Machine',
Tera = 'Tera',
UltraBeast = 'Ultra Beast',
V = 'V',
Vunion = 'V-UNION',
VMax = 'VMAX',
RapidStrike = 'Rapid Strike',
SingleStrike = 'Single Strike'
}
Vstar = 'VSTAR',
ex = 'ex',
}
2 changes: 1 addition & 1 deletion test/setService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('Set Service', () => {
const params: Parameter = { q: 'name:Base' };

const result: Set[] = await findSetsByQueries(params);
expect(result[0].name).toEqual('Base');
expect(result[0].name).toEqual('Expedition Base Set');
})

it('should get a single set using a set id', async () => {
Expand Down
8 changes: 6 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"target": "es2015",
"declaration": true,
"outDir": "./dist",
"sourceMap": true,
"experimentalDecorators": true,
"lib": ["ES6"]
"lib": ["ES2015", "DOM"],
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": [
"src/**/*.ts"
Expand Down