import {z} from 'zod' export type CreateApiV1MarketsQuotesArgs = {'Content-Type': 'application/x-www-form-urlencoded', Accept: 'application/json', body: {symbols: string, greeks?: boolean | undefined, includeLotSize?: boolean | undefined}}; export type CreateApiV1MarketsQuotesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const quote = z.object({symbol: z.string().optional(), description: z.string().optional(), exch: z.string().optional(), type: z.string().optional(), last: z.number().optional(), change: z.number().optional(), volume: z.number().int().optional(), open: z.number().optional(), high: z.number().optional(), low: z.number().optional(), close: z.number().optional(), bid: z.number().optional(), ask: z.number().optional(), change_percentage: z.number().optional(), average_volume: z.number().int().optional(), last_volume: z.number().int().optional(), trade_date: z.number().int().optional(), prevclose: z.number().optional(), week_52_high: z.number().optional(), week_52_low: z.number().optional(), bidsize: z.number().int().optional(), bidexch: z.string().optional(), bid_date: z.number().int().optional(), asksize: z.number().int().optional(), askexch: z.string().optional(), ask_date: z.number().int().optional(), open_interest: z.number().int().optional(), contract_size: z.number().int().optional(), expiration_date: z.string().optional(), expiration_type: z.string().optional(), option_type: z.enum(["call", "put"]).optional(), root_symbol: z.string().optional(), underlying: z.string().optional(), strike: z.number().optional(), lot_size: z.number().int().optional(), greeks: z.object({delta: z.number().optional(), gamma: z.number().optional(), theta: z.number().optional(), vega: z.number().optional(), rho: z.number().optional(), phi: z.number().optional(), bid_iv: z.number().optional(), mid_iv: z.number().optional(), ask_iv: z.number().optional(), smv_vol: z.number().optional(), updated_at: z.string().optional()}).optional()}); export const quotesResponse = z.object({quotes: z.object({quote: z.union([quote, z.array(quote)]).optional()}).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 }; /** * Post Quotes * * POST /v1/markets/quotes */ export const createApiV1MarketsQuotes = async (args: CreateApiV1MarketsQuotesArgs, options?: CreateApiV1MarketsQuotesOptions) => { const {baseUrl = "https://api.tradier.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/x-www-form-urlencoded") headers.set('accept', "application/json") if (args['Content-Type'] != null) headers.set("Content-Type", String(args['Content-Type'])) if (args.Accept != null) headers.set("Accept", String(args.Accept)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/markets/quotes${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: new URLSearchParams(Object.entries(args.body).flatMap(([key, value]) => (value == null ? [] : [[key, String(value)]])))}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(quotesResponse, await res.json()) };