import {z} from 'zod' export type ElementCreateRequest = {type: string, parentLocator?: unknown | undefined, elements: Array, coverageTerms?: {[key: string]: Record} | undefined, data?: {[key: string]: Record} | undefined, staticLocator?: unknown | undefined}; 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 Preferences = {installmentPreferences?: InstallmentPreferences | undefined}; export type ContactRoles = {contactLocator: unknown, roles: Array}; export type QuoteCreateRequest = {productName: string, accountLocator: unknown, startTime: string, endTime?: string | undefined, expirationTime?: string | undefined, currency?: string | undefined, timezone?: string | undefined, jurisdiction?: string | undefined, coverageTerms?: {[key: string]: Record} | undefined, data: {[key: string]: Record}, elements?: Array | undefined, durationBasis?: ('years' | 'months' | 'weeks' | 'days' | 'hours') | undefined, preferences?: Preferences | undefined, delinquencyPlanName?: string | undefined, autoRenewalPlanName?: string | undefined, billingLevel?: ('account' | 'inherit' | 'policy') | undefined, region?: string | undefined, quoteGroupLocator?: unknown | undefined, static?: {[key: string]: Record} | undefined, contacts: Array, invoiceFeeAmount: number, termDuration?: number | undefined, startWithTermNumber?: number | undefined, producerCode?: string | undefined, proxyPayerLocator?: unknown | undefined}; export type CreateApiPolicyTenantLocatorQuotesValidatePreviewArgs = {tenantLocator: string, body: QuoteCreateRequest}; export type CreateApiPolicyTenantLocatorQuotesValidatePreviewOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const elementResponse: z.ZodType = z.object({type: z.string(), locator: z.string(), parentLocator: z.string(), elements: z.array(z.lazy(() => elementResponse)).optional(), coverageTerms: z.record(z.string(), z.object({})).optional(), data: z.record(z.string(), z.object({})).optional(), staticLocator: z.string(), originalEffectiveTime: z.string(), category: z.enum(["product", "coverage", "exposure", "exposureGroup", "policyLine"]).optional()}); export const installmentPreferences = z.object({cadence: z.enum(["none", "fullPay", "weekly", "everyOtherWeek", "monthly", "quarterly", "semiannually", "annually", "thirtyDays", "everyNDays"]).optional(), anchorMode: z.enum(["generateDay", "termStartDay", "dueDay"]).optional(), generateLeadDays: z.number().int().optional(), dueLeadDays: z.number().int().optional(), installmentWeights: z.array(z.number()), maxInstallmentsPerTerm: z.number().int().optional(), installmentPlanName: z.string().optional(), anchorType: z.enum(["none", "dayOfMonth", "anchorTime", "dayOfWeek", "weekOfMonth"]).optional(), dayOfMonth: z.number().int().optional(), dayOfWeek: z.enum(["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]).optional(), weekOfMonth: z.enum(["none", "first", "second", "third", "fourth", "fifth"]).optional(), anchorTime: z.string().optional(), autopayLeadDays: z.number().optional()}); export const preferences = z.object({installmentPreferences: installmentPreferences.optional()}); export const validationItemResponse = z.object({elementType: z.string(), locator: z.string(), errors: z.array(z.string())}); export const validationResult = z.object({validationItems: z.array(validationItemResponse).optional(), success: z.boolean()}); export const contactRoles = z.object({contactLocator: z.string(), roles: z.array(z.string())}); export const quoteResponse = z.object({locator: z.string(), quoteState: z.enum(["draft", "validated", "earlyUnderwritten", "priced", "underwritten", "accepted", "issued", "underwrittenBlocked", "declined", "rejected", "refused", "discarded"]), productName: z.string(), accountLocator: z.string(), startTime: z.string().optional(), endTime: z.string().optional(), timezone: z.string().optional(), currency: z.string().optional(), underwritingStatus: z.string().optional(), expirationTime: z.string().optional(), element: elementResponse, preferences: preferences.optional(), policyLocator: z.string().optional(), delinquencyPlanName: z.string().optional(), durationBasis: z.enum(["years", "months", "weeks", "days", "hours"]).optional(), groupLocator: z.string().optional(), autoRenewalPlanName: z.string().optional(), billingLevel: z.enum(["account", "inherit", "policy"]), region: z.string().optional(), quoteNumber: z.string().optional(), duration: z.number().optional(), acceptedTime: z.string().optional(), issuedTime: z.string().optional(), validationResult: validationResult.optional(), quickQuoteLocator: z.string().optional(), contacts: z.array(contactRoles), anonymizedAt: z.string().optional(), invoiceFeeAmount: z.number().optional(), createdBy: z.string().optional(), createdAt: z.string().optional(), jurisdiction: z.string().optional(), producerCode: z.string().optional(), reservedPolicyNumber: z.string().optional(), proxyPayerLocator: z.string().optional(), continuationOfLocator: z.string().optional(), startWithTermNumber: z.number().int().optional(), static: z.record(z.string(), z.object({})).optional(), policyNumber: z.string().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 }; /** * POST /policy/{tenantLocator}/quotes/validatePreview */ export const createApiPolicyTenantLocatorQuotesValidatePreview = async (args: CreateApiPolicyTenantLocatorQuotesValidatePreviewArgs, options: CreateApiPolicyTenantLocatorQuotesValidatePreviewOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/policy/${encodeURIComponent(String(args.tenantLocator))}/quotes/validatePreview${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(quoteResponse, await res.json()) };