add chunk to get cards collection from scryfall
This commit is contained in:
@@ -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;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user