import {z} from 'zod' export type GetApiIchimokuArgs = {symbol?: string | undefined, isin?: string | undefined, figi?: string | undefined, cusip?: string | undefined, interval: '1min' | '5min' | '15min' | '30min' | '45min' | '1h' | '2h' | '4h' | '8h' | '1day' | '1week' | '1month', outputsize?: number | undefined, exchange?: string | undefined, mic_code?: string | undefined, country?: string | undefined, type?: ('American Depositary Receipt' | 'Bond' | 'Bond Fund' | 'Closed-end Fund' | 'Common Stock' | 'Depositary Receipt' | 'Digital Currency' | 'ETF' | 'Exchange-Traded Note' | 'Global Depositary Receipt' | 'Limited Partnership' | 'Mutual Fund' | 'Physical Currency' | 'Preferred Stock' | 'REIT' | 'Right' | 'Structured Product' | 'Trust' | 'Unit' | 'Warrant') | undefined, timezone?: string | undefined, start_date?: string | undefined, end_date?: string | undefined, date?: string | undefined, order?: ('asc' | 'desc') | undefined, prepost?: boolean | undefined, format?: ('JSON' | 'CSV') | undefined, delimiter?: string | undefined, dp?: number | undefined, previous_close?: boolean | undefined, adjust?: ('all' | 'splits' | 'dividends' | 'none') | undefined, conversion_line_period?: number | undefined, base_line_period?: number | undefined, leading_span_b_period?: number | undefined, lagging_span_period?: number | undefined, include_ahead_span_period?: boolean | undefined, include_ohlc?: boolean | undefined}; export type GetApiIchimokuOptions = {baseUrl?: string | undefined, authorizationHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiIchimokuResponse = z.object({meta: z.object({symbol: z.string(), interval: z.string(), currency: z.string().optional(), exchange_timezone: z.string().optional(), exchange: z.string().optional(), mic_code: z.string().optional(), type: z.string(), indicator: z.object({name: z.string(), conversion_line_period: z.number().int(), base_line_period: z.number().int(), leading_span_b_period: z.number().int(), lagging_span_period: z.number().int(), include_ahead_span_period: z.boolean()})}), values: z.array(z.object({datetime: z.string(), tenkan_sen: z.string().optional(), kijun_sen: z.string().optional(), senkou_span_a: z.string(), senkou_span_b: z.string(), chikou_span: z.string().optional()})), status: z.string()}); 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 }; /** * Ichimoku cloud * * GET /ichimoku * * Also accepts auth: queryParameter */ export const getApiIchimoku = async (args: GetApiIchimokuArgs, options?: GetApiIchimokuOptions) => { 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.symbol != null) query.append("symbol", String(args.symbol)) if (args.isin != null) query.append("isin", String(args.isin)) if (args.figi != null) query.append("figi", String(args.figi)) if (args.cusip != null) query.append("cusip", String(args.cusip)) if (args.interval != null) query.append("interval", String(args.interval)) if (args.outputsize != null) query.append("outputsize", String(args.outputsize)) if (args.exchange != null) query.append("exchange", String(args.exchange)) if (args.mic_code != null) query.append("mic_code", String(args.mic_code)) if (args.country != null) query.append("country", String(args.country)) if (args.type != null) query.append("type", String(args.type)) if (args.timezone != null) query.append("timezone", String(args.timezone)) 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.date != null) query.append("date", String(args.date)) if (args.order != null) query.append("order", String(args.order)) if (args.prepost != null) query.append("prepost", String(args.prepost)) if (args.format != null) query.append("format", String(args.format)) if (args.delimiter != null) query.append("delimiter", String(args.delimiter)) if (args.dp != null) query.append("dp", String(args.dp)) if (args.previous_close != null) query.append("previous_close", String(args.previous_close)) if (args.adjust != null) query.append("adjust", String(args.adjust)) if (args.conversion_line_period != null) query.append("conversion_line_period", String(args.conversion_line_period)) if (args.base_line_period != null) query.append("base_line_period", String(args.base_line_period)) if (args.leading_span_b_period != null) query.append("leading_span_b_period", String(args.leading_span_b_period)) if (args.lagging_span_period != null) query.append("lagging_span_period", String(args.lagging_span_period)) if (args.include_ahead_span_period != null) query.append("include_ahead_span_period", String(args.include_ahead_span_period)) if (args.include_ohlc != null) query.append("include_ohlc", String(args.include_ohlc)) if (authorizationHeader !== undefined) headers.set("Authorization", authorizationHeader) const url = `${baseUrl.replace(/\/+$/, '')}/ichimoku${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(getApiIchimokuResponse, await res.json()) };