import {z} from 'zod' export type InstallmentPreferences = {cadence?: ('none' | 'fullPay' | 'weekly' | 'everyOtherWeek' | 'monthly' | 'quarterly' | 'semiannually' | 'annually' | 'thirtyDays' | 'everyNDays') | undefined, anchorMode?: ('generateDay' | 'termStartDay' | 'dueDay') | undefined, generateLeadDays?: number | undefined, dueLeadDays?: number | undefined, installmentWeights: Array, maxInstallmentsPerTerm?: number | undefined, installmentPlanName?: string | undefined, anchorType?: ('none' | 'dayOfMonth' | 'anchorTime' | 'dayOfWeek' | 'weekOfMonth') | undefined, dayOfMonth?: number | undefined, dayOfWeek?: ('monday' | 'tuesday' | 'wednesday' | 'thursday' | 'friday' | 'saturday' | 'sunday') | undefined, weekOfMonth?: ('none' | 'first' | 'second' | 'third' | 'fourth' | 'fifth') | undefined, anchorTime?: string | undefined, autopayLeadDays?: number | undefined}; export type PreviewChargeRequest = {amount: number, chargeType: string, elementLocator?: unknown | undefined, elementStaticLocator?: unknown | undefined}; export type QuoteBillingPreviewRequest = {accountLocator: unknown, productName: string, termStartTime: string, termEndTime: string, timezone?: string | undefined, durationBasis?: ('years' | 'months' | 'weeks' | 'days' | 'hours') | undefined, currency?: string | undefined, invoiceFeeAmount?: number | undefined, installmentPreferences?: InstallmentPreferences | undefined, charges: Array}; export type GetApiBillingTenantLocatorInvoicesQuotesStatelessPreviewArgs = {tenantLocator: string, quotePreviewRequest: QuoteBillingPreviewRequest, includeZeroAmountInvoices?: boolean | undefined, count?: number | undefined}; export type GetApiBillingTenantLocatorInvoicesQuotesStatelessPreviewOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const invoiceItemPreview = z.object({chargeType: z.string(), chargeCategory: z.string(), amount: z.number(), quoteLocator: z.string().optional(), policyLocator: z.string().optional(), elementType: z.string(), elementStaticLocator: z.string(), transactionLocators: z.array(z.string())}); export const invoicePreviewResponse = z.object({generateTime: z.string(), dueTime: z.string(), startTime: z.string(), endTime: z.string(), invoiceItems: z.array(invoiceItemPreview), totalAmount: z.number(), autopayTime: z.string(), installmentLocators: z.array(z.string())}); export const getApiBillingTenantLocatorInvoicesQuotesStatelessPreviewResponse = z.array(invoicePreviewResponse); 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/{tenantLocator}/invoices/quotes/statelessPreview */ export const getApiBillingTenantLocatorInvoicesQuotesStatelessPreview = async (args: GetApiBillingTenantLocatorInvoicesQuotesStatelessPreviewArgs, options: GetApiBillingTenantLocatorInvoicesQuotesStatelessPreviewOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.quotePreviewRequest != null) query.append("quotePreviewRequest", JSON.stringify(args.quotePreviewRequest)) if (args.includeZeroAmountInvoices != null) query.append("includeZeroAmountInvoices", String(args.includeZeroAmountInvoices)) if (args.count != null) query.append("count", String(args.count)) const url = `${baseUrl.replace(/\/+$/, '')}/billing/${encodeURIComponent(String(args.tenantLocator))}/invoices/quotes/statelessPreview${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(getApiBillingTenantLocatorInvoicesQuotesStatelessPreviewResponse, await res.json()) };