1
0
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:
Florian Bouillon 2021-11-22 14:46:03 +01:00 committed by GitHub
parent ac3a62c0ec
commit 227637fd97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 40 additions and 19 deletions

5
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,5 @@
{
"files.exclude": {
"**/*.js": true
}
}

View File

@ -2,6 +2,7 @@ import { objectLoop } from '@dzeio/object-util'
import { Card as SDKCard, CardResume, SupportedLanguages } from '@tcgdex/sdk'
import Set from './Set'
import { Pagination } from '../../interfaces'
import { lightCheck } from '../../util'
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) {
let list : Array<SDKCard> = (require(`../../../generated/${lang}/cards.json`) as Array<SDKCard>)
.filter((c) => objectLoop(params, (it, key) => {
if (typeof it === "string") {
return c[key as 'localId'].toLowerCase().includes(it.toLowerCase())
}
return c[key as 'localId'].includes(it)
return lightCheck(c[key as 'localId'], it)
}))
if (pagination) {
list = list
@ -83,14 +81,10 @@ export default class Card implements LocalCard {
return objectLoop(params, (it, key) => {
if (key === 'set' && typeof it === 'string') {
return (
c['set'].id.toLowerCase().includes(it.toLowerCase()) ||
(c['set'].name ? c['set'].name.toLowerCase().includes(it.toLowerCase()) : false)
lightCheck(c['set'].id, it) || lightCheck(c['set'].name, it)
)
}
if (typeof it === "string") {
return c[key as 'localId'].toLowerCase().includes(it.toLowerCase())
}
return c[key as 'localId'].includes(it)
return lightCheck(c[key as 'localId'], it)
})
})
if (!res) {

View File

@ -2,6 +2,7 @@ import { objectLoop } from '@dzeio/object-util'
import { Serie as SDKSerie, SerieResume, SupportedLanguages } from '@tcgdex/sdk'
import Set from './Set'
import { Pagination } from '../../interfaces'
import { lightCheck } from '../../util'
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) {
let list = (require(`../../../generated/${lang}/series.json`) as Array<SDKSerie>)
.filter((c) => objectLoop(params, (it, key) => {
return c[key as 'id'].includes(it)
return lightCheck(c[key as 'id'], it)
}))
if (pagination) {
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 {
const res = (require(`../../../generated/${lang}/series.json`) as Array<SDKSerie>)
.find((c) => {
return objectLoop(params, (it, key) => {
if (typeof it === 'string') {
return c[key as 'id'].toLowerCase().includes(it.toLowerCase())
}
return c[key as 'id'].includes(it)
})
return objectLoop(params, (it, key) => lightCheck(c[key as 'id'], it))
})
if (!res) {
return undefined

View File

@ -3,6 +3,7 @@ import { Set as SDKSet, SetResume, SupportedLanguages } from '@tcgdex/sdk'
import Card from './Card'
import { Pagination } from '../../interfaces'
import Serie from './Serie'
import { lightCheck } from '../../util'
interface variants {
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) {
let list = (require(`../../../generated/${lang}/sets.json`) as Array<SDKSet>)
.filter((c) => objectLoop(params, (it, key) => {
return c[key as 'id'].includes(it)
return lightCheck(c[key as 'id'], it)
}))
if (pagination) {
list = list
@ -65,7 +67,7 @@ export default class Set implements LocalSet {
} else if (typeof it === 'string') {
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) {

View File

@ -163,6 +163,7 @@ server
break
default:
result = Card.find(lang, {[endpointToField[endpoint]]: id})
.map((c) => c.resume())
}
if (!result) {
return res.status(404).send({error: "Endpoint or id not found"})

View File

@ -55,3 +55,26 @@ export function tree(path: string, padding = 0) {
} 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
}
}