UNPKG

guerrillamail-node-api

Version:
245 lines (244 loc) 11.5 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const axios_1 = __importDefault(require("axios")); class GuerrilaMailApi { constructor(options = {}) { this.options = options; this.email_address = ""; this.email_user = ""; this.email_timestamp = undefined; this.email_creation = ""; this.email_expiration = ""; this.alias = ""; this.sid_token = ""; this.alias_error = ""; this.site_id = undefined; this.site = ""; this.inbox = []; this.count = undefined; this.users = undefined; this.deleted_ids = []; this.inboxDsc = []; this.url = `http://api.guerrillamail.com/ajax.php?`; this.params = { ip: options.ip || this.randomIp(), agent: options.agent || this.randomAgent(), lang: options.lang || "en" }; } get_email_address() { return __awaiter(this, void 0, void 0, function* () { const params = Object.assign(Object.assign({}, this.params), { f: 'get_email_address' }); const emailData = yield axios_1.default.get(this.url, { params }) .then(result => result.data) .catch(err => { throw new Error(err); }); yield this.updateProperties(emailData); return emailData; }); } set_email_user(email_user = this.email_user) { return __awaiter(this, void 0, void 0, function* () { if (!this.sid_token || this.sid_token === "") { return { status: 403, message: "No Session Token found. Please, provide one" }; } const params = Object.assign(Object.assign({}, this.params), { f: 'set_email_user', sid_token: this.sid_token, email_user: email_user }); const emailData = yield axios_1.default.get(this.url, { params }) .then(result => result.data) .catch(err => { throw new Error(err); }); yield this.updateProperties(emailData); if (email_user == this.email_user) this.email_expiration = yield this.addMore60minutesToExpiration(this.email_expiration); return emailData; }); } check_email(sequency = 1) { return __awaiter(this, void 0, void 0, function* () { if (!this.sid_token || this.sid_token === "") { return { status: 403, message: "No Session Token found. Please, provide one" }; } const params = Object.assign(Object.assign({}, this.params), { f: 'check_email', seq: sequency, sid_token: this.sid_token }); const emailData = yield axios_1.default.get(this.url, { params }) .then(result => result.data) .catch(err => { throw new Error(err); }); if (emailData.error) { return { status: 403, message: emailData.error }; } yield this.updateProperties(emailData); return emailData; }); } get_email_list(sequency = "", limit = 20) { return __awaiter(this, void 0, void 0, function* () { if (!this.sid_token || this.sid_token === "") { return { status: 403, message: "No Session Token found. Please, provide one" }; } const params = Object.assign(Object.assign({}, this.params), { f: 'get_email_list', seq: sequency, offset: limit, sid_token: this.sid_token }); const emailData = yield axios_1.default.get(this.url, { params }) .then(result => result.data) .catch(err => { throw new Error(err); }); if (!emailData.email) { return { status: 403, message: "No Email Found." }; } yield this.updateProperties(emailData); return emailData; }); } fetch_email(email_id) { return __awaiter(this, void 0, void 0, function* () { if (!this.sid_token || this.sid_token === "") { return { status: 403, message: "No Session Token found. Please, provide one" }; } const params = Object.assign(Object.assign({}, this.params), { f: 'fetch_email', sid_token: this.sid_token, email_id: email_id }); const emailData = yield axios_1.default.get(this.url, { params }) .then(result => result.data) .catch(err => { throw new Error(err); }); return emailData; }); } forget_me() { return __awaiter(this, void 0, void 0, function* () { if (!this.sid_token || this.sid_token === "") { return { status: 403, message: "No Session Token found. Please, provide one" }; } if (!this.email_address || this.email_address === "") { return { status: 403, message: "No Email Address found. Please, provide one" }; } const params = Object.assign(Object.assign({}, this.params), { f: 'forget_me', sid_token: this.sid_token, email_addr: this.email_address }); const emailData = yield axios_1.default.delete(this.url, { params }) .then(result => result.data) .catch(err => { throw new Error(err); }); yield this.eraseProperties(); return emailData; }); } del_email(email_ids) { return __awaiter(this, void 0, void 0, function* () { if (!this.sid_token || this.sid_token === "") { return { status: 403, message: "No Session Token found. Please, provide one" }; } const params = Object.assign(Object.assign({}, this.params), { f: 'del_email', sid_token: this.sid_token, email_ids: email_ids }); const emailData = yield axios_1.default.delete(this.url, { params }) .then(result => result.data) .catch(err => { throw new Error(err); }); yield this.get_email_list(); yield this.updateProperties(emailData); return emailData; }); } get_older_list(sequency = "", limit = 20) { return __awaiter(this, void 0, void 0, function* () { if (!this.sid_token || this.sid_token === "") { return { status: 403, message: "No Session Token found. Please, provide one" }; } const params = Object.assign(Object.assign({}, this.params), { f: 'get_older_list', sid_token: this.sid_token, seq: sequency, limit: limit }); const emailData = yield axios_1.default.get(this.url, { params }) .then(result => result.data) .catch(err => { throw new Error(err); }); if (!emailData.email) { return { status: 403, message: "No Email Found." }; } yield this.updateProperties(Object.assign(Object.assign({}, emailData), { inboxDsc: emailData.list })); return emailData; }); } remaining_time() { return __awaiter(this, void 0, void 0, function* () { if (!this.email_creation) { return { status: 403, message: "No Session Started. Please, start a session." }; } if (!this.email_expiration) { return { status: 403, message: "No Session Started. Please, start a session." }; } const creationDate = new Date(this.email_creation); const expirationDate = new Date(this.email_expiration); const timeDifferenceInMilliseconds = expirationDate.getTime() - creationDate.getTime(); return timeDifferenceInMilliseconds / (1000 * 60); }); } updateProperties(emailData) { return __awaiter(this, void 0, void 0, function* () { this.email_address = emailData.email_addr ? emailData.email_addr : this.email_address; this.email_user = emailData.email_addr ? emailData.email_addr.split('@')[0] : this.email_user; this.email_timestamp = emailData.email_timestamp ? emailData.email_timestamp : this.email_timestamp; this.email_creation = emailData.email_timestamp ? this.convertTimestampToLocaleDate(emailData.email_timestamp) : this.email_creation; this.email_expiration = emailData.email_timestamp ? this.getEmailExpiration(emailData.email_timestamp) : this.email_expiration; this.alias = emailData.alias ? emailData.alias : this.alias; this.sid_token = emailData.sid_token ? emailData.sid_token : this.sid_token; this.site_id = emailData.site_id ? emailData.site_id : this.site_id; this.site = emailData.site ? emailData.site : this.site; this.inbox = !emailData.inboxDsc && emailData.list ? emailData.list : this.inbox; this.count = emailData.count ? emailData.count : this.count; this.users = emailData.users ? emailData.users : this.users; this.deleted_ids = emailData.deleted_ids ? emailData.deleted_ids : this.deleted_ids; this.inboxDsc = emailData.inboxDsc ? emailData.inboxDsc : this.inboxDsc; }); } eraseProperties() { return __awaiter(this, void 0, void 0, function* () { this.email_address = ""; this.email_user = ""; this.email_timestamp = undefined; this.email_creation = ""; this.email_expiration = ""; this.email_creation = ""; this.alias = ""; this.sid_token = ""; this.alias_error = ""; this.site_id = undefined; this.site = ""; this.inbox = []; this.count = undefined; this.users = undefined; this.deleted_ids = []; this.inboxDsc = []; }); } randomAgent() { return "Mozilla_foo_bar"; } randomIp() { return "127.0.0.1"; } convertTimestampToLocaleDate(timestamp) { return new Date(timestamp * 1000).toISOString(); } getEmailExpiration(timestamp) { return new Date((timestamp + 18 * 60) * 1000).toISOString(); } addMore60minutesToExpiration(date) { return __awaiter(this, void 0, void 0, function* () { const dateObject = new Date(date); dateObject.setMinutes(dateObject.getMinutes() + 60); return dateObject.toISOString(); }); } } exports.default = GuerrilaMailApi;