Some checks failed
Monorepo Pipeline / ⚡ Prioritize Release (push) Successful in 2s
Monorepo Pipeline / 🧪 Test (push) Failing after 51s
Monorepo Pipeline / 🧹 Lint (push) Failing after 2m25s
Monorepo Pipeline / 🏗️ Build (push) Successful in 2m28s
Monorepo Pipeline / 🚀 Release (push) Has been skipped
Monorepo Pipeline / 🐳 Build Gatekeeper (Product) (push) Has been skipped
Monorepo Pipeline / 🐳 Build Build-Base (push) Has been skipped
Monorepo Pipeline / 🐳 Build Production Runtime (push) Has been skipped
65 lines
1.6 KiB
TypeScript
65 lines
1.6 KiB
TypeScript
import axios from "axios";
|
|
|
|
export interface SerperResult {
|
|
relatedSearches: string[];
|
|
peopleAlsoAsk: string[];
|
|
organicSnippets: string[];
|
|
estimatedTotalResults: number;
|
|
}
|
|
|
|
/**
|
|
* Fetch Google search data via Serper's /search endpoint.
|
|
* Extracts related searches, People Also Ask, organic snippets,
|
|
* and totalResults as a search volume proxy.
|
|
*/
|
|
export async function fetchSerperData(
|
|
query: string,
|
|
apiKey: string,
|
|
locale: { gl: string; hl: string } = { gl: "de", hl: "de" },
|
|
): Promise<SerperResult> {
|
|
try {
|
|
const response = await axios.post(
|
|
"https://google.serper.dev/search",
|
|
{
|
|
q: query,
|
|
gl: locale.gl,
|
|
hl: locale.hl,
|
|
},
|
|
{
|
|
headers: {
|
|
"X-API-KEY": apiKey,
|
|
"Content-Type": "application/json",
|
|
},
|
|
},
|
|
);
|
|
|
|
const data = response.data;
|
|
|
|
const relatedSearches =
|
|
data.relatedSearches?.map((r: any) => r.query) || [];
|
|
const peopleAlsoAsk = data.peopleAlsoAsk?.map((p: any) => p.question) || [];
|
|
const organicSnippets = data.organic?.map((o: any) => o.snippet) || [];
|
|
const estimatedTotalResults = data.searchInformation?.totalResults
|
|
? parseInt(data.searchInformation.totalResults, 10)
|
|
: 0;
|
|
|
|
return {
|
|
relatedSearches,
|
|
peopleAlsoAsk,
|
|
organicSnippets,
|
|
estimatedTotalResults,
|
|
};
|
|
} catch (error) {
|
|
console.error(
|
|
`Serper API error for query "${query}":`,
|
|
(error as Error).message,
|
|
);
|
|
return {
|
|
relatedSearches: [],
|
|
peopleAlsoAsk: [],
|
|
organicSnippets: [],
|
|
estimatedTotalResults: 0,
|
|
};
|
|
}
|
|
}
|