gmail-getter
Version:
A simple tool that gets emails from the Gmail API
52 lines (51 loc) • 2.01 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchEmailsList = void 0;
const axios_1 = __importDefault(require("axios"));
/**
* Get list of emails
* @param {FetchEmailsListOptions} options - The Options object
* @param {string} options.token OAuth Access token
* @param {string} [options.query] Query that specifies search criteria (https://support.google.com/mail/answer/7190)
* @returns {Promise<Message[]>} List of emails
* @example const emails = await fetchEmailsList('ya01.a123456...', 'from:squier7 subject:Test!')
*/
const fetchEmailsList = async (options) => {
const { token, query } = options;
if (!token) {
throw new Error('Failed to fetch emails lists - access token is missing.');
}
const config = {
method: 'get',
url: `https://gmail.googleapis.com/gmail/v1/users/me/messages`,
timeout: 15000,
headers: { Authorization: `OAuth ${token}` },
params: query ? new URLSearchParams([['q', query]]) : undefined,
};
let response;
try {
response = await axios_1.default.request(config);
}
catch (e) {
throw new Error(`Failed to fetch emails list - API request to Google has failed.
\n${JSON.stringify(e, null, 2)}`);
}
const { data: body } = response;
if (!body) {
throw new Error(`Failed to fetch emails list - unable to parse response body.
\n${JSON.stringify(response, null, 2)}`);
}
const { messages, resultSizeEstimate } = body;
if (!messages && resultSizeEstimate === 0) {
return [];
}
if (messages && messages.length > 0) {
return messages;
}
throw new Error(`Failed to fetch emails list - unable to parse messages from the response body.
\n${JSON.stringify(body, null, 2)}`);
};
exports.fetchEmailsList = fetchEmailsList;