//native
type Newsapi = {
apiKey: string
}
export async function main(
resource: Newsapi,
params: {
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'
category?:
| 'business'
| 'entertainment'
| 'general'
| 'health'
| 'science'
| 'sports'
| 'technology'
sources?: string
q?: string
pageSize?: number // max 100
page?: number
}
) {
const url = new URL('https://newsapi.org/v2/top-headlines')
// 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 && value !== '') {
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