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 GetApiIssuesArgs = {start_cursor?: string | undefined, per_page?: number | undefined, filter?: ({number?: EqFilter | undefined, reported_by_id?: EqFilter | undefined, assigned_contact_ids?: IncludeFilter | undefined, vehicle_id?: EqFilter | undefined, equipment_id?: EqFilter | undefined, labels?: IncludeFilter | undefined, state?: EqFilter | 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 GetApiIssuesOptions = {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 equipmentMini = z.object({id: z.number().int(), name: z.string(), brand: z.string().nullable(), model: z.string().nullable(), serial_number: z.string().nullable()}).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 issuePriority = z.object({id: z.number().int().gte(1).optional(), created_at: z.string().optional(), updated_at: z.string().optional(), account_id: id.optional(), alias: z.string().nullable().optional(), name: z.string().optional(), slug: z.string().optional(), description: z.string().optional(), default: z.boolean().optional(), enabled: z.boolean().optional(), position: z.number().int().optional()}); export const issueSummary = 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"]), 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(), vehicle: 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(), current_job: z.object({name: z.string().optional(), number: z.string().optional()}).nullable().optional()}).optional(), equipment: equipmentMini.optional(), reported_by: contactMini.optional(), fault_rule: faultRule.optional(), labels: labels.optional(), assigned_contacts: z.array(contactMini).optional(), issue_priority: issuePriority.optional()}); export const getApiIssuesResponse = 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({vehicle_id: eqFilter.optional()}), z.object({equipment_id: eqFilter.optional()}), z.object({labels: includeFilter.optional()}), z.object({state: eqFilter.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(issueSummary).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 * * GET /issues */ export const getApiIssues = async (args: GetApiIssuesArgs, options?: GetApiIssuesOptions) => { 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(/\/+$/, '')}/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(getApiIssuesResponse, await res.json()) };