mirror of
https://github.com/tcgdex/compiler.git
synced 2025-04-22 02:32:10 +00:00
39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import { StringEndpointList, StringEndpoint } from '@tcgdex/sdk/interfaces'
|
|
import { Card, Languages } from '../db/interfaces'
|
|
import { Endpoint } from '../interfaces'
|
|
import { cardToCardSimple, getCards } from '../utils/cardUtil'
|
|
|
|
export default class implements Endpoint<StringEndpointList, StringEndpoint, {}, Record<string, Array<[string, Card]>>> {
|
|
public constructor(
|
|
private lang: keyof Languages
|
|
) {}
|
|
|
|
public async index(common: Record<string, Array<[string, Card]>>) {
|
|
return Object.keys(common)
|
|
}
|
|
|
|
public async item(common: Record<string, Array<[string, Card]>>) {
|
|
const items: Record<string, StringEndpoint> = {}
|
|
for (const key of Object.keys(common)) {
|
|
const val = common[key]
|
|
items[key] = {
|
|
name: key,
|
|
cards: await Promise.all(val.map(([id, card]) => cardToCardSimple(id, card, this.lang)))
|
|
}
|
|
}
|
|
return items
|
|
}
|
|
|
|
public async common() {
|
|
return (await getCards(this.lang)).reduce((p, c) => {
|
|
const retreat = c[1].retreat
|
|
if (!retreat) return p
|
|
if (!p[retreat]) {
|
|
p[retreat] = []
|
|
}
|
|
p[retreat].push(c)
|
|
return p
|
|
}, {} as Record<number, Array<[string, Card]>>)
|
|
}
|
|
}
|