Compare commits
34 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| bf7c9b0c10 | |||
| 520a2116c3 | |||
| 41617881fc | |||
| e681d3a8db | |||
| 4cb8376cd1 | |||
| 59cba1b831 | |||
| 2e34be017f | |||
| 1921973712 | |||
| a2b3fc3333 | |||
| aa0ca788ff | |||
| 648ebe40b4 | |||
| dfead83c2e | |||
| 04a1f912d3 | |||
| 8304485af8 | |||
| dbfe4c4c99 | |||
| 7b4520e557 | |||
| 2499e665c7 | |||
| 91d579261d | |||
| 9713eaa497 | |||
| 8dec64ce20 | |||
| f3778623c6 | |||
| 32e9bfb578 | |||
| 0927395f0f | |||
| c46b721652 | |||
| 5c67dcf4bf | |||
| 8293febb19 | |||
| 88010e4c74 | |||
| 4cec0fb24a | |||
| c9a4490e82 | |||
| 08be3f8fff | |||
| 6ab4da3cbf | |||
| 349342db11 | |||
| aed5a32a3e | |||
| 7ebfb80cb1 |
36
.github/workflows/sdk_test.yaml
vendored
Normal file
36
.github/workflows/sdk_test.yaml
vendored
Normal file
@ -0,0 +1,36 @@
|
||||
name: Build
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
- dev
|
||||
|
||||
jobs:
|
||||
test:
|
||||
name: Pokemon TCG TypeScript Integration Tests
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: 'Checkout'
|
||||
uses: actions/checkout@v1
|
||||
|
||||
- name: 'Setup Node'
|
||||
uses: actions/setup-node@v1
|
||||
with:
|
||||
node-version: 12
|
||||
|
||||
- name: 'Install dependencies'
|
||||
run: 'npm ci'
|
||||
|
||||
- name: 'Format SDK'
|
||||
run: 'npm run prettier'
|
||||
|
||||
- name: 'Lint SDK'
|
||||
run: 'npm run lint'
|
||||
|
||||
- name: 'Test SDK'
|
||||
run: 'npm run test'
|
||||
|
||||
- name: 'Build SDK'
|
||||
run: 'npm run build'
|
||||
157
MIGRATING.md
Normal file
157
MIGRATING.md
Normal file
@ -0,0 +1,157 @@
|
||||
# Migrating from v1 to v2
|
||||
|
||||
The functions and typings in V2 the SDK have been re-named to improve developer experience.
|
||||
The guide will provide you with the steps to help you migrate your application.
|
||||
|
||||
## Type Changes
|
||||
Interfaces are no longer prefixed with the letter `I`. **Interfaces in the TypeScript compile are used solely for type-checking purposes.**
|
||||
Originally, `ICard` and `ISet` existed to be implemented with the `Card` and `Set` classes respectively. Because each function is now an independently exported function,
|
||||
the TypeScript interfaces can be used as intended, which is to simply describe the shape of the data from the API:
|
||||
|
||||
`IQuery` was also changed to `Parameter` to complement the [new parameter syntax](https://docs.pokemontcg.io/#api_v2cards_list) in V2 of the API.
|
||||
|
||||
* `ICard` -> `Card`
|
||||
* `ISet` -> `Set`
|
||||
* `IAbility` -> `Ability`
|
||||
* `IResistance` -> `Resistance`
|
||||
* `IWeakness` -> `Weakness`
|
||||
* `IAttack` -> `Attack`
|
||||
* `IQuery` -> `Parameter`
|
||||
|
||||
## Function Changes
|
||||
The API functions are no longer abstracted in classes. Instead, each function is an individual export which still returns
|
||||
data of type `Card` or `Set` to reduce redundancy in the SDK. The function names were renamed to reveal intent as clearly as possible. This results
|
||||
in code that is self documenting, and enforces a convention where the API functionality is described
|
||||
thoroughly for developers.
|
||||
|
||||
There is no change in the function return types, nor underlying functionality. Functions will continue to return a generic promised based on the resource (i.e. `Promise<Card>`, `Promise<Set>`).
|
||||
|
||||
It is recommended to refactor the function names with the find-and-replace tool in your IDE or text editor in order for your project
|
||||
to be compatible with V2 of the SDK.
|
||||
|
||||
## Card Functions
|
||||
### `PokemonTCG.Card.all()` -> `PokemonTCG.getAllCards()`
|
||||
```typescript
|
||||
import { PokemonTCG } from 'pokemon-tcg-sdk-typescript';
|
||||
|
||||
// V1
|
||||
PokemonTCG.Card.all();
|
||||
|
||||
// V2
|
||||
PokemonTCG.getAllCards();
|
||||
```
|
||||
|
||||
### `PokemonTCG.Card.find()` -> `PokemonTCG.findCardByID()`
|
||||
```typescript
|
||||
import { PokemonTCG } from 'pokemon-tcg-sdk-typescript';
|
||||
|
||||
// V1
|
||||
PokemonTCG.Card.find('xy7-54').then((card: PokemonTCG.Card) => {
|
||||
console.log(card.name) // Gardevoir
|
||||
});
|
||||
|
||||
// V2
|
||||
PokemonTCG.findCardByID('xy7-54').then((card: PokemonTCG.Card) => {
|
||||
console.log(card.name) // Gardevoir
|
||||
});
|
||||
```
|
||||
|
||||
### `PokemonTCG.Card.where()` -> `PokemonTCG.findCardsByQueries()`
|
||||
```typescript
|
||||
import { PokemonTCG } from 'pokemon-tcg-sdk-typescript';
|
||||
|
||||
// V1
|
||||
const paramsV1: PokemonTCG.IQuery[] = [{ name: 'name', value: 'Gardevoir' }];
|
||||
|
||||
PokemonTCG.Card.where(paramsV1).then((cards: PokemonTCG.Card[]) => {
|
||||
console.log(card[0].name) // Gardevoir
|
||||
});
|
||||
|
||||
// V2
|
||||
const paramsV2: PokemonTCG.Parameter[] = { q: 'id:xy7-54' };
|
||||
|
||||
PokemonTCG.findCardsByQueries(paramsV2).then((cards: PokemonTCG.Card[]) => {
|
||||
console.log(card[0].name) // Gardevoir
|
||||
});
|
||||
```
|
||||
## Set Functions
|
||||
### `PokemonTCG.Set.all()` -> `PokemonTCG.getAllSets()`
|
||||
```typescript
|
||||
import { PokemonTCG } from 'pokemon-tcg-sdk-typescript';
|
||||
|
||||
// V1
|
||||
PokemonTCG.Set.all();
|
||||
|
||||
// V2
|
||||
PokemonTCG.getAllSets();
|
||||
```
|
||||
### `PokemonTCG.Set.find()` -> `PokemonTCG.findSetByID()`
|
||||
```typescript
|
||||
import { PokemonTCG } from 'pokemon-tcg-sdk-typescript';
|
||||
|
||||
// V1
|
||||
PokemonTCG.Set.find('base1').then((set: PokemonTCG.Set) => {
|
||||
console.log(set.name) // Base
|
||||
});
|
||||
|
||||
// V2
|
||||
PokemonTCG.findSetByID('base1').then((set: PokemonTCG.Set) => {
|
||||
console.log(set.name) // Base
|
||||
});
|
||||
```
|
||||
|
||||
### `PokemonTCG.Set.where()` -> `PokemonTCG.findSetsByQueries()`
|
||||
```typescript
|
||||
import { PokemonTCG } from 'pokemon-tcg-sdk-typescript';
|
||||
|
||||
// V1
|
||||
const paramsV1: PokemonTCG.IQuery[] = [{ name: 'name', value:'Base' }];
|
||||
|
||||
PokemonTCG.Set.where(paramsV1).then((sets: PokemonTCG.Set[]) => {
|
||||
console.log(sets[0].name) // Base
|
||||
});
|
||||
|
||||
// V2
|
||||
const paramsV2: PokemonTCG.Parameter[] = { q: 'name:Base' };
|
||||
|
||||
PokemonTCG.findSetsByQueries(paramsV2).then((sets: PokemonTCG.Set[]) => {
|
||||
console.log(sets[0].name) // Base
|
||||
});
|
||||
```
|
||||
|
||||
## Meta Functions
|
||||
|
||||
### `PokemonTCG.Meta.allTypes()` -> `PokemonTCG.getTypes()`
|
||||
```typescript
|
||||
import { PokemonTCG } from 'pokemon-tcg-sdk-typescript';
|
||||
|
||||
// V1
|
||||
PokemonTCG.Meta.getAllTypes();
|
||||
|
||||
// V2
|
||||
PokemonTCG.getTypes();
|
||||
```
|
||||
|
||||
### `PokemonTCG.Meta.allSupertypes()` -> `PokemonTCG.getSupertypes()`
|
||||
```typescript
|
||||
import { PokemonTCG } from 'pokemon-tcg-sdk-typescript';
|
||||
|
||||
// V1
|
||||
PokemonTCG.Meta.allSupertypes();
|
||||
|
||||
// V2
|
||||
PokemonTCG.getSupertypes();
|
||||
```
|
||||
|
||||
### `PokemonTCG.Meta.allSubtypes()` -> `PokemonTCG.getSubtypes()`
|
||||
```typescript
|
||||
import { PokemonTCG } from 'pokemon-tcg-sdk-typescript';
|
||||
|
||||
// V1
|
||||
PokemonTCG.Meta.allSubtypes();
|
||||
|
||||
// V2
|
||||
PokemonTCG.getSubtypes();
|
||||
```
|
||||
|
||||
|
||||
233
README.md
233
README.md
@ -1,156 +1,163 @@
|
||||
# Pokemon TCG SDK TypeScript
|
||||
# Pokémon TCG TypeScript SDK
|
||||
|
||||
This is the TypeScript SDK for the [Pokemon TCG API](https://pokemontcg.io).
|
||||
*Now supporting Version 2 of the Pokémon TCG API! Please refer to the [V1 to V2 Migration](https://docs.pokemontcg.io/#documentationmigration) section of the
|
||||
official API docs for more information.*
|
||||
|
||||
***See the [migration guide](MIGRATING.md) for steps to update your app to use the latest supported version of the SDK***
|
||||
|
||||
[](https://discord.gg/dpsTCvg)
|
||||

|
||||
|
||||
This is the TypeScript SDK for the [Pokémon Trading Card Game API](https://docs.pokemontcg.io).
|
||||
|
||||
## Installation
|
||||
|
||||
**npm**
|
||||
|
||||
npm install --save pokemon-tcg-sdk-typescript
|
||||
npm install pokemon-tcg-sdk-typescript
|
||||
|
||||
**yarn**
|
||||
|
||||
yarn add pokemon-tcg-sdk-typescript
|
||||
|
||||
## Class Definitions
|
||||
|
||||
### Card
|
||||
|
||||
# Configuration
|
||||
The SDK works out of the box! Simply import the SDK, and you're ready to go:
|
||||
```typescript
|
||||
ability: IAbility;
|
||||
ancientTrait?: IAncientTrait;
|
||||
artist: string;
|
||||
attacks: IAttack[];
|
||||
convertedRetreatCost: number;
|
||||
evolvesFrom: string;
|
||||
hp: string;
|
||||
id: string;
|
||||
imageUrl: string;
|
||||
imageUrlHiRes: string;
|
||||
name: string;
|
||||
nationalPokedexNumber: number;
|
||||
number: string;
|
||||
rarity: string;
|
||||
resistances: IResistance[];
|
||||
retreatCost: string[];
|
||||
series: string;
|
||||
set: string;
|
||||
setCode: string;
|
||||
subtype: string;
|
||||
supertype: string;
|
||||
text: string[];
|
||||
types: string[];
|
||||
weaknesses: IWeakness[];
|
||||
import { PokemonTCG } from 'pokemon-tcg-sdk-typescript';
|
||||
|
||||
PokemonTCG.findCardByID('xy7-54').then((card: PokemonTCG.Card) => {
|
||||
console.log(card.name) // Gardevoir
|
||||
})
|
||||
```
|
||||
|
||||
### IAbility
|
||||
It is recommended to use an API key for version 2 of the API. By default, requests are limited to 20,000/day. Requests are rate limited to 1000 requests a day, and a maximum of 30 per minute.
|
||||
|
||||
To use the SDK with an API key, create an account at https://dev.pokemontcg.io to grab an API key.
|
||||
Then set your API key to the environment variable `POKEMONTCG_API_KEY` in a `.env` file.
|
||||
**Make sure to use this exact environment variable, otherwise the SDK will not be able to read the API key.**
|
||||
|
||||
# Usage
|
||||
All function calls return generic promises like `Promise<T>` or `Promise<T[]>`
|
||||
|
||||
## Card Methods
|
||||
* [findCardByID()](#findcardbyid)
|
||||
* [findCardsByQueries()](#findcardbyqueries)
|
||||
* [getAllCards()](#getallcards)
|
||||
* [getTypes()](#gettypes)
|
||||
* [getSupertypes()](#getsupertypes)
|
||||
* [getSubtypes()](#getsubtypes)
|
||||
* [getRarities()](#getrarities)
|
||||
|
||||
## Set Methods
|
||||
* [findSetByID()](#findsetbyid)
|
||||
* [findSetsByQueries()](#findsetbyqueries)
|
||||
* [getAllSets()](#getallsets)
|
||||
|
||||
### findCardByID()
|
||||
Returns a single Pokémon card given an ID.
|
||||
```typescript
|
||||
name: string;
|
||||
text: string;
|
||||
type: string;
|
||||
import { PokemonTCG } from 'pokemon-tcg-sdk-typescript';
|
||||
|
||||
PokemonTCG.findCardByID('xy7-54').then((card: PokemonTCG.Card) => {
|
||||
console.log(card.name) // Gardevoir
|
||||
});
|
||||
```
|
||||
|
||||
### IAttack
|
||||
|
||||
### findCardByQueries()
|
||||
Returns an array of cards filtered through a search query.
|
||||
```typescript
|
||||
cost: string[];
|
||||
name: string;
|
||||
text: string;
|
||||
damage: string;
|
||||
convertedEnergyCost: string;
|
||||
import { PokemonTCG } from 'pokemon-tcg-sdk-typescript';
|
||||
|
||||
const params: PokemonTCG.Query[] = { q: 'id:xy7-54' };
|
||||
|
||||
PokemonTCG.findCardsByQueries(params).then((cards: PokemonTCG.Card[]) => {
|
||||
console.log(card[0].name) // Gardevoir
|
||||
});
|
||||
```
|
||||
### getAllCards()
|
||||
Returns all Pokémon cards available through recursive pagination.
|
||||
```typescript
|
||||
import { PokemonTCG } from 'pokemon-tcg-sdk-typescript';
|
||||
|
||||
PokemonTCG.getAllCards();
|
||||
```
|
||||
|
||||
### IResistance, IWeakness
|
||||
|
||||
### getTypes()
|
||||
Returns all Energy Types
|
||||
```typescript
|
||||
type: string;
|
||||
value: string;
|
||||
import { PokemonTCG } from 'pokemon-tcg-sdk-typescript';
|
||||
|
||||
PokemonTCG.getTypes();
|
||||
```
|
||||
|
||||
### Set
|
||||
|
||||
### getSupertypes()
|
||||
Returns all Super Types
|
||||
```typescript
|
||||
code: string;
|
||||
expandedLegal: boolean;
|
||||
logoUrl: string;
|
||||
name: string;
|
||||
ptcgoCode: string;
|
||||
releaseDate: string;
|
||||
series: string;
|
||||
standardLegal: boolean;
|
||||
symbolUrl: string;
|
||||
totalCards: number;
|
||||
updatedAt: string;
|
||||
import { PokemonTCG } from 'pokemon-tcg-sdk-typescript';
|
||||
|
||||
PokemonTCG.getSupertypes();
|
||||
```
|
||||
|
||||
### IQuery
|
||||
|
||||
### getSubtypes()
|
||||
Returns all Sub Types
|
||||
```typescript
|
||||
{ name: string, value: string | number }
|
||||
import { PokemonTCG } from 'pokemon-tcg-sdk-typescript';
|
||||
|
||||
PokemonTCG.getSubtypes();
|
||||
```
|
||||
|
||||
## Method Definitions
|
||||
|
||||
### getRarities()
|
||||
Returns all card Rarities
|
||||
```typescript
|
||||
Card.find(id: string): Promise<Card>
|
||||
Card.where(params: IQuery[]): Promise<Card[]>
|
||||
Card.all(): Promise<Card[]>
|
||||
import { PokemonTCG } from 'pokemon-tcg-sdk-typescript';
|
||||
|
||||
Set.find(id: string): Promise<Set>
|
||||
Set.where(params: IQuery[]): Promise<Set[]>
|
||||
Set.all(): Promise<Set[]>
|
||||
PokemonTCG.getRarities();
|
||||
```
|
||||
### findSetByID()
|
||||
Returns a single Pokémon card given an ID.
|
||||
```typescript
|
||||
import { PokemonTCG } from 'pokemon-tcg-sdk-typescript';
|
||||
|
||||
Meta.allTypes(): Promise<string[]>
|
||||
Meta.allSubtypes(): Promise<string[]>
|
||||
Meta.allSupertypes(): Promise<string[]>
|
||||
PokemonTCG.findSetByID('base1').then((set: PokemonTCG.Set) => {
|
||||
console.log(set.name) // Base
|
||||
});
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
All of the calls return generic promises like `Promise<T>` or `Promise<T[]>`. The type is determined from the class making the call. The examples here are using the `Card` class but the usage for the other classes are the same.
|
||||
|
||||
### findSetByQueries()
|
||||
Returns an array of cards filtered through a search query.
|
||||
```typescript
|
||||
import { PokemonTCG } from 'pokemon-tcg-sdk-typescript'
|
||||
import { PokemonTCG } from 'pokemon-tcg-sdk-typescript';
|
||||
|
||||
PokemonTCG.Card.find('xy1')
|
||||
.then(card => {
|
||||
// do stuff with the card
|
||||
})
|
||||
.catch(error => {
|
||||
// do something with the error
|
||||
});
|
||||
const params: PokemonTCG.Query[] = { q: 'name:Base' };
|
||||
|
||||
let params: PokemonTCG.IQuery[] = [{ name: 'name', value: 'Charizard' }];
|
||||
PokemonTCG.Card.where(params)
|
||||
.then(cards => {
|
||||
// do stuff with the cards
|
||||
})
|
||||
.catch(error => {
|
||||
// do something with the error
|
||||
});
|
||||
PokemonTCG.findSetsByQueries(params).then((sets: PokemonTCG.Set[]) => {
|
||||
console.log(sets[0].name) // Base
|
||||
});
|
||||
```
|
||||
### getAllSets()
|
||||
Returns all Pokémon sets available through recursive pagination.
|
||||
```typescript
|
||||
import { PokemonTCG } from 'pokemon-tcg-sdk-typescript';
|
||||
|
||||
PokemonTCG.Card.all()
|
||||
.then(cards => {
|
||||
// do stuff with the cards
|
||||
})
|
||||
.catch(error => {
|
||||
// do something with the error
|
||||
});
|
||||
PokemonTCG.getAllSets();
|
||||
```
|
||||
|
||||
## Contributing
|
||||
* Fork it (click the Fork button at the top of the page)
|
||||
* Create your feature branch (git checkout -b my-new-feature)
|
||||
* Make some changes and fix some bugs!
|
||||
* Run the tests `npm run-script test`
|
||||
* Test your changes in a project of yours:
|
||||
* Create a link with [npm](https://docs.npmjs.com/cli/link.html) or [yarn](https://yarnpkg.com/lang/en/docs/cli/link/) (depending on what tool you installed this SDK with)
|
||||
* In your project that **uses** the SDK, install the linked package with `yarn/npm link pokemon-tcg-sdk-typescript`
|
||||
* Verify the SDK behaves as expected, and your changes took effect
|
||||
* Commit your changes (git commit -am 'Add some feature')
|
||||
* Push to the branch (git push origin my-new-feature)
|
||||
* Create a new Pull Request
|
||||
Contributions are welcome! If you want to contribute, feel free to fork the repository, open and issue, then submit a pull request.
|
||||
ESLint and Prettier are used to enforce a consistent coding style.
|
||||
|
||||
### Setup
|
||||
Raring to code your heart out? Awesome! Here's how to get started:
|
||||
1. Open and issue with a bug or feature. Ensure the change is not already being worked on.
|
||||
2. Fork and clone the repository from the `master` branch.
|
||||
3. Create a feature branch.
|
||||
4. Run `npm ci` to install exact versions specified in the `package-lock.json`.
|
||||
5. Code your heart out!
|
||||
6. Run `npm run test` to run ESLint and Jest tests.
|
||||
7. (OPTIONAL) Test your changes in a project of yours:
|
||||
1. Create a link with `npm` or `yarn` (depending on what tool you installed this SDK with)
|
||||
2. In your project that uses the SDK, install the linked package with `yarn/npm link pokemon-tcg-sdk-typescript`
|
||||
3. Verify the SDK behaves as expected, and your changes took effect
|
||||
8. Submit a [pull request](https://github.com/PokemonTCG/pokemon-tcg-sdk-typescript/compare)! 🎉
|
||||
|
||||
## TODO
|
||||
* Add more testing?
|
||||
|
||||
@ -3,5 +3,5 @@ module.exports = {
|
||||
"transform": {
|
||||
"^.+\\.(ts|tsx)$": "ts-jest"
|
||||
},
|
||||
"testTimeout": 30000
|
||||
"testTimeout": 300000
|
||||
}
|
||||
2
package-lock.json
generated
2
package-lock.json
generated
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "pokemon-tcg-sdk-typescript",
|
||||
"version": "1.2.6",
|
||||
"version": "2.0.0",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
|
||||
10
package.json
10
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "pokemon-tcg-sdk-typescript",
|
||||
"version": "2.0.0",
|
||||
"version": "2.1.0-beta.1",
|
||||
"description": "Typescript SDK for the PokemonTCG API (https://pokemontcg.io)",
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
@ -11,11 +11,11 @@
|
||||
"SDK"
|
||||
],
|
||||
"scripts": {
|
||||
"format": "prettier --config .prettierrc.json 'src/**/*.ts' --write",
|
||||
"prettier": "prettier --config .prettierrc.json 'src/**/*.ts' --write",
|
||||
"lint": "eslint . --ext .ts",
|
||||
"lint-and-fix": "eslint . --ext .ts --fix",
|
||||
"test": "jest",
|
||||
"build": "tsc"
|
||||
"lint:fix": "eslint . --ext .ts --fix",
|
||||
"test": "npm run lint && jest",
|
||||
"build": "npm run lint:fix && tsc"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import * as axios from 'axios';
|
||||
import { Query } from './interfaces/query';
|
||||
import { Parameter } from './interfaces/parameter';
|
||||
|
||||
export class Client {
|
||||
private readonly POKEMONTCG_API_BASE_URL: string =
|
||||
@ -21,7 +21,7 @@ export class Client {
|
||||
return Client.instance;
|
||||
}
|
||||
|
||||
async get<T>(resource: string, params?: Query[] | string): Promise<T> {
|
||||
async get<T>(resource: string, params?: Parameter | string): Promise<T> {
|
||||
let url = `${this.POKEMONTCG_API_URL}/${resource}`;
|
||||
const headers = {
|
||||
'Content-Type': 'application/json',
|
||||
@ -36,13 +36,10 @@ export class Client {
|
||||
};
|
||||
|
||||
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)}`;
|
||||
url += `/${params}`;
|
||||
} else if (params) {
|
||||
url += `?${this.stringify(params)}`;
|
||||
}
|
||||
|
||||
return axios.default
|
||||
.get<T>(url, config)
|
||||
@ -52,20 +49,16 @@ export class Client {
|
||||
.catch((error) => Promise.reject(error));
|
||||
}
|
||||
|
||||
private paramsToQuery(params: Query[]): string {
|
||||
let query = '';
|
||||
const paramsLength: number = params.length;
|
||||
private stringify(params: Parameter): string {
|
||||
const queryString = Object.keys(params)
|
||||
.map(
|
||||
(key: string) =>
|
||||
`${encodeURIComponent(key)}=${encodeURIComponent(
|
||||
params[key]
|
||||
)}`
|
||||
)
|
||||
.join('&');
|
||||
|
||||
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;
|
||||
return queryString;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +0,0 @@
|
||||
export enum Parameter {
|
||||
Query = 'q',
|
||||
Page = 'page',
|
||||
PageSize = 'pageSize',
|
||||
Order = 'orderBy',
|
||||
}
|
||||
@ -22,4 +22,6 @@ export enum Subtype {
|
||||
TechnicalMachine = 'Technical Machine',
|
||||
V = 'V',
|
||||
VMax = 'VMAX',
|
||||
RapidStrike = 'Rapid Strike',
|
||||
SingleStrike = 'Single Strike'
|
||||
}
|
||||
|
||||
6
src/interfaces/parameter.ts
Normal file
6
src/interfaces/parameter.ts
Normal file
@ -0,0 +1,6 @@
|
||||
export interface Parameter {
|
||||
q?: string;
|
||||
orderBy?: string;
|
||||
page?: number;
|
||||
pageSize?: number;
|
||||
}
|
||||
@ -1,4 +0,0 @@
|
||||
export interface Query {
|
||||
name: string;
|
||||
value: string | number;
|
||||
}
|
||||
@ -2,15 +2,18 @@
|
||||
export * from './interfaces/ability';
|
||||
export * from './interfaces/attack';
|
||||
export * from './interfaces/card';
|
||||
export * from './interfaces/query';
|
||||
export * from './interfaces/parameter';
|
||||
export * from './interfaces/stats';
|
||||
export * from './interfaces/set';
|
||||
export * from './interfaces/image';
|
||||
export * from './interfaces/tcgplayer';
|
||||
export * from './interfaces/legality';
|
||||
|
||||
// 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';
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { Query } from '../interfaces/query';
|
||||
import { Parameter } from '../interfaces/parameter';
|
||||
import { Card } from '../interfaces/card';
|
||||
import { Type } from '../enums/type';
|
||||
import { Supertype } from '../enums/supertype';
|
||||
@ -6,26 +6,37 @@ import { Subtype } from '../enums/subtype';
|
||||
import { Rarity } from '../enums/rarity';
|
||||
import { Client } from '../client';
|
||||
|
||||
async function paginateAllCards(pageNumber: number, params?: Parameter): Promise<Card[]> {
|
||||
let currentPage = pageNumber;
|
||||
const client: Client = Client.getInstance();
|
||||
const response: Card[] = await client.get<Card[]>('cards', { pageSize: 250, page: currentPage, ...params });
|
||||
|
||||
if (response.length === 0) {
|
||||
return response;
|
||||
} else {
|
||||
currentPage++;
|
||||
return response.concat(await paginateAllCards(currentPage));
|
||||
}
|
||||
}
|
||||
|
||||
export async function getAllCards(params?: Parameter): Promise<Card[]> {
|
||||
const startingPage = 1;
|
||||
const response: Card[] = await paginateAllCards(startingPage, params);
|
||||
return response;
|
||||
}
|
||||
|
||||
export async function findCardByID(id: string): Promise<Card> {
|
||||
const client: Client = Client.getInstance();
|
||||
const response: Card = await client.get<Card>('cards', id);
|
||||
return response;
|
||||
}
|
||||
|
||||
export async function findCardsByQueries(params: Query[]): Promise<Card[]> {
|
||||
export async function findCardsByQueries(params: Parameter): Promise<Card[]> {
|
||||
const client: Client = Client.getInstance();
|
||||
const response: Card[] = await client.get<Card[]>('cards', params);
|
||||
return response;
|
||||
}
|
||||
|
||||
export async function getAllCards(): Promise<Card[]> {
|
||||
const param = 'pageSize:250';
|
||||
|
||||
const client: Client = Client.getInstance();
|
||||
const response: Card[] = await client.get<Card[]>('cards', param);
|
||||
return response;
|
||||
}
|
||||
|
||||
export async function getTypes(): Promise<Type[]> {
|
||||
const client: Client = Client.getInstance();
|
||||
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { Query } from '../interfaces/query';
|
||||
import { Parameter } from '../interfaces/parameter';
|
||||
import { Set } from '../interfaces/set';
|
||||
import { Client } from '../client';
|
||||
|
||||
@ -8,16 +8,16 @@ export async function findSetByID(id: string): Promise<Set> {
|
||||
return response;
|
||||
}
|
||||
|
||||
export async function findSetsByQueries(params: Query[]): Promise<Set[]> {
|
||||
export async function findSetsByQueries(params: Parameter): Promise<Set[]> {
|
||||
const client: Client = Client.getInstance();
|
||||
const response: Set[] = await client.get<Set[]>('sets', params);
|
||||
return response;
|
||||
}
|
||||
|
||||
export async function getAllSets(): Promise<Set[]> {
|
||||
const param = 'pageSize:250';
|
||||
const params: Parameter = { pageSize: 250 };
|
||||
|
||||
const client: Client = Client.getInstance();
|
||||
const response: Set[] = await client.get<Set[]>('sets', param);
|
||||
const response: Set[] = await client.get<Set[]>('sets', params);
|
||||
return response;
|
||||
}
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import { findCardByID, findCardsByQueries, getAllCards, getSupertypes, getSubtypes, getTypes, getRarities } from "../src/services/cardService";
|
||||
import { Query } from "../src/interfaces/query";
|
||||
import { findCardByID, findCardsByQueries, getSupertypes, getSubtypes, getTypes, getRarities } from "../src/services/cardService";
|
||||
import { Parameter } from "../src/interfaces/parameter";
|
||||
import { Card } from "../src/interfaces/card";
|
||||
import { Type } from '../src/enums/type';
|
||||
import { Supertype } from '../src/enums/supertype';
|
||||
@ -8,10 +8,7 @@ import { Rarity } from '../src/enums/rarity';
|
||||
|
||||
describe('Card Service', () => {
|
||||
it('should get a single card using query parameters', async () => {
|
||||
const params: Query[] = [{
|
||||
name: 'id',
|
||||
value: 'xy7-54'
|
||||
}]
|
||||
const params: Parameter = { q: 'id:xy7-54' }
|
||||
|
||||
const result: Card[] = await findCardsByQueries(params);
|
||||
expect(result[0].name).toEqual('Gardevoir');
|
||||
@ -22,9 +19,10 @@ describe('Card Service', () => {
|
||||
expect(result.name).toEqual('Gardevoir');
|
||||
})
|
||||
|
||||
it('should get a default list of cards using the cards resource with no query params', async () => {
|
||||
const results: Card[] = await getAllCards();
|
||||
expect(results).toHaveLength(250);
|
||||
it('should get a maximum of 250 cards given a page number by default', async () => {
|
||||
const totalCards = 250
|
||||
const results: Card[] = await findCardsByQueries({ page: 1});
|
||||
expect(results).toHaveLength(totalCards);
|
||||
});
|
||||
|
||||
it('should get a list of card supertypes', async () => {
|
||||
|
||||
@ -1,13 +1,10 @@
|
||||
import { findSetByID, findSetsByQueries, getAllSets } from '../src/services/setService';
|
||||
import { Query } from "../src/interfaces/query";
|
||||
import { Parameter } from "../src/interfaces/parameter";
|
||||
import { Set } from "../src/interfaces/set";
|
||||
|
||||
describe('Set Service', () => {
|
||||
it('should get a single set using query parameters', async () => {
|
||||
const params: Query[] = [{
|
||||
name: 'name',
|
||||
value: 'Base'
|
||||
}];
|
||||
const params: Parameter = { q: 'name:Base' };
|
||||
|
||||
const result: Set[] = await findSetsByQueries(params);
|
||||
expect(result[0].name).toEqual('Base');
|
||||
|
||||
Reference in New Issue
Block a user