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

export interface aniSearchBirthday {
	name: string;
	image: string;
}

const isAniSearchBirthday = (entry: unknown): entry is aniSearchBirthday =>
	typeof entry === "object" &&
	entry !== null &&
	typeof (entry as { name?: unknown }).name === "string" &&
	typeof (entry as { image?: unknown }).image === "string";

export const aniSearchBirthdays = async (
	month: number,
	day: number,
): Promise<aniSearchBirthday[]> => {
	const response = await fetch(
		root(`/api/birthdays/primary?month=${month}&day=${day}`),
		{},
	);

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

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

	if (!Array.isArray(data))
		throw new Error("Primary birthdays response was not an array.");

	return data.filter(isAniSearchBirthday);
};