import {z} from 'zod' export type AdvertiserDefinedEventMappingType = 'SIGNUP' | 'ADD_TO_CART' | 'LEAD' | 'CHECKOUT' | 'SUBSCRIBE' | 'ADD_TO_WISHLIST' | 'ADD_PAYMENT_INFO' | 'INITIATE_CHECKOUT' | 'CONTACT' | 'CUSTOMIZE_PRODUCT' | 'FIND_LOCATION' | 'SCHEDULE' | 'SUBMIT_APPLICATION' | 'START_TRIAL' | 'PAGE_VISIT' | 'VIEW_CATEGORY' | 'VIEW_CONTENT' | 'SEARCH' | 'WATCH_VIDEO'; export type AdvertiserDefinedEventInput = {mapped_conversion_type: AdvertiserDefinedEventMappingType, name: string}; export type AdvertiserDefinedEventsCreateRequest = {items: Array}; export type PatchApiAdAccountsAdAccountIdAdvertiserDefinedEventsArgs = {ad_account_id: string, body: AdvertiserDefinedEventsCreateRequest}; export type PatchApiAdAccountsAdAccountIdAdvertiserDefinedEventsOptions = {baseUrl?: string | undefined, pinterestOauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const advertiserDefinedEventProcessingRecord = z.object({exceptions: z.array(z.string()).optional(), name: z.string(), status: z.string()}); export const patchApiAdAccountsAdAccountIdAdvertiserDefinedEventsResponse = z.object({items: z.array(advertiserDefinedEventProcessingRecord)}); 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 }; /** * Update advertiser defined events * * PATCH /ad_accounts/{ad_account_id}/advertiser_defined_events * * Also accepts auth: client_credentials */ export const patchApiAdAccountsAdAccountIdAdvertiserDefinedEvents = async (args: PatchApiAdAccountsAdAccountIdAdvertiserDefinedEventsArgs, options?: PatchApiAdAccountsAdAccountIdAdvertiserDefinedEventsOptions) => { 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 (pinterestOauth2 !== undefined) headers.set('authorization', `Bearer ${pinterestOauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/ad_accounts/${encodeURIComponent(String(args.ad_account_id))}/advertiser_defined_events${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(patchApiAdAccountsAdAccountIdAdvertiserDefinedEventsResponse, await res.json()) };