UNPKG

disposable-guerrillamail

Version:

A simple wrapper on top of Guerrillamail REST API using TypeScript

96 lines (95 loc) 4.24 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 }); const axios_1 = require("axios"); class Email { constructor() { this.BASE_URL = `http://api.guerrillamail.com/ajax.php`; this.emailAddress = ''; this.token = ''; } /** * Initializes a new random email address and returns the address back. * The random address can be overriden if a custom email address is provided. * createEmailAccount() -> 'dbyziheu@guerrillamailblock.com' * createEmailAccount('Faisal') -> 'Faisal@guerrillamailblock.com' */ createEmailAccount(customEmailAddress = '') { return __awaiter(this, void 0, void 0, function* () { try { const { data } = yield axios_1.default({ method: 'GET', url: `${this.BASE_URL}?f=get_email_address`, }); const { sid_token: emailToken, email_addr: generatedEmailAddress } = data; this.token = emailToken; this.emailAddress = generatedEmailAddress; if (customEmailAddress) { const { data: customEmailAddressData } = yield axios_1.default({ method: 'GET', url: `${this.BASE_URL}?f=set_email_user&email_user=${customEmailAddress}&sid_token=${this.token}`, }); const { email_addr: createdCustomEmailAddress } = customEmailAddressData; this.emailAddress = createdCustomEmailAddress; } return this.emailAddress; } catch (error) { throw new Error(`Error while creating a new email account: ${error}`); } }); } /** * Gets a maximum of 10 emails from the specified offset. * Offset of 0 will fetch a list of the first 10 emails, offset of 10 will fetch a list of the next 10, and so on. */ getLatestEmails(offset = 0) { return __awaiter(this, void 0, void 0, function* () { if (!this.token) { throw new Error('No token for this email. Maybe you need to call createEmailAccount() to create the account first?'); } try { const { data } = yield axios_1.default({ method: 'GET', url: `${this.BASE_URL}?f=get_email_list&offset=${offset}&sid_token=${this.token}`, }); return data.list; } catch (error) { throw new Error(`Error while refreshingInbox: ${error}`); } }); } /** * Gets a detailed information about a single email using the emailId. */ getEmailDetails(emailId = '') { return __awaiter(this, void 0, void 0, function* () { if (!this.token) { throw new Error('No token for this email. Maybe you need to call createEmailAccount() to create the account first?'); } if (!emailId) { throw new Error('No email ID is provided.'); } try { const { data } = yield axios_1.default({ method: 'GET', url: `${this.BASE_URL}?f=fetch_email&email_id=${emailId}&sid_token=${this.token}`, }); return data; } catch (error) { throw new Error(`Error while getEmail: ${error}`); } }); } } exports.Email = Email;