|
| 1 | +import tokenMetrics from "../../token_metrics.app.mjs"; |
| 2 | +import { ENDPOINTS, FILTER_DEFINITIONS } from "../../common/constants.mjs"; |
| 3 | +import { buildParams, generateFilterSummary } from "../../common/utils.mjs"; |
| 4 | + |
| 5 | +const endpoint = ENDPOINTS.INDICES_PERFORMANCE; |
| 6 | + |
| 7 | +export default { |
| 8 | + key: "token_metrics-get-indices-performance", |
| 9 | + name: "Get Indices Performance", |
| 10 | + description: `${endpoint.description}. [See the documentation](https://developers.tokenmetrics.com/reference/indices-performance)`, |
| 11 | + version: "0.0.1", |
| 12 | + type: "action", |
| 13 | + props: { |
| 14 | + tokenMetrics, |
| 15 | + // Filter props based on endpoint configuration and API documentation |
| 16 | + id: { |
| 17 | + ...FILTER_DEFINITIONS.id, |
| 18 | + description: "ID of the index. Example: 1", |
| 19 | + }, |
| 20 | + startDate: { |
| 21 | + ...FILTER_DEFINITIONS.start_date, |
| 22 | + description: "Start Date accepts date as a string - YYYY-MM-DD format. Example: 2025-01-01", |
| 23 | + }, |
| 24 | + endDate: { |
| 25 | + ...FILTER_DEFINITIONS.end_date, |
| 26 | + description: "End Date accepts date as a string - YYYY-MM-DD format. Example: 2025-06-01", |
| 27 | + }, |
| 28 | + // Pagination props |
| 29 | + limit: { |
| 30 | + propDefinition: [ |
| 31 | + tokenMetrics, |
| 32 | + "limit", |
| 33 | + ], |
| 34 | + description: "Limit the number of items in response. Defaults to 50", |
| 35 | + default: 50, |
| 36 | + }, |
| 37 | + page: { |
| 38 | + propDefinition: [ |
| 39 | + tokenMetrics, |
| 40 | + "page", |
| 41 | + ], |
| 42 | + description: "Enables pagination and data retrieval control by skipping a specified number of items before fetching data. Page should be a non-negative integer, with 1 indicating the beginning of the dataset. Defaults to 1", |
| 43 | + default: 1, |
| 44 | + }, |
| 45 | + }, |
| 46 | + async run({ $ }) { |
| 47 | + // Build parameters using utility function |
| 48 | + const params = buildParams(this, endpoint.filters); |
| 49 | + |
| 50 | + try { |
| 51 | + const response = await this.tokenMetrics.getIndicesPerformance({ |
| 52 | + $, |
| 53 | + params, |
| 54 | + }); |
| 55 | + |
| 56 | + // Generate summary using utility function |
| 57 | + const filterSummary = generateFilterSummary(this, endpoint.filters); |
| 58 | + |
| 59 | + // Use $ context for export |
| 60 | + if ($ && $.export) { |
| 61 | + const dataLength = response.data?.length || 0; |
| 62 | + $.export("$summary", `Successfully retrieved historical performance data for index with ${dataLength} data points${filterSummary}`); |
| 63 | + } |
| 64 | + |
| 65 | + return response; |
| 66 | + } catch (error) { |
| 67 | + // Enhanced error handling |
| 68 | + const errorMessage = error.response?.data?.message || error.message || "An error occurred"; |
| 69 | + const statusCode = error.response?.status; |
| 70 | + |
| 71 | + if ($ && $.export) { |
| 72 | + $.export("$summary", `Error: ${errorMessage}`); |
| 73 | + } |
| 74 | + |
| 75 | + // Throw a more descriptive error |
| 76 | + throw new Error(`Token Metrics API Error (${statusCode || 'Unknown'}): ${errorMessage}`); |
| 77 | + } |
| 78 | + }, |
| 79 | +}; |
0 commit comments