0

Astronomy

by
Published Dec 20, 2024

Returns location and astronomy object

Script weatherapi Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Weatherapi = {
3
	apiKey: string
4
}
5

6
export async function main(
7
	auth: Weatherapi,
8
	params: {
9
		q: string
10
		dt: string
11
	}
12
) {
13
	const url = new URL(`http://api.weatherapi.com/v1/astronomy.json`)
14

15
	// Append the API key for authentication
16
	url.searchParams.append('key', auth.apiKey)
17

18
	// Append the query parameters if provided
19
	for (const [key, value] of Object.entries(params)) {
20
		if (value !== undefined && value !== '') {
21
			url.searchParams.append(key, value.toString())
22
		}
23
	}
24

25
	const response = await fetch(url.toString(), {
26
		method: 'GET'
27
	})
28

29
	if (!response.ok) {
30
		const errorText = await response.text()
31
		throw new Error(
32
			`WeatherAPI request failed: ${response.status} ${response.statusText} - ${errorText}`
33
		)
34
	}
35

36
	return await response.json()
37
}
38