mirror of
https://github.com/tcgdex/javascript-sdk.git
synced 2025-07-13 18:45:09 +00:00
Compare commits
11 Commits
Author | SHA1 | Date | |
---|---|---|---|
d965aabad7
|
|||
e22d63d2ee
|
|||
5d2b836af6
|
|||
dd7e252027
|
|||
c4fff9b370
|
|||
610d2590e8 | |||
b180369514
|
|||
17ffd73fb3
|
|||
ec4f5d1a84
|
|||
ae7f3077c0
|
|||
2c0a12a2e8 |
27
Request.ts
27
Request.ts
@ -23,7 +23,7 @@ export class Request<T = any> {
|
|||||||
this.url = url
|
this.url = url
|
||||||
}
|
}
|
||||||
|
|
||||||
public async get(): Promise<T> {
|
public async get(): Promise<T | undefined> {
|
||||||
const now = new Date()
|
const now = new Date()
|
||||||
if (
|
if (
|
||||||
this.fetched &&
|
this.fetched &&
|
||||||
@ -34,22 +34,17 @@ export class Request<T = any> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Fetch Response
|
// Fetch Response
|
||||||
try {
|
const resp = await fetch(this.url, {
|
||||||
const resp = await fetch(this.url, {
|
headers: {
|
||||||
headers: {
|
"Content-Type": "text/plain"
|
||||||
"Content-Type": "text/plain"
|
|
||||||
}
|
|
||||||
})
|
|
||||||
if (resp.status !== 200) {
|
|
||||||
throw new Error(`Error request ended with the code (${resp.status})`)
|
|
||||||
}
|
}
|
||||||
const response = await resp.json()
|
})
|
||||||
this.response = response
|
if (resp.status !== 200) {
|
||||||
this.fetched = now
|
return undefined
|
||||||
return response
|
|
||||||
} catch (e) {
|
|
||||||
console.error(e)
|
|
||||||
throw new Error('An error occured')
|
|
||||||
}
|
}
|
||||||
|
const response = await resp.json()
|
||||||
|
this.response = response
|
||||||
|
this.fetched = now
|
||||||
|
return response
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,34 +3,37 @@ import { List } from "./General";
|
|||||||
import LangList from "./LangList";
|
import LangList from "./LangList";
|
||||||
import Expansion from "./Expansion";
|
import Expansion from "./Expansion";
|
||||||
|
|
||||||
|
|
||||||
export type SetRequest = SetSingle
|
export type SetRequest = SetSingle
|
||||||
|
|
||||||
|
export interface SetSingleRaw extends SetSingle {
|
||||||
|
releaseDate: string
|
||||||
|
}
|
||||||
|
|
||||||
export type SetSingle = {
|
export type SetSingle = {
|
||||||
name: string
|
name: string
|
||||||
code: string
|
code: string
|
||||||
|
|
||||||
expansionCode?: string
|
expansionCode?: string
|
||||||
tcgoCode?: string
|
tcgoCode?: string
|
||||||
|
|
||||||
cardCount: {
|
cardCount: {
|
||||||
total: number
|
total: number
|
||||||
official: number
|
official: number
|
||||||
}
|
}
|
||||||
|
|
||||||
releaseDate: Date|string
|
releaseDate: Date | string
|
||||||
|
|
||||||
legal?: {
|
legal?: {
|
||||||
standard: boolean
|
standard: boolean
|
||||||
expanded: boolean
|
expanded: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
images?: {
|
images?: {
|
||||||
symbol?: string
|
symbol?: string
|
||||||
logo?: string
|
logo?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
list: Array<CardSimple>
|
list: Array<CardSimple>
|
||||||
}
|
}
|
||||||
|
|
||||||
export type SetSimple = {
|
export type SetSimple = {
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
{
|
{
|
||||||
"name": "@tcgdex/sdk",
|
"name": "@tcgdex/sdk",
|
||||||
"version": "1.4.2",
|
"version": "1.6.1",
|
||||||
"main": "./tcgdex.js",
|
"main": "./tcgdex.js",
|
||||||
"types": "./tcgdex.d.ts",
|
"types": "./tcgdex.d.ts",
|
||||||
"repository": "https://git.delta-wings.net/tcgdex/javascript-sdk.git",
|
"repository": "https://github.com/tcgdex/javascript-sdk.git",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node-fetch": "2.5.7",
|
"@types/node-fetch": "2.5.7",
|
||||||
|
41
tcgdex.ts
41
tcgdex.ts
@ -1,8 +1,8 @@
|
|||||||
import { Langs } from './interfaces/Langs'
|
import { SetSingle, SetSimple, SetList, SetSingleRaw } from './interfaces/Set'
|
||||||
import { SetSingle, SetSimple, SetList } from './interfaces/Set'
|
|
||||||
import { CardSingle, CardList, CardSimple } from './interfaces/Card'
|
import { CardSingle, CardList, CardSimple } from './interfaces/Card'
|
||||||
import { ExpansionSingle, ExpansionList } from './interfaces/Expansion'
|
import { ExpansionSingle, ExpansionList } from './interfaces/Expansion'
|
||||||
import RequestWrapper from './Request'
|
import RequestWrapper from './Request'
|
||||||
|
import { Langs } from './interfaces/LangList'
|
||||||
|
|
||||||
export default class TCGdex {
|
export default class TCGdex {
|
||||||
public static defaultLang: Langs = "en"
|
public static defaultLang: Langs = "en"
|
||||||
@ -25,48 +25,67 @@ export default class TCGdex {
|
|||||||
return this.getBaseUrl()
|
return this.getBaseUrl()
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getCard(id: string|number, set: string): Promise<CardSingle>;
|
public async getCard(id: string|number, set: string): Promise<CardSingle | undefined>
|
||||||
public async getCard(id: string): Promise<CardSingle>;
|
public async getCard(id: string): Promise<CardSingle | undefined>
|
||||||
public async getCard(id: string|number, set?: string): Promise<CardSingle> {
|
public async getCard(id: string|number, set?: string): Promise<CardSingle | undefined> {
|
||||||
const txt = set ? `sets/${set}` : "cards"
|
const txt = set ? `sets/${set}` : "cards"
|
||||||
const req = this.rwgr<CardSingle>(`${this.gbu()}/${txt}/${id}/`)
|
const req = this.rwgr<CardSingle>(`${this.gbu()}/${txt}/${id}/`)
|
||||||
return req.get()
|
return req.get()
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getCards(set?: string): Promise<Array<CardSimple>> {
|
public async getCards(set?: string): Promise<Array<CardSimple> | undefined> {
|
||||||
if (set) {
|
if (set) {
|
||||||
const setSingle = await this.getSet(set)
|
const setSingle = await this.getSet(set)
|
||||||
|
if (!setSingle) {
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
return setSingle.list
|
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 req = this.rwgr<CardList>(`${this.gbu()}/cards/`)
|
||||||
const resp = await req.get()
|
const resp = await req.get()
|
||||||
|
if (!resp) {
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
return resp.list
|
return resp.list
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getSet(set: string): Promise<SetSingle> {
|
public async getSet(set: string, transformDate: false): Promise<SetSingleRaw | undefined>
|
||||||
|
public async getSet(set: string, transformDate?: true): Promise<SetSingle | undefined>
|
||||||
|
public async getSet(set: string, transformDate?: boolean): Promise<SetSingle | SetSingleRaw | undefined> {
|
||||||
const req = this.rwgr<SetSingle>(`${this.gbu()}/sets/${set}/`)
|
const req = this.rwgr<SetSingle>(`${this.gbu()}/sets/${set}/`)
|
||||||
const resp = await req.get()
|
const resp = await req.get()
|
||||||
|
if (!resp) {
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
if (!transformDate) {
|
||||||
|
return resp as SetSingleRaw
|
||||||
|
}
|
||||||
return Object.assign(resp, {releaseDate: new Date(resp.releaseDate)}) as SetSingle
|
return Object.assign(resp, {releaseDate: new Date(resp.releaseDate)}) as SetSingle
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getExpansion(expansion: string): Promise<ExpansionSingle> {
|
public async getExpansion(expansion: string): Promise<ExpansionSingle | undefined> {
|
||||||
const req = this.rwgr<ExpansionSingle>(`${this.gbu()}/expansions/${expansion}/`)
|
const req = this.rwgr<ExpansionSingle>(`${this.gbu()}/expansions/${expansion}/`)
|
||||||
return req.get()
|
return req.get()
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getExpansions(): Promise<ExpansionList> {
|
public async getExpansions(): Promise<ExpansionList | undefined> {
|
||||||
const req = this.rwgr<ExpansionList>(`${this.gbu()}/expansions/`)
|
const req = this.rwgr<ExpansionList>(`${this.gbu()}/expansions/`)
|
||||||
return req.get()
|
return req.get()
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getSets(expansion?: string): Promise<Array<SetSimple>> {
|
public async getSets(expansion?: string): Promise<Array<SetSimple> | undefined> {
|
||||||
if (expansion) {
|
if (expansion) {
|
||||||
const expansionSingle = await this.getExpansion(expansion)
|
const expansionSingle = await this.getExpansion(expansion)
|
||||||
|
if (!expansionSingle) {
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
return expansionSingle.sets
|
return expansionSingle.sets
|
||||||
}
|
}
|
||||||
const req = this.rwgr<SetList>(`${this.gbu()}/sets/`)
|
const req = this.rwgr<SetList>(`${this.gbu()}/sets/`)
|
||||||
const list = await req.get()
|
const list = await req.get()
|
||||||
|
if (!list) {
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
return list.list
|
return list.list
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user