From ac3f8cc23edaffbd5d1574c833a85cccf297b818 Mon Sep 17 00:00:00 2001 From: Zachary Date: Mon, 8 Jul 2024 17:48:45 -0700 Subject: [PATCH] Add fetchAll method --- lib/imap-flow.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/lib/imap-flow.js b/lib/imap-flow.js index 6799a8d..edc4782 100644 --- a/lib/imap-flow.js +++ b/lib/imap-flow.js @@ -2352,6 +2352,36 @@ class ImapFlow extends EventEmitter { } } + /** + * Fetch messages from currently opened mailbox + * This will fetch all messages before resolving the promise, unlike .fetch() which is async generator + * + * @param {SequenceString | Number[] | SearchObject} range Range of messages to fetch + * @param {FetchQueryObject} query Fetch query + * @param {Object} [options] + * @param {Boolean} [options.uid] If `true` then uses UID numbers instead of sequence numbers for `range` + * @param {BigInt} [options.changedSince] If set then only messages with a higher modseq value are returned. Ignored if server does not support `CONDSTORE` extension. + * @param {Boolean} [options.binary=false] If `true` then requests a binary response if the server supports this + * @returns {Promise} Array of Message data object + * + * @example + * let mailbox = await client.mailboxOpen('INBOX'); + * // fetch UID for all messages in a mailbox + * const messages = await client.fetchAll('1:*', {uid: true}); + * for (let msg of messages){ + * console.log(msg.uid); + * // You can not run any IMAP commands in this loop + * } + */ + async fetchAll(range, query, options) { + const results = []; + const generator = this.fetch(range, query, options); + for await (const message of generator) { + results.push(message); + } + return results; + } + /** * Fetch a single message from currently opened mailbox *