getIntradayData.ts•4.78 kB
import { z } from 'zod';
import type { MarketstackClient, MarketstackApiParams } from '../../marketstackClient.js';
// Define the input schema shape for the Intraday Data tool
const getIntradayDataInputSchemaShape = {
symbols: z
.string()
.describe(
'Specify one or multiple comma-separated stock symbols (tickers) for your request, e.g. `AAPL` or `AAPL,MSFT`. Each symbol consumes one API request. Maximum: 100 symbols'
),
exchange: z
.string()
.optional()
.describe(
'Filter your results based on a specific stock exchange by specifying the MIC identification of a stock exchange. Example: `IEXG`'
),
interval: z
.enum(['1min', '5min', '10min', '15min', '30min', '1hour', '3hour', '6hour', '12hour', '24hour'])
.optional()
.default('1hour')
.describe(
'Specify your preferred data interval. Available values: `1min`, `5min`, `10min`, `15min`, `30min`, `1hour` (Default), `3hour`, `6hour`, `12hour` and `24hour`.'
),
sort: z
.enum(['DESC', 'ASC'])
.optional()
.default('DESC')
.describe(
'By default, results are sorted by date/time descending. Use this parameter to specify a sorting order. Available values: `DESC` (Default), `ASC`.'
),
date_from: z
.string()
.optional()
.describe(
'Filter results based on a specific timeframe by passing a from-date in `YYYY-MM-DD` format. You can also specify an exact time in ISO-8601 date format, e.g. `2020-05-21T00:00:00+0000`.'
),
date_to: z
.string()
.optional()
.describe(
'Filter results based on a specific timeframe by passing an end-date in `YYYY-MM-DD` format. You can also specify an exact time in ISO-8601 date format, e.g. `2020-05-21T00:00:00+0000`.'
),
limit: z
.number()
.int()
.min(1)
.max(1000)
.optional()
.default(100)
.describe(
'Specify a pagination limit (number of results per page) for your API request. Default limit value is `100`, maximum allowed limit value is `1000`.'
),
offset: z
.number()
.int()
.min(0)
.optional()
.default(0)
.describe(
'Specify a pagination offset value for your API request. Example: An offset value of `100` combined with a limit value of 10 would show results 100-110. Default value is `0`, starting with the first available result.'
),
after_hours: z
.boolean()
.optional()
.default(false)
.describe('If set to true, includes pre and post market data if available. By default is set to false.'),
// Note: The documentation also mentions /intraday/[date] and /intraday/latest, but the parameters section
// seems to describe the /intraday endpoint with query parameters. We'll implement the query parameter
// approach for now as it's more flexible for date ranges.
};
type RawSchemaShape = typeof getIntradayDataInputSchemaShape;
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 Intraday Data tool
const getIntradayDataHandler = async (input: Input, client: MarketstackClient): Promise<Output> => {
try {
const { symbols, exchange, interval, sort, date_from, date_to, limit, offset, after_hours } = input;
const apiRequestParams: MarketstackApiParams = {
endpoint: 'intraday',
symbols,
...(exchange && { exchange }), // Include if exchange is provided
...(interval && { interval }), // Include if interval is provided
...(sort && { sort }), // Include if sort is provided
...(date_from && { date_from }), // Include if date_from is provided
...(date_to && { date_to }), // Include if date_to is provided
...(limit && { limit }), // Include if limit is provided
...(offset && { offset }), // Include if offset is provided
...(after_hours && { after_hours }), // Include if after_hours is true
};
const data = await client.fetchApiData(apiRequestParams);
return data;
} catch (error: unknown) {
console.error('getIntradayData tool error:', error);
const message = error instanceof Error ? error.message : 'An unknown error occurred.';
throw new Error(`getIntradayData 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 getIntradayDataTool: MarketstackToolDefinition = {
name: 'get_intraday_data',
description: 'Obtain intraday data for one or multiple stock tickers.',
inputSchemaShape: getIntradayDataInputSchemaShape,
handler: getIntradayDataHandler,
};