n8n-nodes-arubacentral
Version:
n8n community node for Aruba Central API integration with comprehensive monitoring, configuration, and management capabilities
85 lines (84 loc) • 3.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatResponse = formatResponse;
const logger_1 = require("./logger");
/**
* Formats the API response into n8n node items
*
* @param response The API response to format
* @returns Formatted array of node items
*/
function formatResponse(response) {
logger_1.logger.debug('formatter:format', 'Formatting response');
// If there's no response, return empty array
if (!response) {
logger_1.logger.debug('formatter:format', 'No response data');
return [{ json: { success: false, message: 'No data returned' } }];
}
// If response is null or not an object, return it directly
if (response === null || typeof response !== 'object') {
logger_1.logger.debug('formatter:format', 'Response is not an object, returning directly');
return [{ json: { data: response } }];
}
// For paginated responses with known properties
if (response.count !== undefined) {
logger_1.logger.debug('formatter:format', `Paginated response with count: ${response.count}`);
// Known data properties in Aruba Central API responses
const possibleArrayProps = [
'aps',
'clients',
'bssids',
'samples',
'trails',
'devices',
'switches',
'gateways',
'data',
'results',
'items',
];
// Try to find the data array
for (const prop of possibleArrayProps) {
if (Array.isArray(response[prop])) {
const items = response[prop].map((item) => ({ json: item }));
logger_1.logger.debug('formatter:format', `Found ${items.length} items in property "${prop}"`);
return items;
}
}
// If no array property is found but we know it's paginated,
// try to find any array in the response
const anyArrayProp = Object.keys(response).find((key) => Array.isArray(response[key]));
if (anyArrayProp) {
const items = response[anyArrayProp].map((item) => ({ json: item }));
logger_1.logger.debug('formatter:format', `Found ${items.length} items in detected array property "${anyArrayProp}"`);
return items;
}
}
// For single entity responses
const entityProps = ['ap', 'client', 'switch', 'gateway', 'device', 'data', 'result', 'item'];
for (const prop of entityProps) {
if (response[prop] !== undefined) {
logger_1.logger.debug('formatter:format', `Found single entity in property "${prop}"`);
return [{ json: response[prop] }];
}
}
// If the response is already an array
if (Array.isArray(response)) {
logger_1.logger.debug('formatter:format', `Response is an array with ${response.length} items`);
return response.map((item) => ({ json: item }));
}
// For responses with a data property containing the result
if (response.data !== undefined) {
if (Array.isArray(response.data)) {
logger_1.logger.debug('formatter:format', `Found array in "data" property with ${response.data.length} items`);
return response.data.map((item) => ({ json: item }));
}
else if (typeof response.data === 'object' && response.data !== null) {
logger_1.logger.debug('formatter:format', 'Found object in "data" property');
return [{ json: response.data }];
}
}
// Default fallback - return the entire response
logger_1.logger.debug('formatter:format', 'Using default fallback, returning entire response as single item');
return [{ json: response }];
}