UNPKG

n8n-nodes-hdw

Version:

n8n node to access Horizon Data Wave API services

297 lines 13.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.HdwInstagram = void 0; class HdwInstagram { constructor() { this.description = { displayName: 'HDW Instagram', name: 'hdwInstagram', icon: 'file:hdw_logo.png', group: ['transform'], version: 1, usableAsTool: true, subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}', description: 'Integrate with Horizon Data Wave Instagram API', defaults: { name: 'HDW Instagram', }, inputs: ["main" /* NodeConnectionType.Main */], outputs: ["main" /* NodeConnectionType.Main */], credentials: [ { name: 'hdwLinkedinApi', required: true, }, ], requestDefaults: { baseURL: 'https://api.horizondatawave.ai', headers: { Accept: 'application/json', 'Content-Type': 'application/json', }, }, properties: [ { displayName: 'Resource', name: 'resource', type: 'options', noDataExpression: true, options: [ { name: 'User', value: 'user' }, { name: 'Post', value: 'post' }, ], default: 'user', }, { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, displayOptions: { show: { resource: ['user'] } }, options: [ { name: 'Get Profile', value: 'getProfile', description: 'Get Instagram user profile', action: 'Get instagram user profile', routing: { request: { method: 'POST', url: '/api/instagram/user', body: { user: '={{$parameter["user"]}}', timeout: '={{$parameter["timeout"]}}', with_creation_date: '={{$parameter["with_creation_date"]}}', }, }, }, }, { name: 'Get Posts', value: 'getPosts', description: 'Get Instagram user posts', action: 'Get instagram user posts', routing: { request: { method: 'POST', url: '/api/instagram/user/posts', body: { user: '={{$parameter["user"]}}', count: '={{$parameter["count"]}}', timeout: '={{$parameter["timeout"]}}', }, }, }, }, ], default: 'getProfile', }, { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, displayOptions: { show: { resource: ['post'] } }, options: [ { name: 'Get Comments', value: 'getComments', description: 'Get Instagram post comments', action: 'Get instagram post comments', routing: { request: { method: 'POST', url: '/api/instagram/post/comments', body: { post: '={{$parameter["post"]}}', count: '={{$parameter["count"]}}', timeout: '={{$parameter["timeout"]}}', }, }, }, }, ], default: 'getComments', }, { displayName: 'User', name: 'user', type: 'string', required: true, default: '', placeholder: 'cristiano', description: 'Instagram username, user ID, or URL', displayOptions: { show: { resource: ['user'] } }, }, { displayName: 'Count', name: 'count', type: 'number', default: 12, description: 'Maximum number of posts to return', displayOptions: { show: { resource: ['user'], operation: ['getPosts'] } }, }, { displayName: 'Post ID', name: 'post', type: 'string', required: true, default: '', placeholder: '3676612811870810696_1777543238', description: 'Instagram post ID', displayOptions: { show: { resource: ['post'], operation: ['getComments'] } }, }, { displayName: 'Count', name: 'count', type: 'number', default: 20, description: 'Maximum number of comments to return', displayOptions: { show: { resource: ['post'], operation: ['getComments'] } }, }, { displayName: 'With Creation Date', name: 'with_creation_date', type: 'boolean', default: false, description: 'Whether to include account creation date in the response', displayOptions: { show: { resource: ['user'], operation: ['getProfile'] } }, }, { displayName: 'Timeout', name: 'timeout', type: 'number', default: 300, description: 'Timeout in seconds (20-1500)', }, ], }; } async execute() { const items = this.getInputData(); const returnData = []; const baseURL = 'https://api.horizondatawave.ai'; const delayInMs = 100; const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); for (let i = 0; i < items.length; i++) { try { const resource = this.getNodeParameter('resource', i); const operation = this.getNodeParameter('operation', i); const timeout = this.getNodeParameter('timeout', i); let endpoint = ''; const body = { timeout }; if (resource === 'user') { const user = this.getNodeParameter('user', i); body.user = user; if (operation === 'getProfile') { endpoint = '/api/instagram/user'; body.with_creation_date = this.getNodeParameter('with_creation_date', i, false); } else if (operation === 'getPosts') { endpoint = '/api/instagram/user/posts'; body.count = this.getNodeParameter('count', i); } } else if (resource === 'post') { if (operation === 'getComments') { endpoint = '/api/instagram/post/comments'; body.post = this.getNodeParameter('post', i); body.count = this.getNodeParameter('count', i); } } const options = { method: 'POST', url: `${baseURL}${endpoint}`, body, headers: { Accept: 'application/json', 'Content-Type': 'application/json', }, json: true, }; const responseData = await this.helpers.httpRequestWithAuthentication.call(this, 'hdwLinkedinApi', options); if (Array.isArray(responseData)) { for (const element of responseData) { returnData.push({ json: element }); } } else { returnData.push({ json: responseData }); } if (i < items.length - 1) { await sleep(delayInMs); } } catch (error) { // Enhanced error handling to extract information from headers and response body let errorMessage = error.message; let errorDetails = 'No detailed error information available'; let httpStatus = ''; let apiError = ''; let requestId = ''; let executionTime = ''; let tokenPoints = ''; // Extract information from HTTP response if available if (error.response) { httpStatus = error.response.status || ''; // Extract custom headers from HDW API if (error.response.headers) { apiError = error.response.headers['x-error'] || ''; requestId = error.response.headers['x-request-id'] || ''; executionTime = error.response.headers['x-execution-time'] || ''; tokenPoints = error.response.headers['x-token-points'] || ''; } // Try to get error details from response body if (error.response.data) { if (typeof error.response.data === 'string') { errorDetails = error.response.data; } else if (typeof error.response.data === 'object') { errorDetails = JSON.stringify(error.response.data); } } // If we have API error from headers, use it as the main error message if (apiError) { errorMessage = `${apiError} (HTTP ${httpStatus})`; } // Build comprehensive error details const detailParts = []; if (apiError) detailParts.push(`API Error: ${apiError}`); if (httpStatus) detailParts.push(`HTTP Status: ${httpStatus}`); if (requestId) detailParts.push(`Request ID: ${requestId}`); if (executionTime) detailParts.push(`Execution Time: ${executionTime}s`); if (tokenPoints) detailParts.push(`Token Points: ${tokenPoints}`); if (error.response.data && error.response.data !== '{}') { detailParts.push(`Response Body: ${typeof error.response.data === 'object' ? JSON.stringify(error.response.data) : error.response.data}`); } if (detailParts.length > 0) { errorDetails = detailParts.join(' | '); } } if (this.continueOnFail()) { returnData.push({ json: { error: errorMessage, details: errorDetails, httpStatus: httpStatus, apiError: apiError, requestId: requestId, executionTime: executionTime, tokenPoints: tokenPoints, }, }); continue; } throw error; } } return [returnData]; } } exports.HdwInstagram = HdwInstagram; //# sourceMappingURL=HdwInstagram.node.js.map