import {z} from 'zod' export type LikeEqFilter = {like: string} | {eq: string}; export type EqFilter = {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 GetApiVehiclesVehicleIdWarrantiesArgs = {vehicle_id: string, start_cursor?: string | undefined, per_page?: number | undefined, filter?: ({name?: LikeEqFilter | undefined, coverage_type?: EqFilter | undefined, vendor_id?: EqFilter | undefined, start_date?: LtLteGtGteFilter | undefined, end_date?: LtLteGtGteFilter | undefined, created_at?: LtLteGtGteFilter | undefined, updated_at?: LtLteGtGteFilter | undefined}) | undefined, sort?: ({id?: SortAscDescDefaultDesc | undefined, name?: SortAscDesc | undefined, start_date?: SortAscDesc | undefined, end_date?: SortAscDesc | undefined, created_at?: SortAscDesc | undefined, updated_at?: SortAscDesc | undefined}) | undefined}; export type GetApiVehiclesVehicleIdWarrantiesOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, accountToken?: string | undefined, fetch?: typeof fetch | undefined}; export const likeEqFilter = z.union([z.object({like: z.string()}), z.object({eq: z.string()})]); export const eqFilter = 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 id = z.number().int().gte(1); export const nullableId = z.number().int().gte(1).nullable(); export const createdAt = z.string(); export const updatedAt = z.string(); export const warranty = z.object({id: id, vehicle_id: id, name: z.string(), description: z.string().nullable().optional(), contract_number: z.string().nullable().optional(), coverage_type: z.enum(["standard", "extended"]).nullable().optional(), manufacturer_name: z.string().nullable().optional(), start_date: z.string(), end_date: z.string().nullable().optional(), start_primary_meter: z.string().nullable().optional(), end_primary_meter: z.string().nullable().optional(), start_secondary_meter: z.string().nullable().optional(), end_secondary_meter: z.string().nullable().optional(), primary_meter_distance: z.number().int().nullable().optional(), secondary_meter_distance: z.number().int().nullable().optional(), primary_meter_unlimited: z.boolean(), secondary_meter_unlimited: z.boolean(), term_duration_unit: z.enum(["months", "years"]), term_duration_value: z.number().int(), vendor_id: nullableId.optional(), vendor_name: z.string().nullable().optional(), vendor_warranties_url: z.string().nullable().optional(), status: z.enum(["active", "expiring_soon", "expired"]).nullable().optional(), created_at: createdAt, updated_at: updatedAt}); export const getApiVehiclesVehicleIdWarrantiesResponse = 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({name: likeEqFilter.optional()}), z.object({coverage_type: eqFilter.optional()}), z.object({vendor_id: eqFilter.optional()}), z.object({start_date: ltLteGtGteFilter.optional()}), z.object({end_date: ltLteGtGteFilter.optional()}), z.object({created_at: ltLteGtGteFilter.optional()}), z.object({updated_at: ltLteGtGteFilter.optional()})])).optional(), sorted_by: z.array(z.union([z.object({id: sortAscDesc.optional()}), z.object({name: sortAscDesc.optional()}), z.object({start_date: sortAscDesc.optional()}), z.object({end_date: sortAscDesc.optional()}), z.object({created_at: sortAscDesc.optional()}), z.object({updated_at: sortAscDesc.optional()})])).optional(), records: z.array(warranty).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 Warranties * * GET /vehicles/{vehicle_id}/warranties */ export const getApiVehiclesVehicleIdWarranties = async (args: GetApiVehiclesVehicleIdWarrantiesArgs, options?: GetApiVehiclesVehicleIdWarrantiesOptions) => { 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(/\/+$/, '')}/vehicles/${encodeURIComponent(String(args.vehicle_id))}/warranties${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(getApiVehiclesVehicleIdWarrantiesResponse, await res.json()) };