UNPKG

n8n-nodes-aluvia

Version:

n8n community node for making HTTP requests through Aluvia's mobile connectivity network

478 lines 20.6 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Aluvia = void 0; const n8n_workflow_1 = require("n8n-workflow"); const aluvia_ts_sdk_1 = __importDefault(require("aluvia-ts-sdk")); const { got } = require('got'); const proxy_agent_1 = require("proxy-agent"); class Aluvia { constructor() { this.description = { displayName: 'Aluvia', name: 'aluvia', icon: 'file:aluvia.svg', group: ['transform'], version: 1, subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}', description: 'Send HTTP requests via Aluvia\'s mobile connectivity network using your Aluvia token', defaults: { name: 'Aluvia', }, inputs: ['main'], outputs: ['main'], credentials: [ { name: 'aluviaToken', required: true, }, ], properties: [ { displayName: 'Resource', name: 'resource', type: 'options', noDataExpression: true, options: [ { name: 'Mobile', value: 'mobile', }, ], default: 'mobile', }, { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, displayOptions: { show: { resource: ['mobile'], }, }, options: [ { name: 'Mobile Request', value: 'mobileRequest', description: 'Send an HTTP request through Aluvia\'s mobile connectivity network', action: 'Send a mobile request', }, ], default: 'mobileRequest', }, { displayName: 'Method', name: 'method', type: 'options', displayOptions: { show: { resource: ['mobile'], operation: ['mobileRequest'], }, }, options: [ { name: 'GET', value: 'GET', }, { name: 'POST', value: 'POST', }, { name: 'PUT', value: 'PUT', }, { name: 'PATCH', value: 'PATCH', }, { name: 'DELETE', value: 'DELETE', }, ], default: 'GET', description: 'HTTP method to use for the request', }, { displayName: 'URL', name: 'url', type: 'string', required: true, displayOptions: { show: { resource: ['mobile'], operation: ['mobileRequest'], }, }, default: '', placeholder: 'https://api.example.com/endpoint', description: 'The URL to send the request to', }, { displayName: 'Query Parameters', name: 'queryParameters', placeholder: 'Add Parameter', type: 'fixedCollection', typeOptions: { multipleValues: true, }, displayOptions: { show: { resource: ['mobile'], operation: ['mobileRequest'], }, }, default: {}, options: [ { name: 'parameter', displayName: 'Parameter', values: [ { displayName: 'Name', name: 'name', type: 'string', default: '', description: 'Name of the query parameter', }, { displayName: 'Value', name: 'value', type: 'string', default: '', description: 'Value of the query parameter', }, ], }, ], }, { displayName: 'Headers', name: 'headers', placeholder: 'Add Header', type: 'fixedCollection', typeOptions: { multipleValues: true, }, displayOptions: { show: { resource: ['mobile'], operation: ['mobileRequest'], }, }, default: {}, options: [ { name: 'parameter', displayName: 'Header', values: [ { displayName: 'Name', name: 'name', type: 'string', default: '', description: 'Name of the header', }, { displayName: 'Value', name: 'value', type: 'string', default: '', description: 'Value of the header', }, ], }, ], }, { displayName: 'Body Content Type', name: 'bodyContentType', type: 'options', displayOptions: { show: { resource: ['mobile'], operation: ['mobileRequest'], method: ['POST', 'PUT', 'PATCH'], }, }, options: [ { name: 'None', value: 'none', }, { name: 'JSON', value: 'json', }, { name: 'Form URL Encoded', value: 'form-urlencoded', }, ], default: 'none', description: 'Content type for the request body', }, { displayName: 'JSON Body', name: 'jsonBody', type: 'json', displayOptions: { show: { resource: ['mobile'], operation: ['mobileRequest'], bodyContentType: ['json'], }, }, default: '{}', description: 'JSON body to send with the request', }, { displayName: 'Form Body', name: 'formBody', placeholder: 'Add Field', type: 'fixedCollection', typeOptions: { multipleValues: true, }, displayOptions: { show: { resource: ['mobile'], operation: ['mobileRequest'], bodyContentType: ['form-urlencoded'], }, }, default: {}, options: [ { name: 'parameter', displayName: 'Field', values: [ { displayName: 'Name', name: 'name', type: 'string', default: '', description: 'Name of the form field', }, { displayName: 'Value', name: 'value', type: 'string', default: '', description: 'Value of the form field', }, ], }, ], }, { displayName: 'Options', name: 'options', type: 'collection', placeholder: 'Add Option', displayOptions: { show: { resource: ['mobile'], operation: ['mobileRequest'], }, }, default: {}, options: [ { displayName: 'Timeout', name: 'timeout', type: 'number', default: 10000, description: 'Request timeout in milliseconds', }, ], }, ], }; } async execute() { const items = this.getInputData(); const returnData = []; for (let i = 0; i < items.length; i++) { try { const credentials = await this.getCredentials('aluviaToken'); const apiToken = credentials.token; if (!apiToken) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Aluvia token is required. Please configure the Aluvia Token credential.', { itemIndex: i }); } const resource = this.getNodeParameter('resource', i); const operation = this.getNodeParameter('operation', i); if (resource === 'mobile' && operation === 'mobileRequest') { const method = this.getNodeParameter('method', i); const url = this.getNodeParameter('url', i); const queryParameters = this.getNodeParameter('queryParameters', i); const headers = this.getNodeParameter('headers', i); const bodyContentType = this.getNodeParameter('bodyContentType', i, 'none'); const options = this.getNodeParameter('options', i, {}); const aluvia = new aluvia_ts_sdk_1.default(apiToken); let credential; try { credential = await aluvia.first(); if (!credential) { const [created] = await aluvia.create(1); credential = created; } } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Failed to get Aluvia connectivity credentials: ${(error === null || error === void 0 ? void 0 : error.message) || 'Unknown error'}`, { itemIndex: i }); } if (!credential) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'No Aluvia connectivity credentials available. Please check your Aluvia token and account status.', { itemIndex: i }); } const proxyUrl = credential.url(); const requestOptions = { method, url, agent: { http: new proxy_agent_1.ProxyAgent({ getProxyForUrl: () => proxyUrl }), https: new proxy_agent_1.ProxyAgent({ getProxyForUrl: () => proxyUrl }), }, timeout: { request: options.timeout || 10000, }, retry: { limit: 0, }, }; if (queryParameters.parameter && queryParameters.parameter.length > 0) { const searchParams = new URLSearchParams(); for (const param of queryParameters.parameter) { if (param.name) { searchParams.append(param.name, param.value); } } requestOptions.searchParams = searchParams; } if (headers.parameter && headers.parameter.length > 0) { requestOptions.headers = {}; for (const header of headers.parameter) { if (header.name) { requestOptions.headers[header.name] = header.value; } } } if (['POST', 'PUT', 'PATCH'].includes(method) && bodyContentType !== 'none') { if (bodyContentType === 'json') { const jsonBody = this.getNodeParameter('jsonBody', i); try { requestOptions.json = JSON.parse(jsonBody); } catch { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Invalid JSON in body', { itemIndex: i, }); } } else if (bodyContentType === 'form-urlencoded') { const formBody = this.getNodeParameter('formBody', i); if (formBody.parameter && formBody.parameter.length > 0) { const form = new URLSearchParams(); for (const field of formBody.parameter) { if (field.name) { form.append(field.name, field.value); } } requestOptions.body = form.toString(); requestOptions.headers = requestOptions.headers || {}; requestOptions.headers['Content-Type'] = 'application/x-www-form-urlencoded'; } } } try { const response = await got(requestOptions); let responseData; const contentType = response.headers['content-type'] || ''; if (contentType.includes('application/json')) { try { responseData = JSON.parse(response.body); } catch { responseData = { statusCode: response.statusCode, headers: response.headers, body: response.body, }; } } else { responseData = { statusCode: response.statusCode, headers: response.headers, body: response.body, }; } returnData.push({ json: responseData, pairedItem: { item: i, }, }); } catch (error) { if (error.response) { let errorData; const contentType = error.response.headers['content-type'] || ''; if (contentType.includes('application/json')) { try { errorData = JSON.parse(error.response.body); } catch { errorData = { statusCode: error.response.statusCode, headers: error.response.headers, body: error.response.body, }; } } else { errorData = { statusCode: error.response.statusCode, headers: error.response.headers, body: error.response.body, }; } returnData.push({ json: errorData, pairedItem: { item: i, }, }); } else { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Request failed: ${error.message}`, { itemIndex: i, }); } } } } catch (error) { if (this.continueOnFail()) { returnData.push({ json: { error: (error === null || error === void 0 ? void 0 : error.message) || 'Unknown error', }, pairedItem: { item: i, }, }); continue; } throw error; } } return [returnData]; } } exports.Aluvia = Aluvia; //# sourceMappingURL=Aluvia.node.js.map