import {z} from 'zod' export type EqFilter = {eq: string}; export type IncludeFilter = {include: string}; export type EqLtLteGtGteFilter = {eq: string} | {lt: string} | {lte: string} | {gt: string} | {gte: string}; export type LtLteGtGteFilter = {lt: string} | {lte: string} | {gt: string} | {gte: string}; export type SortAscDesc = 'asc' | 'desc'; export type SortAscDescDefaultDesc = 'asc' | 'desc'; export type GetApiVehiclesIdIssuesArgs = {id: string, start_cursor?: string | undefined, per_page?: number | undefined, filter?: ({number?: EqFilter | undefined, reported_by_id?: EqFilter | undefined, assigned_contact_ids?: IncludeFilter | undefined, labels?: IncludeFilter | undefined, state?: IncludeFilter | undefined, due_date?: EqLtLteGtGteFilter | undefined, resolved_at?: LtLteGtGteFilter | undefined, reported_at?: LtLteGtGteFilter | undefined, closed_at?: LtLteGtGteFilter | undefined, reopened_at?: LtLteGtGteFilter | undefined, created_at?: LtLteGtGteFilter | undefined, updated_at?: LtLteGtGteFilter | undefined}) | undefined, sort?: ({due_date?: SortAscDesc | undefined, resolved_at?: SortAscDesc | undefined, reported_at?: SortAscDesc | undefined, updated_at?: SortAscDesc | undefined, created_at?: SortAscDesc | undefined, id?: SortAscDescDefaultDesc | undefined}) | undefined}; export type GetApiVehiclesIdIssuesOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, accountToken?: string | undefined, fetch?: typeof fetch | undefined}; export const eqFilter = z.object({eq: z.string()}); export const includeFilter = z.object({include: z.string()}); export const eqLtLteGtGteFilter = z.union([z.object({eq: z.string()}), z.object({lt: z.string()}), z.object({lte: z.string()}), z.object({gt: z.string()}), z.object({gte: z.string()})]); export const ltLteGtGteFilter = z.union([z.object({lt: z.string()}), z.object({lte: z.string()}), z.object({gt: z.string()}), z.object({gte: z.string()})]); export const sortAscDesc = z.enum(["asc", "desc"]); export const customFields = z.object({}).nullable(); 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 id = z.number().int().gte(1); export const faultRule = z.object({id: z.number().int().gte(1).optional(), created_at: z.string().optional(), updated_at: z.string().optional(), account_id: id, code: z.string(), description: z.string().nullable(), priority_handling: z.enum(["off", "ignore", "critical"]).nullable(), name: z.string().nullable()}); export const labels = z.array(z.object({id: id.optional(), name: z.string().optional()})); export const inspectionItem = z.object({id: z.number().int().optional(), inspection_form_version_id: z.number().int().optional(), account_id: z.number().int().optional(), type: z.enum(["InspectionDateItem", "InspectionDatetimeItem", "InspectionDropdownItem", "InspectionFreeTextItem", "InspectionMeterEntryItem", "InspectionNumericItem", "InspectionPassFailItem", "InspectionPhotoItem", "InspectionSectionItem", "InspectionSignatureItem", "InspectionSingleCheckboxItem"]).optional(), label: z.string().optional(), short_description: z.string().nullable().optional(), instructions: z.string().nullable().optional(), required: z.boolean().optional(), position: z.number().int().optional(), require_remark_for_fail: z.boolean().optional(), pass_label: z.string().nullable().optional(), fail_label: z.string().nullable().optional(), reuse_pass_fail_label: z.boolean().optional(), require_secondary_meter_if_one_exists: z.boolean().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), version_key: z.string().nullable().optional(), na_label: z.string().nullable().optional(), enable_na_option_for_submission: z.boolean().optional(), require_remark_for_pass: z.boolean().optional(), dropdown_options: z.array(z.string()).optional(), numeric_range_min: z.number().nullable().optional(), numeric_range_max: z.number().nullable().optional(), require_meter_entry_photo_verification: z.boolean().optional()}); export const vehicleIssueSummary = z.object({id: z.number().int(), number: z.string(), state: z.enum(["Open", "Closed", "Resolved"]), created_at: z.string(), updated_at: z.string(), name: z.string(), description: z.string().nullable(), reported_at: z.string().nullable(), resolved_at: z.string().nullable(), resolved_by_id: z.number().int().nullable().optional(), closed_at: z.string().nullable().optional(), closed_by_id: z.number().int().nullable().optional(), reopened_at: z.string().nullable().optional(), reopened_by_id: z.number().int().nullable().optional(), resolvable_id: z.number().int().nullable(), resolvable_type: z.enum(["ServiceEntry", "WorkOrder"]).nullable(), due_date: z.string().nullable(), due_primary_meter_value: z.string().nullable(), due_secondary_meter_value: z.number().nullable(), overdue: z.boolean(), asset_type: z.enum(["Vehicle", "Equipment"]), inspection_form_id: z.number().int().nullable(), submitted_inspection_form_id: z.number().int().nullable(), fault_id: z.number().int().nullable(), external_id: z.string().nullable(), watchers_count: z.number().int(), comments_count: z.number().int(), documents_count: z.number().int(), images_count: z.number().int(), custom_fields: customFields.optional(), reported_by: contactMini.optional(), fault_rule: faultRule.optional(), labels: labels.optional(), inspection_items: z.array(inspectionItem).optional(), assigned_contacts: z.array(contactMini).optional()}); export const getApiVehiclesIdIssuesResponse = z.object({start_cursor: z.string().nullable().optional(), next_cursor: z.string().nullable().optional(), per_page: z.number().int().optional(), estimated_remaining_count: z.number().int().optional(), filtered_by: z.array(z.union([z.object({reported_by_id: eqFilter.optional()}), z.object({labels: includeFilter.optional()}), z.object({state: includeFilter.optional()}), z.object({due_date: eqLtLteGtGteFilter.optional()}), z.object({resolved_at: ltLteGtGteFilter.optional()}), z.object({reported_at: ltLteGtGteFilter.optional()}), z.object({created_at: ltLteGtGteFilter.optional()}), z.object({updated_at: ltLteGtGteFilter.optional()}), z.object({assigned_contact_ids: includeFilter.optional()}), z.object({closed_at: ltLteGtGteFilter.optional()}), z.object({reopened_at: ltLteGtGteFilter.optional()})])).optional(), sorted_by: z.array(z.union([z.object({id: sortAscDesc.optional()}), z.object({due_date: sortAscDesc.optional()}), z.object({updated_at: sortAscDesc.optional()}), z.object({created_at: sortAscDesc.optional()}), z.object({resolved_at: sortAscDesc.optional()}), z.object({reported_at: sortAscDesc.optional()})])).optional(), records: z.array(vehicleIssueSummary).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 }; /** * List Issues associated with a Vehicle * * GET /vehicles/{id}/issues */ export const getApiVehiclesIdIssues = async (args: GetApiVehiclesIdIssuesArgs, options?: GetApiVehiclesIdIssuesOptions) => { const {baseUrl = "https://secure.fleetio.com/api", fetch: fetchFn = fetch, apiKey, accountToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.start_cursor != null) query.append("start_cursor", String(args.start_cursor)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.filter != null) query.append("filter", JSON.stringify(args.filter)) if (args.sort != null) query.append("sort", JSON.stringify(args.sort)) if (apiKey !== undefined) headers.set("Authorization", apiKey) if (accountToken !== undefined) headers.set("Account-Token", accountToken) const url = `${baseUrl.replace(/\/+$/, '')}/vehicles/${encodeURIComponent(String(args.id))}/issues${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(getApiVehiclesIdIssuesResponse, await res.json()) };