import {z} from 'zod' export type GetApiApplicationCartPricingArgs = {applicationId?: number | undefined}; export type GetApiApplicationCartPricingOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const appStatus = z.object({applicationId: z.string().nullable().optional(), status: z.string().nullable().optional(), approvalAmount: z.number().optional(), isOkToDeliver: z.boolean().optional(), merchantCustomerId: z.string().nullable().optional()}); export const pricingDetails = z.object({appStatus: appStatus.optional(), cashPrice: z.number().optional(), costOfRental: z.number().optional(), depositAmount: z.number().optional(), depositSalesTax: z.number().optional(), effectiveLeaseBalance: z.number().optional(), estimatedTax: z.number().optional(), initialBalance: z.number().optional(), initialBalancePlusEstimatedTax: z.number().optional(), initialPaymentAmount: z.number().optional(), invoiceTotal: z.number().optional(), invoiceRemaining: z.number().optional(), depositNet: z.number().optional(), nextPaymentDate: z.string().nullable().optional(), ninetyDayBuyout: z.number().optional(), totalNumberOfPayment: z.number().int().optional(), paymentFrequency: z.string().nullable().optional(), periodicPaymentAmount: z.number().optional(), salesTaxRate: z.number().optional(), sumOfAllPayments: z.number().optional(), termInMonths: z.number().int().optional(), totalLeasePrice: z.number().optional(), totalDueAtSigning: z.number().optional(), totalRetailerDeliveryFees: z.number().optional(), totalTax: z.number().optional()}); 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 pricing information * * GET /application/cart/pricing */ export const getApiApplicationCartPricing = async (args: GetApiApplicationCartPricingArgs, options?: GetApiApplicationCartPricingOptions) => { const {baseUrl = "https://capi.beta.progleasing.com/api/v1", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.applicationId != null) query.append("applicationId", String(args.applicationId)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/application/cart/pricing${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(pricingDetails, await res.json()) };