UNPKG

n8n-nodes-dext

Version:

n8n node for Dext Precision API

179 lines (178 loc) 7.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Dext = void 0; class Dext { constructor() { this.description = { displayName: 'Dext', name: 'dext', icon: 'file:dext.svg', group: ['input'], version: 1, subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}', description: 'Interact with Dext Precision API', defaults: { name: 'Dext', }, inputs: ['main'], outputs: ['main'], credentials: [ { name: 'dextApi', required: true, }, ], requestDefaults: { baseURL: 'https://api.xavier-analytics.com', headers: { Accept: 'application/json', 'Content-Type': 'application/json', }, }, properties: [ { displayName: 'Resource', name: 'resource', type: 'options', noDataExpression: true, options: [ { name: 'Client', value: 'client', }, ], default: 'client', }, { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, displayOptions: { show: { resource: ['client'], }, }, options: [ { name: 'Get All', value: 'getAll', description: 'Get all clients', action: 'Get all clients', }, { name: 'Get', value: 'get', description: 'Get a client by ID', action: 'Get a client', }, { name: 'Get Activity Stats', value: 'getActivityStats', description: 'Get activity statistics for a client', action: 'Get activity statistics for a client', }, ], default: 'getAll', }, { displayName: 'Client ID', name: 'clientId', type: 'string', required: true, displayOptions: { show: { resource: ['client'], operation: ['get', 'getActivityStats'], }, }, default: '', description: 'The ID of the client', }, { displayName: 'Return All', name: 'returnAll', type: 'boolean', displayOptions: { show: { resource: ['client'], operation: ['getAll'], }, }, default: false, description: 'Whether to return all results or only up to a given limit', }, { displayName: 'Limit', name: 'limit', type: 'number', displayOptions: { show: { resource: ['client'], operation: ['getAll'], returnAll: [false], }, }, typeOptions: { minValue: 1, }, default: 50, description: 'Max number of results to return', }, ], }; } async execute() { const items = this.getInputData(); const returnData = []; const length = items.length; const resource = this.getNodeParameter('resource', 0); const operation = this.getNodeParameter('operation', 0); for (let i = 0; i < length; i++) { try { if (resource === 'client') { if (operation === 'getAll') { const returnAll = this.getNodeParameter('returnAll', i); const response = await this.helpers.requestWithAuthentication.call(this, 'dextApi', { method: 'GET', url: '/clients', }); if (returnAll) { returnData.push.apply(returnData, response.map((item) => ({ json: item }))); } else { const limit = this.getNodeParameter('limit', i); returnData.push.apply(returnData, response.slice(0, limit).map((item) => ({ json: item }))); } } if (operation === 'get') { const clientId = this.getNodeParameter('clientId', i); const response = await this.helpers.requestWithAuthentication.call(this, 'dextApi', { method: 'GET', url: `/clients/${clientId}`, }); returnData.push({ json: response }); } if (operation === 'getActivityStats') { const clientId = this.getNodeParameter('clientId', i); const response = await this.helpers.requestWithAuthentication.call(this, 'dextApi', { method: 'GET', url: `/clients/${clientId}/activity-stats`, }); returnData.push({ json: response }); } } } catch (error) { if (this.continueOnFail()) { const errorMessage = error instanceof Error ? error.message : 'Unknown error'; returnData.push({ json: { error: errorMessage } }); continue; } throw error; } } return this.prepareOutputData(returnData); } } exports.Dext = Dext;