Skip to content

Commit e18265e

Browse files
added indices holdings tokens endpoint action
1 parent 8c25d8e commit e18265e

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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_HOLDINGS;
6+
7+
export default {
8+
key: "token_metrics-get-indices-holdings",
9+
name: "Get Indices Holdings",
10+
description: `${endpoint.description}. [See the documentation](https://developers.tokenmetrics.com/reference/indices-holdings)`,
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+
},
21+
async run({ $ }) {
22+
// Build parameters using utility function
23+
const params = buildParams(this, endpoint.filters);
24+
25+
try {
26+
const response = await this.tokenMetrics.getIndicesHoldings({
27+
$,
28+
params,
29+
});
30+
31+
// Generate summary using utility function
32+
const filterSummary = generateFilterSummary(this, endpoint.filters);
33+
34+
// Use $ context for export
35+
if ($ && $.export) {
36+
const dataLength = response.data?.length || 0;
37+
$.export("$summary", `Successfully retrieved holdings for index with ${dataLength} tokens${filterSummary}`);
38+
}
39+
40+
return response;
41+
} catch (error) {
42+
// Enhanced error handling
43+
const errorMessage = error.response?.data?.message || error.message || "An error occurred";
44+
const statusCode = error.response?.status;
45+
46+
if ($ && $.export) {
47+
$.export("$summary", `Error: ${errorMessage}`);
48+
}
49+
50+
// Throw a more descriptive error
51+
throw new Error(`Token Metrics API Error (${statusCode || 'Unknown'}): ${errorMessage}`);
52+
}
53+
},
54+
};

components/token_metrics/common/constants.mjs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,13 @@ export const ENDPOINTS = {
205205
"indices_type",
206206
],
207207
},
208+
INDICES_HOLDINGS: {
209+
path: "/indices-holdings",
210+
description: "This endpoint returns the current holdings of the given Index, along with their respective weight in %",
211+
filters: [
212+
"id",
213+
],
214+
},
208215
};
209216

210217
// Common filter definitions that can be reused across endpoints
@@ -334,6 +341,12 @@ export const FILTER_DEFINITIONS = {
334341
},
335342
],
336343
},
344+
id: {
345+
type: "integer",
346+
label: "ID",
347+
description: "ID of the index. Example: 1",
348+
optional: false,
349+
},
337350
};
338351

339352
// Common error messages

components/token_metrics/token_metrics.app.mjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,5 +281,15 @@ export default {
281281
params,
282282
});
283283
},
284+
async getIndicesHoldings({
285+
$ = this,
286+
params = {},
287+
}) {
288+
return this.makeApiCall({
289+
$,
290+
endpoint: "/indices-holdings",
291+
params,
292+
});
293+
},
284294
},
285295
};

0 commit comments

Comments
 (0)