mirror of
https://github.com/tcgdex/javascript-sdk.git
synced 2025-07-13 10:35:11 +00:00
Compare commits
11 Commits
dependabot
...
master
Author | SHA1 | Date | |
---|---|---|---|
6849546c1a | |||
95a658f98a | |||
c866b4022f | |||
978a676a79 | |||
1f801f4a94 | |||
4e4bcf9d26
|
|||
0b14e8ddec | |||
fc4a31ef39 | |||
26acb1eb61
|
|||
c03183bd6f
|
|||
07403cb3ae
|
@ -1,18 +1,19 @@
|
|||||||
/// <reference types="jest" />
|
import { expect, test, vi } from 'vitest'
|
||||||
|
import TCGdex, { Query } from '../src/tcgdex'
|
||||||
|
|
||||||
const { default: TCGdex, Query } = require("../src/tcgdex")
|
// change timeout of execution
|
||||||
import fetch from 'node-fetch'
|
vi.setConfig({ testTimeout: 120000 })
|
||||||
|
|
||||||
const fakeFetch = (response, status = 200) => jest.fn(() =>
|
const fakeFetch = (response: any, status = 200) => vi.fn(() =>
|
||||||
Promise.resolve({
|
Promise.resolve({
|
||||||
status: status,
|
status: status,
|
||||||
json: () => Promise.resolve(response),
|
json: () => Promise.resolve(response)
|
||||||
})
|
})
|
||||||
);
|
)
|
||||||
|
|
||||||
test('Basic test', async () => {
|
test('Basic test', async () => {
|
||||||
const tcgdex = new TCGdex('en')
|
const tcgdex = new TCGdex('en')
|
||||||
TCGdex.fetch = fakeFetch({ ok: true })
|
TCGdex.fetch = fakeFetch({ ok: true }) as any
|
||||||
const res = await tcgdex.fetch('cards', 'basic-test')
|
const res = await tcgdex.fetch('cards', 'basic-test')
|
||||||
expect(res).toEqual({ ok: true })
|
expect(res).toEqual({ ok: true })
|
||||||
expect(TCGdex.fetch).toHaveBeenCalledTimes(1)
|
expect(TCGdex.fetch).toHaveBeenCalledTimes(1)
|
||||||
@ -20,7 +21,7 @@ test('Basic test', async () => {
|
|||||||
|
|
||||||
test('endpoint errors', async () => {
|
test('endpoint errors', async () => {
|
||||||
const tcgdex = new TCGdex('en')
|
const tcgdex = new TCGdex('en')
|
||||||
TCGdex.fetch = fakeFetch({ ok: 'a' })
|
TCGdex.fetch = fakeFetch({ ok: 'a' }) as any
|
||||||
await expect(tcgdex.fetch('non existing endpoint')).rejects.toThrow()
|
await expect(tcgdex.fetch('non existing endpoint')).rejects.toThrow()
|
||||||
await expect(tcgdex.fetch()).rejects.toThrow()
|
await expect(tcgdex.fetch()).rejects.toThrow()
|
||||||
})
|
})
|
||||||
@ -67,7 +68,7 @@ test(`test get set from card`, async () => {
|
|||||||
TCGdex.fetch = fetch
|
TCGdex.fetch = fetch
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
await (await tcgdex.card.get('swsh1-136')).getSet()
|
await (await tcgdex.card.get('swsh1-136'))!.getSet()
|
||||||
).toBeTruthy()
|
).toBeTruthy()
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -76,7 +77,7 @@ test(`test get serie from set`, async () => {
|
|||||||
TCGdex.fetch = fetch
|
TCGdex.fetch = fetch
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
await (await tcgdex.set.get('swsh1')).getSerie()
|
await (await tcgdex.set.get('swsh1'))!.getSerie()
|
||||||
).toBeTruthy()
|
).toBeTruthy()
|
||||||
})
|
})
|
||||||
|
|
@ -1,43 +1,44 @@
|
|||||||
const TCGdex = require("../src/tcgdex").default
|
import { expect, test, vi } from 'vitest'
|
||||||
const fetch = require('node-fetch')
|
import TCGdex from '../src/tcgdex'
|
||||||
|
|
||||||
const fakeFetch = (response, status = 200) => jest.fn(() =>
|
// change timeout of execution
|
||||||
|
vi.setConfig({ testTimeout: 120000 })
|
||||||
|
|
||||||
|
const fakeFetch = (response, status = 200) => vi.fn(() =>
|
||||||
Promise.resolve({
|
Promise.resolve({
|
||||||
status: status,
|
status: status,
|
||||||
json: () => Promise.resolve(response),
|
json: () => Promise.resolve(response),
|
||||||
})
|
})
|
||||||
);
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
test('Basic test', async () => {
|
test('Basic test', async () => {
|
||||||
const tcgdex = new TCGdex('en')
|
const tcgdex = new TCGdex('en')
|
||||||
TCGdex.fetch = fakeFetch({ok: true})
|
TCGdex.fetch = fakeFetch({ ok: true }) as any
|
||||||
const res = await tcgdex.fetch('cards', 'basic-test')
|
const res = await tcgdex.fetch('cards', 'basic-test')
|
||||||
expect(res).toEqual({ok: true})
|
expect(res).toEqual({ ok: true })
|
||||||
expect(TCGdex.fetch).toHaveBeenCalledTimes(1)
|
expect(TCGdex.fetch).toHaveBeenCalledTimes(1)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('Cache test', async () => {
|
test('Cache test', async () => {
|
||||||
const tcgdex = new TCGdex('en')
|
const tcgdex = new TCGdex('en')
|
||||||
TCGdex.fetch = fakeFetch({ok: 'a'})
|
TCGdex.fetch = fakeFetch({ ok: 'a' }) as any
|
||||||
const res1 = await tcgdex.fetch('cards', 'cache-test')
|
const res1 = await tcgdex.fetch('cards', 'cache-test')
|
||||||
expect(res1).toEqual({ok: 'a'})
|
expect(res1).toEqual({ ok: 'a' })
|
||||||
TCGdex.fetch = fakeFetch({ok: 'b'})
|
TCGdex.fetch = fakeFetch({ ok: 'b' }) as any
|
||||||
const res2 = await tcgdex.fetch('cards', 'cache-test')
|
const res2 = await tcgdex.fetch('cards', 'cache-test')
|
||||||
expect(res2).toEqual({ok: 'a'})
|
expect(res2).toEqual({ ok: 'a' })
|
||||||
})
|
})
|
||||||
|
|
||||||
test('endpoint errors', async () => {
|
test('endpoint errors', async () => {
|
||||||
const tcgdex = new TCGdex('en')
|
const tcgdex = new TCGdex('en')
|
||||||
TCGdex.fetch = fakeFetch({ok: 'a'})
|
TCGdex.fetch = fakeFetch({ ok: 'a' }) as any
|
||||||
await expect(tcgdex.fetch('non existing endpoint')).rejects.toThrow()
|
await expect(tcgdex.fetch('non existing endpoint')).rejects.toThrow()
|
||||||
await expect(tcgdex.fetch()).rejects.toThrow()
|
await expect(tcgdex.fetch()).rejects.toThrow()
|
||||||
})
|
})
|
||||||
|
|
||||||
test('404 test', async () => {
|
test('404 test', async () => {
|
||||||
const tcgdex = new TCGdex('en')
|
const tcgdex = new TCGdex('en')
|
||||||
TCGdex.fetch = fakeFetch(undefined, 404)
|
TCGdex.fetch = fakeFetch(undefined, 404) as any
|
||||||
expect(
|
expect(
|
||||||
await tcgdex.fetch('cards', '404-test')
|
await tcgdex.fetch('cards', '404-test')
|
||||||
).not.toBeDefined()
|
).not.toBeDefined()
|
||||||
@ -47,15 +48,15 @@ test('test real endpoints', async () => {
|
|||||||
const tcgdex = new TCGdex('en')
|
const tcgdex = new TCGdex('en')
|
||||||
TCGdex.fetch = fetch
|
TCGdex.fetch = fetch
|
||||||
const endpoints = [
|
const endpoints = [
|
||||||
{endpoint: 'fetchCard', params: ['swsh1-1']},
|
{ endpoint: 'fetchCard', params: ['swsh1-1'] },
|
||||||
{endpoint: 'fetchCard', params: ['1', 'Sword & Shield']},
|
{ endpoint: 'fetchCard', params: ['1', 'Sword & Shield'] },
|
||||||
{endpoint: 'fetchCards', params: ['swsh1']},
|
{ endpoint: 'fetchCards', params: ['swsh1'] },
|
||||||
{endpoint: 'fetchCards', params: []},
|
{ endpoint: 'fetchCards', params: [] },
|
||||||
{endpoint: 'fetchSet', params: ['swsh1']},
|
{ endpoint: 'fetchSet', params: ['swsh1'] },
|
||||||
{endpoint: 'fetchSets', params: ['swsh']},
|
{ endpoint: 'fetchSets', params: ['swsh'] },
|
||||||
{endpoint: 'fetchSets', params: []},
|
{ endpoint: 'fetchSets', params: [] },
|
||||||
{endpoint: 'fetchSeries', params: []},
|
{ endpoint: 'fetchSeries', params: [] },
|
||||||
{endpoint: 'fetchSerie', params: ['swsh']},
|
{ endpoint: 'fetchSerie', params: ['swsh'] },
|
||||||
]
|
]
|
||||||
|
|
||||||
for await (const item of endpoints) {
|
for await (const item of endpoints) {
|
3675
package-lock.json
generated
3675
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@tcgdex/sdk",
|
"name": "@tcgdex/sdk",
|
||||||
"version": "2.6.0-beta.4",
|
"version": "2.7.0",
|
||||||
"main": "./dist/tcgdex.node.js",
|
"main": "./dist/tcgdex.node.js",
|
||||||
"module": "./dist/tcgdex.node.mjs",
|
"module": "./dist/tcgdex.node.mjs",
|
||||||
"types": "./dist/tcgdex.node.d.ts",
|
"types": "./dist/tcgdex.node.d.ts",
|
||||||
@ -44,10 +44,12 @@
|
|||||||
"@types/node-fetch": "^2",
|
"@types/node-fetch": "^2",
|
||||||
"@typescript-eslint/eslint-plugin": "^5",
|
"@typescript-eslint/eslint-plugin": "^5",
|
||||||
"@typescript-eslint/parser": "^5",
|
"@typescript-eslint/parser": "^5",
|
||||||
|
"@vitest/coverage-v8": "^2.1.8",
|
||||||
"eslint": "^8",
|
"eslint": "^8",
|
||||||
"jest": "^29",
|
"jest": "^29",
|
||||||
"tsup": "^7",
|
"tsup": "^7",
|
||||||
"typescript": "^5"
|
"typescript": "^5",
|
||||||
|
"vitest": "^2"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=12"
|
"node": ">=12"
|
||||||
@ -63,7 +65,7 @@
|
|||||||
"build": "rm -rf dist && tsup ./src/tcgdex.node.ts --format cjs,esm --dts --clean && tsup ./src/tcgdex.browser.ts --format iife --global-name TCGdex --sourcemap",
|
"build": "rm -rf dist && tsup ./src/tcgdex.node.ts --format cjs,esm --dts --clean && tsup ./src/tcgdex.browser.ts --format iife --global-name TCGdex --sourcemap",
|
||||||
"prepublishOnly": "npm run build",
|
"prepublishOnly": "npm run build",
|
||||||
"lint": "eslint",
|
"lint": "eslint",
|
||||||
"test": "jest --coverage"
|
"test": "vitest run --coverage"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"dist"
|
"dist"
|
||||||
|
13
src/interfaces.d.ts
vendored
13
src/interfaces.d.ts
vendored
@ -22,9 +22,18 @@ interface variants {
|
|||||||
firstEdition?: boolean
|
firstEdition?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface booster {
|
||||||
|
id: string
|
||||||
|
name: string
|
||||||
|
logo?: string
|
||||||
|
artwork_front?: string
|
||||||
|
artwork_back?: string
|
||||||
|
}
|
||||||
|
|
||||||
export type SetList = Array<SetResume>
|
export type SetList = Array<SetResume>
|
||||||
export type SerieList = Array<SerieResume>
|
export type SerieList = Array<SerieResume>
|
||||||
export type CardList = Array<CardResume>
|
export type CardList = Array<CardResume>
|
||||||
|
export type BoosterList = Array<booster>
|
||||||
|
|
||||||
export interface SetResume {
|
export interface SetResume {
|
||||||
id: string
|
id: string
|
||||||
@ -105,6 +114,8 @@ export interface Set extends SetResume {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cards: CardList
|
cards: CardList
|
||||||
|
|
||||||
|
boosters?: BoosterList
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface CardResume {
|
export interface CardResume {
|
||||||
@ -302,6 +313,8 @@ export interface Card<SetType extends SetResume = SetResume> extends CardResume
|
|||||||
*/
|
*/
|
||||||
expanded: boolean
|
expanded: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
|
boosters?: BoosterList
|
||||||
}
|
}
|
||||||
|
|
||||||
export type StringEndpointList = Array<string>
|
export type StringEndpointList = Array<string>
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import CardResume from './CardResume'
|
import CardResume from './CardResume'
|
||||||
import type { Variants } from './Other'
|
import type { Booster, Variants } from './Other'
|
||||||
import type TCGdexSet from './Set'
|
import type TCGdexSet from './Set'
|
||||||
import type SetResume from './SetResume'
|
import type SetResume from './SetResume'
|
||||||
|
|
||||||
@ -188,6 +188,8 @@ export default class Card extends CardResume {
|
|||||||
expanded: boolean
|
expanded: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boosters?: Array<Booster>
|
||||||
|
|
||||||
public override async getCard(): Promise<Card> {
|
public override async getCard(): Promise<Card> {
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
9
src/models/Other.d.ts
vendored
9
src/models/Other.d.ts
vendored
@ -4,3 +4,12 @@ export interface Variants {
|
|||||||
holo?: boolean
|
holo?: boolean
|
||||||
firstEdition?: boolean
|
firstEdition?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface Booster {
|
||||||
|
|
||||||
|
id: string
|
||||||
|
name: string
|
||||||
|
logo?: string
|
||||||
|
artwork_front?: string
|
||||||
|
artwork_back?: string
|
||||||
|
}
|
@ -1,7 +1,7 @@
|
|||||||
import { objectLoop } from '@dzeio/object-util'
|
import { objectLoop } from '@dzeio/object-util'
|
||||||
import CardResume from './CardResume'
|
import CardResume from './CardResume'
|
||||||
import Model from './Model'
|
import Model from './Model'
|
||||||
import type { Variants } from './Other'
|
import type { Booster, Variants } from './Other'
|
||||||
import type SerieResume from './SerieResume'
|
import type SerieResume from './SerieResume'
|
||||||
|
|
||||||
// biome-ignore lint/suspicious/noShadowRestrictedNames: <explanation>
|
// biome-ignore lint/suspicious/noShadowRestrictedNames: <explanation>
|
||||||
@ -70,6 +70,8 @@ export default class Set extends Model {
|
|||||||
|
|
||||||
public cards!: Array<CardResume>
|
public cards!: Array<CardResume>
|
||||||
|
|
||||||
|
public boosters?: Array<Booster>
|
||||||
|
|
||||||
public async getSerie() {
|
public async getSerie() {
|
||||||
return this.sdk.serie.get(this.serie.id)
|
return this.sdk.serie.get(this.serie.id)
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import type CacheInterface from '@cachex/core'
|
import type CacheInterface from '@cachex/core'
|
||||||
import LocalStorageCache from '@cachex/web-storage'
|
|
||||||
import MemoryCache from '@cachex/memory'
|
import MemoryCache from '@cachex/memory'
|
||||||
|
import LocalStorageCache from '@cachex/web-storage'
|
||||||
import Query from './Query'
|
import Query from './Query'
|
||||||
import Endpoint from './endpoints/Endpoint'
|
import Endpoint from './endpoints/Endpoint'
|
||||||
import SimpleEndpoint from './endpoints/SimpleEndpoint'
|
import SimpleEndpoint from './endpoints/SimpleEndpoint'
|
||||||
@ -372,6 +372,7 @@ export default class TCGdex {
|
|||||||
|
|
||||||
// parse, put to cache and return
|
// parse, put to cache and return
|
||||||
const json = await resp.json()
|
const json = await resp.json()
|
||||||
|
|
||||||
this.cache.set(path, json, this.cacheTTL)
|
this.cache.set(path, json, this.cacheTTL)
|
||||||
return json as T
|
return json as T
|
||||||
}
|
}
|
||||||
@ -397,8 +398,13 @@ export default class TCGdex {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// export the old interfaces
|
||||||
|
export type * from './interfaces.d.ts'
|
||||||
|
|
||||||
export * from './models/Card'
|
// export the new models items and the Query
|
||||||
export {
|
export {
|
||||||
Query
|
CardModel, CardResumeModel, Endpoint, Model, Query, SerieModel,
|
||||||
|
SerieResume as SerieResumeModel,
|
||||||
|
SetModel,
|
||||||
|
SetResumeModel, SimpleEndpoint
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ export function detectContext(): 'browser' | 'server' {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ENDPOINTS: Array<Endpoints> = [
|
export const ENDPOINTS: ReadonlyArray<Endpoints> = [
|
||||||
'cards', 'categories', 'dex-ids', 'energy-types',
|
'cards', 'categories', 'dex-ids', 'energy-types',
|
||||||
'hp', 'illustrators', 'rarities', 'regulation-marks',
|
'hp', 'illustrators', 'rarities', 'regulation-marks',
|
||||||
'retreats', 'series', 'sets', 'stages', 'suffixes',
|
'retreats', 'series', 'sets', 'stages', 'suffixes',
|
||||||
|
Reference in New Issue
Block a user