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();
|
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;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user