import {z} from 'zod' export type GetApiTiresIdArgs = {id: string}; export type GetApiTiresIdOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, accountToken?: string | undefined, fetch?: typeof fetch | undefined}; export const id = z.number().int().gte(1); export const nullableId = z.number().int().gte(1).nullable(); export const tire = z.object({id: id, created_at: z.string().optional(), updated_at: z.string().optional(), tire_position_id: nullableId.optional(), part_id: nullableId.optional(), axle_config_id: nullableId.optional(), pressure: z.number().nullable().optional(), tread_depth: z.number().nullable().optional(), current_meter: z.number().optional(), tin: z.string().nullable().optional(), tread_depth_health: z.enum(["unknown", "normal", "warning", "low"]).optional(), pressure_health: z.enum(["unknown", "normal", "warning", "low"]).optional(), installed_at: z.string().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()}).nullable().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 }; /** * Retrieve Tire * * GET /tires/{id} */ export const getApiTiresId = async (args: GetApiTiresIdArgs, options?: GetApiTiresIdOptions) => { 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 (apiKey !== undefined) headers.set("Authorization", apiKey) if (accountToken !== undefined) headers.set("Account-Token", accountToken) const url = `${baseUrl.replace(/\/+$/, '')}/tires/${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(tire, await res.json()) };