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