import {z} from 'zod' export type GetApiApiTrackYoutubeIdTopShortsArgs = {id: number, type?: ('likes' | 'views' | 'comments' | 'trending' | 'created_at') | undefined, limit?: number | undefined, bySoundId?: boolean | undefined}; export type GetApiApiTrackYoutubeIdTopShortsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiTrackYoutubeIdTopShortsResponse = z.object({obj: z.object({total: z.number().int().gte(0).nullable().optional(), available_count: z.number().int().nullable().optional(), data: z.array(z.object({video_id: z.string().nullable().optional(), created_at: z.string().nullable().optional(), modified_at: z.string().nullable().optional(), username: z.string().nullable().optional(), video_title: z.string().nullable().optional(), cm_id: z.number().int().nullable().optional(), sound_id: z.string().nullable().optional(), token_parameter: z.string().nullable().optional(), thumbnail_url: z.string().nullable().optional(), link: z.string().nullable().optional(), shorts_track_page: z.string().nullable().optional(), views: z.number().int().gte(0).nullable().optional(), likes: z.number().int().gte(0).nullable().optional(), comments: z.number().int().gte(0).nullable().optional(), stats: z.object({views: z.number().int().gte(0).nullable().optional(), likes: z.number().int().gte(0).nullable().optional(), comments: z.number().int().gte(0).nullable().optional()}).nullable().optional(), influencercountries: z.array(z.string()).nullable().optional(), youtubevideohistoricalstats: z.array(z.unknown()).nullable().optional(), metadata_for_url: z.object({video_id: z.string().nullable().optional(), username: z.string().nullable().optional(), views: z.union([z.number(), z.string()]).nullable().optional(), likes: z.union([z.number(), z.string()]).nullable().optional(), comments: z.union([z.number(), z.string()]).nullable().optional(), created_at: z.string().nullable().optional()}).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 }; /** * List Top YouTube Shorts for a Track * * GET /api/track/youtube/{id}/topShorts */ export const getApiApiTrackYoutubeIdTopShorts = async (args: GetApiApiTrackYoutubeIdTopShortsArgs, options?: GetApiApiTrackYoutubeIdTopShortsOptions) => { 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.type != null) query.append("type", String(args.type)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.bySoundId != null) query.append("bySoundId", String(args.bySoundId)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/track/youtube/${encodeURIComponent(String(args.id))}/topShorts${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(getApiApiTrackYoutubeIdTopShortsResponse, await res.json()) };