Skip to content

Commit d807eaa

Browse files
committed
Add axios, Use async/await Add JSDoc comments and Remove request
1 parent d5682ea commit d807eaa

File tree

614 files changed

+341
-112864
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

614 files changed

+341
-112864
lines changed

.gitignore

+130-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,130 @@
1-
node_modules
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
lerna-debug.log*
8+
.pnpm-debug.log*
9+
10+
# Diagnostic reports (https://nodejs.org/api/report.html)
11+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
12+
13+
# Runtime data
14+
pids
15+
*.pid
16+
*.seed
17+
*.pid.lock
18+
19+
# Directory for instrumented libs generated by jscoverage/JSCover
20+
lib-cov
21+
22+
# Coverage directory used by tools like istanbul
23+
coverage
24+
*.lcov
25+
26+
# nyc test coverage
27+
.nyc_output
28+
29+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
30+
.grunt
31+
32+
# Bower dependency directory (https://bower.io/)
33+
bower_components
34+
35+
# node-waf configuration
36+
.lock-wscript
37+
38+
# Compiled binary addons (https://nodejs.org/api/addons.html)
39+
build/Release
40+
41+
# Dependency directories
42+
node_modules/
43+
jspm_packages/
44+
45+
# Snowpack dependency directory (https://snowpack.dev/)
46+
web_modules/
47+
48+
# TypeScript cache
49+
*.tsbuildinfo
50+
51+
# Optional npm cache directory
52+
.npm
53+
54+
# Optional eslint cache
55+
.eslintcache
56+
57+
# Optional stylelint cache
58+
.stylelintcache
59+
60+
# Microbundle cache
61+
.rpt2_cache/
62+
.rts2_cache_cjs/
63+
.rts2_cache_es/
64+
.rts2_cache_umd/
65+
66+
# Optional REPL history
67+
.node_repl_history
68+
69+
# Output of 'npm pack'
70+
*.tgz
71+
72+
# Yarn Integrity file
73+
.yarn-integrity
74+
75+
# dotenv environment variable files
76+
.env
77+
.env.development.local
78+
.env.test.local
79+
.env.production.local
80+
.env.local
81+
82+
# parcel-bundler cache (https://parceljs.org/)
83+
.cache
84+
.parcel-cache
85+
86+
# Next.js build output
87+
.next
88+
out
89+
90+
# Nuxt.js build / generate output
91+
.nuxt
92+
dist
93+
94+
# Gatsby files
95+
.cache/
96+
# Comment in the public line in if your project uses Gatsby and not Next.js
97+
# https://nextjs.org/blog/next-9-1#public-directory-support
98+
# public
99+
100+
# vuepress build output
101+
.vuepress/dist
102+
103+
# vuepress v2.x temp and cache directory
104+
.temp
105+
.cache
106+
107+
# Docusaurus cache and generated files
108+
.docusaurus
109+
110+
# Serverless directories
111+
.serverless/
112+
113+
# FuseBox cache
114+
.fusebox/
115+
116+
# DynamoDB Local files
117+
.dynamodb/
118+
119+
# TernJS port file
120+
.tern-port
121+
122+
# Stores VSCode versions used for testing VSCode extensions
123+
.vscode-test
124+
125+
# yarn v2
126+
.yarn/cache
127+
.yarn/unplugged
128+
.yarn/build-state.yml
129+
.yarn/install-state.gz
130+
.pnp.*

README.md

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
1-
node-temp-mail
2-
===================
1+
# node-temp-mail
32

43
Node wrapper for creating and fetching temporary, disposable emails, as well as their new messages.
54

6-
#### Installation
7-
```
5+
## Installation
6+
7+
```shell
88
npm install node-temp-mail
99
```
1010

11-
#### Usage
12-
```
13-
var TempMail = require('node-temp-mail');
11+
## Usage
12+
13+
```javascript
14+
const TempMail = require("node-temp-mail");
1415

1516
// Let's create an address object so it can be accessed by the module.
16-
var address = new TempMail("testAddress");
17+
const address = new TempMail("testAddress");
1718

1819
// We already have the address object, so now let's access it and get a list of the emails in a nice & neat json object.
19-
address.fetchEmails(function(err,body){
20+
address.fetchEmails((err, body) => {
2021
console.log(body);
2122
});
2223

24+
// Or we can use the async/await syntax
25+
const body = await address.fetchEmails();
26+
2327
// If for any reason you need to see the full temporary email address, you can use the following function.
24-
address.getAddress()
28+
address.getAddress();
2529
```

index.js

+50-55
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,65 @@
1-
var request = require('request');
1+
const { default: axios } = require("axios");
2+
const nodify = require("./nodify");
23

