@@ -11,10 +11,29 @@ const BASE_URL = "https://api.tempmail.lol";
11
11
* Create a new Inbox.
12
12
* @param cb {function} Callback function.
13
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.
14
17
*/
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 ) => {
17
33
const inbox = new Inbox ( json . address , json . token ) ;
34
+ if ( json . error ) {
35
+ cb ( undefined , new Error ( json . error ) ) ;
36
+ }
18
37
cb ( inbox , null ) ;
19
38
} ) . catch ( ( err ) => {
20
39
cb ( undefined , err ) ;
@@ -25,11 +44,28 @@ function createInbox(cb: (inbox: Inbox | undefined, err: Error | null) => any, r
25
44
* Create a new Inbox asynchronously.
26
45
*
27
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.
28
48
* @returns {Promise<Inbox> } Promise with the Inbox.
49
+ * @throws {Error } if the custom domain does not exist or `rush` and `domain` are present.
29
50
*/
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 ) ;
32
65
const json = await res . json ( ) ;
66
+
67
+ if ( json . error ) throw new Error ( json . error ) ;
68
+
33
69
return new Inbox ( json . address , json . token ) ;
34
70
}
35
71
@@ -82,10 +118,43 @@ async function checkInboxAsync(inbox: Inbox | string): Promise<Email[]> {
82
118
return json . email . map ( ( email : Email ) => new Email ( email . from , email . to , email . subject , email . body , email . html , email . date , email . ip ) ) ;
83
119
}
84
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 ) ) ;
151
+ }
152
+
85
153
export {
86
154
Inbox ,
87
155
Email ,
88
156
89
157
createInbox , createInboxAsync ,
90
158
checkInbox , checkInboxAsync ,
159
+ checkCustomInbox , checkCustomInboxAsync
91
160
} ;
0 commit comments