import {z} from 'zod' export type GetApiApiV1PublicAuctionsHistoryArgs = {limit?: number | undefined, cursor?: string | undefined, state?: ('scheduled' | 'active' | 'settling' | 'settled') | undefined, include_stats?: (boolean | null) | undefined, from_date?: (number | null) | undefined, to_date?: (number | null) | undefined, slot_from?: (number | null) | undefined, slot_to?: (number | null) | undefined, sort_by?: ('createdAt' | 'slot' | 'state' | 'endTime' | 'settledAt') | undefined, sort_order?: ('asc' | 'desc') | undefined}; export type GetApiApiV1PublicAuctionsHistoryOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV1PublicAuctionsHistoryResponse = z.object({success: z.literal(true), timestamp: z.number(), data: z.array(z.object({id: z.string(), slot: z.number().int().gte(0), state: z.enum(["scheduled", "active", "settling", "settled"]), createdAt: z.number(), scheduledStart: z.number(), actualStart: z.number().optional(), endTime: z.number(), settledAt: z.number().optional(), minBid: z.string(), elasticSupply: z.object({basePrice: z.string(), maxCapacity: z.number().int().gte(0), minCapacity: z.number().int().gte(0), elasticity: z.number().gte(0).lte(1)}), currentSupply: z.number().int().gte(0), totalBids: z.number().int().gte(0), clearingPrice: z.string().optional(), highestBidAmount: z.string().optional()})), cursor: z.string().optional(), hasMore: z.boolean(), total: z.number().optional(), count: z.number(), metadata: z.object({totalPages: z.number().optional(), currentPage: z.number().optional(), pageSize: z.number(), firstItemTimestamp: z.number().optional(), lastItemTimestamp: z.number().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 historical auctions with enhanced pagination and filtering * * GET /api/v1/public/auctions/history */ export const getApiApiV1PublicAuctionsHistory = async (args: GetApiApiV1PublicAuctionsHistoryArgs, options?: GetApiApiV1PublicAuctionsHistoryOptions) => { const {baseUrl = "https://xga-auction-system.workers.dev", fetch: fetchFn = fetch} = 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.state != null) query.append("state", String(args.state)) if (args.include_stats != null) query.append("include_stats", String(args.include_stats)) if (args.from_date != null) query.append("from_date", String(args.from_date)) if (args.to_date != null) query.append("to_date", String(args.to_date)) if (args.slot_from != null) query.append("slot_from", String(args.slot_from)) if (args.slot_to != null) query.append("slot_to", String(args.slot_to)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.sort_order != null) query.append("sort_order", String(args.sort_order)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/public/auctions/history${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(getApiApiV1PublicAuctionsHistoryResponse, await res.json()) };