import {z} from 'zod' export type GetApiV1McSaleListingsArgs = {limit?: number | undefined, cursor?: string | undefined, source?: string | undefined, posted_after?: string | undefined, posted_before?: string | undefined, state?: string | undefined, min_price?: number | undefined, max_price?: number | undefined, amazon_status?: string | undefined, highway_setup?: boolean | undefined}; export type GetApiV1McSaleListingsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const mcSaleListingPost = z.object({source: z.enum(["facebook_group", "facebook_page", "telegram", "mcxchange", "bizbuysell"]).optional(), source_url: z.string().nullable().optional(), source_name: z.string().nullable().optional(), posted_at: z.string().nullable().optional(), engagement: z.object({likes: z.number().int().optional(), comments: z.number().int().optional(), shares: z.number().int().optional()}).nullable().optional()}).nullable(); export const mcSaleListingAuthority = z.object({type: z.string().nullable().optional(), status: z.string().nullable().optional(), state: z.string().nullable().optional(), mc_number: z.string().nullable().optional(), dot_number: z.string().nullable().optional(), granted_date: z.string().nullable().optional(), granted_date_precision: z.string().nullable().optional(), reinstated_date: z.string().nullable().optional(), ein_disclosed: z.boolean().optional()}).nullable(); export const mcSaleListingSafety = z.object({total_inspections: z.number().int().nullable().optional(), total_violations: z.number().int().nullable().optional(), total_crashes: z.number().int().nullable().optional(), vehicle_oos_pct: z.number().nullable().optional(), driver_oos_pct: z.number().nullable().optional(), vehicle_oos_count: z.number().int().nullable().optional(), driver_oos_count: z.number().int().nullable().optional()}).nullable(); export const mcSaleListingAssets = z.object({highway_setup: z.boolean().nullable().optional(), amazon_status: z.string().nullable().optional(), bank_name: z.string().nullable().optional(), factoring_name: z.string().nullable().optional(), insurer: z.string().nullable().optional(), insurance_limit_k: z.number().nullable().optional(), fleet_size: z.number().int().nullable().optional(), email_included: z.boolean().nullable().optional(), phone_included: z.boolean().nullable().optional()}).nullable(); export const mcSaleFeedItem = z.object({id: z.string().optional(), post_url: z.string().nullable().optional(), post_text: z.string().nullable().optional(), seller_name: z.string().nullable().optional(), phone: z.string().nullable().optional(), email: z.string().nullable().optional(), linked_dot_numbers: z.array(z.number().int()).optional(), post: mcSaleListingPost.optional(), authority: mcSaleListingAuthority.optional(), safety: mcSaleListingSafety.optional(), included_assets: mcSaleListingAssets.optional(), asking_price_usd: z.number().nullable().optional(), sold: z.boolean().nullable().optional()}); export const mcSaleListingsFeedResponse = z.object({listings: z.array(mcSaleFeedItem).optional(), limit: z.number().int().optional(), has_more: z.boolean().optional(), next_cursor: z.string().nullable().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 }; /** * Raw authority-for-sale listings feed * * GET /v1/mc-sale-listings */ export const getApiV1McSaleListings = async (args: GetApiV1McSaleListingsArgs, options?: GetApiV1McSaleListingsOptions) => { 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.cursor != null) query.append("cursor", String(args.cursor)) if (args.source != null) query.append("source", String(args.source)) if (args.posted_after != null) query.append("posted_after", String(args.posted_after)) if (args.posted_before != null) query.append("posted_before", String(args.posted_before)) if (args.state != null) query.append("state", String(args.state)) if (args.min_price != null) query.append("min_price", String(args.min_price)) if (args.max_price != null) query.append("max_price", String(args.max_price)) if (args.amazon_status != null) query.append("amazon_status", String(args.amazon_status)) if (args.highway_setup != null) query.append("highway_setup", String(args.highway_setup)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/mc-sale-listings${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(mcSaleListingsFeedResponse, await res.json()) };