Skip to content

Commit 80f85b2

Browse files
4.0.0
1 parent 2acb9cd commit 80f85b2

File tree

4 files changed

+63
-26
lines changed

4 files changed

+63
-26
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88

99
This is an API for the temporary email service [TempMail.lol](https://tempmail.lol).
1010

11+
## If you are using a BananaCrumbs ID/Token and have not migrated, install version 3.1.0!!!
12+
13+
## Upgrading from 3.1.0
14+
15+
The only difference between 3.1.0 and 4.0.0 is the fact that the constructor only accepts an API Key, and not a BananaCrumbs ID.
16+
1117
## Upgrading from 2.0.3
1218

1319
The version 3.0.0+ is vastly different from the old version of the API. Please read the changes before using the new version of the API.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tempmail.lol",
3-
"version": "3.1.0",
3+
"version": "4.0.0",
44
"description": "API to generate temporary email addresses.",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/CreateInboxOptions.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
export type CreateInboxOptions = {
3+
community?: boolean,
4+
prefix?: string,
5+
domain?: string,
6+
};

src/index.ts

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,37 @@ import fetch from "node-fetch";
44

55
import Inbox from "./Inbox";
66
import Email from "./Email";
7+
import { CreateInboxOptions } from "./CreateInboxOptions";
78

8-
const BASE_URL = "https://api.tempmail.lol";
9+
const BASE_URL = "https://api.tempmail.lol/v2";
910

1011
export class TempMail {
1112

1213
constructor(
13-
private bananacrumbs_id?: string,
14-
private bananacrumbs_mfa?: string,
14+
private api_key?: string,
1515
) {}
1616

17-
private async makeRequest(url: string): Promise<any> {
17+
private async makeRequest(url: string, post_data?: any, method?: "POST" | "GET" | "DELETE"): Promise<any> {
1818

1919
let headers = {
2020
"User-Agent": "TempMailJS/3.0.0"
2121
};
2222

2323
//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;
2726
}
2827

2928
const raw = await fetch(BASE_URL + url, {
3029
headers,
30+
method: method ? method : (post_data ? "POST" : "GET"),
31+
body: post_data ? JSON.stringify(post_data) : undefined,
3132
});
3233

3334
//check for errors
3435
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
3738
throw new Error("Invalid BananaCrumbs credentials provided.");
3839
} else if(raw.status === 414) {
3940
throw new Error("The provided webhook URL was too long (max 128 characters).");
@@ -53,17 +54,10 @@ export class TempMail {
5354
* @param domain {string} the specific domain to use.
5455
* @returns {Inbox} the Inbox object with the address and token.
5556
*/
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";
5859

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);
6761

6862
return {
6963
address: r.address,
@@ -80,13 +74,13 @@ export class TempMail {
8074
async checkInbox(authentication: string | Inbox): Promise<Email[] | undefined> {
8175
const token = authentication instanceof Inbox ? authentication.token : authentication;
8276

83-
const r = await this.makeRequest(`/auth/${token}`);
77+
const r = await this.makeRequest(`/inbox?token=${token}`);
8478

85-
if(r.token && r.token === "invalid") {
79+
if(r.expired) {
8680
return undefined;
8781
}
8882

89-
return r.email;
83+
return r.emails;
9084
}
9185

9286
/**
@@ -98,7 +92,7 @@ export class TempMail {
9892
* @param token {string} the pre-SHA512 token to use for authentication.
9993
* @returns {Email[]} the emails, or undefined if there was an issue checking.
10094
*/
101-
async checkCustomDomain(domain: string, token: string): Promise<Email[]> {
95+
async checkCustomDomainLegacy(domain: string, token: string): Promise<Email[]> {
10296

10397
const r = await this.makeRequest(`/custom/${token}/${domain}`);
10498

@@ -113,21 +107,52 @@ export class TempMail {
113107
return emails;
114108
}
115109

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+
116121
/**
117122
* Set the webhook for this account
118123
* @param webhook_url {string} the webhook URL to use.
119124
*/
120125
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+
});
122129
}
123130

124131
/**
125132
* Remove a webhook from the account.
126133
*/
127134
async removeWebhook(): Promise<void> {
128-
return this.makeRequest(`/webhook/remove`);
135+
return this.makeRequest(`/webhook`, undefined, "DELETE");
129136
}
130137

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+
}
131156
}
132157

133158
export {

0 commit comments

Comments
 (0)