import {z} from 'zod' export type GetApiV1GoogleProductsDetailArgs = {product_id: string, q: string, gl?: (string | null) | undefined, hl?: string | undefined, domain?: string | undefined, include_offers?: boolean | undefined, include_variants?: boolean | undefined, resolve_deep_urls?: boolean | undefined}; export type GetApiV1GoogleProductsDetailOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const productSeller = z.object({name: z.string(), price: z.string().nullable().optional(), link: z.string().nullable().optional(), shipping: z.string().nullable().optional()}); export const productDetailResponse = z.object({title: z.string().nullable().optional(), description: z.string().nullable().optional(), brand: z.string().nullable().optional(), rating: z.number().nullable().optional(), reviews_count: z.number().int().nullable().optional(), price_range: z.string().nullable().optional(), sellers: z.array(productSeller).optional(), specs: z.record(z.string(), z.string()).optional(), images: z.array(z.string()).optional(), product_id: z.string().nullable().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 immersive product detail * * GET /v1/google/products/detail */ export const getApiV1GoogleProductsDetail = async (args: GetApiV1GoogleProductsDetailArgs, options?: GetApiV1GoogleProductsDetailOptions) => { 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.product_id != null) query.append("product_id", String(args.product_id)) if (args.q != null) query.append("q", String(args.q)) if (args.gl != null) query.append("gl", String(args.gl)) if (args.hl != null) query.append("hl", String(args.hl)) if (args.domain != null) query.append("domain", String(args.domain)) if (args.include_offers != null) query.append("include_offers", String(args.include_offers)) if (args.include_variants != null) query.append("include_variants", String(args.include_variants)) if (args.resolve_deep_urls != null) query.append("resolve_deep_urls", String(args.resolve_deep_urls)) if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/google/products/detail${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(productDetailResponse, await res.json()) };