import {z} from 'zod' export type GetApiV1AnalyticsYoutubeVideoRetentionArgs = {videoId: string, accountId: string, fromDate?: string | undefined, toDate?: string | undefined, startDate?: string | undefined, endDate?: string | undefined}; export type GetApiV1AnalyticsYoutubeVideoRetentionOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const youTubeVideoRetentionResponse = z.object({success: z.boolean().optional(), accountId: z.string().optional(), videoId: z.string().optional(), title: z.string().nullable().optional(), publishedAt: z.string().nullable().optional(), durationSeconds: z.number().int().nullable().optional(), dateRange: z.object({startDate: z.string().optional(), endDate: z.string().optional()}).optional(), provisionalSince: z.string().optional(), segmentMetricsAvailable: z.boolean().optional(), retentionCurve: z.array(z.object({elapsedVideoTimeRatio: z.number().optional(), audienceWatchRatio: z.number().optional(), relativeRetentionPerformance: z.number().optional(), startedWatching: z.number().int().optional(), stoppedWatching: z.number().int().optional(), totalSegmentImpressions: z.number().int().optional()})).optional(), note: z.string().optional(), scopeStatus: z.object({hasAnalyticsScope: z.boolean().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 }; /** * Get YouTube video retention curve * * GET /v1/analytics/youtube/video-retention */ export const getApiV1AnalyticsYoutubeVideoRetention = async (args: GetApiV1AnalyticsYoutubeVideoRetentionArgs, options?: GetApiV1AnalyticsYoutubeVideoRetentionOptions) => { const {baseUrl = "https://zernio.com/api", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.videoId != null) query.append("videoId", String(args.videoId)) if (args.accountId != null) query.append("accountId", String(args.accountId)) if (args.fromDate != null) query.append("fromDate", String(args.fromDate)) if (args.toDate != null) query.append("toDate", String(args.toDate)) if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/analytics/youtube/video-retention${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(youTubeVideoRetentionResponse, await res.json()) };