fix: export models (#290)

This commit is contained in:
Florian Bouillon 2025-01-14 16:27:21 +01:00 committed by GitHub
parent fc4a31ef39
commit 0b14e8ddec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 2831 additions and 236 deletions

View File

@ -1,21 +1,19 @@
/// <reference types="jest" />
const { default: TCGdex, Query } = require("../src/tcgdex")
import fetch from 'node-fetch'
import { expect, test, vi } from 'vitest'
import TCGdex, { Query } from '../src/tcgdex'
// change timeout of execution
jest.setTimeout(120000)
vi.setConfig({ testTimeout: 120000 })
const fakeFetch = (response, status = 200) => jest.fn(() =>
const fakeFetch = (response: any, status = 200) => vi.fn(() =>
Promise.resolve({
status: status,
json: () => Promise.resolve(response),
json: () => Promise.resolve(response)
})
);
)
test('Basic test', async () => {
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')
expect(res).toEqual({ ok: true })
expect(TCGdex.fetch).toHaveBeenCalledTimes(1)
@ -23,7 +21,7 @@ test('Basic test', async () => {
test('endpoint errors', async () => {
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()).rejects.toThrow()
})
@ -70,7 +68,7 @@ test(`test get set from card`, async () => {
TCGdex.fetch = fetch
expect(
await (await tcgdex.card.get('swsh1-136')).getSet()
await (await tcgdex.card.get('swsh1-136'))!.getSet()
).toBeTruthy()
})
@ -79,7 +77,7 @@ test(`test get serie from set`, async () => {
TCGdex.fetch = fetch
expect(
await (await tcgdex.set.get('swsh1')).getSerie()
await (await tcgdex.set.get('swsh1'))!.getSerie()
).toBeTruthy()
})

View File

@ -1,18 +1,19 @@
const TCGdex = require("../src/tcgdex").default
const fetch = require('node-fetch')
import { expect, test, vi } from 'vitest'
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({
status: status,
json: () => Promise.resolve(response),
})
);
)
test('Basic test', async () => {
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')
expect(res).toEqual({ ok: true })
expect(TCGdex.fetch).toHaveBeenCalledTimes(1)
@ -20,24 +21,24 @@ test('Basic test', async () => {
test('Cache test', async () => {
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')
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')
expect(res2).toEqual({ ok: 'a' })
})
test('endpoint errors', async () => {
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()).rejects.toThrow()
})
test('404 test', async () => {
const tcgdex = new TCGdex('en')
TCGdex.fetch = fakeFetch(undefined, 404)
TCGdex.fetch = fakeFetch(undefined, 404) as any
expect(
await tcgdex.fetch('cards', '404-test')
).not.toBeDefined()

2978
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -44,10 +44,12 @@
"@types/node-fetch": "^2",
"@typescript-eslint/eslint-plugin": "^5",
"@typescript-eslint/parser": "^5",
"@vitest/coverage-v8": "^2.1.8",
"eslint": "^8",
"jest": "^29",
"tsup": "^7",
"typescript": "^5"
"typescript": "^5",
"vitest": "^2"
},
"engines": {
"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",
"prepublishOnly": "npm run build",
"lint": "eslint",
"test": "jest --coverage"
"test": "vitest run --coverage"
},
"files": [
"dist"

View File

@ -1,6 +1,6 @@
import type CacheInterface from '@cachex/core'
import LocalStorageCache from '@cachex/web-storage'
import MemoryCache from '@cachex/memory'
import LocalStorageCache from '@cachex/web-storage'
import Query from './Query'
import Endpoint from './endpoints/Endpoint'
import SimpleEndpoint from './endpoints/SimpleEndpoint'
@ -372,6 +372,7 @@ export default class TCGdex {
// parse, put to cache and return
const json = await resp.json()
this.cache.set(path, json, this.cacheTTL)
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 {
Query
CardModel, CardResumeModel, Endpoint, Model, Query, SerieModel,
SerieResume as SerieResumeModel,
SetModel,
SetResumeModel, SimpleEndpoint
}

View File

@ -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',
'hp', 'illustrators', 'rarities', 'regulation-marks',
'retreats', 'series', 'sets', 'stages', 'suffixes',