mirror of
https://github.com/tcgdex/javascript-sdk.git
synced 2025-07-13 18:45:09 +00:00
Compare commits
16 Commits
Author | SHA1 | Date | |
---|---|---|---|
eb96cb971c | |||
9902888304 | |||
2f1a293e3d | |||
04d13bd450 | |||
a886302e05 | |||
75bc7249f1 | |||
00316364fa | |||
3cdc149845 | |||
d0088d62cb | |||
405cd8542a | |||
53afc67b72 | |||
593b17b55f | |||
f1f4ef3d94 | |||
5315c9511d | |||
561ec85545 | |||
f7044c7b10 |
46
README.md
46
README.md
@ -1,8 +1,44 @@
|
|||||||
current endpoints
|
# TCGdex Javacript SDK
|
||||||
|
|
||||||
```js
|
## Install
|
||||||
console.log(await tcgdex.getCard("1", "hgss1"))
|
|
||||||
console.log(await tcgdex.getSet("hgss1"))
|
|
||||||
console.log(await tcgdex.getExpansion("hgss"))
|
|
||||||
|
|
||||||
|
### Yarn/npm
|
||||||
|
|
||||||
|
```bash
|
||||||
|
yarn add @tcgdex/sdk
|
||||||
|
```
|
||||||
|
or with npm
|
||||||
|
```bash
|
||||||
|
npm install @tcgdex/sdk
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
_Note: a complete documentation is in progress_
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
import TCGdex from '@tcgdex/sdk'
|
||||||
|
import TranslationUtil from '@tcgdex/sdk/TranslationUtil'
|
||||||
|
import Tag from '@tcgdex/sdk/interfaces/Tag'
|
||||||
|
|
||||||
|
// init the class
|
||||||
|
const tcgdex = new TCGdex("en") // Lang code (Optionnal) (See TranslationUtil.ts line 3)
|
||||||
|
|
||||||
|
// change lang
|
||||||
|
tcgdex.lang = "fr"
|
||||||
|
|
||||||
|
// get Card object wih global id
|
||||||
|
await tcgdex.getCard("base1-1")
|
||||||
|
|
||||||
|
// get Card object with local id and set
|
||||||
|
await tcgdex.getCard(1, "base1")
|
||||||
|
|
||||||
|
// get Set informations
|
||||||
|
await tcgdex.getSet("base1")
|
||||||
|
|
||||||
|
// get Expansion
|
||||||
|
await tcgdex.getExpansion("base")
|
||||||
|
|
||||||
|
// Translate information from code to the lang
|
||||||
|
TranslationUtil.translate("tag", Tag.STAGE2, "en")
|
||||||
```
|
```
|
||||||
|
@ -1,10 +1,20 @@
|
|||||||
import LangList, { Langs } from "./interfaces/LangList";
|
import LangList, { Langs } from "./interfaces/LangList";
|
||||||
|
import AbilityType from "./interfaces/AbilityType";
|
||||||
|
import Category from "./interfaces/Category";
|
||||||
|
import Rarity from "./interfaces/Rarity";
|
||||||
|
import Tag from "./interfaces/Tag";
|
||||||
|
import Type from "./interfaces/Type";
|
||||||
|
|
||||||
type possibilities = "abilityType" | "category" | "rarity" | "tag" | "type"
|
type possibilities = "abilityType" | "category" | "rarity" | "tag" | "type"
|
||||||
|
|
||||||
export default class TranslationUtil {
|
export default class TranslationUtil {
|
||||||
|
public static translate(master: "abilityType",a: AbilityType, lang: Langs): string|undefined;
|
||||||
|
public static translate(master: "category",a: Category, lang: Langs): string|undefined;
|
||||||
|
public static translate(master: "rarity",a: Rarity, lang: Langs): string|undefined;
|
||||||
|
public static translate(master: "tag",a: Tag, lang: Langs): string|undefined;
|
||||||
|
public static translate(master: "type",a: Type, lang: Langs): string|undefined;
|
||||||
public static translate(master: possibilities,a: number, lang: Langs): string|undefined {
|
public static translate(master: possibilities,a: number, lang: Langs): string|undefined {
|
||||||
const trans = require(`./${master}`).default as translations
|
const trans = require(`./translations/${master}`).default as translations
|
||||||
const tmp = trans[lang]
|
const tmp = trans[lang]
|
||||||
if (!tmp) return
|
if (!tmp) return
|
||||||
return tmp[a]
|
return tmp[a]
|
||||||
|
@ -3,6 +3,12 @@ export interface AbilityTypeSimple {
|
|||||||
name: string
|
name: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type AbilityTypeSingle = {
|
||||||
|
id: AbilityType
|
||||||
|
name: string
|
||||||
|
cards: string
|
||||||
|
}
|
||||||
|
|
||||||
enum AbilityType {
|
enum AbilityType {
|
||||||
POKEBODY,
|
POKEBODY,
|
||||||
POKEPOWER,
|
POKEPOWER,
|
||||||
|
@ -11,6 +11,7 @@ import Set from "./Set";
|
|||||||
|
|
||||||
export interface CardSimple {
|
export interface CardSimple {
|
||||||
id: string
|
id: string
|
||||||
|
localId: string|number
|
||||||
name: string
|
name: string
|
||||||
image: string
|
image: string
|
||||||
}
|
}
|
||||||
@ -32,11 +33,43 @@ export interface CardSingle {
|
|||||||
name: string
|
name: string
|
||||||
code: string
|
code: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Some Pokémons have item like a berry
|
||||||
|
*/
|
||||||
|
item?: {
|
||||||
|
name: string
|
||||||
|
effect: string
|
||||||
|
}
|
||||||
cardTypes?: {
|
cardTypes?: {
|
||||||
normal: boolean
|
/**
|
||||||
reverse: boolean
|
* normal card without anything special
|
||||||
holo: boolean
|
*
|
||||||
firstEd: boolean
|
*
|
||||||
|
* @type {boolean} consider `undefined` to true
|
||||||
|
*/
|
||||||
|
normal?: boolean
|
||||||
|
/**
|
||||||
|
* Card which has a holographic background
|
||||||
|
* but not the picture
|
||||||
|
*
|
||||||
|
* @type {boolean} `undefined` === `true`
|
||||||
|
*/
|
||||||
|
reverse?: boolean
|
||||||
|
/**
|
||||||
|
* Card which has a hologaphic picture
|
||||||
|
*
|
||||||
|
* @type {boolean} `undefined` === `false`
|
||||||
|
*/
|
||||||
|
holo?: boolean
|
||||||
|
/**
|
||||||
|
* Card which can have a `1st ed` icon
|
||||||
|
*
|
||||||
|
* only the base expansion should received it
|
||||||
|
*
|
||||||
|
* @type {boolean} `undefined` === `false`
|
||||||
|
*/
|
||||||
|
firstEd?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pokémon only
|
// Pokémon only
|
||||||
@ -95,6 +128,11 @@ type Card = {
|
|||||||
// If card is trainer or energy effect is here
|
// If card is trainer or energy effect is here
|
||||||
effect?: LangList<string>
|
effect?: LangList<string>
|
||||||
|
|
||||||
|
item?: {
|
||||||
|
name: LangList<string>
|
||||||
|
effect: LangList<string>
|
||||||
|
}
|
||||||
|
|
||||||
weaknesses?: Array<{
|
weaknesses?: Array<{
|
||||||
type: Type
|
type: Type
|
||||||
value?: string
|
value?: string
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { CardSimple } from "./Card";
|
import { CardSimple } from "./Card";
|
||||||
|
import { List } from "./General";
|
||||||
|
|
||||||
export type IllustratorSingle = {
|
export type IllustratorSingle = {
|
||||||
id: number,
|
id: number,
|
||||||
@ -11,10 +12,7 @@ export interface IllustratorSimple {
|
|||||||
name: string
|
name: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IllustratorsList {
|
export type IllustratorsList = List<IllustratorSimple>
|
||||||
count: number
|
|
||||||
list: Array<IllustratorSimple>
|
|
||||||
}
|
|
||||||
|
|
||||||
interface Illustrator {
|
interface Illustrator {
|
||||||
id: number
|
id: number
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
|
import { List } from "./General"
|
||||||
|
import { CardSimple } from "./Card"
|
||||||
|
|
||||||
export enum Rarity {
|
export enum Rarity {
|
||||||
Common,
|
Common,
|
||||||
Uncommon,
|
Uncommon,
|
||||||
@ -30,3 +33,11 @@ export interface RaritySimple {
|
|||||||
id: Rarity
|
id: Rarity
|
||||||
name: string
|
name: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type RaritySingle = {
|
||||||
|
id: Rarity
|
||||||
|
name: string
|
||||||
|
cards: Array<CardSimple>
|
||||||
|
}
|
||||||
|
|
||||||
|
export type RarityList = List<RaritySimple>
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { CardSimple } from "./Card";
|
import { CardSimple } from "./Card";
|
||||||
|
import { List } from "./General";
|
||||||
|
|
||||||
export type RetreatSimple = number
|
export type RetreatSimple = number
|
||||||
|
|
||||||
@ -7,7 +8,4 @@ export interface RetreatSingle {
|
|||||||
cards: Array<CardSimple>
|
cards: Array<CardSimple>
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface RetreatList {
|
export type RetreatList = List<RetreatSimple>
|
||||||
count: number,
|
|
||||||
list: Array<RetreatSimple>
|
|
||||||
}
|
|
||||||
|
@ -36,6 +36,8 @@ export type SetSingle = {
|
|||||||
export type SetSimple = {
|
export type SetSimple = {
|
||||||
code: string
|
code: string
|
||||||
name: string
|
name: string
|
||||||
|
logo?: string
|
||||||
|
symbol?: string
|
||||||
total: number
|
total: number
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
|
import { List } from "./General"
|
||||||
|
import { CardSimple } from "./Card"
|
||||||
|
|
||||||
enum Tag {
|
enum Tag {
|
||||||
BASIC,
|
BASIC,
|
||||||
BASICENERGY,
|
BASICENERGY,
|
||||||
@ -29,3 +32,11 @@ export interface TagSimple {
|
|||||||
id: Tag
|
id: Tag
|
||||||
name: string
|
name: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type TagSingle = {
|
||||||
|
id: Tag
|
||||||
|
name: string
|
||||||
|
cards: Array<CardSimple>
|
||||||
|
}
|
||||||
|
|
||||||
|
export type TagList = List<TagSimple>
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
|
import { List } from "./General"
|
||||||
|
import { CardSimple } from "./Card"
|
||||||
|
|
||||||
enum Type {
|
enum Type {
|
||||||
COLORLESS,
|
COLORLESS,
|
||||||
DARKNESS,
|
DARKNESS,
|
||||||
@ -17,4 +20,12 @@ export interface TypeSimple {
|
|||||||
name: string
|
name: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type TypeSingle = {
|
||||||
|
id: Type
|
||||||
|
name: string
|
||||||
|
cards: Array<CardSimple>
|
||||||
|
}
|
||||||
|
|
||||||
|
export type TypeList = List<TypeSimple>
|
||||||
|
|
||||||
export default Type
|
export default Type
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@tcgdex/sdk",
|
"name": "@tcgdex/sdk",
|
||||||
"version": "1.0.5",
|
"version": "1.2.1",
|
||||||
"main": "./tcgdex.js",
|
"main": "./tcgdex.js",
|
||||||
"types": "./types/tcgdex.d.ts",
|
"types": "./types/tcgdex.d.ts",
|
||||||
"repository": "https://git.delta-wings.net/tcgdex/javascript-sdk.git",
|
"repository": "https://git.delta-wings.net/tcgdex/javascript-sdk.git",
|
||||||
|
87
tcgdex.ts
87
tcgdex.ts
@ -1,11 +1,11 @@
|
|||||||
import fetch from 'isomorphic-unfetch'
|
import fetch from 'isomorphic-unfetch'
|
||||||
import { Langs } from './interfaces/Langs'
|
import { Langs } from './interfaces/Langs'
|
||||||
import { SetSingle, SetRequest } from './interfaces/Set'
|
import { SetSingle, SetRequest, SetSimple, SetList } from './interfaces/Set'
|
||||||
import { CardSingle } from './interfaces/Card'
|
import { CardSingle, CardList, CardSimple } from './interfaces/Card'
|
||||||
import { ExpansionSingle } from './interfaces/Expansion'
|
import { ExpansionSingle, ExpansionList } from './interfaces/Expansion'
|
||||||
|
|
||||||
export default class TCGDex {
|
export default class TCGdex {
|
||||||
private lang: Langs = "en"
|
public lang: Langs = "en"
|
||||||
|
|
||||||
public constructor(lang?: Langs) {
|
public constructor(lang?: Langs) {
|
||||||
if (lang) this.lang = lang
|
if (lang) this.lang = lang
|
||||||
@ -19,10 +19,12 @@ export default class TCGDex {
|
|||||||
return this.getBaseUrl()
|
return this.getBaseUrl()
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getCard(id: string, set?: string): Promise<CardSingle> {
|
public async getCard(id: string|number, set: string): Promise<CardSingle>;
|
||||||
|
public async getCard(id: string): Promise<CardSingle>;
|
||||||
|
public async getCard(id: string|number, set?: string): Promise<CardSingle> {
|
||||||
try {
|
try {
|
||||||
const txt = set ? `sets/${set}` : "cards"
|
const txt = set ? `sets/${set}` : "cards"
|
||||||
const resp = await fetch(`${this.gbu()}/${txt}/${id}`)
|
const resp = await fetch(`${this.gbu()}/${txt}/${id}/`)
|
||||||
if (resp.status !== 200) throw new Error("Card not found")
|
if (resp.status !== 200) throw new Error("Card not found")
|
||||||
try {
|
try {
|
||||||
return await resp.json()
|
return await resp.json()
|
||||||
@ -34,9 +36,35 @@ export default class TCGDex {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async getCards(set?: string): Promise<Array<CardSimple>> {
|
||||||
|
if (set) {
|
||||||
|
try {
|
||||||
|
const setSingle = await this.getSet(set)
|
||||||
|
return setSingle.list
|
||||||
|
} catch (e) {
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
console.warn("note: while it's possible to fetch every cards at once it's not recommended as it take much more time than any other requests")
|
||||||
|
const resp = await fetch(`${this.gbu()}/cards/`)
|
||||||
|
if (resp.status !== 200) {
|
||||||
|
throw new Error("Could not fetch cards")
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const t: CardList = await resp.json()
|
||||||
|
return t.list
|
||||||
|
} catch (e) {
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public async getSet(set: string): Promise<SetSingle> {
|
public async getSet(set: string): Promise<SetSingle> {
|
||||||
try {
|
try {
|
||||||
const resp = await fetch(`${this.gbu()}/sets/${set}`)
|
const resp = await fetch(`${this.gbu()}/sets/${set}/`)
|
||||||
console.log(resp.status)
|
console.log(resp.status)
|
||||||
if (resp.status !== 200) throw new Error("Set not found")
|
if (resp.status !== 200) throw new Error("Set not found")
|
||||||
try {
|
try {
|
||||||
@ -52,7 +80,7 @@ export default class TCGDex {
|
|||||||
|
|
||||||
public async getExpansion(expansion: string): Promise<ExpansionSingle> {
|
public async getExpansion(expansion: string): Promise<ExpansionSingle> {
|
||||||
try {
|
try {
|
||||||
const resp = await fetch(`${this.gbu()}/expansions/${expansion}`)
|
const resp = await fetch(`${this.gbu()}/expansions/${expansion}/`)
|
||||||
if (resp.status !== 200) throw new Error("Expansion not found")
|
if (resp.status !== 200) throw new Error("Expansion not found")
|
||||||
try {
|
try {
|
||||||
return await resp.json()
|
return await resp.json()
|
||||||
@ -63,4 +91,45 @@ export default class TCGDex {
|
|||||||
throw e
|
throw e
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async getExpansions(): Promise<ExpansionList> {
|
||||||
|
try {
|
||||||
|
const resp = await fetch(`${this.gbu()}/expansions/`)
|
||||||
|
if (resp.status !== 200) throw new Error("Could not fetch expansions")
|
||||||
|
try {
|
||||||
|
return await resp.json()
|
||||||
|
} catch (e) {
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getSets(expansion?: string): Promise<Array<SetSimple>> {
|
||||||
|
if (expansion) {
|
||||||
|
try {
|
||||||
|
const expansionSingle = await this.getExpansion(expansion)
|
||||||
|
return expansionSingle.sets
|
||||||
|
} catch (e) {
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
const resp = await fetch(`${this.gbu()}/sets/`)
|
||||||
|
if (resp.status !== 200) {
|
||||||
|
throw new Error("Could not fetch sets")
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const sets: SetList = await resp.json()
|
||||||
|
return sets.list
|
||||||
|
} catch (e) {
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (e) {
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user