import {z} from 'zod' export type ConversionEventAppInfo = {app_id?: string | undefined, app_name?: string | undefined, app_package_name?: string | undefined, app_store?: string | undefined, app_version?: string | undefined, install_time?: number | undefined, user_agent?: string | undefined, window_height?: number | undefined, window_width?: number | undefined}; export type ConversionEventsDataItemsCustomDataContentsItems = {id?: string | undefined, item_brand?: string | undefined, item_brand_id?: string | undefined, item_category?: string | undefined, item_name?: string | undefined, item_price?: string | undefined, quantity?: number | undefined}; export type ConversionEventsDataItemsCustomData = {content_brand?: (string | null) | undefined, content_category?: (string | null) | undefined, content_ids?: Array | undefined, content_name?: (string | null) | undefined, contents?: Array | undefined, currency?: (string | null) | undefined, external_measurement_id?: (string | null) | undefined, external_measurement_vendor_id?: (number | null) | undefined, np?: (string | null) | undefined, num_items?: number | undefined, opt_out_type?: (string | null) | undefined, order_id?: (string | null) | undefined, predicted_ltv?: (string | null) | undefined, search_string?: (string | null) | undefined, value?: (string | null) | undefined}; export type FormFactor = 'desktop' | 'laptop' | 'cellphone' | 'tablet' | 'smartwatch' | 'tv' | 'vr' | 'console' | 'other'; export type NetworkType = 'wifi' | 'cellular_2g' | 'cellular_3g' | 'cellular_4g' | 'cellular_5g' | 'cellular_6g' | 'ethernet' | 'unknown'; export type OsFamily = 'ios' | 'android' | 'macos' | 'windows' | 'linux' | 'bsd' | 'other'; export type ConversionEventDeviceInfo = {battery_level?: number | undefined, brand?: string | undefined, carrier?: string | undefined, cpu_cores?: number | undefined, external_storage_free_space?: number | undefined, external_storage_size?: number | undefined, form_factor?: FormFactor | undefined, kernel_version?: string | undefined, languages?: Array | undefined, locale?: string | undefined, model?: string | undefined, network_type?: NetworkType | undefined, os_family?: OsFamily | undefined, os_name?: string | undefined, os_release_name?: string | undefined, os_version?: string | undefined, screen_density?: number | undefined, screen_height?: number | undefined, screen_width?: number | undefined, storage_free_space?: number | undefined, storage_size?: number | undefined, timezone?: string | undefined, timezone_abbr?: string | undefined, type?: string | undefined}; export type ConversionEventsUserDataProperties = {click_id?: (string | null) | undefined, client_ip_address?: string | undefined, client_user_agent?: string | undefined, country?: Array | undefined, ct?: Array | undefined, db?: Array | undefined, em?: Array | undefined, external_id?: Array | undefined, fn?: Array | undefined, ge?: Array | undefined, hashed_maids?: Array | undefined, ln?: Array | undefined, partner_id?: (string | null) | undefined, ph?: Array | undefined, st?: Array | undefined, zp?: Array | undefined}; export type ConversionEventsDataItems = {action_source: string, app_id?: (string | null) | undefined, app_info?: ConversionEventAppInfo | undefined, app_name?: (string | null) | undefined, app_version?: (string | null) | undefined, custom_data?: ConversionEventsDataItemsCustomData | undefined, device_brand?: (string | null) | undefined, device_carrier?: (string | null) | undefined, device_info?: ConversionEventDeviceInfo | undefined, device_model?: (string | null) | undefined, device_type?: (string | null) | undefined, event_id: string, event_name: string, event_source_url?: (string | null) | undefined, event_time: number, language?: (string | null) | undefined, opt_out?: boolean | undefined, os_version?: (string | null) | undefined, partner_name?: (string | null) | undefined, user_data: ConversionEventsUserDataProperties, wifi?: boolean | undefined}; export type ConversionEventsCreate = {data: Array}; export type CreateApiAdAccountsAdAccountIdEventsArgs = {ad_account_id: string, test?: boolean | undefined, body: ConversionEventsCreate}; export type CreateApiAdAccountsAdAccountIdEventsOptions = {baseUrl?: string | undefined, pinterestOauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const eventProcessingStatus = z.enum(["failed", "processed"]); export const conversionApiResponseEventsItems = z.object({error_message: z.string().nullable().optional(), status: eventProcessingStatus, warning_message: z.string().nullable().optional()}); export const conversionEvents = z.object({events: z.array(conversionApiResponseEventsItems), num_events_processed: z.number().int(), num_events_received: z.number().int()}); 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 }; /** * Send conversions * * POST /ad_accounts/{ad_account_id}/events * * Also accepts auth: conversion_token */ export const createApiAdAccountsAdAccountIdEvents = async (args: CreateApiAdAccountsAdAccountIdEventsArgs, options?: CreateApiAdAccountsAdAccountIdEventsOptions) => { const {baseUrl = "https://api.pinterest.com/v5", fetch: fetchFn = fetch, pinterestOauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.test != null) query.append("test", String(args.test)) if (pinterestOauth2 !== undefined) headers.set('authorization', `Bearer ${pinterestOauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/ad_accounts/${encodeURIComponent(String(args.ad_account_id))}/events${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(conversionEvents, await res.json()) };