Edits history of script submission #11409 for ' Timezone (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
    	}
    ) {
    	const url = new URL(`http://api.weatherapi.com/v1/timezone.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