Edits history of script submission #11198 for ' List Headline Sources (newsapi)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Newsapi = {
    	apiKey: string
    }
    
    export async function main(
    	resource: Newsapi,
    	params?: {
    		category?:
    			| 'business'
    			| 'entertainment'
    			| 'general'
    			| 'health'
    			| 'science'
    			| 'sports'
    			| 'technology'
    		country?:
    			| 'ae'
    			| 'ar'
    			| 'at'
    			| 'au'
    			| 'be'
    			| 'bg'
    			| 'br'
    			| 'ca'
    			| 'ch'
    			| 'cn'
    			| 'co'
    			| 'cu'
    			| 'cz'
    			| 'de'
    			| 'eg'
    			| 'fr'
    			| 'gb'
    			| 'gr'
    			| 'hk'
    			| 'hu'
    			| 'id'
    			| 'ie'
    			| 'il'
    			| 'in'
    			| 'it'
    			| 'jp'
    			| 'kr'
    			| 'lt'
    			| 'lv'
    			| 'ma'
    			| 'mx'
    			| 'my'
    			| 'ng'
    			| 'nl'
    			| 'no'
    			| 'nz'
    			| 'ph'
    			| 'pl'
    			| 'pt'
    			| 'ro'
    			| 'rs'
    			| 'ru'
    			| 'sa'
    			| 'se'
    			| 'sg'
    			| 'si'
    			| 'sk'
    			| 'th'
    			| 'tr'
    			| 'tw'
    			| 'ua'
    			| 'us'
    			| 've'
    			| 'za'
    		language?:
    			| 'ar'
    			| 'de'
    			| 'en'
    			| 'es'
    			| 'fr'
    			| 'he'
    			| 'it'
    			| 'nl'
    			| 'no'
    			| 'pt'
    			| 'ru'
    			| 'se'
    			| 'ud'
    			| 'zh'
    	}
    ) {
    	const url = new URL('https://newsapi.org/v2/top-headlines/sources')
    
    	// Append the API key for authentication
    	url.searchParams.append('apiKey', resource.apiKey)
    
    	// Append the query parameters if provided
    	for (const [key, value] of Object.entries(params || {})) {
    		if (value !== undefined) {
    			url.searchParams.append(key, value.toString())
    		}
    	}
    
    	const response = await fetch(url.toString(), {
    		method: 'GET',
    		headers: {
    			accept: 'application/json'
    		}
    	})
    
    	if (!response.ok) {
    		const error = await response.text()
    		console.error('Error:', error)
    		throw new Error(`HTTP error! status: ${response.status}`)
    	}
    
    	const data = await response.json()
    
    	return data
    }
    

    Submitted by hugo697 536 days ago