import {z} from 'zod' export type LikeEqFilter = {like: string} | {eq: string}; export type IncludeFilter = {include: string}; export type EqFilter = {eq: 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 GetApiEquipmentArgs = {start_cursor?: string | undefined, per_page?: number | undefined, filter?: ({brand?: LikeEqFilter | undefined, name?: LikeEqFilter | undefined, model?: LikeEqFilter | undefined, serial_number?: LikeEqFilter | undefined, labels?: IncludeFilter | undefined, current_location_entry_id?: EqFilter | undefined, group_id?: EqFilter | undefined, in_service_date?: EqLtLteGtGteFilter | undefined, out_of_service_date?: EqLtLteGtGteFilter | undefined, purchase_date?: EqLtLteGtGteFilter | undefined, created_at?: LtLteGtGteFilter | undefined, updated_at?: LtLteGtGteFilter | undefined}) | undefined, sort?: ({in_service_date?: SortAscDesc | undefined, out_of_service_date?: SortAscDesc | undefined, purchase_date?: SortAscDesc | undefined, created_at?: SortAscDesc | undefined, updated_at?: SortAscDesc | undefined, id?: SortAscDescDefaultDesc | undefined}) | undefined}; export type GetApiEquipmentOptions = {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 includeFilter = z.object({include: z.string()}); export const eqFilter = z.object({eq: 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 vendorMini = z.object({name: z.string(), phone: z.string().nullable(), city: z.string().nullable(), region: z.string().nullable(), postal_code: z.string().nullable(), country: z.string().nullable(), external_id: z.string().nullable()}).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 attachmentPermissions = z.object({read_photos: z.boolean().optional(), manage_photos: z.boolean().optional(), read_documents: z.boolean().optional(), manage_documents: z.boolean().optional()}).nullable(); export const equipmentSummary = z.object({created_at: z.string(), updated_at: z.string(), id: z.number().int(), name: z.string(), brand: z.string().nullable(), model: z.string().nullable(), serial_number: z.string().nullable(), equipment_type_id: z.number().int(), type_name: z.string(), equipment_status_id: z.number().int(), status_name: z.string(), group_id: z.number().int().nullable(), purchase_vendor_id: z.number().int().nullable(), purchase_price_cents: z.number().int().nullable(), purchase_date: z.string().nullable(), estimated_resale_price_cents: z.number().int().nullable(), warranty_expiration_date: z.string().nullable(), purchase_comments: z.string().nullable(), custom_fields: customFields.optional(), in_service_date: z.string().nullable(), estimated_service_life: z.number().int().nullable(), out_of_service_date: z.string().nullable(), archived_at: z.string().nullable(), status_color: z.string().nullable(), purchase_vendor: vendorMini.optional(), linked_vehicle: vehicleMini.optional(), assigned_contact: contactMini.optional(), attachment_permissions: attachmentPermissions, default_image_url: z.string().nullable(), assetable_type: z.string().nullable(), comments_count: z.number().int().gte(0), documents_count: z.number().int().gte(0), images_count: z.number().int().gte(0), is_watched: z.boolean(), watchers_count: z.number().int().gte(0)}); export const getApiEquipmentResponse = 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({brand: likeEqFilter.optional()}), z.object({name: likeEqFilter.optional()}), z.object({model: likeEqFilter.optional()}), z.object({serial_number: likeEqFilter.optional()}), z.object({labels: includeFilter.optional()}), z.object({current_location_entry_id: eqFilter.optional()}), z.object({group_id: eqFilter.optional()}), z.object({in_service_date: eqLtLteGtGteFilter.optional()}), z.object({out_of_service_date: eqLtLteGtGteFilter.optional()}), z.object({purchase_date: eqLtLteGtGteFilter.optional()}), z.object({created_at: ltLteGtGteFilter.optional()}), z.object({updated_at: ltLteGtGteFilter.optional()})])).optional(), sorted_by: z.array(z.union([z.object({in_service_date: sortAscDesc.optional()}), z.object({out_of_service_date: sortAscDesc.optional()}), z.object({purchase_date: sortAscDesc.optional()}), z.object({created_at: sortAscDesc.optional()}), z.object({updated_at: sortAscDesc.optional()}), z.object({id: sortAscDesc.optional()})])).optional(), records: z.array(equipmentSummary).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 Equipment * * GET /equipment */ export const getApiEquipment = async (args: GetApiEquipmentArgs, options?: GetApiEquipmentOptions) => { 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(/\/+$/, '')}/equipment${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(getApiEquipmentResponse, await res.json()) };