import {z} from 'zod' export type AudienceType = 'CUSTOMER_LIST' | 'VISITOR' | 'ENGAGEMENT' | 'ACTALIKE' | 'PERSONA'; export type Currency = 'UNK' | 'USD' | 'GBP' | 'CAD' | 'EUR' | 'AUD' | 'NZD' | 'SEK' | 'ILS' | 'CHF' | 'HKD' | 'JPY' | 'SGD' | 'KRW' | 'NOK' | 'DKK' | 'PLN' | 'RON' | 'HUF' | 'CZK' | 'BRL' | 'MXN' | 'ARS' | 'CLP' | 'COP' | 'INR' | 'TRY'; export type LineItem = {product_brand?: string | undefined, product_category?: string | undefined, product_id?: number | undefined, product_name?: string | undefined, product_price?: string | undefined, product_quantity?: number | undefined, product_variant?: string | undefined, product_variant_id?: string | undefined}; export type EventData = {currency?: Currency | undefined, lead_type?: string | undefined, line_items?: LineItem | undefined, order_id?: string | undefined, order_quantity?: number | undefined, page_name?: string | undefined, promo_code?: string | undefined, property?: string | undefined, search_query?: string | undefined, value?: string | undefined, video_title?: string | undefined}; export type AudienceObjectiveType = 'AWARENESS' | 'CONSIDERATION' | 'WEB_CONVERSION' | 'CATALOG_SALES' | 'VIDEO_COMPLETION' | 'SALES'; export type AdAccountsAudienceRule = {ad_account_id?: string | undefined, ad_id?: Array | undefined, campaign_id?: Array | undefined, country?: string | undefined, customer_list_id?: string | undefined, engagement_domain?: Array | undefined, engagement_type?: string | undefined, engager_type?: number | undefined, event?: string | undefined, event_data?: EventData | undefined, event_source?: Record | undefined, ingestion_source?: Record | undefined, objective_type?: Array | undefined, percentage?: (string | number) | undefined, pin_id?: Array | undefined, prefill?: boolean | undefined, retention_days?: number | undefined, seed_id?: (Array | Array) | undefined, url?: Array | undefined, visitor_source_id?: (string | Array) | undefined}; export type AdAccountsAudienceCreate = {ad_account_id?: string | undefined, audience_type?: AudienceType | undefined, description?: (string | null) | undefined, name?: string | undefined, rule?: AdAccountsAudienceRule | undefined}; export type CreateApiAdAccountsAdAccountIdAudiencesArgs = {ad_account_id: string, body: AdAccountsAudienceCreate}; export type CreateApiAdAccountsAdAccountIdAudiencesOptions = {baseUrl?: string | undefined, pinterestOauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const audienceType = z.enum(["CUSTOMER_LIST", "VISITOR", "ENGAGEMENT", "ACTALIKE", "PERSONA"]); export const currency = z.enum(["UNK", "USD", "GBP", "CAD", "EUR", "AUD", "NZD", "SEK", "ILS", "CHF", "HKD", "JPY", "SGD", "KRW", "NOK", "DKK", "PLN", "RON", "HUF", "CZK", "BRL", "MXN", "ARS", "CLP", "COP", "INR", "TRY"]); export const lineItem = z.object({product_brand: z.string().optional(), product_category: z.string().optional(), product_id: z.number().int().optional(), product_name: z.string().optional(), product_price: z.string().optional(), product_quantity: z.number().int().optional(), product_variant: z.string().optional(), product_variant_id: z.string().optional()}); export const eventData = z.object({currency: currency.optional(), lead_type: z.string().optional(), line_items: lineItem.optional(), order_id: z.string().optional(), order_quantity: z.number().int().optional(), page_name: z.string().optional(), promo_code: z.string().optional(), property: z.string().optional(), search_query: z.string().optional(), value: z.string().optional(), video_title: z.string().optional()}); export const audienceObjectiveType = z.enum(["AWARENESS", "CONSIDERATION", "WEB_CONVERSION", "CATALOG_SALES", "VIDEO_COMPLETION", "SALES"]); export const adAccountsAudienceRule = z.object({ad_account_id: z.string().optional(), ad_id: z.array(z.string()).optional(), campaign_id: z.array(z.string()).optional(), country: z.string().optional(), customer_list_id: z.string().optional(), engagement_domain: z.array(z.string()).optional(), engagement_type: z.string().optional(), engager_type: z.number().int().optional(), event: z.string().optional(), event_data: eventData.optional(), event_source: z.object({}).optional(), ingestion_source: z.object({}).optional(), objective_type: z.array(audienceObjectiveType).optional(), percentage: z.union([z.string(), z.number().int()]).optional(), pin_id: z.array(z.string()).optional(), prefill: z.boolean().optional(), retention_days: z.number().int().optional(), seed_id: z.union([z.array(z.number().int()), z.array(z.string())]).optional(), url: z.array(z.string()).optional(), visitor_source_id: z.union([z.string(), z.array(z.string())]).optional()}); export const audienceStatus = z.enum(["INITIALIZING", "READY", "TOO_SMALL", "ELIGIBLE", "PERSONAS_INELIGIBLE_SIZE", "PERSONAS_INITIALIZING"]); export const adAccountsAudience = z.object({ad_account_id: z.string().optional(), audience_type: audienceType.optional(), created_by_company_name: z.string().nullable().optional(), created_timestamp: z.number().int().nullable().optional(), description: z.string().nullable().optional(), id: z.string(), is_nca: z.boolean().optional(), name: z.string().optional(), rule: adAccountsAudienceRule.optional(), size: z.number().int().nullable().optional(), status: audienceStatus.optional(), type: z.string().optional(), updated_timestamp: z.number().int().nullable().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 audience * * POST /ad_accounts/{ad_account_id}/audiences */ export const createApiAdAccountsAdAccountIdAudiences = async (args: CreateApiAdAccountsAdAccountIdAudiencesArgs, options?: CreateApiAdAccountsAdAccountIdAudiencesOptions) => { 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))}/audiences${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(adAccountsAudience, await res.json()) };