import {z} from 'zod' export type AdGroupAudienceSizingCreativeTypes = 'REGULAR' | 'VIDEO' | 'SHOPPING' | 'CAROUSEL' | 'MAX_VIDEO' | 'SHOP_THE_PIN' | 'COLLECTION' | 'IDEA'; export type MatchType = 'BROAD' | 'PHRASE' | 'EXACT' | 'EXACT_NEGATIVE' | 'PHRASE_NEGATIVE'; export type AdGroupAudienceSizingKeyword = {match_type: MatchType, value: string}; export type AdgroupPlacementGroupType = 'ALL' | 'SEARCH' | 'BROWSE' | 'OTHER'; export type AdGroupAudienceSizingProductGroupId = string; 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 AdGroupAudienceSizingCreate = {auto_targeting_enabled?: boolean | undefined, creative_types?: (Array | null) | undefined, keywords?: (Array | null) | undefined, placement_group?: AdgroupPlacementGroupType | undefined, product_group_ids?: (Array | null) | undefined, targeting_spec?: TargetingSpecOptimal | undefined}; export type CreateApiAdAccountsAdAccountIdAdGroupsAudienceSizingArgs = {ad_account_id: string, body: AdGroupAudienceSizingCreate}; export type CreateApiAdAccountsAdAccountIdAdGroupsAudienceSizingOptions = {baseUrl?: string | undefined, pinterestOauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const adGroupAudienceSizing = z.object({audience_size_lower_bound: z.number().optional(), audience_size_upper_bound: z.number().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 }; /** * Get audience sizing * * POST /ad_accounts/{ad_account_id}/ad_groups/audience_sizing * * Also accepts auth: client_credentials */ export const createApiAdAccountsAdAccountIdAdGroupsAudienceSizing = async (args: CreateApiAdAccountsAdAccountIdAdGroupsAudienceSizingArgs, options?: CreateApiAdAccountsAdAccountIdAdGroupsAudienceSizingOptions) => { 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))}/ad_groups/audience_sizing${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(adGroupAudienceSizing, await res.json()) };