import {z} from 'zod' export type GetApiV1CarriersDotNumberEquipmentForSaleArgs = {dot_number: string, limit?: number | undefined, page?: number | undefined}; export type GetApiV1CarriersDotNumberEquipmentForSaleOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const equipmentForSaleItem = z.object({equipment_type: z.enum(["truck", "trailer"]).optional(), vin: z.string().optional(), last_seen_in_inspection: z.string().nullable().optional(), inspection_count: z.number().int().optional(), listing_title: z.string().nullable().optional(), manufacturer: z.string().nullable().optional(), model: z.string().nullable().optional(), model_year: z.number().int().nullable().optional(), price: z.number().nullable().optional(), category: z.string().nullable().optional(), description: z.string().nullable().optional()}).and(z.record(z.string(), z.unknown())); export const pagination = z.object({page: z.number().int().optional(), limit: z.number().int().optional(), total_results: z.number().int().optional(), total_pages: z.number().int().optional()}); export const equipmentForSaleResponse = z.object({dot_number: z.string().optional(), total_listings: z.number().int().optional(), equipment: z.array(equipmentForSaleItem).optional(), pagination: pagination.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 }; /** * Get carrier fleet VINs listed for sale * * GET /v1/carriers/{dot_number}/equipment-for-sale */ export const getApiV1CarriersDotNumberEquipmentForSale = async (args: GetApiV1CarriersDotNumberEquipmentForSaleArgs, options?: GetApiV1CarriersDotNumberEquipmentForSaleOptions) => { const {baseUrl = "https://api.runalphaloops.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.page != null) query.append("page", String(args.page)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/carriers/${encodeURIComponent(String(args.dot_number))}/equipment-for-sale${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(equipmentForSaleResponse, await res.json()) };