temp-disposable-email 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/LICENSE +21 -0
- package/README.md +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/services/accountService.d.ts +36 -0
- package/dist/services/authService.d.ts +2 -0
- package/dist/services/messageService.d.ts +53 -0
- package/dist/utils/api.d.ts +6 -0
- package/dist/utils/constant.d.ts +1 -0
- package/dist/utils/delay.d.ts +1 -0
- package/dist/utils/generateRandomName.d.ts +1 -0
- package/dist/utils/getVerificationCode.d.ts +16 -0
- package/jest.config.js +7 -0
- package/package.json +44 -0
- package/src/index.ts +8 -0
- package/src/services/accountService.ts +103 -0
- package/src/services/authService.ts +23 -0
- package/src/services/messageService.ts +155 -0
- package/src/utils/api.ts +40 -0
- package/src/utils/constant.ts +4 -0
- package/src/utils/delay.ts +2 -0
- package/src/utils/generateRandomName.ts +2 -0
- package/src/utils/getVerificationCode.ts +27 -0
- package/tsconfig.json +13 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# 1.1.0 (2024-11-28)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* account and authentication services with dotenv support ([d886b69](https://github.com/pirasanthan-jesugeevegan/temp-disposable-email/commit/d886b69f54169b24654ea60de37985a522058ac8))
|
|
7
|
+
* add GitHub Actions workflows for NPM publishing and release management ([4a4ac8f](https://github.com/pirasanthan-jesugeevegan/temp-disposable-email/commit/4a4ac8f980604ef80b3ae6a542c70da233816390))
|
|
8
|
+
* add Jest configuration and GitHub Actions workflow for CI, along with comprehensive tests for services and utilities ([e110b58](https://github.com/pirasanthan-jesugeevegan/temp-disposable-email/commit/e110b583b60f2d9f5c4c048535ea1e266ff93ae4))
|
|
9
|
+
* add TypeScript dependency to package.json and package-lock.json ([f709340](https://github.com/pirasanthan-jesugeevegan/temp-disposable-email/commit/f709340ec77ad7900877c7c89ad36a4d654df73c))
|
|
10
|
+
* add verification code extraction utility and update ignore files ([bd0f6a9](https://github.com/pirasanthan-jesugeevegan/temp-disposable-email/commit/bd0f6a9a82f8569f40b1d8e3826610359e41076c))
|
|
11
|
+
* enhance message service by exporting additional interfaces and updating options parameter ([a161235](https://github.com/pirasanthan-jesugeevegan/temp-disposable-email/commit/a1612354648e611c43c449131569fc40b3ef508f))
|
|
12
|
+
* implement message retrieval and deletion services with API integration ([d898bcf](https://github.com/pirasanthan-jesugeevegan/temp-disposable-email/commit/d898bcf01de4ba8ed5da4bf8aa23e669dc84e5db))
|
|
13
|
+
* update GitHub Actions release workflow to use GITHUB_TOKEN instead of PA_TOKEN ([7d0cce7](https://github.com/pirasanthan-jesugeevegan/temp-disposable-email/commit/7d0cce7f462be80e8af81697138af5bad32c69e8))
|
|
14
|
+
* update GitHub Actions workflow to support Node.js 18.x ([2f56cad](https://github.com/pirasanthan-jesugeevegan/temp-disposable-email/commit/2f56cad1e705b25e7b7555c8dd289605046b7920))
|
|
15
|
+
* update release workflow to trigger on master branch ([79bc990](https://github.com/pirasanthan-jesugeevegan/temp-disposable-email/commit/79bc9909cc21a8e537886ae7cd77ed5a4f597d0a))
|
|
16
|
+
* update release workflow to use PA_TOKEN for GitHub Actions ([d3e8883](https://github.com/pirasanthan-jesugeevegan/temp-disposable-email/commit/d3e888365ed55813db309ea7fd547d32ad373cf6))
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Pirasanthan Jesugeevegan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# temp-disposable-email
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a new email inbox with a unique address.
|
|
3
|
+
*
|
|
4
|
+
* This function selects an available domain, generates an email
|
|
5
|
+
* address, and attempts to create an account on the Mail.tm service.
|
|
6
|
+
* If account creation is rate-limited, the function retries with a
|
|
7
|
+
* delay. Upon successful creation, it authenticates the account and
|
|
8
|
+
* stores the token and account ID for future API calls.
|
|
9
|
+
*
|
|
10
|
+
* @param {string} [username] - Optional username; a random one is generated if not provided.
|
|
11
|
+
* @returns {Promise<string>} The generated email address.
|
|
12
|
+
*
|
|
13
|
+
* @throws {Error} If no domains are available or account creation fails.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* const email = await createInbox("customUser");
|
|
17
|
+
* console.log(email); // Outputs: "customUser@mail.tm"
|
|
18
|
+
*/
|
|
19
|
+
export declare const createInbox: (username?: string) => Promise<string>;
|
|
20
|
+
/**
|
|
21
|
+
* Deletes the account created during `createInbox`.
|
|
22
|
+
*
|
|
23
|
+
* This function removes the email account from the Mail.tm service
|
|
24
|
+
* and clears the stored authentication token and account ID.
|
|
25
|
+
* Subsequent API calls requiring authentication will fail until a
|
|
26
|
+
* new account is created.
|
|
27
|
+
*
|
|
28
|
+
* @returns {Promise<void>} Resolves when the account is successfully deleted.
|
|
29
|
+
*
|
|
30
|
+
* @throws {Error} If no account is authenticated or deletion fails.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* await deleteAccount();
|
|
34
|
+
* console.log("Account deleted successfully.");
|
|
35
|
+
*/
|
|
36
|
+
export declare const deleteAccount: () => Promise<void>;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export interface MessageContent {
|
|
2
|
+
from: {
|
|
3
|
+
address: string;
|
|
4
|
+
};
|
|
5
|
+
to: {
|
|
6
|
+
address: string;
|
|
7
|
+
}[];
|
|
8
|
+
subject: string;
|
|
9
|
+
intro: string;
|
|
10
|
+
text: string;
|
|
11
|
+
html: string;
|
|
12
|
+
}
|
|
13
|
+
export interface GetEmailOptions {
|
|
14
|
+
maxWaitTime?: number;
|
|
15
|
+
waitInterval?: number;
|
|
16
|
+
logPolling?: boolean;
|
|
17
|
+
deleteAfterRead?: boolean;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Retrieves the latest message from the inbox.
|
|
21
|
+
*
|
|
22
|
+
* If messages are available, this function fetches the first one and
|
|
23
|
+
* returns its details. Polling options can be specified to wait for
|
|
24
|
+
* messages if the inbox is empty. Optionally, the message can be
|
|
25
|
+
* deleted after reading.
|
|
26
|
+
*
|
|
27
|
+
* @param {GetEmailOptions} [options] - Optional settings for polling and deletion.
|
|
28
|
+
* @returns {Promise<MessageContent | null>} The email content (sender, recipient, subject, text, HTML), or `null` if no messages are found.
|
|
29
|
+
*
|
|
30
|
+
* @throws {Error} If no messages are available within the polling timeout or authentication fails.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* const message = await getRecentEmail({ maxWaitTime: 5000, waitInterval: 1000, logPolling: true });
|
|
34
|
+
* console.log(message.subject); // Outputs: "Hello!"
|
|
35
|
+
*/
|
|
36
|
+
export declare const getRecentEmail: (options?: GetEmailOptions) => Promise<MessageContent | null>;
|
|
37
|
+
/**
|
|
38
|
+
* Deletes a message by its unique ID from the Mail.tm inbox.
|
|
39
|
+
*
|
|
40
|
+
* This function requires a valid authentication token. If the
|
|
41
|
+
* token is not set, an error is thrown. The message is deleted
|
|
42
|
+
* using the Mail.tm API, and a success or failure message is logged.
|
|
43
|
+
*
|
|
44
|
+
* @param {string} messageId - The unique ID of the message to delete.
|
|
45
|
+
* @returns {Promise<void>} Resolves after the message is deleted.
|
|
46
|
+
*
|
|
47
|
+
* @throws {Error} If authentication is missing or the deletion fails.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* await deleteMessage("12345");
|
|
51
|
+
* console.log("Message deleted successfully.");
|
|
52
|
+
*/
|
|
53
|
+
export declare const deleteMessage: (messageId: string) => Promise<void>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const BASE_URL: string | undefined;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const delay: (ms: number) => Promise<void>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const generateRandomName: () => string;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extracts a verification code from the provided email content.
|
|
3
|
+
*
|
|
4
|
+
* This function scans the given text for a sequence of 5 or more
|
|
5
|
+
* consecutive digits and returns the first valid verification code.
|
|
6
|
+
* If no valid sequence is found, the function returns `null`.
|
|
7
|
+
*
|
|
8
|
+
* @param {string} text - The content of the email, typically the body.
|
|
9
|
+
* @returns {Promise<string | null>} The first verification code found, or `null` if no valid code exists.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const emailContent = "Your code is 123456.";
|
|
13
|
+
* const verificationCode = await getVerificationCode(emailContent);
|
|
14
|
+
* console.log(verificationCode); // Output: "123456"
|
|
15
|
+
*/
|
|
16
|
+
export declare const getVerificationCode: (text: string) => Promise<string | null>;
|
package/jest.config.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "temp-disposable-email",
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "tsc",
|
|
8
|
+
"test": "jest",
|
|
9
|
+
"lint": "eslint src --fix",
|
|
10
|
+
"prepare": "npm run build"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/pirasanthan-jesugeevegan/temp-disposable-email.git"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"temp-email",
|
|
18
|
+
"disposable-email",
|
|
19
|
+
"email",
|
|
20
|
+
"email-testing",
|
|
21
|
+
"automation",
|
|
22
|
+
"polling",
|
|
23
|
+
"cypress",
|
|
24
|
+
"playwright"
|
|
25
|
+
],
|
|
26
|
+
"author": "Pirasanthan Jesugeevegan",
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"bugs": {
|
|
29
|
+
"url": "https://github.com/pirasanthan-jesugeevegan/temp-disposable-email/issues"
|
|
30
|
+
},
|
|
31
|
+
"homepage": "https://github.com/pirasanthan-jesugeevegan/temp-disposable-email#readme",
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"axios": "^1.7.8",
|
|
34
|
+
"dotenv": "^16.4.5"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@types/jest": "^29.5.14",
|
|
38
|
+
"@types/node": "^22.10.0",
|
|
39
|
+
"axios-mock-adapter": "^2.1.0",
|
|
40
|
+
"jest": "^29.7.0",
|
|
41
|
+
"ts-jest": "^29.2.5",
|
|
42
|
+
"typescript": "^5.7.2"
|
|
43
|
+
}
|
|
44
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
import { authenticate, getToken } from './authService';
|
|
3
|
+
import { generateRandomName } from '../utils/generateRandomName';
|
|
4
|
+
import { delay } from '../utils/delay';
|
|
5
|
+
import { BASE_URL } from '../utils/constant';
|
|
6
|
+
|
|
7
|
+
let accountId: string | null = null;
|
|
8
|
+
/**
|
|
9
|
+
* Creates a new email inbox with a unique address.
|
|
10
|
+
*
|
|
11
|
+
* This function selects an available domain, generates an email
|
|
12
|
+
* address, and attempts to create an account on the Mail.tm service.
|
|
13
|
+
* If account creation is rate-limited, the function retries with a
|
|
14
|
+
* delay. Upon successful creation, it authenticates the account and
|
|
15
|
+
* stores the token and account ID for future API calls.
|
|
16
|
+
*
|
|
17
|
+
* @param {string} [username] - Optional username; a random one is generated if not provided.
|
|
18
|
+
* @returns {Promise<string>} The generated email address.
|
|
19
|
+
*
|
|
20
|
+
* @throws {Error} If no domains are available or account creation fails.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* const email = await createInbox("customUser");
|
|
24
|
+
* console.log(email); // Outputs: "customUser@mail.tm"
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
export const createInbox = async (username?: string): Promise<string> => {
|
|
28
|
+
const domainsResponse = await axios.get(`${BASE_URL}/domains`, {
|
|
29
|
+
headers: { accept: 'application/ld+json' },
|
|
30
|
+
});
|
|
31
|
+
const domains = domainsResponse.data['hydra:member'].map(
|
|
32
|
+
(domain: { domain: string }) => domain.domain
|
|
33
|
+
);
|
|
34
|
+
if (domains.length === 0) throw new Error('No available domains.');
|
|
35
|
+
|
|
36
|
+
const emailAddress = `${username || generateRandomName()}@${domains[0]}`;
|
|
37
|
+
const password = generateRandomName();
|
|
38
|
+
|
|
39
|
+
let attempts = 0;
|
|
40
|
+
const maxRetries = 5;
|
|
41
|
+
|
|
42
|
+
while (attempts < maxRetries) {
|
|
43
|
+
try {
|
|
44
|
+
const accountResponse = await axios.post(
|
|
45
|
+
`${BASE_URL}/accounts`,
|
|
46
|
+
{ address: emailAddress, password },
|
|
47
|
+
{
|
|
48
|
+
headers: {
|
|
49
|
+
accept: 'application/json',
|
|
50
|
+
'Content-Type': 'application/json',
|
|
51
|
+
},
|
|
52
|
+
}
|
|
53
|
+
);
|
|
54
|
+
accountId = accountResponse.data.id;
|
|
55
|
+
await authenticate(emailAddress, password);
|
|
56
|
+
return emailAddress;
|
|
57
|
+
} catch (error: any) {
|
|
58
|
+
if (error.response?.status === 429) {
|
|
59
|
+
attempts++;
|
|
60
|
+
const retryAfter = error.response.headers['retry-after']
|
|
61
|
+
? parseInt(error.response.headers['retry-after'])
|
|
62
|
+
: 5;
|
|
63
|
+
await delay(retryAfter * 1000);
|
|
64
|
+
} else {
|
|
65
|
+
throw error;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
throw new Error('Failed to create account after multiple retries.');
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Deletes the account created during `createInbox`.
|
|
74
|
+
*
|
|
75
|
+
* This function removes the email account from the Mail.tm service
|
|
76
|
+
* and clears the stored authentication token and account ID.
|
|
77
|
+
* Subsequent API calls requiring authentication will fail until a
|
|
78
|
+
* new account is created.
|
|
79
|
+
*
|
|
80
|
+
* @returns {Promise<void>} Resolves when the account is successfully deleted.
|
|
81
|
+
*
|
|
82
|
+
* @throws {Error} If no account is authenticated or deletion fails.
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* await deleteAccount();
|
|
86
|
+
* console.log("Account deleted successfully.");
|
|
87
|
+
*/
|
|
88
|
+
export const deleteAccount = async (): Promise<void> => {
|
|
89
|
+
const token = getToken();
|
|
90
|
+
if (!token || !accountId) {
|
|
91
|
+
throw new Error(
|
|
92
|
+
'Account information missing. Create and authenticate an account first.'
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
await axios.delete(`${BASE_URL}/accounts/${accountId}`, {
|
|
97
|
+
headers: {
|
|
98
|
+
accept: '*/*',
|
|
99
|
+
Authorization: `Bearer ${token}`,
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
accountId = null;
|
|
103
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
import { BASE_URL } from '../utils/constant';
|
|
3
|
+
|
|
4
|
+
let token: string | null = null;
|
|
5
|
+
|
|
6
|
+
export const authenticate = async (
|
|
7
|
+
email: string,
|
|
8
|
+
password: string
|
|
9
|
+
): Promise<void> => {
|
|
10
|
+
const response = await axios.post(
|
|
11
|
+
`${BASE_URL}/token`,
|
|
12
|
+
{ address: email, password },
|
|
13
|
+
{
|
|
14
|
+
headers: {
|
|
15
|
+
accept: 'application/json',
|
|
16
|
+
'Content-Type': 'application/json',
|
|
17
|
+
},
|
|
18
|
+
}
|
|
19
|
+
);
|
|
20
|
+
token = response.data.token;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export const getToken = (): string | null => token;
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
import { getToken } from './authService';
|
|
3
|
+
import { BASE_URL } from '../utils/constant';
|
|
4
|
+
import { fetchMessageContent, fetchMessages } from '../utils/api';
|
|
5
|
+
|
|
6
|
+
export interface MessageContent {
|
|
7
|
+
from: { address: string };
|
|
8
|
+
to: { address: string }[];
|
|
9
|
+
subject: string;
|
|
10
|
+
intro: string;
|
|
11
|
+
text: string;
|
|
12
|
+
html: string;
|
|
13
|
+
}
|
|
14
|
+
export interface GetEmailOptions {
|
|
15
|
+
maxWaitTime?: number;
|
|
16
|
+
waitInterval?: number;
|
|
17
|
+
logPolling?: boolean;
|
|
18
|
+
deleteAfterRead?: boolean;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Retrieves the latest message from the inbox.
|
|
23
|
+
*
|
|
24
|
+
* If messages are available, this function fetches the first one and
|
|
25
|
+
* returns its details. Polling options can be specified to wait for
|
|
26
|
+
* messages if the inbox is empty. Optionally, the message can be
|
|
27
|
+
* deleted after reading.
|
|
28
|
+
*
|
|
29
|
+
* @param {GetEmailOptions} [options] - Optional settings for polling and deletion.
|
|
30
|
+
* @returns {Promise<MessageContent | null>} The email content (sender, recipient, subject, text, HTML), or `null` if no messages are found.
|
|
31
|
+
*
|
|
32
|
+
* @throws {Error} If no messages are available within the polling timeout or authentication fails.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* const message = await getRecentEmail({ maxWaitTime: 5000, waitInterval: 1000, logPolling: true });
|
|
36
|
+
* console.log(message.subject); // Outputs: "Hello!"
|
|
37
|
+
*/
|
|
38
|
+
export const getRecentEmail = async (
|
|
39
|
+
options?: GetEmailOptions
|
|
40
|
+
): Promise<MessageContent | null> => {
|
|
41
|
+
const token = getToken();
|
|
42
|
+
const {
|
|
43
|
+
maxWaitTime = 30000,
|
|
44
|
+
waitInterval = 2000,
|
|
45
|
+
logPolling = false,
|
|
46
|
+
deleteAfterRead = false,
|
|
47
|
+
} = options || {};
|
|
48
|
+
if (!token)
|
|
49
|
+
throw new Error('Authentication required. Call createInbox() first.');
|
|
50
|
+
|
|
51
|
+
if (!options) {
|
|
52
|
+
const messages = await fetchMessages();
|
|
53
|
+
if (messages.length === 0) throw new Error('No messages available');
|
|
54
|
+
const messageId = messages[0].id;
|
|
55
|
+
const { from, to, subject, intro, text, html } = await fetchMessageContent(
|
|
56
|
+
messageId
|
|
57
|
+
);
|
|
58
|
+
if (deleteAfterRead) {
|
|
59
|
+
await deleteMessage(messageId);
|
|
60
|
+
}
|
|
61
|
+
return {
|
|
62
|
+
from: from.address,
|
|
63
|
+
to: to[0].address,
|
|
64
|
+
subject,
|
|
65
|
+
intro,
|
|
66
|
+
text,
|
|
67
|
+
html,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const startTime = Date.now();
|
|
72
|
+
|
|
73
|
+
if (logPolling) {
|
|
74
|
+
console.log(
|
|
75
|
+
`Polling started with a timeout of ${
|
|
76
|
+
maxWaitTime / 1000
|
|
77
|
+
}sec and interval of ${waitInterval / 1000}sec.`
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
while (Date.now() - startTime < maxWaitTime) {
|
|
81
|
+
const messages = await fetchMessages();
|
|
82
|
+
if (messages.length > 0) {
|
|
83
|
+
if (logPolling) {
|
|
84
|
+
console.log(`Found ${messages.length} message(s), fetching details...`);
|
|
85
|
+
}
|
|
86
|
+
const messageId = messages[0].id;
|
|
87
|
+
if (logPolling) {
|
|
88
|
+
console.log(`Message retrieved: ${messageId}`);
|
|
89
|
+
}
|
|
90
|
+
const { from, to, subject, intro, text, html } =
|
|
91
|
+
await fetchMessageContent(messageId);
|
|
92
|
+
|
|
93
|
+
if (deleteAfterRead) {
|
|
94
|
+
await deleteMessage(messageId);
|
|
95
|
+
}
|
|
96
|
+
return {
|
|
97
|
+
from: from.address,
|
|
98
|
+
to: to[0].address,
|
|
99
|
+
subject,
|
|
100
|
+
intro,
|
|
101
|
+
text,
|
|
102
|
+
html,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
await new Promise((resolve) => setTimeout(resolve, waitInterval));
|
|
106
|
+
if (logPolling) {
|
|
107
|
+
console.log(
|
|
108
|
+
`No messages found, waiting for ${waitInterval / 1000} seconds...`
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (logPolling) {
|
|
113
|
+
console.log(
|
|
114
|
+
`Waiting timeout of ${
|
|
115
|
+
maxWaitTime / 1000
|
|
116
|
+
} seconds reached. No messages found.`
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
throw new Error(
|
|
120
|
+
`No messages available within ${maxWaitTime / 1000} seconds timeout`
|
|
121
|
+
);
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Deletes a message by its unique ID from the Mail.tm inbox.
|
|
126
|
+
*
|
|
127
|
+
* This function requires a valid authentication token. If the
|
|
128
|
+
* token is not set, an error is thrown. The message is deleted
|
|
129
|
+
* using the Mail.tm API, and a success or failure message is logged.
|
|
130
|
+
*
|
|
131
|
+
* @param {string} messageId - The unique ID of the message to delete.
|
|
132
|
+
* @returns {Promise<void>} Resolves after the message is deleted.
|
|
133
|
+
*
|
|
134
|
+
* @throws {Error} If authentication is missing or the deletion fails.
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* await deleteMessage("12345");
|
|
138
|
+
* console.log("Message deleted successfully.");
|
|
139
|
+
*/
|
|
140
|
+
export const deleteMessage = async (messageId: string): Promise<void> => {
|
|
141
|
+
const token = getToken();
|
|
142
|
+
if (!token)
|
|
143
|
+
throw new Error('Authentication required. Call createInbox() first.');
|
|
144
|
+
|
|
145
|
+
try {
|
|
146
|
+
await axios.delete(`${BASE_URL}/messages/${messageId}`, {
|
|
147
|
+
headers: {
|
|
148
|
+
accept: '*/*',
|
|
149
|
+
Authorization: `Bearer ${token}`,
|
|
150
|
+
},
|
|
151
|
+
});
|
|
152
|
+
} catch (error) {
|
|
153
|
+
console.error(`Failed to delete message with ID ${messageId}: ${error}`);
|
|
154
|
+
}
|
|
155
|
+
};
|
package/src/utils/api.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
import { BASE_URL } from './constant';
|
|
3
|
+
import { getToken } from '../services/authService';
|
|
4
|
+
|
|
5
|
+
export const getAuthHeaders = () => {
|
|
6
|
+
const token = getToken();
|
|
7
|
+
if (!token) {
|
|
8
|
+
throw new Error('Authentication required. Token not found.');
|
|
9
|
+
}
|
|
10
|
+
return {
|
|
11
|
+
accept: 'application/json',
|
|
12
|
+
Authorization: `Bearer ${token}`,
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export const fetchMessages = async (): Promise<any[]> => {
|
|
17
|
+
try {
|
|
18
|
+
const res = await axios.get(`${BASE_URL}/messages`, {
|
|
19
|
+
headers: getAuthHeaders(),
|
|
20
|
+
});
|
|
21
|
+
console.log(JSON.stringify(res.data));
|
|
22
|
+
|
|
23
|
+
return res.data;
|
|
24
|
+
} catch (error) {
|
|
25
|
+
console.error('Error fetching messages:', error);
|
|
26
|
+
throw new Error('Failed to fetch messages. Please try again later.');
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export const fetchMessageContent = async (messageId: string): Promise<any> => {
|
|
31
|
+
try {
|
|
32
|
+
const res = await axios.get(`${BASE_URL}/messages/${messageId}`, {
|
|
33
|
+
headers: getAuthHeaders(),
|
|
34
|
+
});
|
|
35
|
+
return res.data;
|
|
36
|
+
} catch (error) {
|
|
37
|
+
console.error(`Error fetching message content for ID ${messageId}:`, error);
|
|
38
|
+
throw new Error('Failed to fetch message content. Please try again later.');
|
|
39
|
+
}
|
|
40
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extracts a verification code from the provided email content.
|
|
3
|
+
*
|
|
4
|
+
* This function scans the given text for a sequence of 5 or more
|
|
5
|
+
* consecutive digits and returns the first valid verification code.
|
|
6
|
+
* If no valid sequence is found, the function returns `null`.
|
|
7
|
+
*
|
|
8
|
+
* @param {string} text - The content of the email, typically the body.
|
|
9
|
+
* @returns {Promise<string | null>} The first verification code found, or `null` if no valid code exists.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const emailContent = "Your code is 123456.";
|
|
13
|
+
* const verificationCode = await getVerificationCode(emailContent);
|
|
14
|
+
* console.log(verificationCode); // Output: "123456"
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
export const getVerificationCode = async (
|
|
18
|
+
text: string
|
|
19
|
+
): Promise<string | null> => {
|
|
20
|
+
console.log('Extracting the verification code from the email content...');
|
|
21
|
+
const matches = text.match(/\b\d{5,}\b/);
|
|
22
|
+
if (matches) {
|
|
23
|
+
return matches[0];
|
|
24
|
+
}
|
|
25
|
+
console.warn('No verification code found in the provided email content.');
|
|
26
|
+
return null;
|
|
27
|
+
};
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES6",
|
|
4
|
+
"module": "CommonJS",
|
|
5
|
+
"declaration": true,
|
|
6
|
+
"outDir": "./dist",
|
|
7
|
+
"strict": true,
|
|
8
|
+
"esModuleInterop": true,
|
|
9
|
+
"emitDeclarationOnly": true
|
|
10
|
+
},
|
|
11
|
+
"include": ["src/**/*"],
|
|
12
|
+
"exclude": ["node_modules", "tests"]
|
|
13
|
+
}
|