import {z} from 'zod' export type PinterestLibPaginationOrder = 'ASCENDING' | 'DESCENDING'; export type GetApiAdAccountsAdAccountIdPromotionsArgs = {bookmark?: string | undefined, page_size?: number | undefined, order?: PinterestLibPaginationOrder | undefined, ad_account_id: string}; export type GetApiAdAccountsAdAccountIdPromotionsOptions = {baseUrl?: string | undefined, pinterestOauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const discountStatus = z.enum(["OTHER", "ACTIVE", "PAUSED", "SCHEDULED", "EXPIRED"]); export const promotionType = z.enum(["VARIABLE", "SITEWIDE", "CHECKOUT", "SAVE_X_ON_Y", "BUY_X_GET_Y", "SPEND_X_SAVE_Y", "FREE_SHIPPING", "FREE_SHIPPING_MINIMUM", "FREE_SHIPPING_WITH_DISCOUNT", "SITEWIDE_IN_STORES", "EXTRA_PERCENT_OFF", "GIFT_WITH_PURCHASE", "GIFT_WITH_PURCHASE_MINIMUM", "FIXED", "PERCENT_OFF_CLEARANCE", "X_OFF_Y", "GIFT_WITH_FIRST_PURCHASE", "BUY_X_GET_ONE_FREE", "CASH_BACK", "POINTS_ON_ALL_PURCHASES", "BONUS", "POINTS_WITH_PURCHASE", "CUSTOM"]); export const entityStatus = z.enum(["ACTIVE", "PAUSED", "ARCHIVED", "DRAFT", "DELETED_DRAFT"]); 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 promotionTemplateValue = z.object({amount: z.number().optional(), currency_code: currency.optional(), custom_text: z.string().optional(), percent: z.number().optional()}); export const promotion = z.object({ad_account_id: z.string(), discount_status: discountStatus.optional(), end_time: z.number().int().optional(), external_id: z.string().max(64).optional(), id: z.string().max(18), platform_type: z.string().optional(), promotion_code: z.string().optional(), promotion_custom_id: z.string().max(50).optional(), promotion_title: z.string(), promotion_type: promotionType, start_time: z.number().int().optional(), status: entityStatus.optional(), template_values: z.array(promotionTemplateValue).optional()}); export const getApiAdAccountsAdAccountIdPromotionsResponse = z.object({bookmark: z.string().nullable().optional(), items: z.array(promotion)}); 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 promotions * * GET /ad_accounts/{ad_account_id}/promotions */ export const getApiAdAccountsAdAccountIdPromotions = async (args: GetApiAdAccountsAdAccountIdPromotionsArgs, options?: GetApiAdAccountsAdAccountIdPromotionsOptions) => { const {baseUrl = "https://api.pinterest.com/v5", fetch: fetchFn = fetch, pinterestOauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.bookmark != null) query.append("bookmark", String(args.bookmark)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.order != null) query.append("order", String(args.order)) if (pinterestOauth2 !== undefined) headers.set('authorization', `Bearer ${pinterestOauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/ad_accounts/${encodeURIComponent(String(args.ad_account_id))}/promotions${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiAdAccountsAdAccountIdPromotionsResponse, await res.json()) };