import {z} from 'zod' export type GetApiV1AnalyticsYoutubeChannelInsightsArgs = {accountId: string, metrics?: string | undefined, fromDate?: string | undefined, toDate?: string | undefined, since?: string | undefined, until?: string | undefined, metricType?: ('time_series' | 'total_value') | undefined}; export type GetApiV1AnalyticsYoutubeChannelInsightsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const instagramAccountInsightsResponse = z.object({success: z.boolean().optional(), accountId: z.string().optional(), platform: z.enum(["facebook", "instagram", "youtube", "linkedin", "tiktok", "pinterest"]).optional(), dateRange: z.object({since: z.string().optional(), until: z.string().optional()}).optional(), metricType: z.enum(["time_series", "total_value"]).optional(), breakdown: z.string().optional(), metrics: z.record(z.string(), z.object({total: z.number().optional(), values: z.array(z.object({date: z.string().optional(), value: z.number().optional()})).optional(), breakdowns: z.array(z.object({dimension: z.string().optional(), value: z.number().optional()})).optional(), unit: z.enum(["micro_amount", "unspecified"]).optional(), currency: z.string().nullable().optional()})).optional(), unavailableMetrics: z.array(z.object({metric: z.string().optional(), reason: z.enum(["not_enrolled", "permission_missing", "unsupported_metric", "no_data", "unreadable_value", "mixed_currency", "upstream_error"]).optional(), message: z.string().optional()})).optional(), dataDelay: z.string().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 channel insights * * GET /v1/analytics/youtube/channel-insights */ export const getApiV1AnalyticsYoutubeChannelInsights = async (args: GetApiV1AnalyticsYoutubeChannelInsightsArgs, options?: GetApiV1AnalyticsYoutubeChannelInsightsOptions) => { 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.accountId != null) query.append("accountId", String(args.accountId)) if (args.metrics != null) query.append("metrics", String(args.metrics)) if (args.fromDate != null) query.append("fromDate", String(args.fromDate)) if (args.toDate != null) query.append("toDate", String(args.toDate)) if (args.since != null) query.append("since", String(args.since)) if (args.until != null) query.append("until", String(args.until)) if (args.metricType != null) query.append("metricType", String(args.metricType)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/analytics/youtube/channel-insights${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(instagramAccountInsightsResponse, await res.json()) };