n8n-nodes-arubacentralnextgen
Version:
n8n community node for Aruba Central NextGen API integration with modern monitoring and management capabilities
72 lines (71 loc) • 2.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.handlePagination = handlePagination;
const apiRequest_1 = require("./apiRequest");
const logger_1 = require("./logger");
/**
* Helper function to handle pagination for Aruba Central Next Gen API
*/
async function handlePagination(endpoint, queryParams, returnAll, limit, itemsPath = 'items') {
// Check debug mode
let debugMode = false;
try {
debugMode = this.getNodeParameter('debugMode', 0, false);
}
catch (error) {
// Silently ignore error
}
if (debugMode)
(0, logger_1.logDebug)('Starting pagination');
// If returning all results, handle pagination
if (returnAll) {
const params = { ...queryParams };
let allItems = [];
let hasMore = true;
let pageCount = 0;
// Continue fetching until no more pages
while (hasMore) {
try {
pageCount++;
if (debugMode)
(0, logger_1.logDebug)(`Fetching page ${pageCount}`);
const response = await apiRequest_1.apiRequest.call(this, 'GET', endpoint, {}, params, {
skipDebug: pageCount > 1,
});
// Extract items based on the response structure
const items = response.devices || response.items || response.results || response[itemsPath] || [];
if (items && items.length > 0) {
allItems = allItems.concat(items);
}
// Check for more pages
if (response.next && response.next !== '') {
params.next = response.next;
}
else {
hasMore = false;
if (debugMode)
(0, logger_1.logDebug)(`Pagination complete, fetched ${allItems.length} total items`);
}
}
catch (error) {
(0, logger_1.logWarning)(`Error on page ${pageCount}`, error);
throw new Error(`Failed to fetch page ${pageCount}: ${error.message}`);
}
}
// Return consistent format with count and items
return {
count: allItems.length,
[itemsPath]: allItems,
};
}
else {
// For single page requests, just limit the results
const params = {
...queryParams,
limit,
};
if (debugMode)
(0, logger_1.logDebug)(`Making single page request with limit ${limit}`);
return await apiRequest_1.apiRequest.call(this, 'GET', endpoint, {}, params);
}
}