import {z} from 'zod' export type GetApiV1StocksBulkcandlesResolutionArgs = {format?: ('csv' | 'json') | undefined, resolution: string, symbol: string, date?: string | undefined, from?: string | undefined, to?: string | undefined, countback?: number | undefined, exchange?: string | undefined, extended?: boolean | undefined, country?: string | undefined, adjustsplits?: boolean | undefined, adjustdividends?: boolean | undefined, dateformat?: ('spreadsheet' | 'timestamp' | 'unix') | undefined, limit?: number | undefined, offset?: number | undefined, headers?: boolean | undefined, columns?: string | undefined, human?: boolean | undefined}; export type GetApiV1StocksBulkcandlesResolutionOptions = {baseUrl: string, ApiAuthentication?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1StocksBulkcandlesResolutionResponse = 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 }; /** * Get bulk candles for stocks. This endpoint returns daily candle data for multiple stocks. * * GET /v1/stocks/bulkcandles/{resolution}/ * * Also accepts auth: JWT Authentication */ export const getApiV1StocksBulkcandlesResolution = async (args: GetApiV1StocksBulkcandlesResolutionArgs, options: GetApiV1StocksBulkcandlesResolutionOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiAuthentication} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.format != null) query.append("format", String(args.format)) if (args.date != null) query.append("date", String(args.date)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.countback != null) query.append("countback", String(args.countback)) if (args.exchange != null) query.append("exchange", String(args.exchange)) if (args.extended != null) query.append("extended", String(args.extended)) if (args.country != null) query.append("country", String(args.country)) if (args.adjustsplits != null) query.append("adjustsplits", String(args.adjustsplits)) if (args.adjustdividends != null) query.append("adjustdividends", String(args.adjustdividends)) if (args.dateformat != null) query.append("dateformat", String(args.dateformat)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.headers != null) query.append("headers", String(args.headers)) if (args.columns != null) query.append("columns", String(args.columns)) if (args.human != null) query.append("human", String(args.human)) if (ApiAuthentication !== undefined) query.set("token", ApiAuthentication) const url = `${baseUrl.replace(/\/+$/, '')}/v1/stocks/bulkcandles/${encodeURIComponent(String(args.resolution))}/${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(getApiV1StocksBulkcandlesResolutionResponse, await res.json()) };