import {z} from 'zod' export type GetApiV1GoogleTrendsSearchArgs = {q: string, data_type?: ('TIMESERIES' | 'GEO_MAP' | 'GEO_MAP_0' | 'RELATED_TOPICS' | 'RELATED_QUERIES') | undefined, geo?: string | undefined, date?: string | undefined, cat?: number | undefined, gprop?: string | undefined, region?: (string | null) | undefined, language?: (string | null) | undefined, tz?: string | undefined}; export type GetApiV1GoogleTrendsSearchOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const trendsTimelineValue = z.object({query: z.string(), value: z.number().int(), extracted_value: z.number().int().nullable().optional()}); export const trendsTimelinePoint = z.object({date: z.string(), timestamp: z.number().int().nullable().optional(), values: z.array(trendsTimelineValue).optional()}); export const trendsRegionInterest = z.object({region: z.string(), region_code: z.string().nullable().optional(), value: z.number().int().optional(), max_value_index: z.number().int().optional()}); export const trendsRelatedItem = z.object({title: z.string(), value: z.string().nullable().optional(), link: z.string().nullable().optional(), topic_id: z.string().nullable().optional(), topic_type: z.string().nullable().optional()}); export const trendsSearchResponse = z.object({query: z.string(), data_type: z.string(), timeline: z.array(trendsTimelinePoint).optional(), averages: z.array(trendsTimelineValue).optional(), regions: z.array(trendsRegionInterest).optional(), top_topics: z.array(trendsRelatedItem).optional(), rising_topics: z.array(trendsRelatedItem).optional(), top_queries: z.array(trendsRelatedItem).optional(), rising_queries: z.array(trendsRelatedItem).optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Google Trends — unified search * * GET /v1/google/trends/search */ export const getApiV1GoogleTrendsSearch = async (args: GetApiV1GoogleTrendsSearchArgs, options?: GetApiV1GoogleTrendsSearchOptions) => { const {baseUrl = "https://scrapebadger.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.q != null) query.append("q", String(args.q)) if (args.data_type != null) query.append("data_type", String(args.data_type)) if (args.geo != null) query.append("geo", String(args.geo)) if (args.date != null) query.append("date", String(args.date)) if (args.cat != null) query.append("cat", String(args.cat)) if (args.gprop != null) query.append("gprop", String(args.gprop)) if (args.region != null) query.append("region", String(args.region)) if (args.language != null) query.append("language", String(args.language)) if (args.tz != null) query.append("tz", String(args.tz)) if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/google/trends/search${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(trendsSearchResponse, await res.json()) };