import {z} from 'zod' export type GetApiHmdsHistoryArgs = {conid: number, period: 'min' | 'h' | 'd' | 'w' | 'm' | 'y', bar?: ('min' | 'h' | 'd' | 'w' | 'm') | undefined, outsideRth?: boolean | undefined}; export type GetApiHmdsHistoryOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const historyResult = z.object({bars: z.object({open: z.number().optional(), startTime: z.string().optional(), startTimeVal: z.number().int().optional(), endTime: z.string().optional(), endTimeVal: z.number().int().optional(), points: z.number().int().optional(), data: z.array(z.object({t: z.number().optional(), o: z.number().optional(), c: z.number().optional(), h: z.number().optional(), l: z.number().optional(), v: z.number().optional()})).optional(), mktDataDelay: z.number().int().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 }; /** * Market Data History (Beta) * * GET /hmds/history */ export const getApiHmdsHistory = async (args: GetApiHmdsHistoryArgs, options?: GetApiHmdsHistoryOptions) => { const {baseUrl = "http://localhost:5000/v1/api", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.conid != null) query.append("conid", String(args.conid)) if (args.period != null) query.append("period", String(args.period)) if (args.bar != null) query.append("bar", String(args.bar)) if (args.outsideRth != null) query.append("outsideRth", String(args.outsideRth)) const url = `${baseUrl.replace(/\/+$/, '')}/hmds/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(historyResult, await res.json()) };