import {z} from 'zod' export type DateTime = string; export type OptionType = 'CALL' | 'PUT'; export type GetApiV1HistoryOptionTradesArgs = {'createdAtDatetime[gte]'?: DateTime | undefined, 'createdAtDatetime[lte]'?: DateTime | undefined, optionType?: OptionType | undefined}; export type GetApiV1HistoryOptionTradesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const optionMarketSymbol = z.string(); export const tradeID = z.string(); export const assetValue = z.string(); export const orderSideAsString = z.string(); export const boolean = z.boolean(); export const dateTime = z.string(); export const timeStampAsString = z.string(); export const optionType = z.enum(["CALL", "PUT"]); export const otcTradeId = z.string(); export const optionTrade = z.object({symbol: optionMarketSymbol, tradeId: tradeID, price: assetValue, quantity: assetValue, side: orderSideAsString, isTaker: boolean, createdAtDatetime: dateTime, createdAtTimestamp: timeStampAsString, optionType: optionType, otcTradeId: otcTradeId}); export const getApiV1HistoryOptionTradesResponse = z.array(optionTrade); 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 Options Market Trades * * GET /v1/history/option-trades */ export const getApiV1HistoryOptionTrades = async (args: GetApiV1HistoryOptionTradesArgs, options?: GetApiV1HistoryOptionTradesOptions) => { const {baseUrl = "https://api.exchange.bullish.com/trading-api", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['createdAtDatetime[gte]'] != null) query.append("createdAtDatetime[gte]", String(args['createdAtDatetime[gte]'])) if (args['createdAtDatetime[lte]'] != null) query.append("createdAtDatetime[lte]", String(args['createdAtDatetime[lte]'])) if (args.optionType != null) query.append("optionType", String(args.optionType)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/history/option-trades${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(getApiV1HistoryOptionTradesResponse, await res.json()) };