import {z} from 'zod' export type CustomFields = Record | null; export type CreateApiAccidentsArgs = {body: {vehicle_id: number, driver_id: number, occurred_at: string, street_address?: (string | null) | undefined, street_address_line_2?: (string | null) | undefined, city: string, region: string, postal_code?: (string | null) | undefined, country?: (string | null) | undefined, latitude?: (number | null) | undefined, longitude?: (number | null) | undefined, detection_source?: ('telematics_crash_detection' | 'driver_self_report' | 'dashcam_safety_event' | 'third_party_report' | 'customer_report' | 'insurance_notification' | null) | undefined, weather?: ('clear' | 'rain' | 'snow' | 'fog' | 'sleet' | 'other' | null) | undefined, road_condition?: ('dry' | 'wet' | 'icy' | 'snow_covered' | 'debris' | 'other' | null) | undefined, accident_type: 'rear_end' | 'side_impact' | 'head_on' | 'single_vehicle' | 'backing' | 'parking_lot' | 'hit_and_run' | 'weather_natural_event' | 'vandalism_theft' | 'jobsite_damage' | 'other', damage_description: string, driver_statement?: (string | null) | undefined, injuries_reported: 'no_injuries' | 'minor' | 'serious' | 'fatal', police_report_number?: (string | null) | undefined, other_party_name?: (string | null) | undefined, other_party_phone?: (string | null) | undefined, other_party_insurance?: (string | null) | undefined, other_party_vehicle?: (string | null) | undefined, witness_name?: (string | null) | undefined, witness_contact?: (string | null) | undefined, additional_notes?: (string | null) | undefined, insurance_claim_number?: (string | null) | undefined, units_involved?: (number | null) | undefined, total_injured?: (number | null) | undefined, driver_at_fault?: (boolean | null) | undefined, total_loss?: boolean | undefined, vehicle_operable?: (boolean | null) | undefined, external_id?: (string | null) | undefined, assigned_contact_ids?: Array | undefined, custom_fields?: CustomFields | undefined}}; export type CreateApiAccidentsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, accountToken?: string | undefined, fetch?: typeof fetch | undefined}; export const customFields = z.object({}).nullable(); export const vehicleMini = z.object({id: z.number().int(), name: z.string(), color: z.string().nullable().optional(), license_plate: z.string().nullable().optional(), vin: z.string().nullable().optional(), year: z.string().nullable().optional(), make: z.string().nullable().optional(), model: z.string().nullable().optional(), trim: z.string().nullable().optional(), registration_expiration_month: z.number().int().gte(0).lte(12).nullable().optional(), registration_state: z.string().nullable().optional(), default_image_url_small: z.string().nullable().optional()}); export const contactMini = z.object({id: z.number().int().optional(), email: z.string().nullable().optional(), name: z.string().nullable().optional(), group_id: z.number().int().nullable().optional(), employee: z.boolean().nullable().optional(), employee_number: z.string().nullable().optional(), default_image_url: z.string().nullable().optional()}).nullable(); export const accident = z.object({id: z.number().int(), state: z.enum(["open", "closed"]), vehicle_id: z.number().int(), driver_id: z.number().int().nullable().optional(), occurred_at: z.string().nullable().optional(), opened_at: z.string().nullable().optional(), closed_at: z.string().nullable().optional(), closed_by_id: z.number().int().nullable().optional(), closed_note: z.string().nullable().optional(), created_by_id: z.number().int().nullable().optional(), street_address: z.string().nullable().optional(), street_address_line_2: z.string().nullable().optional(), city: z.string().nullable().optional(), region: z.string().nullable().optional(), postal_code: z.string().nullable().optional(), country: z.string().nullable().optional(), latitude: z.number().nullable().optional(), longitude: z.number().nullable().optional(), detection_source: z.enum(["telematics_crash_detection", "driver_self_report", "dashcam_safety_event", "third_party_report", "customer_report", "insurance_notification"]).nullable().optional(), weather: z.enum(["clear", "rain", "snow", "fog", "sleet", "other"]).nullable().optional(), road_condition: z.enum(["dry", "wet", "icy", "snow_covered", "debris", "other"]).nullable().optional(), accident_type: z.enum(["rear_end", "side_impact", "head_on", "single_vehicle", "backing", "parking_lot", "hit_and_run", "weather_natural_event", "vandalism_theft", "jobsite_damage", "other"]).nullable().optional(), damage_description: z.string().nullable().optional(), driver_statement: z.string().nullable().optional(), injuries_reported: z.enum(["no_injuries", "minor", "serious", "fatal"]).nullable().optional(), police_report_number: z.string().nullable().optional(), other_party_name: z.string().nullable().optional(), other_party_phone: z.string().nullable().optional(), other_party_insurance: z.string().nullable().optional(), other_party_vehicle: z.string().nullable().optional(), witness_name: z.string().nullable().optional(), witness_contact: z.string().nullable().optional(), additional_notes: z.string().nullable().optional(), insurance_claim_number: z.string().nullable().optional(), units_involved: z.number().int().nullable().optional(), total_injured: z.number().int().nullable().optional(), driver_at_fault: z.boolean().nullable().optional(), total_loss: z.boolean().optional(), vehicle_operable: z.boolean().nullable().optional(), external_id: z.string().nullable().optional(), comments_count: z.number().int().optional(), documents_count: z.number().int().optional(), images_count: z.number().int().optional(), vehicle_archived_at: z.string().nullable().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), work_order_id: z.number().int().nullable().optional(), service_entry_id: z.number().int().nullable().optional(), assigned_contact_ids: z.array(z.number().int()).optional(), custom_fields: customFields.optional(), vehicle: vehicleMini.optional(), driver: contactMini.nullable().optional(), assigned_contacts: z.array(contactMini).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 Accident * * POST /accidents */ export const createApiAccidents = async (args: CreateApiAccidentsArgs, options?: CreateApiAccidentsOptions) => { const {baseUrl = "https://secure.fleetio.com/api", fetch: fetchFn = fetch, apiKey, accountToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("Authorization", apiKey) if (accountToken !== undefined) headers.set("Account-Token", accountToken) const url = `${baseUrl.replace(/\/+$/, '')}/accidents${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(accident, await res.json()) };