-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.ts
51 lines (49 loc) · 1.25 KB
/
api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { API_KEY } from '@env';
import { CardSet, CardSetHash } from './types/tcgTypes';
/*
* retrieve set data and parse into type
* @return { title: string, data: any[] }[]
*/
export const getAllSets = async () => {
const response = await fetch('https://api.pokemontcg.io/v2/sets', {
method: 'GET',
headers: {
'X-Api-Key': `${API_KEY}`,
'Content-Type': 'application/json',
},
});
const result = await response.json();
let data = await result.data;
let sectionListData: CardSetHash = await data.reduce(
(obj: CardSetHash, set: CardSet) => {
return {
...obj,
[set.series]: [...(obj[set.series] || []), set],
};
},
{},
);
return Object.keys(sectionListData).map((series) => ({
title: series,
data: [sectionListData[series]],
}));
};
/*
* retrieve card data from a given set
* @return Card[]
*/
export const getAllCards = async (setID: string) => {
const response = await fetch(
`https://api.pokemontcg.io/v2/cards?q=set.id:${setID}`,
{
method: 'GET',
headers: {
'X-Api-Key': `${API_KEY}`,
'Content-Type': 'application/json',
},
},
);
const result = await response.json();
let data = await result.data; // Card[]
return data;
};