import {z} from 'zod' export type GetApiMarketdataIndicatorsNameHistdataArgs = {name: string, symbol: string, tf: string, from?: string | undefined, to?: string | undefined, count?: number | undefined, params?: string | undefined}; export type GetApiMarketdataIndicatorsNameHistdataOptions = {baseUrl?: string | undefined, Secured?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiMarketdataIndicatorsNameHistdataResponse = z.array(z.number()); 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 technical indicator historical data by name * * GET /marketdata/indicators/{name}/histdata */ export const getApiMarketdataIndicatorsNameHistdata = async (args: GetApiMarketdataIndicatorsNameHistdataArgs, options?: GetApiMarketdataIndicatorsNameHistdataOptions) => { const {baseUrl = "https://api.tradematic.com", fetch: fetchFn = fetch, Secured} = 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.tf != null) query.append("tf", String(args.tf)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.count != null) query.append("count", String(args.count)) if (args.params != null) query.append("params", String(args.params)) if (Secured !== undefined) headers.set("X-API-Key", Secured) const url = `${baseUrl.replace(/\/+$/, '')}/marketdata/indicators/${encodeURIComponent(String(args.name))}/histdata${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(getApiMarketdataIndicatorsNameHistdataResponse, await res.json()) };