mirror of
https://github.com/tcgdex/javascript-sdk.git
synced 2025-04-22 10:42:10 +00:00
Add new functions
Signed-off-by: Avior <florian.bouillon@delta-wings.net>
This commit is contained in:
parent
d0088d62cb
commit
3cdc149845
@ -33,10 +33,34 @@ export interface CardSingle {
|
|||||||
code: string
|
code: 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
|
||||||
|
75
tcgdex.ts
75
tcgdex.ts
@ -1,8 +1,8 @@
|
|||||||
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, SetList, SetSimple } from './interfaces/Set'
|
||||||
import { CardSingle } from './interfaces/Card'
|
import { CardSingle } from './interfaces/Card'
|
||||||
import { ExpansionSingle } from './interfaces/Expansion'
|
import { ExpansionSingle, ExpansionList } from './interfaces/Expansion'
|
||||||
|
|
||||||
export default class TCGdex {
|
export default class TCGdex {
|
||||||
public lang: Langs = "en"
|
public lang: Langs = "en"
|
||||||
@ -24,7 +24,7 @@ export default class TCGdex {
|
|||||||
public async getCard(id: string|number, set?: 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()
|
||||||
@ -36,9 +36,34 @@ export default class TCGdex {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async getCards(set?: string) {
|
||||||
|
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 {
|
||||||
|
return resp.json()
|
||||||
|
} 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 {
|
||||||
@ -54,7 +79,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()
|
||||||
@ -65,4 +90,44 @@ 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 {
|
||||||
|
return resp.json()
|
||||||
|
} catch (e) {
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (e) {
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user