Giter VIP home page Giter VIP logo

onesecmail's Introduction

logo

npm version npm downloads npm install size node version

Package using the 1secmail.com service.

Create and receive email in only 1 second.


Installation

Warning: This package is native ES modules and does not provide a CommonJS export. If your project uses CommonJS, you will have to convert to ESM or use the dynamic import() function.

Using npm:

npm install onesecmail

Using yarn:

yarn add onesecmail

Using pnpm:

pnpm add onesecmail

Usage

Once the package is installed, you can import the library using import:

import { OneSecMail, OneSecMailAPI } from "onesecmail";

Examples

OneSecMail

import { OneSecMail } from "onesecmail";

const mailbox = await OneSecMail("[email protected]");

const messages = await mailbox.getMessages();

for (const message of messages) {
  const fullMessage = await message.fetchFullMessage();
  console.log(fullMessage.serialize());
}

await mailbox.clearMessages();

OneSecMailAPI

import { OneSecMailAPI } from "onesecmail";

const options = {
  retry: 5,
  timeout: 3000,
};

const api = new OneSecMailAPI(options);

const [email] = await api.genRandomMailbox();

const [login, domain] = email.split("@");

const messages = await api.getMessages(login, domain);

for (const message of messages) {
  const fullMessage = await api.readMessage(login, domain, message.id);
  console.log(fullMessage);
}

API

Options

Options can be passed to the constructor and to each method.

// default options
const options: Partial<Options> = {
  retry: 2, // max retry failed requests
  timeout: 10_000, // milliseconds
};

OneSecMail

Create a mailbox

const mailbox = await OneSecMail();
const mailbox = await OneSecMail("[email protected]");
const mailbox = await OneSecMail(options);
const mailbox = await OneSecMail("[email protected]", options);

Returns an instance of OneSecMailbox.

OneSecMailbox.getMessages([options])

const messages = await mailbox.getMessages();
const messages = await mailbox.getMessages(options);

Returns an array of OneSecMailShortMessage instances.

OneSecMailbox.clearMessages([options])

await mailbox.clearMessages();
await mailbox.clearMessages(options);

OneSecMailbox.startPolling([intervalTime])

intervalTime must be at least 1000.

mailbox.startPolling(); // default intervalTime: 5000 milliseconds
mailbox.startPolling(30_000);

OneSecMailbox.stopPolling()

mailbox.stopPolling();

OneSecMailbox events

mailbox.on("newMessage", (message: OneSecMailShortMessage) => {
  console.log(message.serialize());
});
mailbox.on("error", (error: Error) => {
  console.error(error);
});

OneSecMailShortMessage.fetchFullMessage([options])

const fullMessage = await message.fetchFullMessage();
const fullMessage = await message.fetchFullMessage(options);

Returns an instance of OneSecMailMessage if the message still exists, otherwise throw an error.

OneSecMailMessage has an attachments field that contains an array of OneSecMailAttachment instances.

OneSecMailAttachment.download([options])

const file = await attachment.download();
const file = await attachment.download(options);

Returns a Buffer if the file still exists, otherwise throw an error.

OneSecMailAPI

This class is an exact reproduction of the official 1secmail.com API.

Create an instance

const api = new OneSecMailAPI();
const api = new OneSecMailAPI(options);

Instance methods

All methods return a Promise.

genRandomMailbox([count[, options]])

count must be between 1 and 500.

const emailAddresses = await api.genRandomMailbox(); // same to: genRandomMailbox(1)
const emailAddresses = await api.genRandomMailbox(5);
const emailAddresses = await api.genRandomMailbox(options); // same to: genRandomMailbox(1, options)
const emailAddresses = await api.genRandomMailbox(5, options);

Returns an array of generated email addresses.

getDomainList([options])

const domainList = await api.getDomainList();
const domainList = await api.getDomainList(options);

Returns an array of active domains.

getMessages(login, domain[, options])

const messages = await api.getMessages("demo", "1secmail.com");
const messages = await api.getMessages("demo", "1secmail.com", options);

Returns an array of ShortMessage (message with no body and attachments).

readMessage(login, domain, id[, options])

const message = await api.readMessage("demo", "1secmail.com", 639);
const message = await api.readMessage("demo", "1secmail.com", 639, options);

Returns a Message if the message exists, otherwise returns null.

download(login, domain, id, file[, options])

const file = await api.download("demo", "1secmail.com", 639, "iometer.pdf");
const file = await api.download("demo", "1secmail.com", 639, "iometer.pdf", options);

Returns a Buffer if the file exists, otherwise returns null.

onesecmail's People

Contributors

kevinrouchut avatar

Stargazers

 avatar  avatar

Watchers

 avatar

onesecmail's Issues

can not import

Describe the bug

$ ts-node index.ts
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in G:\workspace\gpt-god\node_modules\onesecmail\package.json
at new NodeError (node:internal/errors:399:5)
at exportsNotFound (node:internal/modules/esm/resolve:361:10)
at packageExportsResolve (node:internal/modules/esm/resolve:641:13)
at resolveExports (node:internal/modules/cjs/loader:538:36)
at Function.Module._findPath (node:internal/modules/cjs/loader:607:31)
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1033:27)
at Function.Module._resolveFilename.sharedData.moduleResolveFilenameHook.installedValue [as _resolveFilename] (G:\workspace\gpt-god\gpt4free-ts\node_modules@cspotcode\source-map-support\source-map-support.js:811:30)
at Function.Module._load (node:internal/modules/cjs/loader:893:27)
at Module.require (node:internal/modules/cjs/loader:1113:19)
at require (node:internal/modules/cjs/helpers:103:18) {
code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}

Expected behavior

No response

Actual behavior

No response

Code to reproduce

No response

Package version

v1.1.1

Node.js version

v18.14.0

Operating system

Windows

Checklist

  • I have read the documentation
  • I have tried my code with the latest version of Node.js and this package

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.