@@ -11,10 +11,12 @@ const BASE_URL = "https://api.tempmail.lol";
11
11
* Create a new Inbox.
12
12
* @param cb {function} Callback function.
13
13
*/
14
- function createInbox ( cb : ( inbox : Inbox ) => any ) : void {
15
- fetch ( `${ BASE_URL } /generate` ) . then ( res => res . json ( ) ) . then ( json => {
14
+ function createInbox ( cb : ( inbox : Inbox | undefined , err : Error | null ) => any ) : void {
15
+ fetch ( `${ BASE_URL } /generate` ) . then ( res => res . json ( ) ) . then ( ( json ) => {
16
16
const inbox = new Inbox ( json . address , json . token ) ;
17
- cb ( inbox ) ;
17
+ cb ( inbox , null ) ;
18
+ } ) . catch ( ( err ) => {
19
+ cb ( undefined , err ) ;
18
20
} ) ;
19
21
}
20
22
@@ -32,9 +34,8 @@ async function createInboxAsync(): Promise<Inbox> {
32
34
* Check for new emails on an Inbox.
33
35
* @param inbox {Inbox | string} Inbox or token string to check.
34
36
* @param cb {function} Callback function.
35
- * @throws {Error } If the token is not valid.
36
37
*/
37
- function checkInbox ( inbox : Inbox | string , cb : ( emails : Email [ ] ) => any ) : void {
38
+ function checkInbox ( inbox : Inbox | string , cb : ( emails : Email [ ] , err : Error | null ) => any ) : void {
38
39
39
40
//convert the token to an Inbox
40
41
if ( typeof inbox === "string" ) {
@@ -43,13 +44,13 @@ function checkInbox(inbox: Inbox | string, cb: (emails: Email[]) => any): void {
43
44
44
45
fetch ( `${ BASE_URL } /auth/${ inbox . token } ` ) . then ( res => res . json ( ) ) . then ( json => {
45
46
if ( json . token === "invalid" ) {
46
- throw new Error ( "Invalid token" ) ;
47
+ cb ( [ ] , new Error ( "Invalid token" ) ) ;
47
48
}
48
49
if ( json . email === null ) {
49
- return cb ( [ ] ) ;
50
+ return cb ( [ ] , null ) ;
50
51
}
51
52
const emails = json . email . map ( ( email : Email ) => new Email ( email . from , email . to , email . subject , email . body , email . html , email . date ) ) ;
52
- cb ( emails ) ;
53
+ cb ( emails , null ) ;
53
54
} ) ;
54
55
}
55
56
0 commit comments