@@ -7,154 +7,110 @@ import Email from "./Email";
7
7
8
8
const BASE_URL = "https://api.tempmail.lol" ;
9
9
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 {
19
11
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
+ ) { }
31
16
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 ;
36
27
}
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 ( ) ;
54
43
}
55
44
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
+ } ;
62
68
}
63
69
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 ;
82
86
}
83
87
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 ;
87
107
}
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 ;
108
110
}
109
111
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 ) ) ;
151
112
}
152
113
153
114
export {
154
- Inbox ,
155
- Email ,
156
-
157
- createInbox , createInboxAsync ,
158
- checkInbox , checkInboxAsync ,
159
- checkCustomInbox , checkCustomInboxAsync
115
+ Email , Inbox
160
116
} ;
0 commit comments