@bhavinkumarvegad/playwright-email-utils
Version:
Reusable utilities for handling emails in Playwright tests from Yopmail, Gmail and other providers.
98 lines (97 loc) • 4.96 kB
JavaScript
;
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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GmailAPI = void 0;
const googleapis_1 = require("googleapis");
const dotenv_1 = __importDefault(require("dotenv"));
dotenv_1.default.config();
class GmailAPI {
constructor() { }
static getOAuth2Client(clientId, clientSecret, refreshToken) {
return __awaiter(this, void 0, void 0, function* () {
const oAuth2Client = new googleapis_1.google.auth.OAuth2(clientId, clientSecret, this.GMAIL_REDIRECT_URI);
oAuth2Client.setCredentials({ refresh_token: refreshToken });
return oAuth2Client;
});
}
static getGmail(clientId, clientSecret, refreshToken) {
return __awaiter(this, void 0, void 0, function* () {
const auth = yield this.getOAuth2Client(clientId, clientSecret, refreshToken);
return googleapis_1.google.gmail({ version: 'v1', auth });
});
}
static getAccessToken(clientId, clientSecret, refreshToken) {
return __awaiter(this, void 0, void 0, function* () {
const oAuth2Client = yield this.getOAuth2Client(clientId, clientSecret, refreshToken);
const { token } = yield oAuth2Client.getAccessToken();
return token || '';
});
}
static listMessages(clientId, clientSecret, refreshToken, query) {
return __awaiter(this, void 0, void 0, function* () {
const gmail = yield this.getGmail(clientId, clientSecret, refreshToken);
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
}));
});
}
static getMessage(clientId, clientSecret, refreshToken, msgId) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
const gmail = yield this.getGmail(clientId, clientSecret, refreshToken);
const res = yield gmail.users.messages.get({
userId: 'me',
id: msgId,
format: 'full',
});
// Extract headers
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 || ''
}))) || [];
// Find email metadata
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;
// Decode email body
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
};
});
}
}
exports.GmailAPI = GmailAPI;
GmailAPI.GMAIL_REDIRECT_URI = 'http://localhost';