aboutsummaryrefslogtreecommitdiff
path: root/src/lib/Data/Birthday/primary.ts
blob: 5f8ee1d0973a5a498622ef0ec9fe4b6ec7a7d681 (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
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);
};