import {z} from 'zod' export type GetApiPartsIdArgs = {id: string, with_cross_reference_member_locations?: boolean | undefined, 'part_location_id[]'?: Array | undefined}; export type GetApiPartsIdOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, accountToken?: string | undefined, fetch?: typeof fetch | undefined}; export const id = z.number().int().gte(1); export const createdAt = z.string(); export const updatedAt = z.string(); export const nullableId = z.number().int().gte(1).nullable(); export const customFields = z.object({}).nullable(); export const partCategory = z.object({id: id.optional(), tire_category: z.boolean().optional()}).nullable(); export const partLocationDetail = z.object({id: id, created_at: createdAt.optional(), updated_at: updatedAt.optional(), active: z.boolean().optional(), aisle: z.string().nullable().optional(), available_quantity: z.number().optional(), bin: z.string().nullable().optional(), inventory_sets_count: z.number().int().optional(), has_been_used_in_work_orders: z.boolean().nullable().optional(), name: z.string().optional(), part_location_name: z.string().optional(), part_location_id: z.number().int().optional(), quantity_allocated_to_work_orders: z.number().nullable().optional(), quantity_on_order: z.number().nullable().optional(), reorder_point: z.number().optional(), reorder_point_enabled: z.boolean().optional(), row: z.string().nullable().optional(), track_inventory: z.boolean().optional(), available_quantity_updated_at: z.string().nullable().optional(), part_location_archived_at: 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 partCrossReferenceMemberLocation = z.object({id: id, part_id: id, part_location_id: id, part_location_name: z.string(), available_quantity: z.number().nullable(), reorder_point: z.number().nullable(), average_unit_cost: z.number().nullable(), active: z.boolean(), track_inventory: z.boolean().nullable()}); export const partCrossReferenceMember = z.object({id: nullableId, number: z.string(), description: z.string().nullable(), part_category_name: z.string().nullable(), part_manufacturer_name: z.string().nullable(), manufacturer_part_number: z.string().nullable(), total_quantity: z.number().gte(0), unit_cost: z.number().nullable(), unit_cost_unit: z.string().nullable(), available_quantity: z.number().gte(0), default_image_small_square_url: z.string().nullable(), position: z.number().int(), part_location_details: z.array(partCrossReferenceMemberLocation).optional()}); export const commentableType = z.enum(["Contact", "FleetcorCard", "FuelEntry", "InventoryJournalEntry", "Issue", "Part", "PurchaseOrder", "ServiceEntry", "ServiceReminder", "SubmittedInspectionItem", "Vehicle", "VehicleAssignment", "Vendor", "WexCard", "WorkOrder"]); export const comment = z.object({id: id, created_at: createdAt.optional(), updated_at: updatedAt.optional(), commentable_type: commentableType, commentable_id: id, user_id: id, title: z.string().optional(), comment: z.string(), user_full_name: z.string(), user_image_url: z.string().nullable(), rich_content: z.object({}).nullable(), html_content: z.string().nullable(), with_mentions: z.boolean()}).nullable(); export const documentableType = z.enum(["ServiceEntry", "FuelEntry", "Issue", "WorkOrder", "ExpenseEntry", "Vehicle"]); export const document = z.object({id: id, created_at: createdAt.optional(), updated_at: updatedAt.optional(), name: z.string(), description: z.string(), documentable_id: id, documentable_type: documentableType, file_name: z.string(), file_mime_type: z.string(), file_size: z.number(), file_url: z.string(), full_file_url: z.string()}).nullable(); export const imageableType = z.enum(["ExpenseEntry", "WorkOrder", "Equipment", "IssueResolution", "ServiceEntry", "Vendor", "ServiceProgram", "Issue", "Contact", "Part", "Vehicle", "FuelEntry", "SubmittedInspectionItem"]); export const imageMimeType = z.enum(["image/jpeg", "image/gif", "image/jif", "image/jfif", "image/jfi", "image/tiff", "image/png", "image/jpg", "image/jpe", "image/pdf", "image/webp", "image/tif", "image/psd", "image/raw", "image/arw", "image/cr2", "image/nrw", "image/k25", "image/bmp", "image/dip", "image/ai", "image/eps", "image/heif", "image/heic", "image/ind", "image/indd", "image/indt", "image/jp2", "image/j2k", "image/jpf", "image/jpx", "image/jpm", "image/mj2", "image/x-png"]); export const image2 = z.object({id: id, created_at: createdAt.optional(), updated_at: updatedAt.optional(), imageable_id: id.optional(), imageable_type: imageableType.optional(), file_name: z.string().optional(), file_mime_type: imageMimeType.optional(), file_size: z.number().optional(), file_url: z.string().optional(), full_url: z.string().optional()}).nullable(); export const part = z.object({id: id, created_at: createdAt.optional(), updated_at: updatedAt.optional(), description: z.string().nullable(), manufacturer_part_number: z.string().nullable(), measurement_unit_id: nullableId, number: z.string(), part_category_id: nullableId, part_manufacturer_id: nullableId, upc: z.string().nullable(), total_quantity: z.number().gte(0), custom_fields: customFields, inventory_item: z.boolean(), part_category: partCategory.optional(), part_category_name: z.string().nullable(), part_manufacturer_name: z.string().nullable(), measurement_unit_name: z.string().nullable(), unit_cost: z.number().nullable(), average_unit_cost: z.number().nullable().optional(), part_locations: z.array(partLocationDetail), default_image_url: z.string().nullable(), default_image_url_medium: z.string().nullable(), default_image_url_small: z.string().nullable(), default_image_url_large: z.string().nullable(), archived_at: z.string().nullable(), attachment_permissions: attachmentPermissions, has_active_inventory_sets: z.boolean().optional(), tire_config: z.object({id: id.optional(), aspect_ratio: z.number().optional(), rim_diameter: z.number().optional(), load_index: z.number().optional(), metric_type: z.enum(["EU", "P", "HL", "T", "LT", "C", "ST"]).optional(), width: z.number().optional(), construction: z.enum(["B", "R", "D"]).optional(), speed_rating: z.enum(["A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "Y"]).optional(), factory_tread_depth: z.number().optional(), minimum_tread_depth: z.number().optional(), life_expectancy: z.number().int().optional()}).optional(), cross_reference_members: z.array(partCrossReferenceMember).optional(), cross_reference_group_id: z.number().int().nullable().optional(), comments: z.array(comment), comments_count: z.number().int().gte(0), documents: z.array(document), documents_count: z.number().int().gte(0), images: z.array(image2), images_count: z.number().int().gte(0)}).nullable(); 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 }; /** * Retrieve Part * * GET /parts/{id} */ export const getApiPartsId = async (args: GetApiPartsIdArgs, options?: GetApiPartsIdOptions) => { 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.with_cross_reference_member_locations != null) query.append("with_cross_reference_member_locations", String(args.with_cross_reference_member_locations)) for (const item of args['part_location_id[]'] ?? []) query.append("part_location_id[]", String(item)) if (apiKey !== undefined) headers.set("Authorization", apiKey) if (accountToken !== undefined) headers.set("Account-Token", accountToken) const url = `${baseUrl.replace(/\/+$/, '')}/parts/${encodeURIComponent(String(args.id))}${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(part, await res.json()) };