feat: Allow a user to filters elements on the API (#275)

This commit is contained in:
2024-10-10 14:56:11 +02:00
committed by GitHub
parent f2621890e1
commit bf54ab3809
28 changed files with 1429 additions and 126 deletions

26
src/endpoints/Endpoint.ts Normal file
View File

@ -0,0 +1,26 @@
import type { Endpoints } from '../interfaces'
import Model from '../models/Model'
import type Query from '../Query'
import type TCGdex from '../tcgdex'
export default class Endpoint<Item extends Model, List extends Model> {
public constructor(
protected readonly tcgdex: TCGdex,
protected readonly itemModel: new (sdk: TCGdex) => Item,
protected readonly listModel: new (sdk: TCGdex) => List,
protected readonly endpoint: Endpoints
) { }
public async get(id: string | number): Promise<Item | null> {
const res = await this.tcgdex.fetch(this.endpoint as 'cards', id as string)
if (!res) {
return null
}
return Model.build(new this.itemModel(this.tcgdex), res)
}
public async list(query?: Query): Promise<Array<List>> {
const res = await this.tcgdex.fetchWithQuery([this.endpoint], query?.params)
return (res as Array<object> ?? []).map((it) => Model.build(new this.listModel(this.tcgdex), it))
}
}