It is unknown whether this package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
JSR Score
29%
Published
a month ago (0.0.3)
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879import { z, ZodType } from "npm:zod@3.23.8"; import { getRapidApiClient } from "./utils.ts"; export interface GoogleKeywordInsightKeywordData { text: string; volume: number; competition_level: "LOW" | "MEDIUM" | "HIGH" | "UNSPECIFIED"; competition_index: number; low_bid: number; high_bid: number; trend: number; } export const GoogleKeywordInsightKeywordDataSchema: ZodType<GoogleKeywordInsightKeywordData> = z.object({ text: z.string().describe("The keyword"), volume: z.number().describe("The volume of the keyword"), competition_level: z .enum(["LOW", "MEDIUM", "HIGH", "UNSPECIFIED"]) .describe("The competition level of the keyword"), competition_index: z .number() .describe("The competition index of the keyword"), low_bid: z.number().describe("The low bid of the keyword"), high_bid: z.number().describe("The high bid of the keyword"), trend: z.number().describe("The trend of the keyword"), }); function getHostname(url: string) { return new URL(url).hostname; } export const getGoogleKeywordInsightClient = (kv: Deno.Kv) => ({ globalurl: async ( websiteUrl: string ): Promise<GoogleKeywordInsightKeywordData[]> => { const hostname = getHostname(websiteUrl); const cachedKeywords = await kv.get([ "google-keyword-insight1", "globalurl", hostname, ]); if (cachedKeywords.value) { return ( cachedKeywords.value as { data: GoogleKeywordInsightKeywordData[] } ).data; } const apiClient = getRapidApiClient("google-keyword-insight1"); const response = await apiClient.get<GoogleKeywordInsightKeywordData[]>( "/globalurl", { params: { url: hostname }, } ); // Only getting top 200 volumes as kv value size limit const keywords = response.data .sort((a, b) => b.volume - a.volume) .slice(0, 200); await kv.set(["google-keyword-insight1", "globalurl", hostname], { data: keywords, }); return keywords; }, // globalurl: (websiteUrl: string) => // getCachedRequest<GoogleKeywordInsightKeywordData[]>( // kv, // "google-keyword-insight1", // "/globalurl", // { url: getHostname(websiteUrl) } // ), }); export interface GoogleKeywordInsightClient { globalurl: (websiteUrl: string) => Promise<GoogleKeywordInsightKeywordData[]>; }