aboutsummaryrefslogtreecommitdiff
path: root/src/lib/Data/Birthday/secondary.ts
blob: 4b3880e69d032615e30cd6b63ff67b7d1c6fc782 (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
import root from "$lib/Utility/root";

export interface ACDBBirthday {
	character_image: string;
	name: string;
	origin: string;
}

const isACDBBirthday = (entry: unknown): entry is ACDBBirthday =>
	typeof entry === "object" &&
	entry !== null &&
	typeof (entry as { character_image?: unknown }).character_image ===
		"string" &&
	typeof (entry as { name?: unknown }).name === "string" &&
	typeof (entry as { origin?: unknown }).origin === "string";

export const ACDBBirthdays = async (
	month: number,
	day: number,
): Promise<ACDBBirthday[]> => {
	const response = await fetch(
		root(`/api/birthdays/secondary?month=${month}&day=${day}`),
		{
			headers: {
				Accept: "application/json",
				"Content-Type": "application/json",
				"User-Agent":
					"Mozilla/5.0 (X11; Linux x86_64; rv:120.0) Gecko/20100101 Firefox/120.0",
			},
		},
	);

	if (!response.ok)
		throw new Error(
			`Secondary birthdays request failed with ${response.status}.`,
		);

	const data: unknown = await response.json();

	if (
		typeof data !== "object" ||
		data === null ||
		!Array.isArray((data as { characters?: unknown }).characters)
	)
		throw new Error(
			"Secondary birthdays response did not include a characters array.",
		);

	return (data as { characters: unknown[] }).characters.filter(isACDBBirthday);
};