aboutsummaryrefslogtreecommitdiff
path: root/src/lib/AniList/character.ts
blob: 27caef9992c90c820154247dce12185cb48e1bec (plain) (blame)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
export interface Character {
	name: {
		full: string;
	};
	id: number;
}

export interface CharactersPage {
	data: {
		Page: {
			characters: Character[];
			pageInfo: {
				hasNextPage: boolean;
				currentPage: number;
			};
		};
	};
}

const charactersPage = async (page: number): Promise<CharactersPage> =>
	await (
		await fetch('https://graphql.anilist.co', {
			method: 'POST',
			headers: {
				'Content-Type': 'application/json',
				Accept: 'application/json'
			},
			body: JSON.stringify({
				query: `{ Page(page: ${page}, perPage: 50) {
				characters(isBirthday: true) { name { full } id }
				pageInfo { hasNextPage currentPage }
			} }`
			})
		})
	).json();

export const todaysCharacterBirthdays = async (): Promise<Character[]> => {
	const characters = [];
	let page = 1;
	let currentPage = await charactersPage(page);

	for (const character of currentPage['data']['Page']['characters']) {
		characters.push({
			id: character['id'],
			name: {
				full: character['name']['full']
			}
		});
	}

	while (currentPage['data']['Page']['pageInfo']['hasNextPage']) {
		for (const character of currentPage['data']['Page']['characters']) {
			characters.push({
				id: character['id'],
				name: {
					full: character['name']['full']
				}
			});
		}

		page += 1;
		currentPage = await charactersPage(page);
	}

	return characters;
};