From 227637fd979d96ac9a0ac85c79543577974100a5 Mon Sep 17 00:00:00 2001 From: Avior Date: Mon, 22 Nov 2021 14:46:03 +0100 Subject: [PATCH] fix: Misc Endpoints not working as intended (#181) Signed-off-by: Avior --- .vscode/settings.json | 5 +++++ server/src/V2/Components/Card.ts | 14 ++++---------- server/src/V2/Components/Serie.ts | 10 +++------- server/src/V2/Components/Set.ts | 6 ++++-- server/src/V2/endpoints/jsonEndpoints.ts | 1 + server/src/util.ts | 23 +++++++++++++++++++++++ 6 files changed, 40 insertions(+), 19 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..0b7a1d25f --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.exclude": { + "**/*.js": true + } +} diff --git a/server/src/V2/Components/Card.ts b/server/src/V2/Components/Card.ts index 92ef16fe9..35119e89f 100644 --- a/server/src/V2/Components/Card.ts +++ b/server/src/V2/Components/Card.ts @@ -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 & {set: () => Set} @@ -62,10 +63,7 @@ export default class Card implements LocalCard { public static find(lang: SupportedLanguages, params: Partial> = {}, pagination?: Pagination) { let list : Array = (require(`../../../generated/${lang}/cards.json`) as Array) .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) { diff --git a/server/src/V2/Components/Serie.ts b/server/src/V2/Components/Serie.ts index 8b092033e..0e9a75421 100644 --- a/server/src/V2/Components/Serie.ts +++ b/server/src/V2/Components/Serie.ts @@ -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 & {sets: () => Array} @@ -30,7 +31,7 @@ export default class Serie implements LocalSerie { public static find(lang: SupportedLanguages, params: Partial> = {}, pagination?: Pagination) { let list = (require(`../../../generated/${lang}/series.json`) as Array) .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> = {}): Serie | undefined { const res = (require(`../../../generated/${lang}/series.json`) as Array) .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 diff --git a/server/src/V2/Components/Set.ts b/server/src/V2/Components/Set.ts index cb3c77c72..1a66522c6 100644 --- a/server/src/V2/Components/Set.ts +++ b/server/src/V2/Components/Set.ts @@ -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> = {}, pagination?: Pagination) { let list = (require(`../../../generated/${lang}/sets.json`) as Array) .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) { diff --git a/server/src/V2/endpoints/jsonEndpoints.ts b/server/src/V2/endpoints/jsonEndpoints.ts index 2097100d0..27a107775 100644 --- a/server/src/V2/endpoints/jsonEndpoints.ts +++ b/server/src/V2/endpoints/jsonEndpoints.ts @@ -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"}) diff --git a/server/src/util.ts b/server/src/util.ts index d60394a27..ea53694a0 100644 --- a/server/src/util.ts +++ b/server/src/util.ts @@ -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 + } +}