import {z} from 'zod' export type CreatePublicUtmEventDto = {ai_contact_id: string, customer_id?: (string | null) | undefined, referrer_url?: (string | null) | undefined, user_agent?: (string | null) | undefined, ip_address?: (string | null) | undefined, integration_type?: ('facebook' | 'google' | 'tiktok' | 'snapchat' | 'linkedin' | 'twitter' | 'pinterest' | 'bing' | 'facebook_whatsapp' | 'facebook_instagram' | 'facebook_messenger' | 'unknown' | null) | undefined, campaign_id?: (string | null) | undefined, ad_set_id?: (string | null) | undefined, ad_id?: (string | null) | undefined, ad_source_click_id?: (string | null) | undefined, utm_source?: string | undefined, utm_medium?: string | undefined, utm_term?: string | undefined, utm_content?: string | undefined, utm_referrer?: string | undefined, additional_params: {[key: string]: unknown}}; export type CreateApiApiPublicUtmEventCreateArgs = {'x-api-key': string, body: CreatePublicUtmEventDto}; export type CreateApiApiPublicUtmEventCreateOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiPublicUtmEventCreateResponse = z.object({success: z.boolean().optional(), message: 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 }; /** * Create custom UTM event * * POST /api/public/utm-event/create */ export const createApiApiPublicUtmEventCreate = async (args: CreateApiApiPublicUtmEventCreateArgs, options?: CreateApiApiPublicUtmEventCreateOptions) => { const {baseUrl = "https://api.vambe.me", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-api-key'] != null) headers.set("x-api-key", String(args['x-api-key'])) const url = `${baseUrl.replace(/\/+$/, '')}/api/public/utm-event/create${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(createApiApiPublicUtmEventCreateResponse, await res.json()) };