import {z} from 'zod' export type GetApiApiChartsTiktokUsersArgs = {date?: string | undefined, interval?: ('daily' | 'weekly' | 'monthly' | 'semi-yearly' | 'yearly' | 'all_time') | undefined, type: 'likes' | 'followers', limit?: number | undefined, offset?: number | undefined, latest?: boolean | undefined}; export type GetApiApiChartsTiktokUsersOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiChartsTiktokUsersResponse = z.object({obj: z.object({length: z.number().int().gte(0), data: z.array(z.object({tiktok_user: z.union([z.number().int(), z.string()]).nullable().optional(), cm_artist: z.number().int().nullable().optional(), name: z.string().nullable().optional(), image_url: z.string().nullable().optional(), rank: z.number().int().nullable().optional(), added_at: z.string().nullable().optional(), velocity: z.number().nullable().optional(), pre_rank: z.number().int().nullable().optional(), peak_rank: z.number().int().nullable().optional(), peak_date: z.string().nullable().optional(), time_on_chart: z.union([z.number().int(), z.string()]).nullable().optional(), followers: z.number().int().gte(0).nullable().optional(), likes: z.number().int().gte(0).nullable().optional(), views: z.number().int().gte(0).nullable().optional(), weekly_followers: z.number().int().gte(0).nullable().optional(), daily_followers: z.number().int().gte(0).nullable().optional(), weekly_likes: z.number().int().gte(0).nullable().optional(), daily_likes: z.number().int().gte(0).nullable().optional(), rankStats: z.array(z.object({rank: z.number().int().nullable().optional(), posts: z.number().int().gte(0).nullable().optional(), views: z.number().int().gte(0).nullable().optional(), timestp: z.string().nullable().optional()})).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 }; /** * Get TikTok Top-User Chart * * GET /api/charts/tiktok/users */ export const getApiApiChartsTiktokUsers = async (args: GetApiApiChartsTiktokUsersArgs, options?: GetApiApiChartsTiktokUsersOptions) => { 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.date != null) query.append("date", String(args.date)) if (args.interval != null) query.append("interval", String(args.interval)) if (args.type != null) query.append("type", String(args.type)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.latest != null) query.append("latest", String(args.latest)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/charts/tiktok/users${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(getApiApiChartsTiktokUsersResponse, await res.json()) };