UNPKG

@bhavinkumarvegad/playwright-email-utils

Version:

Reusable utilities for handling emails in Playwright tests from Yopmail, Gmail and other providers.

168 lines (167 loc) 7.51 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.fromGmail = fromGmail; exports.findEmail = findEmail; exports.findEmailWithDetails = findEmailWithDetails; const googleapis_1 = require("googleapis"); const emailBodyParser_1 = require("../utils/emailBodyParser"); function getOAuth2Client(config) { return __awaiter(this, void 0, void 0, function* () { if (!config.refreshToken) { throw new Error('GMAIL_REFRESH_TOKEN is required in your configuration.'); } const oAuth2Client = new googleapis_1.google.auth.OAuth2(config.clientId, config.clientSecret, config.redirectUri || 'http://localhost'); oAuth2Client.setCredentials({ refresh_token: config.refreshToken }); return oAuth2Client; }); } function getGmail(config) { return __awaiter(this, void 0, void 0, function* () { const auth = yield getOAuth2Client(config); return googleapis_1.google.gmail({ version: 'v1', auth }); }); } function listMessages(config, query) { return __awaiter(this, void 0, void 0, function* () { const gmail = yield getGmail(config); const res = yield gmail.users.messages.list({ userId: 'me', q: query, }); return (res.data.messages || []).map(msg => ({ id: msg.id || undefined, labelIds: msg.labelIds || undefined })); }); } function getMessage(config, msgId) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f, _g, _h, _j; const gmail = yield getGmail(config); const res = yield gmail.users.messages.get({ userId: 'me', id: msgId, format: 'full', }); const headers = ((_b = (_a = res.data.payload) === null || _a === void 0 ? void 0 : _a.headers) === null || _b === void 0 ? void 0 : _b.map(h => ({ name: h.name || '', value: h.value || '' }))) || []; const subject = (_c = headers.find(h => h.name.toLowerCase() === 'subject')) === null || _c === void 0 ? void 0 : _c.value; const from = (_d = headers.find(h => h.name.toLowerCase() === 'from')) === null || _d === void 0 ? void 0 : _d.value; const to = (_e = headers.find(h => h.name.toLowerCase() === 'to')) === null || _e === void 0 ? void 0 : _e.value; let body = ''; if ((_g = (_f = res.data.payload) === null || _f === void 0 ? void 0 : _f.body) === null || _g === void 0 ? void 0 : _g.data) { body = Buffer.from(res.data.payload.body.data, 'base64').toString('utf8'); } else if ((_h = res.data.payload) === null || _h === void 0 ? void 0 : _h.parts) { const textPart = res.data.payload.parts.find(part => part.mimeType === 'text/plain'); if ((_j = textPart === null || textPart === void 0 ? void 0 : textPart.body) === null || _j === void 0 ? void 0 : _j.data) { body = Buffer.from(textPart.body.data, 'base64').toString('utf8'); } } return { id: res.data.id || undefined, headers, labelIds: res.data.labelIds || undefined, snippet: res.data.snippet || undefined, body, subject, from, to }; }); } function fromGmail(config) { return __awaiter(this, void 0, void 0, function* () { return { findEmail: (_a) => __awaiter(this, [_a], void 0, function* ({ subject, from, to, afterTime, beforeTime }) { let query = 'label:inbox'; if (subject) query += ` subject:\"${subject}\"`; if (from) query += ` from:${from}`; if (to) query += ` to:${to}`; if (afterTime) { const timestamp = Math.floor(new Date(afterTime).getTime() / 1000); query += ` after:${timestamp}`; } if (beforeTime) { const timestamp = Math.floor(new Date(beforeTime).getTime() / 1000); query += ` before:${timestamp}`; } const messages = yield listMessages(config, query); if (!messages.length || !messages[0].id) return null; return getMessage(config, messages[0].id); }), findEmailWithDetails: (config, query, extractionRules) => __awaiter(this, void 0, void 0, function* () { const email = yield findEmail(config, query); if (!email) { return { email: null, extractedDetails: {} }; } const extractedDetails = {}; for (const rule of extractionRules) { extractedDetails[rule.field] = emailBodyParser_1.EmailBodyParser.extractInfo(email, { type: rule.type, startMarker: rule.startMarker, endMarker: rule.endMarker, regex: rule.regex, }); } return { email, extractedDetails }; }) }; }); } function findEmail(config_1, _a) { return __awaiter(this, arguments, void 0, function* (config, { subject, from, to, afterTime, beforeTime }) { let query = 'label:inbox'; if (subject) query += ` subject:\"${subject}\"`; if (from) query += ` from:${from}`; if (to) query += ` to:${to}`; if (afterTime) { const timestamp = Math.floor(new Date(afterTime).getTime() / 1000); query += ` after:${timestamp}`; } if (beforeTime) { const timestamp = Math.floor(new Date(beforeTime).getTime() / 1000); query += ` before:${timestamp}`; } const messages = yield listMessages(config, query); if (!messages.length || !messages[0].id) return null; return getMessage(config, messages[0].id); }); } function findEmailWithDetails(config, query, extractionRules) { return __awaiter(this, void 0, void 0, function* () { const email = yield findEmail(config, query); if (!email) { return { email: null, extractedDetails: {} }; } const extractedDetails = {}; for (const rule of extractionRules) { extractedDetails[rule.field] = emailBodyParser_1.EmailBodyParser.extractInfo(email, { type: rule.type, startMarker: rule.startMarker, endMarker: rule.endMarker, regex: rule.regex, }); } return { email, extractedDetails }; }); }