Compare commits

..

15 Commits
1.0.8 ... 1.4.1

Author SHA1 Message Date
739fe92eae v1.4.1 2020-04-24 23:34:17 +02:00
8b8c20308f v1.4.0 2020-03-25 15:52:36 +01:00
b1dcd5ae5e Fixed warning for translations
Signed-off-by: Avior <florian.bouillon@delta-wings.net>
2020-03-25 15:50:24 +01:00
e9d6ac12e1 Added a default Language setting for global lang
Signed-off-by: Avior <florian.bouillon@delta-wings.net>
2020-03-25 15:50:08 +01:00
5ac1a18f6f Updated sdk to 1.3.0
Signed-off-by: Avior <florian.bouillon@delta-wings.net>
2020-03-18 09:54:20 +01:00
624337467b v1.3.0 2020-03-18 09:54:05 +01:00
eb96cb971c v1.2.1 2020-03-14 20:08:22 +01:00
9902888304 Fix datas not in interface
Signed-off-by: Avior <florian.bouillon@delta-wings.net>
2020-03-14 20:08:00 +01:00
2f1a293e3d v1.2.0 2020-03-14 20:00:43 +01:00
04d13bd450 Updated DB to add items
Signed-off-by: Avior <florian.bouillon@delta-wings.net>
2020-03-14 19:56:59 +01:00
a886302e05 v1.2.0-beta.1 2020-03-11 23:08:33 +01:00
75bc7249f1 Updated DB
Signed-off-by: Avior <florian.bouillon@delta-wings.net>
2020-03-11 23:05:27 +01:00
00316364fa fix error
Signed-off-by: Avior <florian.bouillon@delta-wings.net>
2020-03-11 21:59:31 +01:00
3cdc149845 Add new functions
Signed-off-by: Avior <florian.bouillon@delta-wings.net>
2020-03-11 21:57:40 +01:00
d0088d62cb Fix translation not getting the correct file
Signed-off-by: Avior <florian.bouillon@delta-wings.net>
2020-03-11 11:45:20 +01:00
7 changed files with 190 additions and 50 deletions

51
Request.ts Normal file
View 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')
}
}
}

View File

@ -5,6 +5,12 @@ import Rarity from "./interfaces/Rarity";
import Tag from "./interfaces/Tag";
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"
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: "type",a: Type, lang: Langs): string|undefined;
public static translate(master: possibilities,a: number, lang: Langs): string|undefined {
const trans = require(`./${master}`).default as translations
const tmp = trans[lang]
let langlist: LangList<Array<string>>|undefined
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
return tmp[a]
}

View File

@ -11,6 +11,7 @@ import Set from "./Set";
export interface CardSimple {
id: string
localId: string|number
name: string
image: string
}
@ -32,16 +33,19 @@ export interface CardSingle {
name: string
code: string
}
cardTypes?: {
normal: boolean
reverse: boolean
holo: boolean
firstEd: boolean
/**
* Some Pokémons have item like a berry
*/
item?: {
name: string
effect: string
}
// Pokémon only
hp?: number
dexId?: number
lvl?: number
type?: Array<TypeSimple>
evolveFrom?: string
evolveTo?: Array<string>
@ -95,6 +99,11 @@ type Card = {
// If card is trainer or energy effect is here
effect?: LangList<string>
item?: {
name: LangList<string>
effect: LangList<string>
}
weaknesses?: Array<{
type: Type
value?: string

View File

@ -36,6 +36,8 @@ export type SetSingle = {
export type SetSimple = {
code: string
name: string
logo?: string
symbol?: string
total: number
}

View File

@ -1,8 +1,19 @@
import { List } from "./General"
import { CardSimple } from "./Card"
/**
* Anum of "Tags" each card can contains
*
* @enum {number}
*/
enum Tag {
/**
* basic pokémon
*/
BASIC,
/**
* Basic Energy
*/
BASICENERGY,
BREAK,
EX,
@ -16,14 +27,44 @@ enum Tag {
SP,
SPECIAL,
STADIUM,
/**
* Stage 1 pokémon
*/
STAGE1,
/**
* Stage 2 Pokémon
*/
STAGE2,
SUPPORTER,
TAGTEAM,
TECHNICALMACHINE,
TOOL,
/**
* V Pokémon
*/
V,
/**
* VMAX Pokémon
*/
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

View File

@ -1,8 +1,8 @@
{
"name": "@tcgdex/sdk",
"version": "1.0.8",
"version": "1.4.1",
"main": "./tcgdex.js",
"types": "./types/tcgdex.d.ts",
"types": "./tcgdex.d.ts",
"repository": "https://git.delta-wings.net/tcgdex/javascript-sdk.git",
"license": "MIT",
"devDependencies": {

View File

@ -1,18 +1,24 @@
import fetch from 'isomorphic-unfetch'
import { Langs } from './interfaces/Langs'
import { SetSingle, SetRequest } from './interfaces/Set'
import { CardSingle } from './interfaces/Card'
import { ExpansionSingle } from './interfaces/Expansion'
import { SetSingle, SetSimple, SetList } from './interfaces/Set'
import { CardSingle, CardList, CardSimple } from './interfaces/Card'
import { ExpansionSingle, ExpansionList } from './interfaces/Expansion'
import RequestWrapper from './Request'
export default class TCGdex {
public lang: Langs = "en"
public static defaultLang: Langs = "en"
public lang?: Langs
public constructor(lang?: Langs) {
if (lang) this.lang = lang
}
public getLang() {
return this.lang || TCGdex.defaultLang
}
private getBaseUrl() {
return `https://api.tcgdex.net/${this.lang}`
return `https://api.tcgdex.net/${this.getLang()}`
}
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): Promise<CardSingle>;
public async getCard(id: string|number, set?: string): Promise<CardSingle> {
try {
const txt = set ? `sets/${set}` : "cards"
const resp = await fetch(`${this.gbu()}/${txt}/${id}`)
if (resp.status !== 200) throw new Error("Card not found")
try {
return await resp.json()
} catch (e) {
throw e
}
} catch (e) {
throw e
const txt = set ? `sets/${set}` : "cards"
const req = this.rwgr<CardSingle>(`${this.gbu()}/${txt}/${id}/`)
return req.get()
}
public async getCards(set?: string): Promise<Array<CardSimple>> {
if (set) {
const setSingle = await this.getSet(set)
return setSingle.list
}
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> {
try {
const resp = await fetch(`${this.gbu()}/sets/${set}`)
console.log(resp.status)
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
}
const req = this.rwgr<SetSingle>(`${this.gbu()}/sets/${set}/`)
const resp = await req.get()
return Object.assign(resp, {releaseDate: new Date(resp.releaseDate)}) as SetSingle
}
public async getExpansion(expansion: string): Promise<ExpansionSingle> {
try {
const resp = await fetch(`${this.gbu()}/expansions/${expansion}`)
if (resp.status !== 200) throw new Error("Expansion not found")
try {
return await resp.json()
} catch (e) {
throw e
}
} catch (e) {
throw e
const req = this.rwgr<ExpansionSingle>(`${this.gbu()}/expansions/${expansion}/`)
return req.get()
}
public async getExpansions(): Promise<ExpansionList> {
const req = this.rwgr<ExpansionList>(`${this.gbu()}/expansions/`)
return req.get()
}
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)
}
}