import {z} from 'zod' export type GetApiOptionsQuotesOptionsTickerArgs = {optionsTicker: string, timestamp?: string | undefined, 'timestamp.gt'?: string | undefined, 'timestamp.gte'?: string | undefined, 'timestamp.lt'?: string | undefined, 'timestamp.lte'?: string | undefined, order?: ('asc' | 'desc') | undefined, limit?: number | undefined, sort?: string | undefined}; export type GetApiOptionsQuotesOptionsTickerOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiOptionsQuotesOptionsTickerResponse = z.object({"request-id": z.string().optional(), status: z.string().optional(), results: z.array(z.object({"ask-exchange": z.number().int().optional(), "ask-price": z.number().optional(), "ask-size": z.number().optional(), "bid-exchange": z.number().int().optional(), "bid-price": z.number().optional(), "bid-size": z.number().optional(), "sequence-number": z.number().int().optional(), "sip-timestamp": z.number().int().optional()})).optional(), "next-url": 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 }; /** * Get Options Quotes * * GET /options/quotes/{optionsTicker} */ export const getApiOptionsQuotesOptionsTicker = async (args: GetApiOptionsQuotesOptionsTickerArgs, options: GetApiOptionsQuotesOptionsTickerOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.timestamp != null) query.append("timestamp", String(args.timestamp)) if (args['timestamp.gt'] != null) query.append("timestamp.gt", String(args['timestamp.gt'])) if (args['timestamp.gte'] != null) query.append("timestamp.gte", String(args['timestamp.gte'])) if (args['timestamp.lt'] != null) query.append("timestamp.lt", String(args['timestamp.lt'])) if (args['timestamp.lte'] != null) query.append("timestamp.lte", String(args['timestamp.lte'])) if (args.order != null) query.append("order", String(args.order)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.sort != null) query.append("sort", String(args.sort)) if (ApiKeyAuth !== undefined) headers.set("X-API-KEY", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/options/quotes/${encodeURIComponent(String(args.optionsTicker))}${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(getApiOptionsQuotesOptionsTickerResponse, await res.json()) };