n8n-nodes-imap-ai
Version:
Simplified IMAP node for n8n with AI-agent support. Clean and modular email and mailbox management for automation workflows.
101 lines • 3.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ImapFlowErrorCatcher = void 0;
exports.createImapClient = createImapClient;
const imapflow_1 = require("imapflow");
class ImapFlowErrorCatcher {
constructor() {
this.messages = [];
this.isCatching = false;
this.catchWarnings = true;
}
static getInstance() {
if (!ImapFlowErrorCatcher.instance) {
ImapFlowErrorCatcher.instance = new ImapFlowErrorCatcher();
}
return ImapFlowErrorCatcher.instance;
}
getMessageFromImapError(error) {
if (!error) {
return 'Unknown IMAP error';
}
if (error.err) {
return error.err.responseText || error.err.message || JSON.stringify(error);
}
if (error.message) {
return error.message;
}
return JSON.stringify(error);
}
startErrorCatching(catchWarnings = true) {
this.messages = [];
this.catchWarnings = catchWarnings;
this.isCatching = true;
}
stopAndGetErrors() {
this.isCatching = false;
const ret_list = this.messages;
this.messages = [];
return ret_list;
}
onImapError(error) {
if (!this.isCatching) {
return;
}
this.messages.push(this.getMessageFromImapError(error));
}
onImapWarning(warning) {
if (!this.isCatching) {
return;
}
if (!this.catchWarnings) {
return;
}
this.messages.push(this.getMessageFromImapError(warning));
}
}
exports.ImapFlowErrorCatcher = ImapFlowErrorCatcher;
function createImapClient(credentials, logger, enableDebugLogs = false) {
const client = new imapflow_1.ImapFlow({
host: credentials.host,
port: credentials.port,
secure: credentials.tls,
tls: {
rejectUnauthorized: !credentials.allowUnauthorizedCerts,
},
auth: {
user: credentials.user,
pass: credentials.password,
},
logger: {
info: (msg) => {
if (enableDebugLogs) {
if (logger) {
logger.info(`IMAP info: ${JSON.stringify(msg)}`);
}
}
},
debug: (msg) => {
if (enableDebugLogs) {
if (logger) {
logger.debug(`IMAP debug: ${JSON.stringify(msg)}`);
}
}
},
error: (error) => {
ImapFlowErrorCatcher.getInstance().onImapError(error);
if (logger) {
logger.error(`IMAP error: ${JSON.stringify(error)}`);
}
},
warn: (warning) => {
ImapFlowErrorCatcher.getInstance().onImapWarning(warning);
if (logger) {
logger.warn(`IMAP warning: ${JSON.stringify(warning)}`);
}
},
},
});
return client;
}
//# sourceMappingURL=ImapUtils.js.map