mirror of
https://github.com/tcgdex/cards-database.git
synced 2025-04-22 19:02:10 +00:00
fix: Misc Endpoints not working as intended (#181)
Signed-off-by: Avior <github@avior.me>
This commit is contained in:
parent
ac3a62c0ec
commit
227637fd97
5
.vscode/settings.json
vendored
Normal file
5
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"files.exclude": {
|
||||||
|
"**/*.js": true
|
||||||
|
}
|
||||||
|
}
|
@ -2,6 +2,7 @@ import { objectLoop } from '@dzeio/object-util'
|
|||||||
import { Card as SDKCard, CardResume, SupportedLanguages } from '@tcgdex/sdk'
|
import { Card as SDKCard, CardResume, SupportedLanguages } from '@tcgdex/sdk'
|
||||||
import Set from './Set'
|
import Set from './Set'
|
||||||
import { Pagination } from '../../interfaces'
|
import { Pagination } from '../../interfaces'
|
||||||
|
import { lightCheck } from '../../util'
|
||||||
|
|
||||||
type LocalCard = Omit<SDKCard, 'set'> & {set: () => Set}
|
type LocalCard = Omit<SDKCard, 'set'> & {set: () => Set}
|
||||||
|
|
||||||
@ -62,10 +63,7 @@ export default class Card implements LocalCard {
|
|||||||
public static find(lang: SupportedLanguages, params: Partial<Record<keyof SDKCard, any>> = {}, pagination?: Pagination) {
|
public static find(lang: SupportedLanguages, params: Partial<Record<keyof SDKCard, any>> = {}, pagination?: Pagination) {
|
||||||
let list : Array<SDKCard> = (require(`../../../generated/${lang}/cards.json`) as Array<SDKCard>)
|
let list : Array<SDKCard> = (require(`../../../generated/${lang}/cards.json`) as Array<SDKCard>)
|
||||||
.filter((c) => objectLoop(params, (it, key) => {
|
.filter((c) => objectLoop(params, (it, key) => {
|
||||||
if (typeof it === "string") {
|
return lightCheck(c[key as 'localId'], it)
|
||||||
return c[key as 'localId'].toLowerCase().includes(it.toLowerCase())
|
|
||||||
}
|
|
||||||
return c[key as 'localId'].includes(it)
|
|
||||||
}))
|
}))
|
||||||
if (pagination) {
|
if (pagination) {
|
||||||
list = list
|
list = list
|
||||||
@ -83,14 +81,10 @@ export default class Card implements LocalCard {
|
|||||||
return objectLoop(params, (it, key) => {
|
return objectLoop(params, (it, key) => {
|
||||||
if (key === 'set' && typeof it === 'string') {
|
if (key === 'set' && typeof it === 'string') {
|
||||||
return (
|
return (
|
||||||
c['set'].id.toLowerCase().includes(it.toLowerCase()) ||
|
lightCheck(c['set'].id, it) || lightCheck(c['set'].name, it)
|
||||||
(c['set'].name ? c['set'].name.toLowerCase().includes(it.toLowerCase()) : false)
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
if (typeof it === "string") {
|
return lightCheck(c[key as 'localId'], it)
|
||||||
return c[key as 'localId'].toLowerCase().includes(it.toLowerCase())
|
|
||||||
}
|
|
||||||
return c[key as 'localId'].includes(it)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
if (!res) {
|
if (!res) {
|
||||||
|
@ -2,6 +2,7 @@ import { objectLoop } from '@dzeio/object-util'
|
|||||||
import { Serie as SDKSerie, SerieResume, SupportedLanguages } from '@tcgdex/sdk'
|
import { Serie as SDKSerie, SerieResume, SupportedLanguages } from '@tcgdex/sdk'
|
||||||
import Set from './Set'
|
import Set from './Set'
|
||||||
import { Pagination } from '../../interfaces'
|
import { Pagination } from '../../interfaces'
|
||||||
|
import { lightCheck } from '../../util'
|
||||||
|
|
||||||
type LocalSerie = Omit<SDKSerie, 'sets'> & {sets: () => Array<Set>}
|
type LocalSerie = Omit<SDKSerie, 'sets'> & {sets: () => Array<Set>}
|
||||||
|
|
||||||
@ -30,7 +31,7 @@ export default class Serie implements LocalSerie {
|
|||||||
public static find(lang: SupportedLanguages, params: Partial<Record<keyof SDKSerie, any>> = {}, pagination?: Pagination) {
|
public static find(lang: SupportedLanguages, params: Partial<Record<keyof SDKSerie, any>> = {}, pagination?: Pagination) {
|
||||||
let list = (require(`../../../generated/${lang}/series.json`) as Array<SDKSerie>)
|
let list = (require(`../../../generated/${lang}/series.json`) as Array<SDKSerie>)
|
||||||
.filter((c) => objectLoop(params, (it, key) => {
|
.filter((c) => objectLoop(params, (it, key) => {
|
||||||
return c[key as 'id'].includes(it)
|
return lightCheck(c[key as 'id'], it)
|
||||||
}))
|
}))
|
||||||
if (pagination) {
|
if (pagination) {
|
||||||
list = list
|
list = list
|
||||||
@ -42,12 +43,7 @@ export default class Serie implements LocalSerie {
|
|||||||
public static findOne(lang: SupportedLanguages, params: Partial<Record<keyof Serie, any>> = {}): Serie | undefined {
|
public static findOne(lang: SupportedLanguages, params: Partial<Record<keyof Serie, any>> = {}): Serie | undefined {
|
||||||
const res = (require(`../../../generated/${lang}/series.json`) as Array<SDKSerie>)
|
const res = (require(`../../../generated/${lang}/series.json`) as Array<SDKSerie>)
|
||||||
.find((c) => {
|
.find((c) => {
|
||||||
return objectLoop(params, (it, key) => {
|
return objectLoop(params, (it, key) => lightCheck(c[key as 'id'], it))
|
||||||
if (typeof it === 'string') {
|
|
||||||
return c[key as 'id'].toLowerCase().includes(it.toLowerCase())
|
|
||||||
}
|
|
||||||
return c[key as 'id'].includes(it)
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
if (!res) {
|
if (!res) {
|
||||||
return undefined
|
return undefined
|
||||||
|
@ -3,6 +3,7 @@ import { Set as SDKSet, SetResume, SupportedLanguages } from '@tcgdex/sdk'
|
|||||||
import Card from './Card'
|
import Card from './Card'
|
||||||
import { Pagination } from '../../interfaces'
|
import { Pagination } from '../../interfaces'
|
||||||
import Serie from './Serie'
|
import Serie from './Serie'
|
||||||
|
import { lightCheck } from '../../util'
|
||||||
|
|
||||||
interface variants {
|
interface variants {
|
||||||
normal?: boolean;
|
normal?: boolean;
|
||||||
@ -48,7 +49,8 @@ export default class Set implements LocalSet {
|
|||||||
public static find(lang: SupportedLanguages, params: Partial<Record<keyof SDKSet, any>> = {}, pagination?: Pagination) {
|
public static find(lang: SupportedLanguages, params: Partial<Record<keyof SDKSet, any>> = {}, pagination?: Pagination) {
|
||||||
let list = (require(`../../../generated/${lang}/sets.json`) as Array<SDKSet>)
|
let list = (require(`../../../generated/${lang}/sets.json`) as Array<SDKSet>)
|
||||||
.filter((c) => objectLoop(params, (it, key) => {
|
.filter((c) => objectLoop(params, (it, key) => {
|
||||||
return c[key as 'id'].includes(it)
|
|
||||||
|
return lightCheck(c[key as 'id'], it)
|
||||||
}))
|
}))
|
||||||
if (pagination) {
|
if (pagination) {
|
||||||
list = list
|
list = list
|
||||||
@ -65,7 +67,7 @@ export default class Set implements LocalSet {
|
|||||||
} else if (typeof it === 'string') {
|
} else if (typeof it === 'string') {
|
||||||
return c[key as 'id'].toLowerCase().includes(it.toLowerCase())
|
return c[key as 'id'].toLowerCase().includes(it.toLowerCase())
|
||||||
}
|
}
|
||||||
return c[key as 'id'].includes(it)
|
return lightCheck(c[key as 'id'], it)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
if (!res) {
|
if (!res) {
|
||||||
|
@ -163,6 +163,7 @@ server
|
|||||||
break
|
break
|
||||||
default:
|
default:
|
||||||
result = Card.find(lang, {[endpointToField[endpoint]]: id})
|
result = Card.find(lang, {[endpointToField[endpoint]]: id})
|
||||||
|
.map((c) => c.resume())
|
||||||
}
|
}
|
||||||
if (!result) {
|
if (!result) {
|
||||||
return res.status(404).send({error: "Endpoint or id not found"})
|
return res.status(404).send({error: "Endpoint or id not found"})
|
||||||
|
@ -55,3 +55,26 @@ export function tree(path: string, padding = 0) {
|
|||||||
} catch {}
|
} catch {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function lightCheck(source: any, item: any): boolean {
|
||||||
|
if (typeof source === 'undefined') {
|
||||||
|
return typeof item === 'undefined'
|
||||||
|
}
|
||||||
|
if (Array.isArray(source)) {
|
||||||
|
for (const sub of source) {
|
||||||
|
const res = lightCheck(sub, item)
|
||||||
|
if (res) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if (typeof source === 'object') {
|
||||||
|
return lightCheck(source[item], true)
|
||||||
|
} else if (typeof source === 'string') {
|
||||||
|
return source.toLowerCase().includes(item.toString().toLowerCase())
|
||||||
|
} else {
|
||||||
|
// console.log(source, item)
|
||||||
|
return source === item
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user