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 ContactRoles = {contactLocator: unknown, roles: Array}; export type QuickQuoteCreateRequest = {productName: string, startTime?: string | undefined, 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, contacts: Array}; export type CreateApiPolicyTenantLocatorQuickquotesPricePreviewArgs = {tenantLocator: string, body: QuickQuoteCreateRequest}; export type CreateApiPolicyTenantLocatorQuickquotesPricePreviewOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const policyChargeResponse = z.object({locator: z.string(), elementLocator: z.string(), chargeType: z.string(), chargeCategory: z.enum(["none", "premium", "tax", "fee", "credit", "invoiceFee", "cededPremium", "nonFinancial", "surcharge"]), amount: z.number(), rate: z.number(), referenceRate: z.number(), tag: z.string().optional(), rateDifference: z.number().optional(), elementStaticLocator: z.string(), reversalOfLocator: z.string().optional(), handling: z.enum(["flat", "normal", "retention"]), invoicing: z.enum(["scheduled", "next", "immediate"])}); 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 quickQuotePriceResponse = z.object({tenantLocator: z.string(), quickQuoteLocator: z.string(), accountLocator: z.string().optional(), quickQuoteState: z.enum(["draft", "validated", "priced", "quoted", "discarded"]), productName: z.string(), startTime: z.string(), endTime: z.string(), duration: z.number(), durationBasis: z.enum(["years", "months", "weeks", "days", "hours"]), items: z.array(policyChargeResponse), validationResult: validationResult.optional(), state: z.enum(["draft", "validated", "priced", "quoted", "discarded"])}); 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}/quickquotes/pricePreview */ export const createApiPolicyTenantLocatorQuickquotesPricePreview = async (args: CreateApiPolicyTenantLocatorQuickquotesPricePreviewArgs, options: CreateApiPolicyTenantLocatorQuickquotesPricePreviewOptions) => { 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))}/quickquotes/pricePreview${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(quickQuotePriceResponse, await res.json()) };