Edits history of script submission #11410 for ' Weather Forecast (weatherapi)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Weatherapi = {
    	apiKey: string
    }
    
    export async function main(
    	auth: Weatherapi,
    	params: {
    		q: string
    		days: number
    		dt?: string
    		unixdt?: number
    		hour?: number
    		alerts?: 'yes' | 'no'
    		aqi?: 'yes' | 'no'
    		tp?: string
    		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/forecast.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 537 days ago