Skip to content

Commit e453bdc

Browse files
3.0.0
1 parent 1f2992c commit e453bdc

File tree

4 files changed

+139
-197
lines changed

4 files changed

+139
-197
lines changed

README.md

Lines changed: 43 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -19,85 +19,70 @@ This has built-in types.
1919

2020
## Usage
2121

22+
First, create a TempMail object:
23+
```js
24+
const tempmail = new TempMail();
25+
26+
//if you have a TempMail Plus account, you can add it here:
27+
const tempmail = new TempMail("24-number id", "32-character token");
28+
```
29+
2230
### Create inbox
2331

24-
To create an inbox, you can use one of the following two functions:
32+
To create an inbox:
2533
```js
26-
//with callback
27-
createInbox((inbox, err) => {
28-
if(err) {
29-
return console.error(err);
30-
}
31-
32-
console.log(`Created new inbox: ${inbox.address}, token: ${inbox.token}`);
33-
}, false); //set to true to use Rush Mode domains.
34-
35-
//read more about Rush Mode: https://tempmail.lol/news/2022/08/03/introducing-rush-mode-for-tempmail/
3634

37-
//async
38-
createInboxAsync(false).then((inbox) => { //set to true to use Rush Mode domains.
39-
console.log(`Created new inbox: ${inbox.address}, token: ${inbox.token}`);
35+
//simply, you can use the following function
36+
tempmail.createInbox().then(inbox => {
37+
console.log(`Inbox: ${inbox.address} with a token of ${inbox.token}`);
4038
});
4139

42-
//await
43-
const inbox = await createInboxAsync();
44-
```
40+
//there are some advanced options as well
4541

46-
You can also specify a parameter after the rush mode parameter to specify the domain of the inbox. For example:
47-
```js
48-
createInbox((inbox, err) => {
49-
if(err) {
50-
return console.error(err);
51-
}
52-
53-
console.log(`Created new inbox: ${inbox.address}, token: ${inbox.token}`);
54-
}, false, "example.com");
42+
//whether or not to create a community address
43+
const community = false;
44+
tempmail.createInbox(community);
45+
46+
47+
//or to use a custom domain
48+
const domain = "example.com";
49+
tempmail.createInbox(false, domain);
5550
```
5651

57-
Will create an inbox with the address `example.com`.
52+
Note that all inboxes expire after 10 minutes since last check, with a hard expiry limit of one hour that cannot be bypassed.
53+
54+
TempMail Plus subscribers can extend this to TEN hours, but the 10-minute check rule still applies.
5855

5956
### Retrieve emails
6057

61-
To get the emails (you can also pass in the Inbox object instead of a token string):
58+
To get the emails in an inbox:
6259
```js
63-
//with callback
64-
checkInbox("token", (emails) => {
65-
emails.forEach((e) => {
66-
console.log(JSON.stringify(e, null, 4));
67-
});
68-
});
6960

70-
//async
71-
checkInboxAsync("token").then((emails) => {
72-
emails.forEach((e) => {
73-
console.log(JSON.stringify(e, null, 4));
74-
});
61+
//you can also pass through the Inbox object instead of the token string
62+
const emails = tempmail.checkInbox("<TOKEN>").then((emails) => {
63+
if(!emails) {
64+
console.log(`Inbox expired since "emails" is undefined...`);
65+
return;
66+
}
67+
68+
console.log(`We got some emails!`);
69+
70+
for(let i = 0; i < emails.length; i++) {
71+
console.log(`email ${i}: ${JSON.stringify(emails[i])}`);
72+
}
7573
});
76-
77-
//await
78-
const emails = await checkInboxAsync("token");
7974
```
8075

8176
### Custom Domains
8277

