n8n-nodes-netbox
Version:
n8n community node for NetBox API integration with comprehensive DCIM, IPAM, Virtualization, Circuits, Wireless, and data center management operations
50 lines (49 loc) • 1.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.cleanFilters = cleanFilters;
/**
* Cleans filter object by removing empty, null, or undefined values.
* This is necessary because n8n collection fields use empty strings as defaults
* but we don't want to send empty query parameters to the API.
*
* @param filters - The raw filters object from n8n parameters
* @returns A cleaned filters object with only non-empty values
*/
function cleanFilters(filters) {
const cleanedFilters = {};
for (const [key, value] of Object.entries(filters)) {
// Skip undefined, null, or empty string values
if (value === undefined || value === null || value === '') {
continue;
}
// For boolean values, only include if explicitly true or false (not undefined)
// Note: false is a valid filter value and should be included
if (typeof value === 'boolean') {
cleanedFilters[key] = value;
continue;
}
// For numbers, include if it's a valid number (0 could be valid in some cases,
// but NetBox IDs start at 1, so we skip 0 for ID fields)
if (typeof value === 'number') {
if (!isNaN(value) && value !== 0) {
cleanedFilters[key] = value;
}
continue;
}
// For strings, include if non-empty (already checked above)
if (typeof value === 'string') {
cleanedFilters[key] = value;
continue;
}
// For arrays, include if non-empty
if (Array.isArray(value) && value.length > 0) {
cleanedFilters[key] = value;
continue;
}
// For objects, include as-is (custom_fields, etc.)
if (typeof value === 'object') {
cleanedFilters[key] = value;
}
}
return cleanedFilters;
}