import {z} from 'zod' export type PinterestLibPaginationOrder = 'ASCENDING' | 'DESCENDING'; export type BillingInvoiceSortField = 'DUE_DATE' | 'BILLING_PERIOD' | 'DOCUMENT_TYPE' | 'TOTAL_AMOUNT' | 'INVOICE_NUMBER'; export type BillingInvoiceStatus = 'OPEN' | 'CLOSED'; export type BillingInvoiceDocumentType = 'INVOICE' | 'CREDIT_MEMO'; export type GetApiAdAccountsAdAccountIdBillingInvoicesArgs = {bookmark?: string | undefined, page_size?: number | undefined, order?: PinterestLibPaginationOrder | undefined, ad_account_id: string, sort?: BillingInvoiceSortField | undefined, status?: BillingInvoiceStatus | undefined, document_type?: BillingInvoiceDocumentType | undefined, start_due_date?: string | undefined, end_due_date?: string | undefined}; export type GetApiAdAccountsAdAccountIdBillingInvoicesOptions = {baseUrl?: string | undefined, pinterestOauth2?: string | undefined, fetch?: typeof fetch | undefined}; 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 billingInvoiceDocumentType = z.enum(["INVOICE", "CREDIT_MEMO"]); export const billingInvoiceStatus = z.enum(["OPEN", "CLOSED"]); export const billingInvoice = z.object({ad_account_id: z.string().optional(), ad_account_name: z.string().optional(), amount_billed_micro_currency: z.number().int().optional(), amount_discount_micro_currency: z.number().int().nullable().optional(), amount_net_micro_currency: z.number().int().nullable().optional(), amount_tax_micro_currency: z.number().int().nullable().optional(), bill_to_country: z.string().optional(), billing_period_end_date: z.string().optional(), billing_period_start_date: z.string().optional(), currency: currency.optional(), document_type: billingInvoiceDocumentType.optional(), id: z.string().optional(), invoice_due_date: z.string().optional(), payment_terms: z.string().optional(), status: billingInvoiceStatus.optional()}); export const getApiAdAccountsAdAccountIdBillingInvoicesResponse = z.object({bookmark: z.string().nullable().optional(), items: z.array(billingInvoice)}); 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 invoices * * GET /ad_accounts/{ad_account_id}/billing_invoices */ export const getApiAdAccountsAdAccountIdBillingInvoices = async (args: GetApiAdAccountsAdAccountIdBillingInvoicesArgs, options?: GetApiAdAccountsAdAccountIdBillingInvoicesOptions) => { 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 (args.sort != null) query.append("sort", String(args.sort)) if (args.status != null) query.append("status", String(args.status)) if (args.document_type != null) query.append("document_type", String(args.document_type)) if (args.start_due_date != null) query.append("start_due_date", String(args.start_due_date)) if (args.end_due_date != null) query.append("end_due_date", String(args.end_due_date)) if (pinterestOauth2 !== undefined) headers.set('authorization', `Bearer ${pinterestOauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/ad_accounts/${encodeURIComponent(String(args.ad_account_id))}/billing_invoices${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(getApiAdAccountsAdAccountIdBillingInvoicesResponse, await res.json()) };