Spaces:
Sleeping
Sleeping
File size: 1,107 Bytes
2c00ea8 |
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 40 41 42 |
import { env } from "$env/dynamic/private";
import { isURL } from "$lib/utils/isUrl";
import type { WebSearchSource } from "$lib/types/WebSearch";
interface YouWebSearch {
hits: YouSearchHit[];
latency: number;
}
interface YouSearchHit {
url: string;
title: string;
description: string;
snippets: string[];
}
export default async function searchWebYouApi(query: string): Promise<WebSearchSource[]> {
const response = await fetch(`https://api.ydc-index.io/search?query=${query}`, {
method: "GET",
headers: {
"X-API-Key": env.YDC_API_KEY,
"Content-type": "application/json; charset=UTF-8",
},
});
if (!response.ok) {
throw new Error(`You.com API returned error code ${response.status} - ${response.statusText}`);
}
const data = (await response.json()) as YouWebSearch;
const formattedResultsWithSnippets = data.hits
.filter(({ url }) => isURL(url))
.map(({ title, url, snippets }) => ({
title,
link: url,
text: snippets?.join("\n") || "",
}))
.sort((a, b) => b.text.length - a.text.length); // desc order by text length
return formattedResultsWithSnippets;
}
|