import {z} from 'zod' export type GetApiDividendsArgs = {symbol?: string | undefined, figi?: string | undefined, isin?: string | undefined, cusip?: string | undefined, exchange?: string | undefined, mic_code?: string | undefined, country?: string | undefined, range?: ('last' | 'next' | '1m' | '3m' | '6m' | 'ytd' | '1y' | '2y' | '5y' | 'full') | undefined, start_date?: string | undefined, end_date?: string | undefined, adjust?: boolean | undefined}; export type GetApiDividendsOptions = {baseUrl?: string | undefined, authorizationHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiDividendsResponse = z.object({meta: z.object({symbol: z.string().optional(), name: z.string().optional(), currency: z.string().optional(), exchange: z.string().optional(), mic_code: z.string().optional(), exchange_timezone: z.string().optional()}), dividends: z.array(z.object({ex_date: z.string(), amount: 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 }; /** * Dividends * * GET /dividends * * Also accepts auth: queryParameter */ export const getApiDividends = async (args: GetApiDividendsArgs, options?: GetApiDividendsOptions) => { 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.figi != null) query.append("figi", String(args.figi)) if (args.isin != null) query.append("isin", String(args.isin)) if (args.cusip != null) query.append("cusip", String(args.cusip)) 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.range != null) query.append("range", String(args.range)) 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 (authorizationHeader !== undefined) headers.set("Authorization", authorizationHeader) const url = `${baseUrl.replace(/\/+$/, '')}/dividends${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(getApiDividendsResponse, await res.json()) };