import {z} from 'zod' export type CreateApiGoogleSearchProductsArgs = {body: {query: string, country_code?: string | undefined, page?: number | undefined, per_page?: number | undefined, sort_by?: ('best_match' | 'top_rated' | 'lowest_price' | 'highest_price') | undefined, product_condition?: ('any' | 'new' | 'used' | 'refurbished') | undefined}}; export type CreateApiGoogleSearchProductsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const googleProduct = z.object({product_id: z.string().optional(), title: z.string().optional(), description: z.string().nullable().optional(), photos: z.array(z.string()).optional(), attributes: z.record(z.string(), z.string()).nullable().optional(), rating: z.number().nullable().optional(), page_url: z.string().optional(), num_reviews: z.number().int().nullable().optional(), reviews_per_rating: z.record(z.string(), z.number().int()).optional(), product_details: z.record(z.string(), z.string()).optional(), product_specs: z.record(z.string(), z.string()).optional()}); export const createApiGoogleSearchProductsResponse = z.object({products: z.array(googleProduct).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 }; /** * Search products on Google * * POST /google/search-products */ export const createApiGoogleSearchProducts = async (args: CreateApiGoogleSearchProductsArgs, options?: CreateApiGoogleSearchProductsOptions) => { const {baseUrl = "https://api.exfunc.dev", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/google/search-products${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiGoogleSearchProductsResponse, await res.json()) };