Skip to content

Commit 5ec68f3

Browse files
update index.ts to include custom domain support
1 parent 5bdd051 commit 5ec68f3

File tree

1 file changed

+73
-4
lines changed

1 file changed

+73
-4
lines changed

src/index.ts

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,29 @@ const BASE_URL = "https://api.tempmail.lol";
1111
* Create a new Inbox.
1212
* @param cb {function} Callback function.
1313
* @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.
1417
*/
15-
function createInbox(cb: (inbox: Inbox | undefined, err: Error | null) => any, rush = false): void {
16-
fetch(`${BASE_URL}/generate${rush ? "/rush" : ""}`).then(res => res.json()).then((json) => {
18+
function createInbox(cb: (inbox: Inbox | undefined, err: Error | null) => any, rush = false, domain: string | undefined = undefined): void {
19+
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+
}
31+
32+
fetch(url).then(res => res.json()).then((json) => {
1733
const inbox = new Inbox(json.address, json.token);
34+
if(json.error) {
35+
cb(undefined, new Error(json.error));
36+
}
1837
cb(inbox, null);
1938
}).catch((err) => {
2039
cb(undefined, err);
@@ -25,11 +44,28 @@ function createInbox(cb: (inbox: Inbox | undefined, err: Error | null) => any, r
2544
* Create a new Inbox asynchronously.
2645
*
2746
* @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.
2848
* @returns {Promise<Inbox>} Promise with the Inbox.
49+
* @throws {Error} if the custom domain does not exist or `rush` and `domain` are present.
2950
*/
30-
async function createInboxAsync(rush: boolean = false): Promise<Inbox> {
31-
const res = await fetch(`${BASE_URL}/generate${rush ? "/rush" : ""}`);
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.");
54+
}
55+
56+
let url = `${BASE_URL}/generate`;
57+
58+
if(rush) {
59+
url += "/rush";
60+
} else if(domain !== undefined) {
61+
url += `/${domain}`;
62+
}
63+
64+
const res = await fetch(url);
3265
const json = await res.json();
66+
67+
if(json.error) throw new Error(json.error);
68+
3369
return new Inbox(json.address, json.token);
3470
}
3571

@@ -82,10 +118,43 @@ async function checkInboxAsync(inbox: Inbox | string): Promise<Email[]> {
82118
return json.email.map((email: Email) => new Email(email.from, email.to, email.subject, email.body, email.html, email.date, email.ip));
83119
}
84120

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));
151+
}
152+
85153
export {
86154
Inbox,
87155
Email,
88156

89157
createInbox, createInboxAsync,
90158
checkInbox, checkInboxAsync,
159+
checkCustomInbox, checkCustomInboxAsync
91160
};

0 commit comments

Comments
 (0)