|
| 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.CORRELATION; |
| 6 | + |
| 7 | +export default { |
| 8 | + key: "token_metrics-get-correlation", |
| 9 | + name: "Get Correlation", |
| 10 | + description: `${endpoint.description}. [See the documentation](https://developers.tokenmetrics.com/reference/correlation)`, |
| 11 | + version: "0.0.1", |
| 12 | + type: "action", |
| 13 | + props: { |
| 14 | + tokenMetrics, |
| 15 | + // Filter props based on endpoint configuration and API documentation |
| 16 | + tokenId: { |
| 17 | + ...FILTER_DEFINITIONS.token_id, |
| 18 | + description: "Comma Separated Token IDs. Click here to access the list of token IDs. Example: 3375,3306", |
| 19 | + }, |
| 20 | + symbol: { |
| 21 | + ...FILTER_DEFINITIONS.symbol, |
| 22 | + description: "Comma Separated Token Symbols. Click here to access the list of token symbols. Example: BTC,ETH", |
| 23 | + }, |
| 24 | + category: { |
| 25 | + ...FILTER_DEFINITIONS.category, |
| 26 | + description: "Comma separated category name. Click here to access the list of categories. Example: layer-1,nft", |
| 27 | + }, |
| 28 | + exchange: { |
| 29 | + ...FILTER_DEFINITIONS.exchange, |
| 30 | + description: "Comma separated exchange name. Click here to access the list of exchanges. Example: gate,binance", |
| 31 | + }, |
| 32 | + // Pagination props |
| 33 | + limit: { |
| 34 | + propDefinition: [ |
| 35 | + tokenMetrics, |
| 36 | + "limit", |
| 37 | + ], |
| 38 | + description: "Limit the number of items in response. Defaults to 50", |
| 39 | + default: 50, |
| 40 | + }, |
| 41 | + page: { |
| 42 | + propDefinition: [ |
| 43 | + tokenMetrics, |
| 44 | + "page", |
| 45 | + ], |
| 46 | + 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", |
| 47 | + default: 1, |
| 48 | + }, |
| 49 | + }, |
| 50 | + async run({ $ }) { |
| 51 | + // Build parameters using utility function |
| 52 | + const params = buildParams(this, endpoint.filters); |
| 53 | + |
| 54 | + try { |
| 55 | + const response = await this.tokenMetrics.getCorrelation({ |
| 56 | + $, |
| 57 | + params, |
| 58 | + }); |
| 59 | + |
| 60 | + // Generate summary using utility function |
| 61 | + const filterSummary = generateFilterSummary(this, endpoint.filters); |
| 62 | + |
| 63 | + // Use $ context for export |
| 64 | + if ($ && $.export) { |
| 65 | + const dataLength = response.data?.length || 0; |
| 66 | + $.export("$summary", `Successfully retrieved correlation data for ${dataLength} tokens${filterSummary}`); |
| 67 | + } |
| 68 | + |
| 69 | + return response; |
| 70 | + } catch (error) { |
| 71 | + // Enhanced error handling |
| 72 | + const errorMessage = error.response?.data?.message || error.message || "An error occurred"; |
| 73 | + const statusCode = error.response?.status; |
| 74 | + |
| 75 | + if ($ && $.export) { |
| 76 | + $.export("$summary", `Error: ${errorMessage}`); |
| 77 | + } |
| 78 | + |
| 79 | + // Throw a more descriptive error |
| 80 | + throw new Error(`Token Metrics API Error (${statusCode || 'Unknown'}): ${errorMessage}`); |
| 81 | + } |
| 82 | + }, |
| 83 | +}; |
0 commit comments