import {z} from 'zod' export type GetApiApiChartsTiktokSoundsArgs = {date?: string | undefined, country?: string | undefined, limit?: number | undefined, offset?: number | undefined, latest?: boolean | undefined}; export type GetApiApiChartsTiktokSoundsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiChartsTiktokSoundsResponse = z.object({obj: z.object({length: z.number().int().gte(0), data: z.array(z.object({timestp: z.string(), country: z.string(), rank: z.number().int().nullable().optional(), tiktok_sound_id: z.string(), tiktok_name_of_sound: z.string().nullable().optional(), tiktok_sound_creator_name: z.string().nullable().optional(), tiktok_image_url: z.string().nullable().optional(), tiktok_play_url: z.string().nullable().optional(), tiktok_official_link: z.string().nullable().optional(), tiktok_last_24_hours_video_count: z.number().int().gte(0).nullable().optional(), tiktok_last_24_hours_video_percentage: z.number().nullable().optional(), tiktok_last_7_days_video_count: z.number().int().gte(0).nullable().optional(), total_video_views: z.number().int().gte(0).nullable().optional(), song_name: z.string().nullable().optional(), artists: z.string().nullable().optional(), song_image_url: z.string().nullable().optional(), spotify_id: z.string().nullable().optional(), label_name: z.string().nullable().optional(), sound_create_date: z.string().nullable().optional(), cm_track: z.number().int().nullable().optional(), is_ai_generated: z.boolean().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 Sound Chart * * GET /api/charts/tiktok/sounds */ export const getApiApiChartsTiktokSounds = async (args: GetApiApiChartsTiktokSoundsArgs, options?: GetApiApiChartsTiktokSoundsOptions) => { 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.country != null) query.append("country", String(args.country)) 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/sounds${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(getApiApiChartsTiktokSoundsResponse, await res.json()) };