Skip to content

Commit ce3e304

Browse files
added indices performance tokens endpoint action
1 parent e18265e commit ce3e304

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
};

components/token_metrics/common/constants.mjs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,15 @@ export const ENDPOINTS = {
212212
"id",
213213
],
214214
},
215+
INDICES_PERFORMANCE: {
216+
path: "/indices-performance",
217+
description: "The Indices Performance endpoint provides historical performance data for a given index, including cumulative return on investment (ROI) over time. This data is useful for analyzing index trends and evaluating investment performance",
218+
filters: [
219+
"id",
220+
"start_date",
221+
"end_date",
222+
],
223+
},
215224
};
216225

217226
// Common filter definitions that can be reused across endpoints

components/token_metrics/token_metrics.app.mjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,5 +291,15 @@ export default {
291291
params,
292292
});
293293
},
294+
async getIndicesPerformance({
295+
$ = this,
296+
params = {},
297+
}) {
298+
return this.makeApiCall({
299+
$,
300+
endpoint: "/indices-performance",
301+
params,
302+
});
303+
},
294304
},
295305
};

0 commit comments

Comments
 (0)