import {z} from 'zod' export type ActionType = 'CLICKTHROUGH' | 'IMPRESSION' | 'VIDEO_V_50_MRC'; export type Country = 'AD' | 'AE' | 'AF' | 'AG' | 'AI' | 'AL' | 'AM' | 'AO' | 'AQ' | 'AR' | 'AS' | 'AT' | 'AU' | 'AW' | 'AX' | 'AZ' | 'BA' | 'BB' | 'BD' | 'BE' | 'BF' | 'BG' | 'BH' | 'BI' | 'BJ' | 'BL' | 'BM' | 'BN' | 'BO' | 'BQ' | 'BR' | 'BS' | 'BT' | 'BV' | 'BW' | 'BY' | 'BZ' | 'CA' | 'CC' | 'CD' | 'CF' | 'CG' | 'CH' | 'CI' | 'CK' | 'CL' | 'CM' | 'CN' | 'CO' | 'CR' | 'CU' | 'CV' | 'CW' | 'CX' | 'CY' | 'CZ' | 'DE' | 'DJ' | 'DK' | 'DM' | 'DO' | 'DZ' | 'EC' | 'EE' | 'EG' | 'EH' | 'ER' | 'ES' | 'ET' | 'FI' | 'FJ' | 'FK' | 'FM' | 'FO' | 'FR' | 'GA' | 'GB' | 'GD' | 'GE' | 'GF' | 'GG' | 'GH' | 'GI' | 'GL' | 'GM' | 'GN' | 'GP' | 'GQ' | 'GR' | 'GS' | 'GT' | 'GU' | 'GW' | 'GY' | 'HK' | 'HM' | 'HN' | 'HR' | 'HT' | 'HU' | 'ID' | 'IE' | 'IL' | 'IM' | 'IN' | 'IO' | 'IQ' | 'IR' | 'IS' | 'IT' | 'JE' | 'JM' | 'JO' | 'JP' | 'KE' | 'KG' | 'KH' | 'KI' | 'KM' | 'KN' | 'KR' | 'KW' | 'KY' | 'KZ' | 'LA' | 'LB' | 'LC' | 'LI' | 'LK' | 'LR' | 'LS' | 'LT' | 'LU' | 'LV' | 'LY' | 'MA' | 'MC' | 'MD' | 'ME' | 'MF' | 'MG' | 'MH' | 'MK' | 'ML' | 'MM' | 'MN' | 'MO' | 'MP' | 'MQ' | 'MR' | 'MS' | 'MT' | 'MU' | 'MV' | 'MW' | 'MX' | 'MY' | 'MZ' | 'NA' | 'NC' | 'NE' | 'NF' | 'NG' | 'NI' | 'NL' | 'NO' | 'NP' | 'NR' | 'NU' | 'NZ' | 'OM' | 'PA' | 'PE' | 'PF' | 'PG' | 'PH' | 'PK' | 'PL' | 'PM' | 'PN' | 'PR' | 'PS' | 'PT' | 'PW' | 'PY' | 'QA' | 'RE' | 'RO' | 'RS' | 'RU' | 'RW' | 'SA' | 'SB' | 'SC' | 'SD' | 'SE' | 'SG' | 'SH' | 'SI' | 'SJ' | 'SK' | 'SL' | 'SM' | 'SN' | 'SO' | 'SR' | 'SS' | 'ST' | 'SV' | 'SX' | 'SY' | 'SZ' | 'TC' | 'TD' | 'TF' | 'TG' | 'TH' | 'TJ' | 'TK' | 'TL' | 'TM' | 'TN' | 'TO' | 'TR' | 'TT' | 'TV' | 'TW' | 'TZ' | 'UA' | 'UG' | 'UM' | 'US' | 'UY' | 'UZ' | 'VA' | 'VC' | 'VE' | 'VG' | 'VI' | 'VN' | 'VU' | 'WF' | 'WS' | 'YE' | 'YT' | 'ZA' | 'ZM' | 'ZW'; export type CreativeType = 'REGULAR' | 'VIDEO' | 'SHOPPING' | 'CAROUSEL' | 'MAX_VIDEO' | 'SHOP_THE_PIN' | 'COLLECTION' | 'IDEA' | 'SHOWCASE' | 'QUIZ' | 'COLLAGE' | 'MAX_WIDTH_REGULAR_COLLECTION' | 'MAX_WIDTH_VIDEO_COLLECTION' | 'APP'; 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 BidFloorObjectiveType = 'AWARENESS' | 'CONSIDERATION' | 'WEB_CONVERSION' | 'CATALOG_SALES' | 'VIDEO_COMPLETION' | 'SALES'; export type AttributionWindows = {click_window_days?: number | undefined, engagement_window_days?: number | undefined, view_window_days?: number | undefined}; export type ConversionEvent = 'PAGE_VISIT' | 'SIGNUP' | 'CHECKOUT' | 'CUSTOM' | 'VIEW_CATEGORY' | 'SEARCH' | 'ADD_TO_CART' | 'WATCH_VIDEO' | 'LEAD' | 'APP_INSTALL'; export type ConversionTagV3GoalMetadata = {attribution_windows?: AttributionWindows | undefined, conversion_event?: ConversionEvent | undefined, conversion_tag_id?: string | undefined, cpa_goal_value_in_micro_currency?: string | undefined, is_roas_optimized?: (boolean | null) | undefined, reporting_event?: string | undefined}; export type FrequencyGoalMetadataTimerange = 'THIRTY_DAY' | 'DAY' | 'SEVEN_DAY' | 'TWENTY_MINUTE' | 'TEN_MINUTE' | 'TWENTY_FOUR_HOUR'; export type FrequencyGoalMetadata = {frequency?: number | undefined, timerange?: FrequencyGoalMetadataTimerange | undefined}; export type ScrollupGoalMetadata = {scrollup_goal_value_in_micro_currency?: (string | null) | undefined}; export type OptimizationGoalMetadata = {conversion_tag_v3_goal_metadata?: ConversionTagV3GoalMetadata | undefined, frequency_goal_metadata?: FrequencyGoalMetadata | undefined, scrollup_goal_metadata?: ScrollupGoalMetadata | undefined}; export type BidFloorSpec = {billable_event: ActionType, countries?: Array | undefined, creative_type?: CreativeType | undefined, currency: Currency, objective_type?: BidFloorObjectiveType | undefined, optimization_goal_metadata?: OptimizationGoalMetadata | undefined}; 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 BidFloorCreate = {bid_floor_specs: Array, targeting_spec?: TargetingSpecOptimal | undefined}; export type CreateApiAdAccountsAdAccountIdBidFloorArgs = {ad_account_id: string, body: BidFloorCreate}; export type CreateApiAdAccountsAdAccountIdBidFloorOptions = {baseUrl?: string | undefined, pinterestOauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const bidFloor = z.object({bid_floors: z.array(z.number().int()).optional(), type: z.string().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 bid floors * * POST /ad_accounts/{ad_account_id}/bid_floor * * Also accepts auth: client_credentials */ export const createApiAdAccountsAdAccountIdBidFloor = async (args: CreateApiAdAccountsAdAccountIdBidFloorArgs, options?: CreateApiAdAccountsAdAccountIdBidFloorOptions) => { 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))}/bid_floor${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(bidFloor, await res.json()) };