Buckets:
| export type SerperResult = { | |
| organic?: Array<{ title: string; link: string; snippet: string }>; | |
| }; | |
| const PERIOD_MAP: Record<string, string> = { | |
| day: "qdr:d", | |
| week: "qdr:w", | |
| month: "qdr:m", | |
| year: "qdr:y", | |
| }; | |
| export async function serperSearch( | |
| query: string, | |
| opts: { num?: number; period?: string } = {}, | |
| ): Promise<SerperResult> { | |
| const apiKey = process.env.SERPER_API_KEY; | |
| if (!apiKey) { | |
| throw new Error( | |
| "SERPER_API_KEY is not set. Get a free key at https://serper.dev (2500 free queries).", | |
| ); | |
| } | |
| const tbs = opts.period ? PERIOD_MAP[opts.period] : undefined; | |
| const response = await fetch("https://google.serper.dev/search", { | |
| method: "POST", | |
| headers: { | |
| "X-API-KEY": apiKey, | |
| "Content-Type": "application/json", | |
| }, | |
| body: JSON.stringify({ | |
| q: query, | |
| num: opts.num ?? 20, | |
| ...(tbs && { tbs }), | |
| }), | |
| }); | |
| if (!response.ok) { | |
| const body = await response.text().catch(() => ""); | |
| throw new Error(`Serper error ${response.status}: ${body}`); | |
| } | |
| return response.json(); | |
| } | |
Xet Storage Details
- Size:
- 1.07 kB
- Xet hash:
- b284fa894584e53c92dbc180dcdcbaee7398f2d89022530f18f02e7f026c48ab
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.