import {z} from 'zod' export type Id = number; export type CustomFields = Record | null; export type CreateApiPartsArgs = {body: {description?: string | undefined, manufacturer_part_number?: string | undefined, measurement_unit_id?: Id | undefined, measurement_unit_name?: string | undefined, number: string, part_category_id?: Id | undefined, part_category_name?: string | undefined, part_manufacturer_id?: Id | undefined, part_manufacturer_name?: string | undefined, unit_cost?: number | undefined, upc?: string | undefined, tire_config_attributes?: ({id?: Id | undefined, aspect_ratio?: number | undefined, rim_diameter?: number | undefined, load_index?: number | undefined, metric_type?: ('EU' | 'P' | 'HL' | 'T' | 'LT' | 'C' | 'ST') | undefined, width?: number | undefined, construction?: ('B' | 'R' | 'D') | undefined, speed_rating?: ('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') | undefined, factory_tread_depth?: number | undefined, minimum_tread_depth?: number | undefined, life_expectancy?: number | undefined}) | 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 CreateApiPartsOptions = {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 measurementUnit = z.object({id: id.optional(), name: z.string().nullable().optional(), description: z.string().nullable().optional(), symbol: z.string().nullable().optional()}).nullable(); export const partCreated = z.object({id: id, created_at: createdAt.optional(), updated_at: updatedAt.optional(), account_id: id, 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(), custom_fields: customFields, unit_cost_cents: z.number().int().nullable().optional(), average_unit_cost_cents: z.number().int().nullable().optional(), images_count: z.number().int(), documents_count: z.number().int(), comments_count: z.number().int(), archived_at: z.string().nullable(), measurement_unit: measurementUnit.optional(), has_active_inventory_sets: z.boolean().optional()}).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 Part * * POST /parts */ export const createApiParts = async (args: CreateApiPartsArgs, options?: CreateApiPartsOptions) => { 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(/\/+$/, '')}/parts${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(partCreated, await res.json()) };