import {z} from 'zod' export type GetApiApiTrackIdTiktokTopSoundsArgs = {id: number, official?: boolean | undefined}; export type GetApiApiTrackIdTiktokTopSoundsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiTrackIdTiktokTopSoundsResponse = z.object({obj: z.array(z.object({tiktok: z.number().int().nullable().optional(), tiktok_track_id: z.string().nullable().optional(), name: z.string().nullable().optional(), creator_name: z.string().nullable().optional(), views: z.number().int().nullable().optional(), posts: z.number().int().nullable().optional(), first_post_date: z.string().nullable().optional(), upload_date: z.string().nullable().optional(), is_official: z.boolean().optional(), is_original: z.boolean().optional(), is_ai_generated: z.boolean().optional(), still_on_tiktok: z.boolean().optional(), thumbnail_url: z.string().nullable().optional(), video_trend: z.array(z.object({date: z.string().nullable().optional(), videos: z.number().int().nullable().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 }; /** * 💎 Premium — List Popular TikTok Sounds for a Track * * GET /api/track/{id}/tiktok-top-sounds */ export const getApiApiTrackIdTiktokTopSounds = async (args: GetApiApiTrackIdTiktokTopSoundsArgs, options?: GetApiApiTrackIdTiktokTopSoundsOptions) => { 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.official != null) query.append("official", String(args.official)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/track/${encodeURIComponent(String(args.id))}/tiktok-top-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(getApiApiTrackIdTiktokTopSoundsResponse, await res.json()) };