mirror of
https://github.com/tcgdex/javascript-sdk.git
synced 2025-04-22 10:42:10 +00:00
Added a version script
It automaticly update the version number without importing package.json Signed-off-by: Avior <florian.bouillon@delta-wings.net>
This commit is contained in:
parent
f858bd5c79
commit
96bc9149c9
15
.gitignore
vendored
15
.gitignore
vendored
@ -1,7 +1,8 @@
|
|||||||
# Dev Files
|
# Dev Files
|
||||||
node_modules
|
node_modules
|
||||||
test.ts
|
test.ts
|
||||||
coverage
|
coverage
|
||||||
|
|
||||||
# Dist files
|
# Dist files
|
||||||
dist
|
dist
|
||||||
|
src/version.json
|
||||||
|
@ -42,7 +42,8 @@
|
|||||||
"unfetch": "^4.2.0"
|
"unfetch": "^4.2.0"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "yarn build:cjs && yarn build:browser && yarn build:es2015",
|
"prebuild": "node scripts/export-version-number.js",
|
||||||
|
"build": "yarn prebuild && yarn build:cjs && yarn build:browser && yarn build:es2015",
|
||||||
"build:cjs": "tsc --project tsconfig.json",
|
"build:cjs": "tsc --project tsconfig.json",
|
||||||
"build:es2015": "tsc --project tsconfig.es2015.json",
|
"build:es2015": "tsc --project tsconfig.es2015.json",
|
||||||
"build:browser": "wp --config webpack.config.js",
|
"build:browser": "wp --config webpack.config.js",
|
||||||
|
4
scripts/export-version-number.js
Normal file
4
scripts/export-version-number.js
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
const { version } = require('../package.json')
|
||||||
|
const fs = require('fs')
|
||||||
|
|
||||||
|
fs.writeFileSync('./src/version.json', JSON.stringify({version}))
|
@ -1,30 +1,31 @@
|
|||||||
import TCGdex from './tcgdex'
|
import TCGdex from './tcgdex'
|
||||||
|
import { version } from './version.json'
|
||||||
export default class Request {
|
|
||||||
|
export default class Request {
|
||||||
// 1 hour of TTL by default
|
|
||||||
public static ttl = 1000 * 60 * 60
|
// 1 hour of TTL by default
|
||||||
|
public static ttl = 1000 * 60 * 60
|
||||||
private static cache: Record<string, {response: any, time: number}> = {}
|
|
||||||
|
private static cache: Record<string, {response: any, time: number}> = {}
|
||||||
public static async fetch<T>(url: string): Promise<T | undefined> {
|
|
||||||
let request = this.cache[url]
|
public static async fetch<T>(url: string): Promise<T | undefined> {
|
||||||
const now = new Date().getTime()
|
let request = this.cache[url]
|
||||||
if (!request || now - request.time > this.ttl) {
|
const now = new Date().getTime()
|
||||||
const unfetch = TCGdex.fetch
|
if (!request || now - request.time > this.ttl) {
|
||||||
const resp = await unfetch(url, {
|
const unfetch = TCGdex.fetch
|
||||||
headers: {
|
const resp = await unfetch(url, {
|
||||||
'user-agent': `@tcgdex/javascript-sdk/${TCGdex.VERSION}`
|
headers: {
|
||||||
}
|
'user-agent': `@tcgdex/javascript-sdk/${version}`
|
||||||
})
|
}
|
||||||
if (resp.status !== 200) {
|
})
|
||||||
return undefined
|
if (resp.status !== 200) {
|
||||||
}
|
return undefined
|
||||||
|
}
|
||||||
this.cache[url] = { response: await resp.json(), time: now }
|
|
||||||
request = this.cache[url]
|
this.cache[url] = { response: await resp.json(), time: now }
|
||||||
}
|
request = this.cache[url]
|
||||||
return request.response
|
}
|
||||||
}
|
return request.response
|
||||||
|
}
|
||||||
}
|
|
||||||
|
}
|
||||||
|
372
src/tcgdex.ts
372
src/tcgdex.ts
@ -1,187 +1,185 @@
|
|||||||
import RequestWrapper from './Request'
|
import RequestWrapper from './Request'
|
||||||
import { Serie, Set, Card, CardResume, SerieList, SetList, SupportedLanguages, StringEndpoint } from './interfaces'
|
import { Serie, Set, Card, CardResume, SerieList, SetList, SupportedLanguages, StringEndpoint } from './interfaces'
|
||||||
type Endpoint = 'cards' | 'categories' | 'hp' | 'illustrators' | 'rarities' | 'retreats' | 'series' | 'sets' | 'types'
|
type Endpoint = 'cards' | 'categories' | 'hp' | 'illustrators' | 'rarities' | 'retreats' | 'series' | 'sets' | 'types'
|
||||||
|
|
||||||
const ENDPOINTS: Array<Endpoint> = ['cards', 'categories', 'hp', 'illustrators', 'rarities', 'retreats', 'series', 'sets', 'types']
|
const ENDPOINTS: Array<Endpoint> = ['cards', 'categories', 'hp', 'illustrators', 'rarities', 'retreats', 'series', 'sets', 'types']
|
||||||
const BASE_URL = 'https://api.tcgdex.net/v2'
|
const BASE_URL = 'https://api.tcgdex.net/v2'
|
||||||
export default class TCGdex {
|
export default class TCGdex {
|
||||||
|
|
||||||
public static fetch: typeof fetch
|
public static fetch: typeof fetch
|
||||||
|
|
||||||
public static readonly VERSION = '2.2.0'
|
/**
|
||||||
|
* @deprecated to change the lang use `this.lang`
|
||||||
/**
|
*/
|
||||||
* @deprecated to change the lang use `this.lang`
|
public static defaultLang: SupportedLanguages = 'en'
|
||||||
*/
|
|
||||||
public static defaultLang: SupportedLanguages = 'en'
|
public constructor(public lang?: SupportedLanguages) {}
|
||||||
|
|
||||||
public constructor(public lang?: SupportedLanguages) {}
|
public getLang(): SupportedLanguages {
|
||||||
|
return this.lang ?? TCGdex.defaultLang ?? 'en'
|
||||||
public getLang(): SupportedLanguages {
|
}
|
||||||
return this.lang ?? TCGdex.defaultLang ?? 'en'
|
|
||||||
}
|
/**
|
||||||
|
* Shortcut to easily fetch a card using both it's global id and it's local ID
|
||||||
/**
|
* @param id the card global/local ID
|
||||||
* Shortcut to easily fetch a card using both it's global id and it's local ID
|
* @param set the card set name/ID (optionnal)
|
||||||
* @param id the card global/local ID
|
* @returns the card object
|
||||||
* @param set the card set name/ID (optionnal)
|
*/
|
||||||
* @returns the card object
|
public async fetchCard(id: string | number, set?: string): Promise<Card | undefined> {
|
||||||
*/
|
const path = set ? ['sets', set] : ['cards']
|
||||||
public async fetchCard(id: string | number, set?: string): Promise<Card | undefined> {
|
// @ts-expect-error the base endpoint is 'sets' or 'cards'
|
||||||
const path = set ? ['sets', set] : ['cards']
|
return this.fetch(...path, id)
|
||||||
// @ts-expect-error the base endpoint is 'sets' or 'cards'
|
}
|
||||||
return this.fetch(...path, id)
|
|
||||||
}
|
/**
|
||||||
|
* Shortcut to easily fetch cards using an optionnal set name/ID
|
||||||
/**
|
* @param set the card set name/ID (optionnal)
|
||||||
* Shortcut to easily fetch cards using an optionnal set name/ID
|
* @returns a card list
|
||||||
* @param set the card set name/ID (optionnal)
|
*/
|
||||||
* @returns a card list
|
public async fetchCards(set?: string): Promise<Array<CardResume> | undefined> {
|
||||||
*/
|
if (set) {
|
||||||
public async fetchCards(set?: string): Promise<Array<CardResume> | undefined> {
|
const fSet = await this.fetch('sets', set)
|
||||||
if (set) {
|
return fSet ? fSet.cards : undefined
|
||||||
const fSet = await this.fetch('sets', set)
|
}
|
||||||
return fSet ? fSet.cards : undefined
|
return this.fetch('cards')
|
||||||
}
|
}
|
||||||
return this.fetch('cards')
|
|
||||||
}
|
/**
|
||||||
|
* @deprecated use `this.fetch('sets', set)`
|
||||||
/**
|
*/
|
||||||
* @deprecated use `this.fetch('sets', set)`
|
public async fetchSet(set: string): Promise<Set | undefined> {
|
||||||
*/
|
return this.fetch('sets', set)
|
||||||
public async fetchSet(set: string): Promise<Set | undefined> {
|
}
|
||||||
return this.fetch('sets', set)
|
|
||||||
}
|
/**
|
||||||
|
* @deprecated use `this.fetch('series', serie)`
|
||||||
/**
|
*/
|
||||||
* @deprecated use `this.fetch('series', serie)`
|
public async fetchSerie(serie: string): Promise<Serie | undefined> {
|
||||||
*/
|
return this.fetch('series', serie)
|
||||||
public async fetchSerie(serie: string): Promise<Serie | undefined> {
|
}
|
||||||
return this.fetch('series', serie)
|
|
||||||
}
|
/**
|
||||||
|
* @deprecated use `this.fetch('series')`
|
||||||
/**
|
*/
|
||||||
* @deprecated use `this.fetch('series')`
|
public async fetchSeries(): Promise<SerieList | undefined> {
|
||||||
*/
|
return this.fetch('series')
|
||||||
public async fetchSeries(): Promise<SerieList | undefined> {
|
}
|
||||||
return this.fetch('series')
|
|
||||||
}
|
/**
|
||||||
|
* Shortcut to easily fetch sets using an optionnal serie name/ID
|
||||||
/**
|
* @param serie the card set name/ID (optionnal)
|
||||||
* Shortcut to easily fetch sets using an optionnal serie name/ID
|
* @returns a card list
|
||||||
* @param serie the card set name/ID (optionnal)
|
*/
|
||||||
* @returns a card list
|
public async fetchSets(serie?: string): Promise<SetList | undefined> {
|
||||||
*/
|
if (serie) {
|
||||||
public async fetchSets(serie?: string): Promise<SetList | undefined> {
|
const fSerie = await this.fetch('series', serie)
|
||||||
if (serie) {
|
return fSerie ? fSerie.sets : undefined
|
||||||
const fSerie = await this.fetch('series', serie)
|
}
|
||||||
return fSerie ? fSerie.sets : undefined
|
return this.fetch('sets')
|
||||||
}
|
}
|
||||||
return this.fetch('sets')
|
|
||||||
}
|
/**
|
||||||
|
* Fetch a card using its global id
|
||||||
/**
|
* @param endpoint_0 'cards'
|
||||||
* Fetch a card using its global id
|
* @param endpoint_1 {string} the card global ID
|
||||||
* @param endpoint_0 'cards'
|
*/
|
||||||
* @param endpoint_1 {string} the card global ID
|
public async fetch(...type: ['cards', string]): Promise<Card | undefined>
|
||||||
*/
|
|
||||||
public async fetch(...type: ['cards', string]): Promise<Card | undefined>
|
/**
|
||||||
|
* Fetch every cards in the database
|
||||||
/**
|
* @param endpoint_0 'cards'
|
||||||
* Fetch every cards in the database
|
*/
|
||||||
* @param endpoint_0 'cards'
|
public async fetch(type: 'cards'): Promise<Array<CardResume> | undefined>
|
||||||
*/
|
|
||||||
public async fetch(type: 'cards'): Promise<Array<CardResume> | undefined>
|
/**
|
||||||
|
* Fetch a card using its local id and its set
|
||||||
/**
|
* @param endpoint_0 'sets'
|
||||||
* Fetch a card using its local id and its set
|
* @param endpoint_1 {string} the set name or ID
|
||||||
* @param endpoint_0 'sets'
|
* @param endpoint_2 {string} the card local ID
|
||||||
* @param endpoint_1 {string} the set name or ID
|
*/
|
||||||
* @param endpoint_2 {string} the card local ID
|
public async fetch(...endpoint: ['sets', string, string]): Promise<Card | undefined>
|
||||||
*/
|
|
||||||
public async fetch(...endpoint: ['sets', string, string]): Promise<Card | undefined>
|
/**
|
||||||
|
* Fetch a set
|
||||||
/**
|
* @param endpoint_0 'sets'
|
||||||
* Fetch a set
|
* @param endpoint_1 {string} the set name or ID
|
||||||
* @param endpoint_0 'sets'
|
*/
|
||||||
* @param endpoint_1 {string} the set name or ID
|
public async fetch(...endpoint: ['sets', string]): Promise<Set | undefined>
|
||||||
*/
|
|
||||||
public async fetch(...endpoint: ['sets', string]): Promise<Set | undefined>
|
/**
|
||||||
|
* Fetch every sets
|
||||||
/**
|
* @param endpoint_0 'sets'
|
||||||
* Fetch every sets
|
*/
|
||||||
* @param endpoint_0 'sets'
|
public async fetch(endpoint: 'sets'): Promise<SetList | undefined>
|
||||||
*/
|
|
||||||
public async fetch(endpoint: 'sets'): Promise<SetList | undefined>
|
/**
|
||||||
|
* Fetch a serie
|
||||||
/**
|
* @param endpoint_0 'series'
|
||||||
* Fetch a serie
|
* @param endpoint_1 {string} the serie name or ID
|
||||||
* @param endpoint_0 'series'
|
*/
|
||||||
* @param endpoint_1 {string} the serie name or ID
|
public async fetch(...endpoint: ['series', string]): Promise<Serie | undefined>
|
||||||
*/
|
|
||||||
public async fetch(...endpoint: ['series', string]): Promise<Serie | undefined>
|
/**
|
||||||
|
* Fetch every series
|
||||||
/**
|
* @param endpoint_0 'series'
|
||||||
* Fetch every series
|
*/
|
||||||
* @param endpoint_0 'series'
|
public async fetch(endpoint: 'series'): Promise<SerieList | undefined>
|
||||||
*/
|
|
||||||
public async fetch(endpoint: 'series'): Promise<SerieList | undefined>
|
/**
|
||||||
|
* Fetch cards depending on a specific filter
|
||||||
/**
|
* @param endpoint_0 {'categories' | 'hp' | 'illustrators' | 'rarities' | 'retreats' | 'types'}
|
||||||
* Fetch cards depending on a specific filter
|
* Possible value 'categories' | 'hp' | 'illustrators' | 'rarities' | 'retreats' | 'types'
|
||||||
* @param endpoint_0 {'categories' | 'hp' | 'illustrators' | 'rarities' | 'retreats' | 'types'}
|
* @param endpoint_1 {string} the value set while fetching the index
|
||||||
* Possible value 'categories' | 'hp' | 'illustrators' | 'rarities' | 'retreats' | 'types'
|
*/
|
||||||
* @param endpoint_1 {string} the value set while fetching the index
|
public async fetch(...endpoint: ['categories' | 'hp' | 'illustrators' | 'rarities' | 'retreats' | 'types', string]): Promise<StringEndpoint | undefined>
|
||||||
*/
|
|
||||||
public async fetch(...endpoint: ['categories' | 'hp' | 'illustrators' | 'rarities' | 'retreats' | 'types', string]): Promise<StringEndpoint | undefined>
|
/**
|
||||||
|
* Fetch cards depending on a specific filter
|
||||||
/**
|
* @param endpoint_0 {'hp' | 'retreats' | 'categories' | 'illustrators' | 'rarities' | 'types'}
|
||||||
* Fetch cards depending on a specific filter
|
* Possible value 'hp' | 'retreats' | 'categories' | 'illustrators' | 'rarities' | 'types'
|
||||||
* @param endpoint_0 {'hp' | 'retreats' | 'categories' | 'illustrators' | 'rarities' | 'types'}
|
* @param endpoint_1 {string} Fetch the possible values to use depending on the endpoint
|
||||||
* Possible value 'hp' | 'retreats' | 'categories' | 'illustrators' | 'rarities' | 'types'
|
*/
|
||||||
* @param endpoint_1 {string} Fetch the possible values to use depending on the endpoint
|
public async fetch(endpoint: 'hp' | 'retreats' | 'categories' | 'illustrators' | 'rarities' | 'types'): Promise<Array<string> | undefined>
|
||||||
*/
|
|
||||||
public async fetch(endpoint: 'hp' | 'retreats' | 'categories' | 'illustrators' | 'rarities' | 'types'): Promise<Array<string> | undefined>
|
/**
|
||||||
|
* Fetch The differents endpoints depending on the first argument
|
||||||
/**
|
* @param endpoint_0 {'hp' | 'retreats' | 'categories' | 'illustrators' | 'rarities' | 'types'}
|
||||||
* Fetch The differents endpoints depending on the first argument
|
* Possible value 'cards' | 'categories' | 'hp' | 'illustrators' | 'rarities' | 'retreats' | 'series' | 'sets' | 'types'
|
||||||
* @param endpoint_0 {'hp' | 'retreats' | 'categories' | 'illustrators' | 'rarities' | 'types'}
|
* @param endpoint_1 {string} (Optionnal) some details to go from the index file to the item file (mostly the ID/name)
|
||||||
* Possible value 'cards' | 'categories' | 'hp' | 'illustrators' | 'rarities' | 'retreats' | 'series' | 'sets' | 'types'
|
* @param endpoint_2 {string} (Optionnal) only for sets the card local ID to fetch the card through the set
|
||||||
* @param endpoint_1 {string} (Optionnal) some details to go from the index file to the item file (mostly the ID/name)
|
*/
|
||||||
* @param endpoint_2 {string} (Optionnal) only for sets the card local ID to fetch the card through the set
|
public async fetch(...endpoint: Array<Endpoint | string>): Promise<any | undefined> {
|
||||||
*/
|
if (endpoint.length === 0) {
|
||||||
public async fetch(...endpoint: Array<Endpoint | string>): Promise<any | undefined> {
|
throw new Error('endpoint to fetch is empty!')
|
||||||
if (endpoint.length === 0) {
|
}
|
||||||
throw new Error('endpoint to fetch is empty!')
|
// @ts-expect-error with the precedent check, we KNOW that type is not empty
|
||||||
}
|
const baseEndpoint = endpoint.shift().toLowerCase() as Endpoint
|
||||||
// @ts-expect-error with the precedent check, we KNOW that type is not empty
|
if (!ENDPOINTS.includes(baseEndpoint)) {
|
||||||
const baseEndpoint = endpoint.shift().toLowerCase() as Endpoint
|
throw new Error(`unknown endpoint to fetch! (${baseEndpoint})`)
|
||||||
if (!ENDPOINTS.includes(baseEndpoint)) {
|
}
|
||||||
throw new Error(`unknown endpoint to fetch! (${baseEndpoint})`)
|
return this.makeRequest(baseEndpoint, ...endpoint)
|
||||||
}
|
}
|
||||||
return this.makeRequest(baseEndpoint, ...endpoint)
|
|
||||||
}
|
/**
|
||||||
|
* Function to make the request and normalize the whole path
|
||||||
/**
|
*/
|
||||||
* Function to make the request and normalize the whole path
|
private makeRequest<T = any>(...url: Array<string | number>) {
|
||||||
*/
|
// Normalize path
|
||||||
private makeRequest<T = any>(...url: Array<string | number>) {
|
const path = url.map((subPath) => encodeURI(
|
||||||
// Normalize path
|
subPath
|
||||||
const path = url.map((subPath) => encodeURI(
|
// Transform numbers to string
|
||||||
subPath
|
.toString()
|
||||||
// Transform numbers to string
|
// replace this special character with an escaped one
|
||||||
.toString()
|
.replace('?', '%3F')
|
||||||
// replace this special character with an escaped one
|
// normalize the string
|
||||||
.replace('?', '%3F')
|
.normalize('NFC')
|
||||||
// normalize the string
|
// remove some special chars by nothing
|
||||||
.normalize('NFC')
|
// eslint-disable-next-line no-misleading-character-class
|
||||||
// remove some special chars by nothing
|
.replace(/["'\u0300-\u036f]/gu, '')
|
||||||
// eslint-disable-next-line no-misleading-character-class
|
)).join('/')
|
||||||
.replace(/["'\u0300-\u036f]/gu, '')
|
return RequestWrapper.fetch<T>(`${BASE_URL}/${this.getLang()}/${path}`)
|
||||||
)).join('/')
|
}
|
||||||
return RequestWrapper.fetch<T>(`${BASE_URL}/${this.getLang()}/${path}`)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
export * from './interfaces'
|
||||||
|
|
||||||
export * from './interfaces'
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user