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 CreateApiPolicyTenantLocatorQuickquotesArgs = {tenantLocator: string, body: QuickQuoteCreateRequest}; export type CreateApiPolicyTenantLocatorQuickquotesOptions = {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 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 quickQuoteResponse = z.object({locator: z.string(), quickQuoteState: z.enum(["draft", "validated", "priced", "quoted", "discarded"]), productName: z.string(), accountLocator: z.string().optional(), startTime: z.string().optional(), endTime: z.string().optional(), duration: z.number().optional(), expirationTime: z.string().optional(), timezone: z.string().optional(), currency: z.string().optional(), durationBasis: z.enum(["years", "months", "weeks", "days", "hours"]).optional(), groupLocator: z.string().optional(), element: elementResponse, validationResult: validationResult.optional(), contacts: z.array(contactRoles), createdBy: z.string().optional(), createdAt: z.string().optional(), anonymizedAt: z.string().optional(), jurisdiction: 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}/quickquotes */ export const createApiPolicyTenantLocatorQuickquotes = async (args: CreateApiPolicyTenantLocatorQuickquotesArgs, options: CreateApiPolicyTenantLocatorQuickquotesOptions) => { 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${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(quickQuoteResponse, await res.json()) };