import {z} from 'zod' export type MatchType = 'BROAD' | 'PHRASE' | 'EXACT' | 'EXACT_NEGATIVE' | 'PHRASE_NEGATIVE'; export type TargetingTemplateKeyword = {match_type?: MatchType | undefined, value?: string | undefined}; export type PlacementGroupType = 'ALL' | 'SEARCH' | 'BROWSE' | 'OTHER'; export type TargetingSpecAgeBucket = '18-24' | '19+' | '20+' | '21+' | '25-34' | '35-44' | '45-49' | '50-54' | '55-64' | '65+'; export type TargetingSpecAppType = 'android_mobile' | 'android_tablet' | 'ipad' | 'iphone' | 'web' | 'web_mobile'; export type PinterestLibIntegerFormatType = string; export type TargetingSpecGender = 'unknown' | 'male' | 'female'; export type TargetingSpecShoppingRetargeting = {exclusion_window?: number | undefined, lookback_window?: number | undefined, tag_types?: Array | undefined}; export type TargetingStrategy = 'CHOOSE_YOUR_OWN' | 'FIND_NEW_CUSTOMERS' | 'RECONNECT_WITH_USERS'; export type TargetingSpecOptimal = {AGE_BUCKET?: (Array | null) | undefined, APPTYPE?: (Array | null) | undefined, AUDIENCE_EXCLUDE?: (Array | null) | undefined, AUDIENCE_INCLUDE?: (Array | null) | undefined, GENDER?: (Array | null) | undefined, GEO?: (Array | null) | undefined, GEO_EXCLUDE?: Array | undefined, INTEREST?: Array | undefined, LOCALE?: (Array | null) | undefined, LOCATION?: (Array | null) | undefined, LOCATION_EXCLUDE?: Array | undefined, MAXIMUM_AGE?: string | undefined, MINIMUM_AGE?: string | undefined, SHOPPING_RETARGETING?: (Array | null) | undefined, TARGETING_STRATEGY?: (Array | null) | undefined}; export type TrackingUrls = {audience_verification?: Array | undefined, buyable_button?: Array | undefined, click?: Array | undefined, engagement?: Array | undefined, impression?: Array | undefined} | null; export type TargetingTemplateCreate = {auto_targeting_enabled?: boolean | undefined, keywords?: Array | undefined, name: string, placement_group?: PlacementGroupType | undefined, targeting_attributes: TargetingSpecOptimal, tracking_urls?: TrackingUrls | undefined}; export type CreateApiAdAccountsAdAccountIdTargetingTemplatesArgs = {ad_account_id: string, body: TargetingTemplateCreate}; export type CreateApiAdAccountsAdAccountIdTargetingTemplatesOptions = {baseUrl?: string | undefined, pinterestOauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const matchType = z.enum(["BROAD", "PHRASE", "EXACT", "EXACT_NEGATIVE", "PHRASE_NEGATIVE"]); export const targetingTemplateKeyword = z.object({match_type: matchType.optional(), value: z.string().optional()}); export const placementGroupType = z.enum(["ALL", "SEARCH", "BROWSE", "OTHER"]); export const targetingTemplateAudienceSizingReachEstimate = z.object({estimate: z.number().int().optional(), lower_bound: z.number().int().optional(), upper_bound: z.number().int().optional()}); export const targetingTemplateAudienceSizing = z.object({reach_estimate: targetingTemplateAudienceSizingReachEstimate.optional()}).nullable(); export const targetingTemplateStatus = z.enum(["ACTIVE", "DELETED"]); export const targetingSpecAgeBucket = z.enum(["18-24", "19+", "20+", "21+", "25-34", "35-44", "45-49", "50-54", "55-64", "65+"]); export const targetingSpecAppType = z.enum(["android_mobile", "android_tablet", "ipad", "iphone", "web", "web_mobile"]); export const pinterestLibIntegerFormatType = z.string(); export const targetingSpecGender = z.enum(["unknown", "male", "female"]); export const targetingSpecShoppingRetargeting = z.object({exclusion_window: z.number().int().optional(), lookback_window: z.number().int().optional(), tag_types: z.array(z.number().int()).optional()}); export const targetingStrategy = z.enum(["CHOOSE_YOUR_OWN", "FIND_NEW_CUSTOMERS", "RECONNECT_WITH_USERS"]); export const targetingSpecOptimal = z.object({AGE_BUCKET: z.array(targetingSpecAgeBucket).nullable().optional(), APPTYPE: z.array(targetingSpecAppType).nullable().optional(), AUDIENCE_EXCLUDE: z.array(pinterestLibIntegerFormatType).nullable().optional(), AUDIENCE_INCLUDE: z.array(pinterestLibIntegerFormatType).nullable().optional(), GENDER: z.array(targetingSpecGender).nullable().optional(), GEO: z.array(z.string()).nullable().optional(), GEO_EXCLUDE: z.array(z.string()).optional(), INTEREST: z.array(z.string()).optional(), LOCALE: z.array(z.string()).nullable().optional(), LOCATION: z.array(z.string()).nullable().optional(), LOCATION_EXCLUDE: z.array(z.string()).optional(), MAXIMUM_AGE: z.string().optional(), MINIMUM_AGE: z.string().optional(), SHOPPING_RETARGETING: z.array(targetingSpecShoppingRetargeting).nullable().optional(), TARGETING_STRATEGY: z.array(targetingStrategy).nullable().optional()}); export const trackingUrls = z.object({audience_verification: z.array(z.string()).optional(), buyable_button: z.array(z.string()).optional(), click: z.array(z.string()).optional(), engagement: z.array(z.string()).optional(), impression: z.array(z.string()).optional()}).nullable(); export const targetingTemplate = z.object({ad_account_id: z.string().optional(), auto_targeting_enabled: z.boolean().optional(), created_time: z.number().int().optional(), id: z.string().optional(), keywords: z.array(targetingTemplateKeyword).optional(), name: z.string(), placement_group: placementGroupType.optional(), sizing: targetingTemplateAudienceSizing.optional(), status: targetingTemplateStatus.optional(), targeting_attributes: targetingSpecOptimal, tracking_urls: trackingUrls.optional(), updated_time: z.number().int().optional(), valid: z.boolean().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 targeting templates * * POST /ad_accounts/{ad_account_id}/targeting_templates */ export const createApiAdAccountsAdAccountIdTargetingTemplates = async (args: CreateApiAdAccountsAdAccountIdTargetingTemplatesArgs, options?: CreateApiAdAccountsAdAccountIdTargetingTemplatesOptions) => { 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))}/targeting_templates${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(targetingTemplate, await res.json()) };