import {z} from 'zod' export type GetApiApiTrackIdTiktokInfluencerStatsArgs = {id: number, dimension?: ('country' | 'category' | 'subcategory' | 'age-gender' | 'language') | undefined, periodDays?: number | undefined}; export type GetApiApiTrackIdTiktokInfluencerStatsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiTrackIdTiktokInfluencerStatsResponse = z.object({obj: z.object({dimension: z.enum(["country", "category", "subcategory", "age-gender", "language"]), period_days: z.union([z.literal(0), z.literal(7), z.literal(30), z.literal(90)]), total: z.number().int().gte(0).nullable().optional(), multi_label: z.boolean(), available_period_days: z.array(z.number()).nullable().optional(), data: z.array(z.object({value: z.string(), count: z.number().int().gte(0), share: z.number().nullable().optional(), delta: z.number().int().nullable().optional(), age_group: z.string().nullable().optional(), gender: z.string().nullable().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 }; /** * 💎 Premium — Get Influencer Breakdown for a Track * * GET /api/track/{id}/tiktok-influencer-stats */ export const getApiApiTrackIdTiktokInfluencerStats = async (args: GetApiApiTrackIdTiktokInfluencerStatsArgs, options?: GetApiApiTrackIdTiktokInfluencerStatsOptions) => { const {baseUrl = "https://api.chartmetric.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.dimension != null) query.append("dimension", String(args.dimension)) if (args.periodDays != null) query.append("periodDays", String(args.periodDays)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/track/${encodeURIComponent(String(args.id))}/tiktok-influencer-stats${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(getApiApiTrackIdTiktokInfluencerStatsResponse, await res.json()) };