import {z} from 'zod' export type BooleanFilter = {eq: 'true' | 'false'}; export type IncludeEqFilter = {include: string} | {eq: string}; export type LikeEqFilter = {like: string} | {eq: string}; export type LtLteGtGteFilter = {lt: string} | {lte: string} | {gt: string} | {gte: string}; export type SortAscDescDefaultDesc = 'asc' | 'desc'; export type SortAscDesc = 'asc' | 'desc'; export type GetApiAxleConfigsArgs = {start_cursor?: string | undefined, per_page?: number | undefined, filter?: ({active?: BooleanFilter | undefined, is_trailer?: BooleanFilter | undefined, vehicle_id?: IncludeEqFilter | undefined, axle_config_template_id?: IncludeEqFilter | undefined, name?: LikeEqFilter | undefined, created_at?: LtLteGtGteFilter | undefined, updated_at?: LtLteGtGteFilter | undefined}) | undefined, sort?: ({id?: SortAscDescDefaultDesc | undefined, created_at?: SortAscDesc | undefined, updated_at?: SortAscDesc | undefined}) | undefined}; export type GetApiAxleConfigsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, accountToken?: string | undefined, fetch?: typeof fetch | undefined}; export const booleanFilter = z.object({eq: z.enum(["true", "false"])}); export const includeEqFilter = z.union([z.object({include: z.string()}), z.object({eq: 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 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 axleConfigSummary = z.object({id: z.number().int().optional(), name: z.string().optional(), description: z.string().nullable().optional(), axle_config_template_id: z.number().int().nullable().optional(), vehicle_id: z.number().int().optional(), active: z.boolean().optional(), axles_count: z.number().int().optional(), is_trailer: z.boolean().optional(), tire_positions_count: z.number().int().optional(), tires_count: z.number().int().optional(), vehicle: vehicleMini.optional()}); export const getApiAxleConfigsResponse = 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({active: booleanFilter.optional()}), z.object({is_trailer: booleanFilter.optional()}), z.object({vehicle_id: includeEqFilter.optional()}), z.object({axle_config_template_id: includeEqFilter.optional()}), z.object({name: includeEqFilter.optional()}), z.object({created_at: ltLteGtGteFilter.optional()}), z.object({updated_at: ltLteGtGteFilter.optional()})])).optional(), sorted_by: z.array(z.union([z.object({updated_at: sortAscDesc.optional()}), z.object({created_at: sortAscDesc.optional()}), z.object({id: sortAscDesc.optional()})])).optional(), records: z.array(axleConfigSummary).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 Axle Configurations * * GET /axle_configs */ export const getApiAxleConfigs = async (args: GetApiAxleConfigsArgs, options?: GetApiAxleConfigsOptions) => { 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(/\/+$/, '')}/axle_configs${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(getApiAxleConfigsResponse, await res.json()) };