import {z} from 'zod' export type GetApiQuotesIdentifierArgs = {identifier: string}; export type GetApiQuotesIdentifierOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const additionalContactFields = z.object({cell_phone: z.string().optional(), contact_roles: z.array(z.enum(["security_alerts", "policy_notices"])), email: z.string(), first_name: z.string().optional(), last_name: z.string().optional()}).and(z.record(z.string(), z.unknown())); export const addressFields = z.object({city: z.string(), state: z.string().optional(), street: z.string(), street2: z.string().optional(), zip: z.string()}); export const insuredMainContactFields = z.object({cell_phone: z.string().min(1).max(255).optional(), email: z.string().min(1).max(255), first_name: z.string().min(1).max(255).optional(), last_name: z.string().min(1).max(255).optional()}); export const clientFields = z.object({additional_contacts: z.array(additionalContactFields).optional(), address: addressFields, broker_email: z.string(), company_id: z.union([z.string(), z.number().int()]).optional(), dba: z.string().nullable().optional(), domains: z.array(z.string()).optional(), industry: z.string().optional(), insured_main_contact: insuredMainContactFields.optional(), name: z.string(), revenue: z.number().int().gte(1).lte(5000000000), secondary_broker_email: z.string().nullable().optional()}); export const contingencyFields = z.object({contingency_id: z.string().optional(), status: z.enum(["open", "closed"]).optional(), text: z.string().optional(), type: z.enum(["pre_bind", "post_bind"]).optional()}); export const coverageFields = z.object({coverage_name: z.string().optional(), limit: z.number().int().optional(), parameter: z.number().int().optional(), parameter_name: z.string().optional(), retention: z.number().int().optional()}); export const policyDates = z.object({effective: z.string(), expiration: z.string().optional(), mpl_retroactive_date: z.string().optional()}); export const surchargesFields = z.object({percentage: z.number().optional(), price: z.number().optional(), title: z.string().optional()}); export const getQuoteResponse = z.object({aggregate_limit: z.number().int().optional(), aggregate_retention: z.number().int().optional(), client: clientFields.optional(), company_id: z.string().optional(), contingencies: z.array(contingencyFields).optional(), coverages: z.array(coverageFields).optional(), dates: policyDates.optional(), error_occurred: z.boolean().optional(), filing_effective_from: z.string().optional(), filing_effective_until: z.string().optional(), insurance_product: z.enum(["CYB", "TEO", "MPL"]).optional(), per_claim_limit: z.number().int().optional(), per_claim_retention: z.number().int().optional(), policy_number: z.string().optional(), premium: z.number().int().optional(), quote_expiration_date: z.string().optional(), quote_identifier: z.string(), quote_status: z.enum(["created", "aq_created", "calculating", "calc_completed", "broker_conflict_referral", "collection_issue", "red_flags_found", "calc_failed", "authority_failed", "eligible", "quick_scan_error", "missing_quick_scan", "aq_not_approved", "offer_email", "stale", "quote_referred", "published", "quote_declined", "quote_expired", "quote_revoked", "quote_bind_requested", "quote_not_bound", "policy_active", "policy_canceled", "policy_expired"]).optional(), red_flags_exist: z.boolean().optional(), surcharges: z.array(surchargesFields).optional(), surcharges_effective_from: z.string().optional(), surcharges_effective_until: 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 }; /** * Find a quote * * GET /quotes/{identifier} */ export const getApiQuotesIdentifier = async (args: GetApiQuotesIdentifierArgs, options: GetApiQuotesIdentifierOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/quotes/${encodeURIComponent(String(args.identifier))}${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(getQuoteResponse, await res.json()) };