import {z} from 'zod' export type ConnectProtocolVersion = number; export type ConnectTimeoutHeader = number; export type BaseObjectsV1MarketSymbol = {assetSymbol?: string | undefined, quoteSymbol?: string | undefined}; export type OrderbookServiceV1GetOrderBookRequest = {market: {marketId: unknown} | {marketSymbol: BaseObjectsV1MarketSymbol}, tickSize?: number | undefined, sizeInUsd?: boolean | undefined, levels?: number | undefined, limit?: (number | null) | undefined, offset?: (number | null) | undefined, totalOrderCount?: (number | null) | undefined}; export type CreateApiOrderbookServiceV1OrderBookServiceGetOrderBookArgs = {'Connect-Protocol-Version': ConnectProtocolVersion, 'Connect-Timeout-Ms'?: ConnectTimeoutHeader | undefined, body: OrderbookServiceV1GetOrderBookRequest}; export type CreateApiOrderbookServiceV1OrderBookServiceGetOrderBookOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const googleProtobufTimestamp = z.string(); export const baseObjectsV1Tick = z.object({price: z.number().optional(), size: z.number().optional(), orderCount: z.number().int().optional(), cumulativeTotal: z.number().optional()}); export const baseObjectsV1Books = z.object({bids: z.array(baseObjectsV1Tick).optional(), asks: z.array(baseObjectsV1Tick).optional()}); export const baseObjectsV1OrderBook = z.object({marketId: z.string().optional(), timestamp: googleProtobufTimestamp.optional(), pageSize: z.number().int().nullable().optional(), offset: z.number().int().nullable().optional(), totalOrderCount: z.number().int().optional(), levels: z.number().int().optional(), tickSize: z.number().optional(), sizeInUsd: z.boolean().optional(), book: baseObjectsV1Books.optional()}); export const orderbookServiceV1GetOrderBookResponse = z.object({orderBook: baseObjectsV1OrderBook.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 }; /** * GetOrderBook * * POST /orderbook_service.v1.OrderBookService/GetOrderBook */ export const createApiOrderbookServiceV1OrderBookServiceGetOrderBook = async (args: CreateApiOrderbookServiceV1OrderBookServiceGetOrderBookArgs, options?: CreateApiOrderbookServiceV1OrderBookServiceGetOrderBookOptions) => { const {baseUrl = "https://api.fairground.fi", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Connect-Protocol-Version'] != null) headers.set("Connect-Protocol-Version", String(args['Connect-Protocol-Version'])) if (args['Connect-Timeout-Ms'] != null) headers.set("Connect-Timeout-Ms", String(args['Connect-Timeout-Ms'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/orderbook_service.v1.OrderBookService/GetOrderBook${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(orderbookServiceV1GetOrderBookResponse, await res.json()) };