UNPKG

n8n-nodes-base

Version:

Base nodes of n8n

72 lines 2.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.egoiApiRequest = egoiApiRequest; exports.getFields = getFields; exports.egoiApiRequestAllItems = egoiApiRequestAllItems; exports.simplify = simplify; const n8n_workflow_1 = require("n8n-workflow"); const fieldCache = {}; async function egoiApiRequest(method, endpoint, body = {}, qs = {}, _headers) { const credentials = await this.getCredentials('egoiApi'); const options = { headers: { accept: 'application/json', Apikey: `${credentials.apiKey}`, }, method, qs, body, url: `https://api.egoiapp.com${endpoint}`, json: true, }; if (Object.keys(body).length === 0) { delete options.body; } try { return await this.helpers.request(options); } catch (error) { throw new n8n_workflow_1.NodeApiError(this.getNode(), error); } } async function getFields(listId) { if (fieldCache[listId]) { return fieldCache[listId]; } fieldCache[listId] = await egoiApiRequest.call(this, 'GET', `/lists/${listId}/fields`); return fieldCache[listId]; } async function egoiApiRequestAllItems(propertyName, method, endpoint, body = {}, query = {}) { const returnData = []; let responseData; query.offset = 0; query.count = 500; do { responseData = await egoiApiRequest.call(this, method, endpoint, body, query); returnData.push.apply(returnData, responseData[propertyName]); query.offset += query.count; } while (responseData[propertyName] && responseData[propertyName].length !== 0); return returnData; } async function simplify(contacts, listId) { let fields = await getFields.call(this, listId); fields = fields.filter((element) => element.type === 'extra'); const fieldsKeyValue = {}; for (const field of fields) { fieldsKeyValue[field.field_id] = field.name; } const data = []; for (const contact of contacts) { const extras = contact.extra.reduce((accumulator, currentValue) => { const key = fieldsKeyValue[currentValue.field_id]; return { [key]: currentValue.value, ...accumulator }; }, {}); data.push({ ...contact.base, ...extras, tags: contact.tags, }); } return data; } //# sourceMappingURL=GenericFunctions.js.map