Skip to content

Commit 8c25d8e

Browse files
added indices tokens endpoint action
1 parent 9868b59 commit 8c25d8e

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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;
6+
7+
export default {
8+
key: "token_metrics-get-indices",
9+
name: "Get Indices",
10+
description: `${endpoint.description}. [See the documentation](https://developers.tokenmetrics.com/reference/indices)`,
11+
version: "0.0.1",
12+
type: "action",
13+
props: {
14+
tokenMetrics,
15+
// Filter props based on endpoint configuration and API documentation
16+
indicesType: {
17+
...FILTER_DEFINITIONS.indices_type,
18+
description: "Filter to return indices by type: 'active' for actively managed, 'passive' for passively managed",
19+
},
20+
// Pagination props
21+
limit: {
22+
propDefinition: [
23+
tokenMetrics,
24+
"limit",
25+
],
26+
description: "Limit the number of items in response. Defaults to 50",
27+
default: 50,
28+
},
29+
page: {
30+
propDefinition: [
31+
tokenMetrics,
32+
"page",
33+
],
34+
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",
35+
default: 1,
36+
},
37+
},
38+
async run({ $ }) {
39+
// Build parameters using utility function
40+
const params = buildParams(this, endpoint.filters);
41+
42+
try {
43+
const response = await this.tokenMetrics.getIndices({
44+
$,
45+
params,
46+
});
47+
48+
// Generate summary using utility function
49+
const filterSummary = generateFilterSummary(this, endpoint.filters);
50+
51+
// Use $ context for export
52+
if ($ && $.export) {
53+
const dataLength = response.data?.length || 0;
54+
$.export("$summary", `Successfully retrieved ${dataLength} crypto indices${filterSummary}`);
55+
}
56+
57+
return response;
58+
} catch (error) {
59+
// Enhanced error handling
60+
const errorMessage = error.response?.data?.message || error.message || "An error occurred";
61+
const statusCode = error.response?.status;
62+
63+
if ($ && $.export) {
64+
$.export("$summary", `Error: ${errorMessage}`);
65+
}
66+
67+
// Throw a more descriptive error
68+
throw new Error(`Token Metrics API Error (${statusCode || 'Unknown'}): ${errorMessage}`);
69+
}
70+
},
71+
};

components/token_metrics/common/constants.mjs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,13 @@ export const ENDPOINTS = {
198198
"exchange",
199199
],
200200
},
201+
INDICES: {
202+
path: "/indices",
203+
description: "Get active and passive crypto indices with performance and market data",
204+
filters: [
205+
"indices_type",
206+
],
207+
},
201208
};
202209

203210
// Common filter definitions that can be reused across endpoints
@@ -311,6 +318,22 @@ export const FILTER_DEFINITIONS = {
311318
description: "Specifies the number of top cryptocurrencies to retrieve, based on their market capitalization. Example: 100",
312319
optional: true,
313320
},
321+
indices_type: {
322+
type: "string",
323+
label: "Indices Type",
324+
description: "Filter to return indices by type: 'active' for actively managed, 'passive' for passively managed",
325+
optional: true,
326+
options: [
327+
{
328+
label: "Active",
329+
value: "active",
330+
},
331+
{
332+
label: "Passive",
333+
value: "passive",
334+
},
335+
],
336+
},
314337
};
315338

316339
// Common error messages

components/token_metrics/token_metrics.app.mjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,5 +271,15 @@ export default {
271271
params,
272272
});
273273
},
274+
async getIndices({
275+
$ = this,
276+
params = {},
277+
}) {
278+
return this.makeApiCall({
279+
$,
280+
endpoint: "/indices",
281+
params,
282+
});
283+
},
274284
},
275285
};

0 commit comments

Comments
 (0)