n8n-nodes-mautic-advanced
Version:
Enhanced n8n node for Mautic with comprehensive API coverage including tags, campaigns, categories, and advanced contact management
99 lines (98 loc) • 3.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.clearMauticLoadOptionsCacheForTests = exports.getCachedMauticLoadOptions = exports.MAUTIC_LOAD_OPTIONS_CACHE_TTL_MS = void 0;
exports.MAUTIC_LOAD_OPTIONS_CACHE_TTL_MS = 60000;
const loadOptionsCache = new Map();
function getCredentialType(authenticationMethod) {
return authenticationMethod === 'credentials' ? 'mauticAdvancedApi' : 'mauticAdvancedOAuth2Api';
}
function withoutTrailingSlashes(value) {
return value.replace(/\/+$/, '');
}
function normalizeCredentialUrl(value) {
if (typeof value !== 'string') {
return '';
}
const trimmed = value.trim();
if (!trimmed) {
return '';
}
try {
const url = new URL(trimmed);
url.protocol = url.protocol.toLowerCase();
url.hostname = url.hostname.toLowerCase();
url.hash = '';
url.search = '';
url.pathname = withoutTrailingSlashes(url.pathname);
return withoutTrailingSlashes(url.toString());
}
catch {
return withoutTrailingSlashes(trimmed);
}
}
function credentialRefPart(value) {
if (typeof value === 'string' && value) {
return value;
}
if (typeof value !== 'object' || value === null) {
return undefined;
}
const credentialRef = value;
if (credentialRef.id) {
return credentialRef.id;
}
if (credentialRef.name) {
return credentialRef.name;
}
return undefined;
}
function getCredentialReference(context, credentialType) {
const contextWithNode = context;
const credentialRef = contextWithNode.getNode?.().credentials?.[credentialType];
const nodeCredentialPart = credentialRefPart(credentialRef);
if (nodeCredentialPart) {
return nodeCredentialPart;
}
return 'unknown';
}
function getCacheKey(authenticationMethod, credentialType, credentialReference, credentials, datasetKey) {
return JSON.stringify([
authenticationMethod,
credentialType,
credentialReference,
normalizeCredentialUrl(credentials.url),
datasetKey,
]);
}
async function getCachedMauticLoadOptions(context, datasetKey, loadOptions, ttlMs = exports.MAUTIC_LOAD_OPTIONS_CACHE_TTL_MS) {
const authenticationMethod = context.getNodeParameter('authentication', 'credentials');
const credentialType = getCredentialType(authenticationMethod);
const credentials = await context.getCredentials(credentialType);
const credentialReference = getCredentialReference(context, credentialType);
const cacheKey = getCacheKey(authenticationMethod, credentialType, credentialReference, credentials, datasetKey);
const now = Date.now();
const cachedEntry = loadOptionsCache.get(cacheKey);
if (cachedEntry && cachedEntry.expiresAt > now) {
return await cachedEntry.promise;
}
if (cachedEntry) {
loadOptionsCache.delete(cacheKey);
}
const promise = (async () => loadOptions())().catch((error) => {
const latestEntry = loadOptionsCache.get(cacheKey);
if (latestEntry?.promise === promise) {
loadOptionsCache.delete(cacheKey);
}
throw error;
});
loadOptionsCache.set(cacheKey, {
expiresAt: now + ttlMs,
promise,
});
return await promise;
}
exports.getCachedMauticLoadOptions = getCachedMauticLoadOptions;
function clearMauticLoadOptionsCacheForTests() {
loadOptionsCache.clear();
}
exports.clearMauticLoadOptionsCacheForTests = clearMauticLoadOptionsCacheForTests;