import {z} from 'zod' export type AdPreviewSourceImage = {image_url: string, promotion_id?: string | undefined, title: string}; export type AdPinPreviewCreativeType = 'SHOPPING' | 'COLLECTION' | 'MAX_VIDEO' | 'MAX_WIDTH_VIDEO_COLLECTION' | 'MAX_WIDTH_REGULAR_COLLECTION'; export type AdPreviewSourcePinId = {creative_type?: AdPinPreviewCreativeType | undefined, pin_id: string}; export type AdShoppingPreviewCreativeType = 'SHOPPING' | 'COLLECTION' | 'CAROUSEL' | 'MAX_WIDTH_COLLECTION'; export type CustomizableCTAType = 'GET_OFFER' | 'LEARN_MORE' | 'ORDER_NOW' | 'SHOP_NOW' | 'SIGN_UP' | 'SUBSCRIBE' | 'BUY_NOW' | 'CONTACT_US' | 'GET_QUOTE' | 'VISIT_SITE' | 'APPLY_NOW' | 'BOOK_NOW' | 'REQUEST_DEMO' | 'REGISTER_NOW' | 'FIND_A_DEALER' | 'ADD_TO_CART' | 'WATCH_NOW' | 'READ_MORE' | 'BUY_TICKETS' | 'DONATE_NOW' | 'DOWNLOAD' | 'EXPLORE_MORE' | 'FIND_A_LOCATION' | 'FIND_RETAILERS' | 'GET_DEAL' | 'GET_RECIPE' | 'GET_SHOWTIMES' | 'ON_SALE' | 'PLAY_GAME' | 'TRY_IT' | 'TAKE_A_PEEK' | 'null' | null; export type BasePreferredMediaType = 'VIDEO' | 'IMAGE'; export type AdPreviewShopping = {catalog_product_group_id: string, creative_type: AdShoppingPreviewCreativeType, customizable_cta_type?: CustomizableCTAType | undefined, hero_image_title?: string | undefined, hero_image_url?: string | undefined, hero_pin_id?: string | undefined, image_tag?: string | undefined, item_id?: string | undefined, preferred_media_type?: BasePreferredMediaType | undefined, show_promotion?: boolean | undefined, video_tag?: string | undefined}; export type AdPreviewRequest = AdPreviewSourceImage | AdPreviewSourcePinId | AdPreviewShopping; export type CreateApiAdAccountsAdAccountIdAdPreviewsArgs = {ad_account_id: string, body: AdPreviewRequest}; export type CreateApiAdAccountsAdAccountIdAdPreviewsOptions = {baseUrl?: string | undefined, pinterestOauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const adPreviewURLResponse = z.object({url: 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 }; /** * Create ad preview with pin or image * * POST /ad_accounts/{ad_account_id}/ad_previews */ export const createApiAdAccountsAdAccountIdAdPreviews = async (args: CreateApiAdAccountsAdAccountIdAdPreviewsArgs, options?: CreateApiAdAccountsAdAccountIdAdPreviewsOptions) => { 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_previews${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(adPreviewURLResponse, await res.json()) };