getEtfHoldings.ts•2.14 kB
import { z } from 'zod';
import type { MarketstackClient, MarketstackApiParams } from '../../marketstackClient.js';
// Define the input schema shape for the ETF Holdings Info tool
const getEtfHoldingsInputSchemaShape = {
ticker: z.string().describe('To get results based on a ETF ticker.'),
date_from: z
.string()
.optional()
.describe('Filter results based on a specific timeframe by passing a from-date in `YYYY-MM-DD` format.'),
date_to: z
.string()
.optional()
.describe('Filter results based on a specific timeframe by passing an end-date in `YYYY-MM-DD` format.'),
};
type RawSchemaShape = typeof getEtfHoldingsInputSchemaShape;
type Input = z.infer<z.ZodObject<RawSchemaShape>>;
type Output = any; // TODO: Define a more specific output type based on Marketstack response
// Define the handler function for the ETF Holdings Info tool
const getEtfHoldingsHandler = async (input: Input, client: MarketstackClient): Promise<Output> => {
try {
const { ticker, date_from, date_to } = input;
const apiRequestParams: MarketstackApiParams = {
endpoint: 'etfholdings',
ticker,
...(date_from && { date_from }), // Include if date_from is provided
...(date_to && { date_to }), // Include if date_to is provided
};
const data = await client.fetchApiData(apiRequestParams);
return data;
} catch (error: unknown) {
console.error('getEtfHoldings tool error:', error);
const message = error instanceof Error ? error.message : 'An unknown error occurred.';
throw new Error(`getEtfHoldings tool failed: ${message}`);
}
};
// Define the tool definition object structure
type MarketstackToolDefinition = {
name: string;
description: string;
inputSchemaShape: RawSchemaShape;
handler: (input: Input, client: MarketstackClient) => Promise<Output>;
};
export const getEtfHoldingsTool: MarketstackToolDefinition = {
name: 'get_etf_holdings',
description: 'Get complete set of exchange-traded funds data based on the unique identifier code of an ETF.',
inputSchemaShape: getEtfHoldingsInputSchemaShape,
handler: getEtfHoldingsHandler,
};