import {z} from 'zod' export type GetApiAdAccountsAdAccountIdAdsCreditDiscountsArgs = {ad_account_id: string, bookmark?: string | undefined, page_size?: number | undefined}; export type GetApiAdAccountsAdAccountIdAdsCreditDiscountsOptions = {baseUrl?: string | undefined, pinterestOauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const adsCreditDiscountType = z.enum(["COUPON", "CREDIT", "COUPON_APPLIED", "CREDIT_APPLIED", "MARKETING_OFFER_CREDIT", "MARKETING_OFFER_CREDIT_APPLIED", "GOODWILL_CREDIT", "GOODWILL_CREDIT_APPLIED", "INTERNAL_CREDIT", "INTERNAL_CREDIT_APPLIED", "PREPAID_CREDIT", "PREPAID_CREDIT_APPLIED", "SALES_INCENTIVE_CREDIT", "SALES_INCENTIVE_CREDIT_APPLIED", "CREDIT_EXPIRED", "FUTURE_CREDIT", "REFERRAL_CREDIT", "INVOICE_SALES_INCENTIVE_CREDIT", "INVOICE_SALES_INCENTIVE_CREDIT_APPLIED", "PREPAID_CREDIT_REFUND", "null"]).nullable(); export const adsCreditDiscountsResponse = z.object({active: z.boolean().optional(), advertiser_id: z.string().optional(), discountCurrency: z.string().nullable().optional(), discountInMicroCurrency: z.number().nullable().optional(), discountType: adsCreditDiscountType.nullable().optional(), remainingDiscountInMicroCurrency: z.number().nullable().optional(), title: z.string().nullable().optional()}); export const getApiAdAccountsAdAccountIdAdsCreditDiscountsResponse = z.object({bookmark: z.string().nullable().optional(), items: z.array(adsCreditDiscountsResponse)}); 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 ads credit discounts * * GET /ad_accounts/{ad_account_id}/ads_credit/discounts */ export const getApiAdAccountsAdAccountIdAdsCreditDiscounts = async (args: GetApiAdAccountsAdAccountIdAdsCreditDiscountsArgs, options?: GetApiAdAccountsAdAccountIdAdsCreditDiscountsOptions) => { 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 (pinterestOauth2 !== undefined) headers.set('authorization', `Bearer ${pinterestOauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/ad_accounts/${encodeURIComponent(String(args.ad_account_id))}/ads_credit/discounts${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(getApiAdAccountsAdAccountIdAdsCreditDiscountsResponse, await res.json()) };