83-
Starting September 1st, you can use custom domains with this API. To use a custom domain, you can use the following function:
84-
```js
85-
//with callback
86-
checkCustomInbox("example.com", "abcdefg...", (emails) => {
87-
emails.forEach((e) => {
88-
console.log(JSON.stringify(e, null, 4));
89-
});
90-
});
78+
#### Note: you will need to be a TempMail Plus subscriber to use custom domains!
9179

92-
//async
93-
checkCustomInboxAsync("example.com", "abcdefg...").then((emails) => {
94-
emails.forEach((e) => {
95-
console.log(JSON.stringify(e, null, 4));
96-
});
80+
```js
81+
tempmail.checkCustomDomain("example.com", "token").then(emails => {
82+
//woohoo, emails!
9783
});
98-
99-
//await
100-
const emails = await checkCustomAsync("example.com", "abcdefg...");
10184
```
10285

86+
You can obtain a token by visiting https://tempmail.lol/custom.html
87+
10388
Custom domains will NOT alert you if the token is invalid.

SECURITY.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
The current supported versions of TempMail are below:
66

77
| Version | Supported |
8-
| ------- | ------------------ |
9-
| 2.x.x | :white_check_mark: |
8+
|---------|--------------------|
9+
| 3.x.x | :white_check_mark: |
10+
| 2.x.x | :x: |
1011
| 1.x.x | :x: |
1112

1213
If you are not on the current major version, it is recommended that you upgrade, as support for that version of the library may be dropped at any time by the server.
1314

1415
## Reporting a Vulnerability
1516

16-
You can report a vulnerability on GitHub Issues or by emailing [security@exploding.email](mailto:security@exploding.email).
17+
You can report a vulnerability on GitHub Issues or by emailing [security@bananacrumbs.us](mailto:security@bananacrumbs.us).

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tempmail.lol",
3-
"version": "2.0.3",
3+
"version": "3.0.0",
44
"description": "API to generate temporary email addresses.",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/index.ts

Lines changed: 91 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -7,154 +7,110 @@ import Email from "./Email";
77

88
const BASE_URL = "https://api.tempmail.lol";
99

10-
/**
11-
* Create a new Inbox.
12-
* @param cb {function} Callback function.
13-
* @param rush {boolean} (optional) Enable Rush Mode (see https://tempmail.lol/news/2022/08/03/introducing-rush-mode-for-tempmail/).
14-
* @param domain {string} (optional) use a specific domain from the website.
15-
*
16-
* @throws {Error} if the custom domain does not exist or `rush` and `domain` are present.
17-
*/
18-
function createInbox(cb: (inbox: Inbox | undefined, err: Error | null) => any, rush = false, domain: string | undefined = undefined): void {
10+
export class TempMail {
1911

20-
if(rush && domain !== undefined) {
21-
throw new Error("You cannot specify both a custom domain and rush mode.");
22-
}
23-
24-
let url = `${BASE_URL}/generate`;
25-
26-
if(rush) {
27-
url += "/rush";
28-
} else if(domain !== undefined) {
29-
url += `/${domain}`;
30-
}
12+
constructor(
13+
private bananacrumbs_id?: string,
14+
private bananacrumbs_mfa?: string,
15+
) {}
3116

32-
fetch(url).then(res => res.json()).then((json) => {
33-
const inbox = new Inbox(json.address, json.token);
34-
if(json.error) {
35-
cb(undefined, new Error(json.error));
17+
private async makeRequest(url: string): Promise<any> {
18+
19+
let headers = {
20+
"User-Agent": "TempMailJS/3.0.0"
21+
};
22+
23+
//if the user is a TempMail Plus subscriber, add the credentials here
24+
if(this.bananacrumbs_id) {
25+
headers["X-BananaCrumbs-ID"] = this.bananacrumbs_id;
26+
headers["X-BananaCrumbs-MFA"] = this.bananacrumbs_mfa;
3627
}
37-
cb(inbox, null);
38-
}).catch((err) => {
39-
cb(undefined, err);
40-
});
41-
}
42-
43-
/**
44-
* Create a new Inbox asynchronously.
45-
*
46-
* @param rush {boolean} (optional) Enable Rush Mode (see https://tempmail.lol/news/2022/08/03/introducing-rush-mode-for-tempmail/).
47-
* @param domain {string} (optional) use a specific domain from the website.
48-
* @returns {Promise<Inbox>} Promise with the Inbox.
49-
* @throws {Error} if the custom domain does not exist or `rush` and `domain` are present.
50-
*/
51-
async function createInboxAsync(rush: boolean = false, domain: string | undefined = undefined): Promise<Inbox> {
52-
if(rush && domain !== undefined) {
53-
throw new Error("You cannot specify both a custom domain and rush mode.");
28+
29+
const raw = await fetch(BASE_URL + url, {
30+
headers,
31+
});
32+
33+
//check for errors
34+
if(raw.status === 402) { //no time left
35+
throw new Error("BananaCrumbs ID has no more time left.");
36+
} else if(raw.status === 400 && this.bananacrumbs_id) { //invalid credentials
37+
throw new Error("Invalid BananaCrumbs credentials provided.");
38+
} else if(!raw.ok) { //other error
39+
throw new Error(`An error occurred while attempting to fetch data from the API: ${await raw.text()}`);
40+
}
41+
42+
return await raw.json();
5443
}
5544

56-
let url = `${BASE_URL}/generate`;
57-
58-
if(rush) {
59-
url += "/rush";
60-
} else if(domain !== undefined) {
61-
url += `/${domain}`;
45+
/**
46+
* Create a new inbox.
47+
*
48+
* @param community {boolean} true to use community (formerly "rush mode") domains
49+
* @param domain {string} the specific domain to use.
50+
* @returns {Inbox} the Inbox object with the address and token.
51+
*/
52+
async createInbox(community?: boolean, domain?: string): Promise<Inbox> {
53+
let url: string;
54+
55+
//craft the URL to use
56+
if(domain) {
57+
url = "/generate/" + domain;
58+
} else {
59+
url = "/generate" + (community ? "/rush" : "");
60+
}
61+
62+
const r = await this.makeRequest(url);
63+
64+
return {
65+
address: r.address,
66+
token: r.token,
67+
};
6268
}
6369

64-
const res = await fetch(url);
65-
const json = await res.json();
66-
67-
if(json.error) throw new Error(json.error);
68-
69-
return new Inbox(json.address, json.token);
70-
}
71-
72-
/**
73-
* Check for new emails on an Inbox.
74-
* @param inbox {Inbox | string} Inbox or token string to check.
75-
* @param cb {function} Callback function.
76-
*/
77-
function checkInbox(inbox: Inbox | string, cb: (emails: Email[], err: Error | null) => any): void {
78-
79-
//convert the token to an Inbox
80-
if(typeof inbox === "string") {
81-
inbox = new Inbox("", inbox);
70+
/**
71+
* Check an inbox for emails.
72+
*
73+
* @param authentication
74+
* @returns {Email[] | undefined} the Email array, or undefined if the inbox has expired.
75+
*/
76+
async checkInbox(authentication: string | Inbox): Promise<Email[] | undefined> {
77+
const token = authentication instanceof Inbox ? authentication.token : authentication;
78+
79+
const r = await this.makeRequest(`/auth/${token}`);
80+
81+
if(r.token && r.token === "invalid") {
82+
return undefined;
83+
}
84+
85+
return r.email;
8286
}
8387

84-
fetch(`${BASE_URL}/auth/${inbox.token}`).then(res => res.json()).then(json => {
85-
if(json.token === "invalid") {
86-
cb([], new Error("Invalid token"));
88+
/**
89+
* Check a custom domain.
90+
*
91+
* Note that this requires a TempMail Plus subscription, as well as being logged in through `this` object.
92+
*
93+
* @param domain {string} the domain to check.
94+
* @param token {string} the pre-SHA512 token to use for authentication.
95+
* @returns {Email[]} the emails, or undefined if there was an issue checking.
96+
*/
97+
async checkCustomDomain(domain: string, token: string): Promise<Email[]> {
98+
99+
const r = await this.makeRequest(`/custom/${token}/${domain}`);
100+
101+
let emails: Email[];
102+
103+
if(r.email === null) {
104+
emails = [];
105+
} else {
106+
emails = r.email;
87107
}
88-
if(json.email === null) {
89-
return cb([], null);
90-
}
91-
const emails = json.email.map((email: Email) => new Email(email.from, email.to, email.subject, email.body, email.html, email.date, email.ip));
92-
cb(emails, null);
93-
});
94-
}
95-
96-
/**
97-
* Check for new emails on an Inbox asynchronously.
98-
*
99-
* @param inbox {Inbox | string} Inbox or token string to check.
100-
* @returns {Promise<Email[]>} Promise with the emails.
101-
* @throws {Error} If the token is invalid.
102-
*/
103-
async function checkInboxAsync(inbox: Inbox | string): Promise<Email[]> {
104-
105-
//convert the token to an Inbox
106-
if(typeof inbox === "string") {
107-
inbox = new Inbox("", inbox);
108+
109+
return emails;
108110
}
109111

110-
const res = await fetch(`${BASE_URL}/auth/${inbox.token}`);
111-
const json = await res.json();
112-
if(json.token === "invalid") {
113-
throw new Error("Invalid token");
114-
}
115-
if(json.email === null) {
116-
return [];
117-
}
118-
return json.email.map((email: Email) => new Email(email.from, email.to, email.subject, email.body, email.html, email.date, email.ip));
119-
}
120-
121-
/**
122-
* Check a custom inbox.
123-
*
124-
* NOTE: this method will not return anything indicating if the token is invalid.
125-
*
126-
* @param domain {string} Domain to check.
127-
* @param key {string} The key for the domain generated by the website.
128-
* @param cb {function} Callback function.
129-
*/
130-
function checkCustomInbox(domain: string, key: string, cb: (emails: Email[]) => any): void {
131-
fetch(`${BASE_URL}/custom/${key}/${domain}`).then(res => res.json()).then(json => {
132-
const emails = json.email.map((email: Email) => new Email(email.from, email.to, email.subject, email.body, email.html, email.date, email.ip));
133-
cb(emails);
134-
});
135-
}
136-
137-
/**
138-
* Check a custom inbox asynchronously.
139-
*
140-
* NOTE: this method will not return anything indicating if the token is invalid.
141-
*
142-
* @param domain {string} Domain to check.
143-
* @param key {string} The key for the domain generated by the website.
144-
*
145-
* @returns {Promise<Email[]>} Promise with the emails.
146-
*/
147-
async function checkCustomInboxAsync(domain: string, key: string): Promise<Email[]> {
148-
const res = await fetch(`${BASE_URL}/custom/${key}/${domain}`);
149-
const json = await res.json();
150-
return json.email.map((email: Email) => new Email(email.from, email.to, email.subject, email.body, email.html, email.date, email.ip));
151112
}
152113

153114
export {
154-
Inbox,
155-
Email,
156-
157-
createInbox, createInboxAsync,
158-
checkInbox, checkInboxAsync,
159-
checkCustomInbox, checkCustomInboxAsync
115+
Email, Inbox
160116
};

0 commit comments

Comments
 (0)