import {z} from 'zod' export type AdditionalContactFields = {cell_phone?: string | undefined, contact_roles: Array<'security_alerts' | 'policy_notices'>, email: string, first_name?: string | undefined, last_name?: string | undefined} | {[key: string]: unknown}; export type AddressFields = {city: string, state?: string | undefined, street: string, street2?: string | undefined, zip: string}; export type InsuredMainContactFields = {cell_phone?: string | undefined, email: string, first_name?: string | undefined, last_name?: string | undefined}; export type ClientFields = {additional_contacts?: Array | undefined, address: AddressFields, broker_email: string, company_id?: (string | number) | undefined, dba?: (string | null) | undefined, domains?: Array | undefined, industry?: string | undefined, insured_main_contact?: InsuredMainContactFields | undefined, name: string, revenue: number, secondary_broker_email?: (string | null) | undefined}; export type PolicyDates = {effective: string, expiration?: string | undefined, mpl_retroactive_date?: string | undefined}; export type QuoteFields = {aggregate_limit: 100000 | 250000 | 300000 | 500000 | 750000 | 900000 | 1000000 | 1500000 | 2000000 | 2500000 | 3000000 | 4000000 | 5000000 | 6000000 | 7000000 | 8000000 | 9000000 | 10000000, aggregate_retention?: (2500 | 5000 | 10000 | 15000 | 25000 | 50000 | 75000 | 100000 | 150000 | 250000 | 500000) | undefined, per_claim_limit?: number | undefined, per_claim_retention?: (500 | 1000 | 1500 | 2500 | 5000 | 7500 | 10000 | 15000 | 20000 | 25000 | 35000 | 50000 | 75000 | 100000 | 150000 | 250000 | 500000) | undefined}; export type PolicyFields = {dates?: PolicyDates | undefined, insurance_product?: ('CYB' | 'TEO' | 'MPL') | undefined, program_code?: (string | null) | undefined, quotes?: Array | undefined}; export type QuestionFields = {id?: string | undefined, value?: (string | number | boolean) | undefined}; export type CreateApiQuotesArgs = {body: {client: ClientFields, policy: PolicyFields, questions: Array}}; export type CreateApiQuotesOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createQuoteResponse = z.object({aggregate_limit: z.number().int().optional(), aggregate_retention: z.number().int().optional(), per_claim_limit: z.number().int().optional(), per_claim_retention: z.number().int().optional(), quote_identifier: z.string(), requested_aggregate_limit: z.number().int(), requested_aggregate_retention: z.number().int().optional(), requested_per_claim_limit: z.number().int().optional(), requested_per_claim_retention: z.number().int().optional()}); export const createQuotesResponse = z.object({company_id: z.string().optional(), quotes: z.array(createQuoteResponse).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 }; /** * Create new quotes * * POST /quotes */ export const createApiQuotes = async (args: CreateApiQuotesArgs, options: CreateApiQuotesOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/quotes${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(createQuotesResponse, await res.json()) };