import {z} from 'zod' export type Id = number; export type NullableId = number | null; export type CustomFields = Record | null; export type EquipmentCreateParameters = {name: string, brand?: string | undefined, model?: string | undefined, serial_number?: string | undefined, type_id: Id, status_id: Id, group_id?: NullableId | undefined, group_hierarchy?: string | undefined, purchase_vendor_id?: NullableId | undefined, purchase_price?: number | undefined, purchase_date?: string | undefined, warranty_expiration_date?: string | undefined, purchase_comments?: string | undefined, in_service_date?: string | undefined, estimated_service_life?: number | undefined, estimated_resale_price?: number | undefined, out_of_service_date?: string | undefined, linked_vehicle_id?: NullableId | undefined, assignee_id?: NullableId | undefined, default_image?: (string | null) | undefined, custom_fields?: CustomFields | undefined, documents_attributes?: (Array<{name?: string | undefined, file_url?: string | undefined, file_mime_type?: string | undefined, file_name?: string | undefined, file_size?: number | undefined}>) | undefined, images_attributes?: (Array<{name?: string | undefined, file_url?: string | undefined, file_mime_type?: string | undefined, file_name?: string | undefined, file_size?: number | undefined}>) | undefined}; export type CreateApiEquipmentArgs = {body: EquipmentCreateParameters}; export type CreateApiEquipmentOptions = {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 equipmentCreated = z.object({id: id, created_at: createdAt, updated_at: updatedAt, account_id: z.number().int(), name: z.string(), brand: z.string().nullable(), model: z.string().nullable(), serial_number: z.string().nullable(), equipment_type_id: id, equipment_status_id: id, group_id: nullableId, purchase_vendor_id: nullableId, purchase_price_cents: z.number().nullable(), purchase_date: z.string().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(), current_location_entry_id: nullableId.optional(), watchers_count: z.number().int(), watchable_options: z.object({}).optional(), bulk_watch_record: z.boolean().optional(), estimated_resale_price_cents: z.number().int().nullable().optional(), label_list: z.array(z.string()).optional(), skip_automatic_watchers_callbacks: z.boolean().optional(), comments_count: z.number().int().gte(0), documents_count: z.number().int().gte(0), 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 }; /** * Create Equipment * * POST /equipment */ export const createApiEquipment = async (args: CreateApiEquipmentArgs, options?: CreateApiEquipmentOptions) => { const {baseUrl = "https://secure.fleetio.com/api", fetch: fetchFn = fetch, apiKey, accountToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") 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: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(equipmentCreated, await res.json()) };