| import type { WebSearchSource } from "$lib/types/WebSearch"; |
| import { env } from "$env/dynamic/private"; |
|
|
| export default async function search(query: string): Promise<WebSearchSource[]> { |
| |
| |
| |
| |
|
|
| const response = await fetch( |
| "https://api.bing.microsoft.com/v7.0/search" + "?q=" + encodeURIComponent(query), |
| { |
| method: "GET", |
| headers: { |
| "Ocp-Apim-Subscription-Key": env.BING_SUBSCRIPTION_KEY, |
| "Content-type": "application/json", |
| }, |
| } |
| ); |
|
|
| |
| const data = (await response.json()) as Record<string, any>; |
|
|
| if (!response.ok) { |
| throw new Error( |
| data["message"] ?? `Bing API returned error code ${response.status} - ${response.statusText}` |
| ); |
| } |
|
|
| |
| const webPages = data["webPages"]?.["value"] ?? []; |
| return webPages.map((page: any) => ({ |
| title: page.name, |
| link: page.url, |
| text: page.snippet, |
| displayLink: page.displayUrl, |
| })); |
| } |
|
|