import {z} from 'zod' export type GetApiV1PricesHistoryArgs = {symbol: string, from: string, to: string, interval?: string | undefined, limit?: number | undefined}; export type GetApiV1PricesHistoryOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const historySeriesEntry = z.object({date: z.string(), open: z.string().nullable().optional(), high: z.string().nullable().optional(), low: z.string().nullable().optional(), close: z.string().nullable().optional(), volume: z.string().nullable().optional(), is_stale: z.boolean(), is_closed: z.boolean().nullable().optional()}).and(z.record(z.string(), z.unknown())); export const historyMeta = z.object({tier: z.string(), count: z.number().int(), ohlc_note: z.string()}).and(z.record(z.string(), z.unknown())); export const historyResponse = z.object({symbol: z.string(), interval: z.string(), from: z.string(), to: z.string(), series: z.array(historySeriesEntry), meta: historyMeta}).and(z.record(z.string(), z.unknown())); 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 }; /** * [DEPRECATED — use /v1/bars] Aggregated close history for a symbol — interval ∈ {1d, 1h}. * * GET /v1/prices/history * * Also accepts auth: ApiKeyAuth */ export const getApiV1PricesHistory = async (args: GetApiV1PricesHistoryArgs, options?: GetApiV1PricesHistoryOptions) => { const {baseUrl = "https://api.goldprice.dev", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.symbol != null) query.append("symbol", String(args.symbol)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.interval != null) query.append("interval", String(args.interval)) if (args.limit != null) query.append("limit", String(args.limit)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/prices/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(historyResponse, await res.json()) };