import {z} from 'zod' export type GetApiApiTrendingTopProductsMetricsArgs = {product_id: string}; export type GetApiApiTrendingTopProductsMetricsOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiTrendingTopProductsMetricsResponse = z.object({data: z.object({info: z.object({comment: z.number().optional(), cost: z.number().optional(), cover_url: z.string().nullable().optional(), cpa: z.number().optional(), ctr: z.number().optional(), cvr: z.number().optional(), ecom_type: z.string().optional(), first_ecom_category: z.object({id: z.string().optional(), label: z.string().optional(), value: z.string().optional()}).optional(), impression: z.number().optional(), like: z.number().optional(), play_six_rate: z.number().optional(), post: z.number().optional(), post_change: z.number().optional(), second_ecom_category: z.object({id: z.string().optional(), label: z.string().optional(), parent_id: z.string().optional(), value: z.string().optional()}).optional(), share: z.number().optional(), third_ecom_category: z.object({id: z.string().optional(), label: z.string().optional(), parent_id: z.string().optional(), value: z.string().optional()}).optional(), url_title: z.string().optional(), ctr_metrics: z.array(z.object({time: z.number().optional(), value: z.number().optional()})).optional(), post_metrics: z.array(z.object({time: z.number().optional(), value: z.number().optional()})).optional()}).optional()}).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 }; /** * Get Top Product Metrics * * GET /api/trending/top-products/metrics */ export const getApiApiTrendingTopProductsMetrics = async (args: GetApiApiTrendingTopProductsMetricsArgs, options?: GetApiApiTrendingTopProductsMetricsOptions) => { const {baseUrl = "https://tiktok-api23.p.rapidapi.com", fetch: fetchFn = fetch, apiKeyAuth} = 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 (apiKeyAuth !== undefined) headers.set("x-rapidapi-key", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/trending/top-products/metrics${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(getApiApiTrendingTopProductsMetricsResponse, await res.json()) };