@@ -4,36 +4,37 @@ import fetch from "node-fetch";
4
4
5
5
import Inbox from "./Inbox" ;
6
6
import Email from "./Email" ;
7
+ import { CreateInboxOptions } from "./CreateInboxOptions" ;
7
8
8
- const BASE_URL = "https://api.tempmail.lol" ;
9
+ const BASE_URL = "https://api.tempmail.lol/v2 " ;
9
10
10
11
export class TempMail {
11
12
12
13
constructor (
13
- private bananacrumbs_id ?: string ,
14
- private bananacrumbs_mfa ?: string ,
14
+ private api_key ?: string ,
15
15
) { }
16
16
17
- private async makeRequest ( url : string ) : Promise < any > {
17
+ private async makeRequest ( url : string , post_data ?: any , method ?: "POST" | "GET" | "DELETE" ) : Promise < any > {
18
18
19
19
let headers = {
20
20
"User-Agent" : "TempMailJS/3.0.0"
21
21
} ;
22
22
23
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 ;
24
+ if ( this . api_key ) {
25
+ headers [ "Authorization" ] = "Bearer " + this . api_key ;
27
26
}
28
27
29
28
const raw = await fetch ( BASE_URL + url , {
30
29
headers,
30
+ method : method ? method : ( post_data ? "POST" : "GET" ) ,
31
+ body : post_data ? JSON . stringify ( post_data ) : undefined ,
31
32
} ) ;
32
33
33
34
//check for errors
34
35
if ( raw . status === 402 ) { //no time left
35
- throw new Error ( "BananaCrumbs ID has no more time left." ) ;
36
- } else if ( raw . status === 403 && this . bananacrumbs_id ) { //invalid credentials
36
+ throw new Error ( "Account has no more time left." ) ;
37
+ } else if ( raw . status === 403 && this . api_key ) { //invalid credentials
37
38
throw new Error ( "Invalid BananaCrumbs credentials provided." ) ;
38
39
} else if ( raw . status === 414 ) {
39
40
throw new Error ( "The provided webhook URL was too long (max 128 characters)." ) ;
@@ -53,17 +54,10 @@ export class TempMail {
53
54
* @param domain {string} the specific domain to use.
54
55
* @returns {Inbox } the Inbox object with the address and token.
55
56
*/
56
- async createInbox ( community ?: boolean , domain ?: string ) : Promise < Inbox > {
57
- let url : string ;
57
+ async createInbox ( options : CreateInboxOptions ) : Promise < Inbox > {
58
+ let url = "/inbox/create" ;
58
59
59
- //craft the URL to use
60
- if ( domain ) {
61
- url = "/generate/" + domain ;
62
- } else {
63
- url = "/generate" + ( community ? "/rush" : "" ) ;
64
- }
65
-
66
- const r = await this . makeRequest ( url ) ;
60
+ const r = await this . makeRequest ( url , options ) ;
67
61
68
62
return {
69
63
address : r . address ,
@@ -80,13 +74,13 @@ export class TempMail {
80
74
async checkInbox ( authentication : string | Inbox ) : Promise < Email [ ] | undefined > {
81
75
const token = authentication instanceof Inbox ? authentication . token : authentication ;
82
76
83
- const r = await this . makeRequest ( `/auth/ ${ token } ` ) ;
77
+ const r = await this . makeRequest ( `/inbox?token= ${ token } ` ) ;
84
78
85
- if ( r . token && r . token === "invalid" ) {
79
+ if ( r . expired ) {
86
80
return undefined ;
87
81
}
88
82
89
- return r . email ;
83
+ return r . emails ;
90
84
}
91
85
92
86
/**
@@ -98,7 +92,7 @@ export class TempMail {
98
92
* @param token {string} the pre-SHA512 token to use for authentication.
99
93
* @returns {Email[] } the emails, or undefined if there was an issue checking.
100
94
*/
101
- async checkCustomDomain ( domain : string , token : string ) : Promise < Email [ ] > {
95
+ async checkCustomDomainLegacy ( domain : string , token : string ) : Promise < Email [ ] > {
102
96
103
97
const r = await this . makeRequest ( `/custom/${ token } /${ domain } ` ) ;
104
98
@@ -113,21 +107,52 @@ export class TempMail {
113
107
return emails ;
114
108
}
115
109
110
+ /**
111
+ * Check a new custom domain. Note that v2 custom domains are different in the way
112
+ * the domain records are made. Please visit https://accounts.tempmail.lol and visit custom
113
+ * domains to see how to set the records.
114
+ *
115
+ * @param domain {string} your domain.
116
+ */
117
+ async checkV2CustomDomain ( domain : string ) : Promise < Email [ ] | undefined > {
118
+ return await this . makeRequest ( "/custom?domain=" + domain ) ;
119
+ }
120
+
116
121
/**
117
122
* Set the webhook for this account
118
123
* @param webhook_url {string} the webhook URL to use.
119
124
*/
120
125
async setWebhook ( webhook_url : string ) : Promise < void > {
121
- return this . makeRequest ( `/webhook/add/` + webhook_url ) ;
126
+ return this . makeRequest ( `/webhook/` , {
127
+ url : webhook_url ,
128
+ } ) ;
122
129
}
123
130
124
131
/**
125
132
* Remove a webhook from the account.
126
133
*/
127
134
async removeWebhook ( ) : Promise < void > {
128
- return this . makeRequest ( `/webhook/remove` ) ;
135
+ return this . makeRequest ( `/webhook` , undefined , "DELETE" ) ;
129
136
}
130
137
138
+ /**
139
+ * Sets a private domain webhook.
140
+ *
141
+ * Read before using: https://github.com/tempmail-lol/server/wiki/v2-API-Endpoints#set-custom-domain-webhook
142
+ */
143
+ async setPrivateDomainWebhook ( domain : string , webhook_url : string ) : Promise < void > {
144
+ return this . makeRequest ( "/private_webhook" , {
145
+ domain : domain ,
146
+ url : webhook_url ,
147
+ } ) ;
148
+ }
149
+
150
+ /**
151
+ * Deletes a private webhook.
152
+ */
153
+ async deletePrivateDomainWebhook ( domain : string ) : Promise < void > {
154
+ return this . makeRequest ( "/private_webhook?domain=" + domain ) ;
155
+ }
131
156
}
132
157
133
158
export {
0 commit comments