add chunk to get cards collection from scryfall

This commit is contained in:
Reynier Matthieu
2025-03-06 15:49:44 +01:00
parent 2ffa49b8f0
commit a077c40c5a
36 changed files with 3068 additions and 3055 deletions

View File

@@ -23,19 +23,32 @@ export const getCardById = async (cardId: string): Promise<Card> => {
return await response.json();
};
export const getCardsByIds = async (cardIds: string[]): Promise<Card[]> => {
const chunkArray = (array: string[], size: number): string[][] => {
const chunkedArray: string[][] = [];
for (let i = 0; i < array.length; i += size) {
chunkedArray.push(array.slice(i, i + size));
}
return chunkedArray;
};
//75 cards per request max
export const getCardsByIds = async (cardIds: string[]): Promise<Card[]> => {
const chunkedCardIds = chunkArray(cardIds, 75);
let allCards: Card[] = [];
for (const chunk of chunkedCardIds) {
const response = await fetch(`${SCRYFALL_API}/cards/collection`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
identifiers: cardIds.map((id) => ({ id })),
identifiers: chunk.map((id) => ({ id })),
}),
});
const data = await response.json();
return data.data;
allCards = allCards.concat(data.data);
}
return allCards;
};