import {z} from 'zod' export type V4AutomationData = {type?: (string | null) | undefined, platform?: ('iOS' | 'Android' | 'null' | null) | undefined, initiators: Array<{uid: string, type: 'event'}>, actions: {push?: (string | null) | undefined, screen?: (string | null) | undefined}, segments: string | null}; export type V4AutomationCreate = {name: string, caption?: string | undefined, status: 'active' | 'inactive' | 'paused', data: V4AutomationData}; export type CreateApiAutomationsArgs = {body: V4AutomationCreate}; export type CreateApiAutomationsOptions = {baseUrl?: string | undefined, secretAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const v4AutomationData = z.object({type: z.string().nullable().optional(), platform: z.enum(["iOS", "Android", "null"]).nullable().optional(), initiators: z.array(z.object({uid: z.string(), type: z.literal("event")})), actions: z.object({push: z.string().nullable().optional(), screen: z.string().nullable().optional()}), segments: z.string().nullable()}); export const v4Automation = z.object({object: z.literal("automation"), id: z.string(), url: z.string(), name: z.string(), caption: z.string(), status: z.string(), data: v4AutomationData}); 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 }; /** * Create an automation * * POST /automations */ export const createApiAutomations = async (args: CreateApiAutomationsArgs, options?: CreateApiAutomationsOptions) => { const {baseUrl = "https://api.qonversion.io/v4", fetch: fetchFn = fetch, secretAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (secretAuth !== undefined) headers.set('authorization', `Bearer ${secretAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/automations${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(v4Automation, await res.json()) };