Spaces:
Sleeping
Sleeping
File size: 1,125 Bytes
037df62 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import type { WebSearchSource } from "$lib/types/WebSearch";
import { env } from "$env/dynamic/private";
export default async function search(query: string): Promise<WebSearchSource[]> {
// const params = {
// q: query,
// // You can add other parameters if needed, like 'count', 'offset', etc.
// };
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",
},
}
);
/* eslint-disable @typescript-eslint/no-explicit-any */
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}`
);
}
// Adapt the data structure from the Bing response to match the WebSearchSource type
const webPages = data["webPages"]?.["value"] ?? [];
return webPages.map((page: any) => ({
title: page.name,
link: page.url,
text: page.snippet,
displayLink: page.displayUrl,
}));
}
|