import {z} from 'zod' export type VideoLeaderboardRequest = {page?: number | undefined, page_size?: number | undefined, sort_by?: ('total_video_gmv' | 'total_units_sold' | 'qualifying_video_count' | null) | undefined, sort_dir?: ('asc' | 'desc') | undefined, start_date?: (string | null) | undefined, end_date?: (string | null) | undefined, product_ids?: (Array | null) | undefined, min_gmv?: (number | null) | undefined}; export type CreateApiVideosLeaderboardArgs = {body: VideoLeaderboardRequest}; export type CreateApiVideosLeaderboardOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, ShopId?: string | undefined, fetch?: typeof fetch | undefined}; export const leaderboardProductBreakdown = z.object({product_id: z.string().nullable().optional(), product_name: z.string().nullable().optional(), video_gmv: z.number().nullable().optional(), units_sold: z.number().int().nullable().optional(), video_count: z.number().int().optional()}); export const leaderboardCreatorItem = z.object({creator_id: z.string().nullable().optional(), creator_handle: z.string().nullable().optional(), total_video_gmv: z.number().nullable().optional(), total_units_sold: z.number().int().nullable().optional(), qualifying_video_count: z.number().int().optional(), products: z.array(leaderboardProductBreakdown).optional()}); export const dateRange = z.object({start_date: z.string().nullable().optional(), end_date: z.string().nullable().optional()}); export const videoLeaderboardResponse = z.object({data: z.array(leaderboardCreatorItem), currency: z.string(), pagination: z.unknown(), date_range: dateRange.nullable().optional(), shops_queried: z.array(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 }; /** * Creator video leaderboard * * POST /videos/leaderboard */ export const createApiVideosLeaderboard = async (args: CreateApiVideosLeaderboardArgs, options?: CreateApiVideosLeaderboardOptions) => { const {baseUrl = "https://api.reacherapp.com/public/v1", fetch: fetchFn = fetch, ApiKeyAuth, ShopId} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) if (ShopId !== undefined) headers.set("x-shop-id", ShopId) const url = `${baseUrl.replace(/\/+$/, '')}/videos/leaderboard${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(videoLeaderboardResponse, await res.json()) };