import {z} from 'zod' export type GetApiV1CarriersDotNumberInsuranceArgs = {dot_number: string, limit?: number | undefined, page?: number | undefined, sort_by?: ('effective_date' | 'insurance_type_code' | 'bipd_maximum_dollar_limit' | 'insurance_company_name') | undefined, sort_order?: ('asc' | 'desc') | undefined, insurance_type?: string | undefined, insurer?: string | undefined, min_coverage?: number | undefined, date_from?: string | undefined, date_to?: string | undefined}; export type GetApiV1CarriersDotNumberInsuranceOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const insurancePolicy = z.object({id: z.number().int().optional(), docket_number: z.string().optional(), dot_number: z.string().optional(), mc_number: z.number().int().nullable().optional(), insurance_type: z.object({code: z.string().optional(), description: z.string().optional()}).optional(), bipd: z.object({class_code: z.string().nullable().optional(), class_description: z.string().nullable().optional(), maximum_dollar_limit: z.number().int().nullable().optional(), underlying_dollar_limit: z.number().int().nullable().optional()}).optional(), policy_number: z.string().nullable().optional(), effective_date: z.string().optional(), form: z.object({code: z.string().optional(), description: z.string().optional()}).optional(), insurance_company_name: z.string().optional()}); export const pagination = z.object({page: z.number().int().optional(), limit: z.number().int().optional(), total_results: z.number().int().optional(), total_pages: z.number().int().optional()}); export const insuranceResponse = z.object({dot_number: z.string().optional(), mc_number: z.number().int().optional(), total_policies: z.number().int().optional(), insurance: z.array(insurancePolicy).optional(), pagination: pagination.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 }; /** * Get carrier insurance filings * * GET /v1/carriers/{dot_number}/insurance */ export const getApiV1CarriersDotNumberInsurance = async (args: GetApiV1CarriersDotNumberInsuranceArgs, options?: GetApiV1CarriersDotNumberInsuranceOptions) => { const {baseUrl = "https://api.runalphaloops.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.page != null) query.append("page", String(args.page)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.sort_order != null) query.append("sort_order", String(args.sort_order)) if (args.insurance_type != null) query.append("insurance_type", String(args.insurance_type)) if (args.insurer != null) query.append("insurer", String(args.insurer)) if (args.min_coverage != null) query.append("min_coverage", String(args.min_coverage)) if (args.date_from != null) query.append("date_from", String(args.date_from)) if (args.date_to != null) query.append("date_to", String(args.date_to)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/carriers/${encodeURIComponent(String(args.dot_number))}/insurance${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(insuranceResponse, await res.json()) };