import {z} from 'zod' export type GetApiAnalyticsLtvArgs = {cohort_from?: number | undefined, cohort_to?: number | undefined, mode?: ('by_days' | 'by_renewals') | undefined, environment?: (0 | 1) | undefined, segment?: string | undefined, revenue_type?: ('gross' | 'net') | undefined, currency?: string | undefined, 'filter[][]'?: Array | undefined}; export type GetApiAnalyticsLtvOptions = {baseUrl?: string | undefined, secretAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const v4AnalyticsSeriesDataPoint = z.object({start_time: z.number().int(), value: z.number()}); export const v4AnalyticsLtv = z.object({object: z.literal("analytics_ltv"), url: z.string(), mode: z.enum(["by_days", "by_renewals"]), cohort_from: z.number().int(), cohort_to: z.number().int(), cohort_users: z.number().int(), paying_users: z.number().int(), revenue_type: z.enum(["gross", "net"]), currency: z.string(), measure: z.literal("usd"), segment: z.string().nullable().optional(), segments: z.array(z.record(z.string(), z.unknown())).optional(), series: z.array(v4AnalyticsSeriesDataPoint)}); 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 LTV analytics data * * GET /analytics/ltv */ export const getApiAnalyticsLtv = async (args: GetApiAnalyticsLtvArgs, options?: GetApiAnalyticsLtvOptions) => { const {baseUrl = "https://api.qonversion.io/v4", fetch: fetchFn = fetch, secretAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.cohort_from != null) query.append("cohort_from", String(args.cohort_from)) if (args.cohort_to != null) query.append("cohort_to", String(args.cohort_to)) if (args.mode != null) query.append("mode", String(args.mode)) if (args.environment != null) query.append("environment", String(args.environment)) if (args.segment != null) query.append("segment", String(args.segment)) if (args.revenue_type != null) query.append("revenue_type", String(args.revenue_type)) if (args.currency != null) query.append("currency", String(args.currency)) for (const item of args['filter[][]'] ?? []) query.append("filter[][]", String(item)) if (secretAuth !== undefined) headers.set('authorization', `Bearer ${secretAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/analytics/ltv${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(v4AnalyticsLtv, await res.json()) };