mirror of
https://github.com/tcgdex/javascript-sdk.git
synced 2025-07-13 10:35:11 +00:00
Compare commits
18 Commits
Author | SHA1 | Date | |
---|---|---|---|
739fe92eae | |||
8b8c20308f | |||
b1dcd5ae5e | |||
e9d6ac12e1 | |||
5ac1a18f6f | |||
624337467b | |||
eb96cb971c | |||
9902888304 | |||
2f1a293e3d | |||
04d13bd450 | |||
a886302e05 | |||
75bc7249f1 | |||
00316364fa | |||
3cdc149845 | |||
d0088d62cb | |||
405cd8542a | |||
53afc67b72 | |||
593b17b55f |
51
Request.ts
Normal file
51
Request.ts
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
import fetch from 'isomorphic-unfetch'
|
||||||
|
|
||||||
|
export default class RequestWrapper {
|
||||||
|
private static cache: Array<Request<any>> = []
|
||||||
|
|
||||||
|
public static getRequest<T>(url: string) {
|
||||||
|
let req = this.cache.find((req) => req.url === url) as Request<T>|undefined
|
||||||
|
if (!req) {
|
||||||
|
req = new Request<T>(url)
|
||||||
|
this.cache.push(req)
|
||||||
|
}
|
||||||
|
return req
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Request<T = any> {
|
||||||
|
public static ttl = 1000 * 60 * 60 // 1 hour
|
||||||
|
private response?: T
|
||||||
|
private fetched?: Date
|
||||||
|
public url: string // url is public for quick url test
|
||||||
|
|
||||||
|
public constructor(url: string) {
|
||||||
|
this.url = url
|
||||||
|
}
|
||||||
|
|
||||||
|
public async get(): Promise<T> {
|
||||||
|
const now = new Date()
|
||||||
|
if (
|
||||||
|
this.fetched &&
|
||||||
|
this.response &&
|
||||||
|
now.getTime() - this.fetched.getTime() < Request.ttl
|
||||||
|
) {
|
||||||
|
return this.response
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch Response
|
||||||
|
try {
|
||||||
|
const resp = await fetch(this.url)
|
||||||
|
if (resp.status !== 200) {
|
||||||
|
throw new Error(`Error request ended with the code (${resp.status})`)
|
||||||
|
}
|
||||||
|
const response = await resp.json()
|
||||||
|
this.response = response
|
||||||
|
this.fetched = now
|
||||||
|
return response
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e)
|
||||||
|
throw new Error('An error occured')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -5,6 +5,12 @@ import Rarity from "./interfaces/Rarity";
|
|||||||
import Tag from "./interfaces/Tag";
|
import Tag from "./interfaces/Tag";
|
||||||
import Type from "./interfaces/Type";
|
import Type from "./interfaces/Type";
|
||||||
|
|
||||||
|
import atTrans from './translations/abilityType'
|
||||||
|
import cTrans from './translations/category'
|
||||||
|
import rTrans from './translations/rarity'
|
||||||
|
import taTrans from './translations/tag'
|
||||||
|
import tyTrans from './translations/type'
|
||||||
|
|
||||||
type possibilities = "abilityType" | "category" | "rarity" | "tag" | "type"
|
type possibilities = "abilityType" | "category" | "rarity" | "tag" | "type"
|
||||||
|
|
||||||
export default class TranslationUtil {
|
export default class TranslationUtil {
|
||||||
@ -14,8 +20,31 @@ export default class TranslationUtil {
|
|||||||
public static translate(master: "tag",a: Tag, 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: "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
|
let langlist: LangList<Array<string>>|undefined
|
||||||
const tmp = trans[lang]
|
switch (master) {
|
||||||
|
case 'abilityType':
|
||||||
|
langlist = atTrans
|
||||||
|
break
|
||||||
|
case 'category':
|
||||||
|
langlist = cTrans
|
||||||
|
break
|
||||||
|
|
||||||
|
case 'rarity':
|
||||||
|
langlist = rTrans
|
||||||
|
break
|
||||||
|
|
||||||
|
case 'tag':
|
||||||
|
langlist = taTrans
|
||||||
|
break
|
||||||
|
|
||||||
|
case 'type':
|
||||||
|
langlist = tyTrans
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (!langlist) return
|
||||||
|
const tmp = langlist[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,16 +33,19 @@ export interface CardSingle {
|
|||||||
name: string
|
name: string
|
||||||
code: string
|
code: string
|
||||||
}
|
}
|
||||||
cardTypes?: {
|
|
||||||
normal: boolean
|
/**
|
||||||
reverse: boolean
|
* Some Pokémons have item like a berry
|
||||||
holo: boolean
|
*/
|
||||||
firstEd: boolean
|
item?: {
|
||||||
|
name: string
|
||||||
|
effect: string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pokémon only
|
// Pokémon only
|
||||||
hp?: number
|
hp?: number
|
||||||
dexId?: number
|
dexId?: number
|
||||||
|
lvl?: number
|
||||||
type?: Array<TypeSimple>
|
type?: Array<TypeSimple>
|
||||||
evolveFrom?: string
|
evolveFrom?: string
|
||||||
evolveTo?: Array<string>
|
evolveTo?: Array<string>
|
||||||
@ -95,6 +99,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,5 +1,19 @@
|
|||||||
|
import { List } from "./General"
|
||||||
|
import { CardSimple } from "./Card"
|
||||||
|
/**
|
||||||
|
* Anum of "Tags" each card can contains
|
||||||
|
*
|
||||||
|
* @enum {number}
|
||||||
|
*/
|
||||||
enum Tag {
|
enum Tag {
|
||||||
|
/**
|
||||||
|
* basic pokémon
|
||||||
|
*/
|
||||||
BASIC,
|
BASIC,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Basic Energy
|
||||||
|
*/
|
||||||
BASICENERGY,
|
BASICENERGY,
|
||||||
BREAK,
|
BREAK,
|
||||||
EX,
|
EX,
|
||||||
@ -13,14 +27,44 @@ enum Tag {
|
|||||||
SP,
|
SP,
|
||||||
SPECIAL,
|
SPECIAL,
|
||||||
STADIUM,
|
STADIUM,
|
||||||
|
/**
|
||||||
|
* Stage 1 pokémon
|
||||||
|
*/
|
||||||
STAGE1,
|
STAGE1,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stage 2 Pokémon
|
||||||
|
*/
|
||||||
STAGE2,
|
STAGE2,
|
||||||
SUPPORTER,
|
SUPPORTER,
|
||||||
TAGTEAM,
|
TAGTEAM,
|
||||||
TECHNICALMACHINE,
|
TECHNICALMACHINE,
|
||||||
TOOL,
|
TOOL,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* V Pokémon
|
||||||
|
*/
|
||||||
V,
|
V,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* VMAX Pokémon
|
||||||
|
*/
|
||||||
VMAX,
|
VMAX,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The card is available with the holographic picture
|
||||||
|
*/
|
||||||
|
HASHOLO,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Card can have a 1st badge
|
||||||
|
*/
|
||||||
|
HAS1ST,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Card is full art (art is not in the frame)
|
||||||
|
*/
|
||||||
|
ISFULLART,
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Tag
|
export default Tag
|
||||||
@ -29,3 +73,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,8 +1,8 @@
|
|||||||
{
|
{
|
||||||
"name": "@tcgdex/sdk",
|
"name": "@tcgdex/sdk",
|
||||||
"version": "1.0.6",
|
"version": "1.4.1",
|
||||||
"main": "./tcgdex.js",
|
"main": "./tcgdex.js",
|
||||||
"types": "./types/tcgdex.d.ts",
|
"types": "./tcgdex.d.ts",
|
||||||
"repository": "https://git.delta-wings.net/tcgdex/javascript-sdk.git",
|
"repository": "https://git.delta-wings.net/tcgdex/javascript-sdk.git",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
88
tcgdex.ts
88
tcgdex.ts
@ -1,18 +1,24 @@
|
|||||||
import fetch from 'isomorphic-unfetch'
|
|
||||||
import { Langs } from './interfaces/Langs'
|
import { Langs } from './interfaces/Langs'
|
||||||
import { SetSingle, SetRequest } from './interfaces/Set'
|
import { SetSingle, 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'
|
||||||
|
import RequestWrapper from './Request'
|
||||||
|
|
||||||
export default class TCGdex {
|
export default class TCGdex {
|
||||||
public lang: Langs = "en"
|
public static defaultLang: Langs = "en"
|
||||||
|
public lang?: Langs
|
||||||
|
|
||||||
public constructor(lang?: Langs) {
|
public constructor(lang?: Langs) {
|
||||||
if (lang) this.lang = lang
|
if (lang) this.lang = lang
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public getLang() {
|
||||||
|
return this.lang || TCGdex.defaultLang
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private getBaseUrl() {
|
private getBaseUrl() {
|
||||||
return `https://api.tcgdex.net/${this.lang}`
|
return `https://api.tcgdex.net/${this.getLang()}`
|
||||||
}
|
}
|
||||||
|
|
||||||
private gbu() {
|
private gbu() {
|
||||||
@ -22,47 +28,49 @@ export default class TCGdex {
|
|||||||
public async getCard(id: string|number, 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): Promise<CardSingle>;
|
||||||
public async getCard(id: string|number, set?: string): Promise<CardSingle> {
|
public async getCard(id: string|number, set?: string): Promise<CardSingle> {
|
||||||
try {
|
const txt = set ? `sets/${set}` : "cards"
|
||||||
const txt = set ? `sets/${set}` : "cards"
|
const req = this.rwgr<CardSingle>(`${this.gbu()}/${txt}/${id}/`)
|
||||||
const resp = await fetch(`${this.gbu()}/${txt}/${id}`)
|
return req.get()
|
||||||
if (resp.status !== 200) throw new Error("Card not found")
|
}
|
||||||
try {
|
|
||||||
return await resp.json()
|
public async getCards(set?: string): Promise<Array<CardSimple>> {
|
||||||
} catch (e) {
|
if (set) {
|
||||||
throw e
|
const setSingle = await this.getSet(set)
|
||||||
}
|
return setSingle.list
|
||||||
} catch (e) {
|
|
||||||
throw e
|
|
||||||
}
|
}
|
||||||
|
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 req = this.rwgr<CardList>(`${this.gbu()}/cards/`)
|
||||||
|
const resp = await req.get()
|
||||||
|
return resp.list
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getSet(set: string): Promise<SetSingle> {
|
public async getSet(set: string): Promise<SetSingle> {
|
||||||
try {
|
const req = this.rwgr<SetSingle>(`${this.gbu()}/sets/${set}/`)
|
||||||
const resp = await fetch(`${this.gbu()}/sets/${set}`)
|
const resp = await req.get()
|
||||||
console.log(resp.status)
|
return Object.assign(resp, {releaseDate: new Date(resp.releaseDate)}) as SetSingle
|
||||||
if (resp.status !== 200) throw new Error("Set not found")
|
|
||||||
try {
|
|
||||||
const setTmp: SetRequest = await resp.json();
|
|
||||||
return Object.assign(setTmp, {releaseDate: new Date(setTmp.releaseDate)}) as SetSingle
|
|
||||||
} catch (e) {
|
|
||||||
throw e
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
throw e
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getExpansion(expansion: string): Promise<ExpansionSingle> {
|
public async getExpansion(expansion: string): Promise<ExpansionSingle> {
|
||||||
try {
|
const req = this.rwgr<ExpansionSingle>(`${this.gbu()}/expansions/${expansion}/`)
|
||||||
const resp = await fetch(`${this.gbu()}/expansions/${expansion}`)
|
return req.get()
|
||||||
if (resp.status !== 200) throw new Error("Expansion not found")
|
}
|
||||||
try {
|
|
||||||
return await resp.json()
|
public async getExpansions(): Promise<ExpansionList> {
|
||||||
} catch (e) {
|
const req = this.rwgr<ExpansionList>(`${this.gbu()}/expansions/`)
|
||||||
throw e
|
return req.get()
|
||||||
}
|
}
|
||||||
} catch (e) {
|
|
||||||
throw e
|
public async getSets(expansion?: string): Promise<Array<SetSimple>> {
|
||||||
|
if (expansion) {
|
||||||
|
const expansionSingle = await this.getExpansion(expansion)
|
||||||
|
return expansionSingle.sets
|
||||||
}
|
}
|
||||||
|
const req = this.rwgr<SetList>(`${this.gbu()}/sets/`)
|
||||||
|
const list = await req.get()
|
||||||
|
return list.list
|
||||||
|
}
|
||||||
|
|
||||||
|
private rwgr<T>(url: string) {
|
||||||
|
return RequestWrapper.getRequest<T>(url)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user