import {z} from 'zod' export type GetApiTimeSeriesCrossArgs = {base: string, base_type?: string | undefined, base_exchange?: string | undefined, base_mic_code?: string | undefined, quote: string, quote_type?: string | undefined, quote_exchange?: string | undefined, quote_mic_code?: string | undefined, interval: '1min' | '5min' | '15min' | '30min' | '45min' | '1h' | '2h' | '4h' | '8h' | '1day' | '1week' | '1month', outputsize?: number | undefined, format?: ('JSON' | 'CSV') | undefined, delimiter?: string | undefined, prepost?: boolean | undefined, start_date?: string | undefined, end_date?: string | undefined, adjust?: boolean | undefined, dp?: number | undefined, timezone?: string | undefined}; export type GetApiTimeSeriesCrossOptions = {baseUrl?: string | undefined, authorizationHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const crossMeta = z.object({base_instrument: z.string(), base_currency: z.string(), base_exchange: z.string(), interval: z.string(), quote_instrument: z.string(), quote_currency: z.string(), quote_exchange: z.string()}); export const timeSeriesCrossItem = z.object({datetime: z.string(), open: z.string(), high: z.string(), low: z.string(), close: z.string()}); export const getApiTimeSeriesCrossResponse = z.object({meta: crossMeta, values: z.array(timeSeriesCrossItem)}); 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 }; /** * Time series cross * * GET /time_series/cross * * Also accepts auth: queryParameter */ export const getApiTimeSeriesCross = async (args: GetApiTimeSeriesCrossArgs, options?: GetApiTimeSeriesCrossOptions) => { const {baseUrl = "https://api.twelvedata.com", fetch: fetchFn = fetch, authorizationHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.base != null) query.append("base", String(args.base)) if (args.base_type != null) query.append("base_type", String(args.base_type)) if (args.base_exchange != null) query.append("base_exchange", String(args.base_exchange)) if (args.base_mic_code != null) query.append("base_mic_code", String(args.base_mic_code)) if (args.quote != null) query.append("quote", String(args.quote)) if (args.quote_type != null) query.append("quote_type", String(args.quote_type)) if (args.quote_exchange != null) query.append("quote_exchange", String(args.quote_exchange)) if (args.quote_mic_code != null) query.append("quote_mic_code", String(args.quote_mic_code)) if (args.interval != null) query.append("interval", String(args.interval)) if (args.outputsize != null) query.append("outputsize", String(args.outputsize)) if (args.format != null) query.append("format", String(args.format)) if (args.delimiter != null) query.append("delimiter", String(args.delimiter)) if (args.prepost != null) query.append("prepost", String(args.prepost)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.adjust != null) query.append("adjust", String(args.adjust)) if (args.dp != null) query.append("dp", String(args.dp)) if (args.timezone != null) query.append("timezone", String(args.timezone)) if (authorizationHeader !== undefined) headers.set("Authorization", authorizationHeader) const url = `${baseUrl.replace(/\/+$/, '')}/time_series/cross${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(getApiTimeSeriesCrossResponse, await res.json()) };