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