-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for SearchApi API (#1196)
* Add SearchApi integration * Remove isURL
- Loading branch information
1 parent
674cc15
commit 7f44ba0
Showing
6 changed files
with
33 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { env } from "$env/dynamic/private"; | ||
import type { WebSearchSource } from "$lib/types/WebSearch"; | ||
|
||
export default async function search(query: string): Promise<WebSearchSource[]> { | ||
const response = await fetch( | ||
`https://www.searchapi.io/api/v1/search?engine=google&hl=en&gl=us&q=${query}`, | ||
{ | ||
method: "GET", | ||
headers: { | ||
Authorization: `Bearer ${env.SEARCHAPI_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"] ?? `SearchApi returned error code ${response.status} - ${response.statusText}` | ||
); | ||
} | ||
|
||
return data["organic_results"] ?? []; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters