import {z} from 'zod' export type GetApiAdAccountsAdAccountIdBillingProfilesArgs = {is_active: boolean, ad_account_id: string, bookmark?: string | undefined, page_size?: number | undefined}; export type GetApiAdAccountsAdAccountIdBillingProfilesOptions = {baseUrl?: string | undefined, pinterestOauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const billingType = z.enum(["CREDIT_CARD", "INVOICE", "INTERNAL", "RECURRING", "PREPAID"]); export const billingProfileCardType = z.enum(["UNKNOWN", "VISA", "MASTERCARD", "AMERICAN_EXPRESS", "DISCOVER", "ELO"]); export const billingProfilePaymentMethodBrand = z.enum(["UNKNOWN", "VISA", "MASTERCARD", "AMERICAN_EXPRESS", "DISCOVER", "SOFORT", "DINERS_CLUB", "ELO", "CARTE_BANCAIRE"]); export const billingProfileStatus = z.enum(["UNSPECIFIED", "VALID", "INVALID", "PENDING", "DELETED", "SECONDARY", "PENDING_SECONDARY"]); export const billingProfilesResponse = z.object({advertiser_id: z.string().optional(), billing_type: billingType.optional(), card_type: billingProfileCardType.optional(), id: z.string().optional(), payment_method_brand: billingProfilePaymentMethodBrand.optional(), status: billingProfileStatus.optional()}); export const getApiAdAccountsAdAccountIdBillingProfilesResponse = z.object({bookmark: z.string().nullable().optional(), items: z.array(billingProfilesResponse)}); 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 billing profiles * * GET /ad_accounts/{ad_account_id}/billing_profiles */ export const getApiAdAccountsAdAccountIdBillingProfiles = async (args: GetApiAdAccountsAdAccountIdBillingProfilesArgs, options?: GetApiAdAccountsAdAccountIdBillingProfilesOptions) => { 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.is_active != null) query.append("is_active", String(args.is_active)) 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))}/billing_profiles${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(getApiAdAccountsAdAccountIdBillingProfilesResponse, await res.json()) };