//native
type Weatherapi = {
apiKey: string
}
export async function main(
auth: Weatherapi,
params: {
q: string
days: number
dt?: string
unixdt?: number
hour?: number
lang?:
| 'ar'
| 'bn'
| 'bg'
| 'zh'
| 'zh_tw'
| 'cs'
| 'da'
| 'nl'
| 'fi'
| 'fr'
| 'de'
| 'el'
| 'hi'
| 'hu'
| 'it'
| 'ja'
| 'jv'
| 'ko'
| 'zh_cmn'
| 'mr'
| 'pl'
| 'pt'
| 'pa'
| 'ro'
| 'ru'
| 'sr'
| 'si'
| 'sk'
| 'es'
| 'sv'
| 'ta'
| 'te'
| 'tr'
| 'uk'
| 'ur'
| 'vi'
| 'zh_wuu'
| 'zh_hsn'
| 'zh_yue'
| 'zu'
}
) {
const url = new URL(`http://api.weatherapi.com/v1/marine.json`)
// Append the API key for authentication
url.searchParams.append('key', auth.apiKey)
// Append the query parameters if provided
for (const [key, value] of Object.entries(params)) {
if (value !== undefined && value !== '') {
url.searchParams.append(key, value.toString())
}
}
const response = await fetch(url.toString(), {
method: 'GET'
})
if (!response.ok) {
const errorText = await response.text()
throw new Error(
`WeatherAPI request failed: ${response.status} ${response.statusText} - ${errorText}`
)
}
return await response.json()
}
Submitted by hugo697 536 days ago