import {z} from 'zod' export type GetApiLastChangeEndpointArgs = {endpoint: 'price_target' | 'recommendations' | 'statistics' | 'insider_transactions' | 'profile' | 'mutual_funds_world_summary' | 'mutual_funds_world' | 'institutional_holders' | 'analyst_rating' | 'income_statement' | 'income_statement_quarterly' | 'cash_flow' | 'cash_flow_quarterly' | 'balance_sheet' | 'balance_sheet_quarterly' | 'mutual_funds_list' | 'mutual_funds_world_sustainability' | 'mutual_funds_world_summary' | 'mutual_funds_world_risk' | 'mutual_funds_world_purchase_info' | 'mutual_funds_world_composition' | 'mutual_funds_world_performance' | 'mutual_funds_world' | 'etfs_list' | 'etfs_world' | 'etfs_world_summary' | 'etfs_world_performance' | 'etfs_world_risk' | 'etfs_world_composition' | 'dividends' | 'splits' | 'money_market_funds_world', start_date?: string | undefined, symbol?: string | undefined, exchange?: string | undefined, mic_code?: string | undefined, country?: string | undefined, page?: number | undefined, outputsize?: number | undefined}; export type GetApiLastChangeEndpointOptions = {baseUrl?: string | undefined, authorizationHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const lastChangeResponseItem = z.object({symbol: z.string(), mic_code: z.string(), last_change: z.string()}); export const getApiLastChangeEndpointResponse = z.object({pagination: z.object({current_page: z.number().int(), per_page: z.number().int()}), data: z.array(lastChangeResponseItem)}); 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 }; /** * Last changes * * GET /last_change/{endpoint} * * Also accepts auth: queryParameter */ export const getApiLastChangeEndpoint = async (args: GetApiLastChangeEndpointArgs, options?: GetApiLastChangeEndpointOptions) => { 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.start_date != null) query.append("start_date", String(args.start_date)) if (args.symbol != null) query.append("symbol", String(args.symbol)) 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.page != null) query.append("page", String(args.page)) if (args.outputsize != null) query.append("outputsize", String(args.outputsize)) if (authorizationHeader !== undefined) headers.set("Authorization", authorizationHeader) const url = `${baseUrl.replace(/\/+$/, '')}/last_change/${encodeURIComponent(String(args.endpoint))}${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(getApiLastChangeEndpointResponse, await res.json()) };