3-
function buildJson(callback, label, currentJson, addressJson, currentIndex, finalIndex) {
4-
5-
request({
6-
url: "https://www.1secmail.com/api/v1/?action=readMessage&login="+label+"&domain=1secmail.com&id="+addressJson[currentIndex].id,
7-
json: true
8-
}, function (error, response, body) {
9-
if (error || response.statusCode !== 200) {
10-
return callback(error || {statusCode: response.statusCode});
11-
}
12-
else {
13-
var messageBody = {}
14-
messageBody.from = body.from;
15-
messageBody.timestamp = body.date;
16-
messageBody.subject = body.subject;
17-
messageBody.message = body.textBody;
18-
currentJson.messages.push(messageBody);
19-
if((currentIndex+1) < finalIndex){
20-
buildJson(callback, label, currentJson, addressJson, (currentIndex+1), finalIndex);
21-
}
22-
else {
23-
callback(null, currentJson);
24-
}
25-
}
26-
});
27-
}
4+
/** @typedef {{ from: string, timestaml: string, subject: string, message: string }} */
5+
/** @typedef {{ address: string, messageCount: number, messages: Message[] }} EmailFetchResult */
286

297
class TempMail {
8+
/** @type {string} */
9+
mailingAddressLabel;
10+
11+
/**
12+
* create a new instance of TempMail
13+
* @param {string} mailingAddressLabel mailing address label
14+
*/
3015
constructor(mailingAddressLabel) {
3116
this.mailingAddressLabel = mailingAddressLabel;
3217
}
3318

34-
fetchEmails(callback){
35-
var isComplete = false;
36-
var mailingAddressLabel = this.mailingAddressLabel;
37-
38-
39-
request({
40-
url: "https://www.1secmail.com/api/v1/?action=getMessages&login="+mailingAddressLabel+"&domain=1secmail.com",
41-
json: true
42-
}, function (error, response, body) {
43-
if (error || response.statusCode !== 200) {
44-
return callback(error || {statusCode: response.statusCode});
19+
/**
20+
* fetchs emails and returns it
21+
* @returns {Promise<EmailFetchResult>}
22+
*/
23+
async #fetchEmails() {
24+
const httpResponse = await axios.get("https://www.1secmail.com/api/v1/?action=getMessages&login=" + this.mailingAddressLabel + "&domain=1secmail.com");
25+
const data = httpResponse.data;
26+
const messages = await Promise.all(data.map(async (email) => {
27+
const mailHttpResponse = await axios.get("https://www.1secmail.com/api/v1/?action=readMessage&login=" + label + "&domain=1secmail.com&id=" + email.id);
28+
const mailData = mailHttpResponse.data;
29+
return {
30+
from: mailData.from,
31+
timestamp: mailData.date,
32+
subject: mailData.subject,
33+
message: mailData.textBody
4534
}
46-
else {
47-
response = {};
48-
response.address = mailingAddressLabel + "@1secmail.com";
49-
response.messageCount = body.length;
50-
response.messages = [];
51-
if(body.length > 0){
52-
buildJson(callback, mailingAddressLabel, response, body, 0, body.length);
53-
}
54-
else {
55-
callback(null, response);
56-
}
57-
}
58-
});
59-
35+
}));
36+
const res = {
37+
address: this.mailingAddressLabel + "@1secmail.com",
38+
messageCount: data.length,
39+
messages
40+
};
41+
return res;
42+
}
6043

44+
/**
45+
* fetchs emails and calls the callback if not provided just returns a promise
46+
* @param {import("./nodify").callback<EmailFetchResult, Error>} callback callback
47+
* @returns {Promise<EmailFetchResult>}
48+
*/
49+
fetchEmails(callback) {
50+
return nodify(this.#fetchEmails(), callback);
6151
}
6252

63-
getAddress(){
64-
var response = {};
65-
response.address = this.mailingAddressLabel + "@1secmail.com";
66-
return response;
53+
/**
54+
* returns the mailing address label
55+
* @returns {{ address: string }}
56+
*/
57+
getAddress() {
58+
return {
59+
address: this.mailingAddressLabel + "@1secmail.com"
60+
}
6761
}
6862
}
6963

7064
module.exports = TempMail;
65+

node_modules/.bin/sshpk-conv

-15
This file was deleted.

node_modules/.bin/sshpk-conv.cmd

-17
This file was deleted.

node_modules/.bin/sshpk-conv.ps1

-18
This file was deleted.

node_modules/.bin/sshpk-sign

-15
This file was deleted.

node_modules/.bin/sshpk-sign.cmd

-17
This file was deleted.

node_modules/.bin/sshpk-sign.ps1

-18
This file was deleted.

0 commit comments

Comments
 (0)