0
Get Public Holidays
One script reply has been approved by the moderators Verified

Get the public, local, religious, and other holidays of a particular country.

Created by hugo697 56 days ago Viewed 13 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 56 days ago
1
type Abstractapi = {
2
	apiKey: string
3
}
4

5
export async function main(
6
	resource: Abstractapi,
7
	countryCode: string,
8
	filter?: {
9
		year?: number
10
		month?: number
11
		day?: number
12
	}
13
) {
14
	const queryParams = new URLSearchParams({
15
		api_key: resource.apiKey,
16
		country: countryCode
17
	})
18

19
	if (filter?.year) {
20
		queryParams.append('year', filter.year.toString())
21
	}
22

23
	if (filter?.month) {
24
		queryParams.append('month', filter.month.toString())
25
	}
26

27
	if (filter?.day) {
28
		queryParams.append('day', filter.day.toString())
29
	}
30

31
	const endpoint = `https://holidays.abstractapi.com/v1?${queryParams.toString()}`
32

33
	const response = await fetch(endpoint, {
34
		method: 'GET'
35
	})
36

37
	if (!response.ok) {
38
		throw new Error(`HTTP error! status: ${response.status}`)
39
	}
40

41
	const data = await response.json()
42

43
	return data
44
}
45