import {z} from 'zod' export type LikeEqFilter = {like: string} | {eq: string}; export type EqFilter = {eq: string}; export type LtLteGtGteFilter = {lt: string} | {lte: string} | {gt: string} | {gte: string}; export type SortAscDescDefaultDesc = 'asc' | 'desc'; export type SortAscDesc = 'asc' | 'desc'; export type GetApiAccountsArgs = {start_cursor?: string | undefined, per_page?: number | undefined, filter?: ({account_name?: LikeEqFilter | undefined, account_url_token?: EqFilter | undefined, created_at?: LtLteGtGteFilter | undefined, updated_at?: LtLteGtGteFilter | undefined}) | undefined, sort?: ({id?: SortAscDescDefaultDesc | undefined, created_at?: SortAscDesc | undefined, updated_at?: SortAscDesc | undefined}) | undefined}; export type GetApiAccountsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, accountToken?: string | undefined, fetch?: typeof fetch | undefined}; export const likeEqFilter = z.union([z.object({like: z.string()}), z.object({eq: z.string()})]); export const eqFilter = z.object({eq: 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 id = z.number().int().gte(1); export const account = z.object({id: z.number().int().gte(1).optional(), created_at: z.string().optional(), updated_at: z.string().optional(), account_membership_id: id.optional(), name: z.string(), logo_url: z.string().nullable().optional(), user_type: z.enum(["admin", "owner", "regular", "guest"]), token: z.string(), time_24_format: z.boolean(), currency_symbol: z.string(), organization_id: z.number().int().nullable().optional(), organization_type: z.enum(["channel_reseller", "simple_reseller", "vendor_partner"]).nullable().optional(), city: z.string().nullable(), region: z.string().nullable(), postal_code: z.string().nullable(), street_address: z.string().nullable(), street_address_line_2: z.string().nullable().optional(), country: z.string().nullable(), read_vehicles: z.boolean(), manage_vehicles: z.boolean(), create_vehicles: z.boolean(), read_fuel_entries: z.boolean(), manage_fuel_entries: z.boolean(), read_service_entries: z.boolean(), manage_service_entries: z.boolean(), read_issues: z.boolean(), manage_issues: z.boolean(), read_work_orders: z.boolean(), manage_work_orders: z.boolean(), read_service_reminders: z.boolean(), manage_service_reminders: z.boolean(), read_vehicle_renewal_reminders: z.boolean(), manage_vehicle_renewal_reminders: z.boolean(), read_comments: z.boolean(), manage_comments: z.boolean(), read_meter_entries: z.boolean(), manage_meter_entries: z.boolean(), read_groups: z.boolean(), read_contacts: z.boolean(), fleetio_manage: z.boolean(), inspections: z.boolean(), update_parts: z.boolean(), update_inventory: z.boolean(), allowed_part_location_ids: z.array(z.number().int()), new_inventory_permissions: z.boolean(), plan: z.string(), state: z.enum(["trialing", "assessing", "active", "past_due", "soft_failure"]), default_meter_unit: z.string().nullable(), default_fuel_volume_unit: z.string().nullable(), default_system_of_measurement: z.enum(["imperial", "metric"]).nullable(), tax_settings: z.object({tax_free_labor: z.boolean().nullable().optional(), tax_2: z.boolean().nullable().optional(), default_tax_1: z.string().nullable().optional(), default_tax_2: z.string().nullable().optional(), default_tax_type: z.enum(["fixed", "percentage"]).nullable().optional()}), next_work_order_number: z.number().int(), offline_inspections: z.boolean().nullable(), inspections_new_item_type_beta: z.boolean().nullable(), paying_customer: z.boolean().nullable(), is_demo: z.boolean().nullable(), require_group: z.boolean().nullable(), require_a_meter_entry_on_service_entry_or_completed_work_order: z.boolean().nullable(), excluded_vehicle_type_ids_from_required_meter_readings: z.array(z.number().int()), excluded_vehicle_type_ids_from_required_repair_priority_class: z.array(z.number().int()), has_ro_notification_subscription: z.boolean().nullable(), require_an_inventory_location_on_wo_part_line_items: z.boolean().nullable(), use_forecasted_dates_for_service_reminders: z.boolean().nullable(), require_a_rpc_on_service_entry_or_completed_work_order: z.boolean().nullable(), vmrs_system_enabled: z.boolean().nullable(), vmrs_assembly_enabled: z.boolean().nullable(), vmrs_component_enabled: z.boolean().nullable(), disable_reason_for_repair_editing: z.boolean().nullable(), disable_system_assembly_component_editing: z.boolean().nullable(), require_vmrs_reason_for_repair_on_service_line_items_as_of: z.string().nullable(), require_vmrs_system_group_on_service_line_items_as_of: z.string().nullable(), go_session_timeout_minutes: z.string().nullable().optional(), live_map_enabled: z.boolean()}); export const getApiAccountsResponse = 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({account_name: likeEqFilter.optional()}), z.object({account_url_token: eqFilter.optional()}), z.object({created_at: ltLteGtGteFilter.optional()}), z.object({updated_at: ltLteGtGteFilter.optional()})])).optional(), sorted_by: z.array(z.union([z.object({updated_at: sortAscDesc.optional()}), z.object({created_at: sortAscDesc.optional()}), z.object({id: sortAscDesc.optional()})])).optional(), records: z.array(account).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 Accounts * * GET /accounts */ export const getApiAccounts = async (args: GetApiAccountsArgs, options?: GetApiAccountsOptions) => { 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(/\/+$/, '')}/accounts${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(getApiAccountsResponse, await res.json()) };