temp-disposable-email 1.2.0 → 1.6.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 +31 -14
- package/dist/index.d.ts +3 -0
- package/dist/index.js +11 -0
- package/dist/services/accountService.d.ts +36 -0
- package/dist/services/accountService.js +97 -0
- package/dist/services/authService.d.ts +2 -0
- package/dist/services/authService.js +19 -0
- package/dist/services/messageService.d.ts +53 -0
- package/dist/services/messageService.js +119 -0
- package/dist/utils/api.d.ts +6 -0
- package/dist/utils/api.js +45 -0
- package/dist/utils/constant.d.ts +1 -0
- package/dist/utils/constant.js +7 -0
- package/dist/utils/delay.d.ts +1 -0
- package/dist/utils/delay.js +5 -0
- package/dist/utils/generateRandomName.d.ts +1 -0
- package/dist/utils/generateRandomName.js +5 -0
- package/dist/utils/getVerificationCode.d.ts +16 -0
- package/dist/utils/getVerificationCode.js +28 -0
- package/package.json +5 -3
- package/src/utils/constant.ts +1 -1
- package/tsconfig.json +32 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,28 +1,45 @@
|
|
|
1
|
-
# [1.
|
|
1
|
+
# [1.6.0](https://github.com/pirasanthan-jesugeevegan/temp-disposable-email/compare/v1.5.0...v1.6.0) (2024-11-28)
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
### Features
|
|
5
5
|
|
|
6
|
-
* update
|
|
6
|
+
* update package version to 1.4.0, add tslib dependency, and modify tsconfig settings ([a6fc115](https://github.com/pirasanthan-jesugeevegan/temp-disposable-email/commit/a6fc115fa263909fbe346f0f86ae95c18b1bea08))
|
|
7
7
|
|
|
8
8
|
|
|
9
9
|
|
|
10
|
-
# [1.
|
|
10
|
+
# [1.5.0](https://github.com/pirasanthan-jesugeevegan/temp-disposable-email/compare/v1.4.0...v1.5.0) (2024-11-28)
|
|
11
11
|
|
|
12
12
|
|
|
13
13
|
### Features
|
|
14
14
|
|
|
15
|
-
*
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
* update
|
|
25
|
-
|
|
15
|
+
* change TypeScript module to 'nodenext' in tsconfig.json ([f62b842](https://github.com/pirasanthan-jesugeevegan/temp-disposable-email/commit/f62b842afe237c5c63a0931568ad46552f92d936))
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
# [1.4.0](https://github.com/pirasanthan-jesugeevegan/temp-disposable-email/compare/v1.3.0...v1.4.0) (2024-11-28)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
### Features
|
|
23
|
+
|
|
24
|
+
* update TypeScript target to ES6 in tsconfig.json ([0edc477](https://github.com/pirasanthan-jesugeevegan/temp-disposable-email/commit/0edc477af52df30c15b251239d6befcb757f2ed3))
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
# [1.3.0](https://github.com/pirasanthan-jesugeevegan/temp-disposable-email/compare/v1.2.0...v1.3.0) (2024-11-28)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
### Features
|
|
32
|
+
|
|
33
|
+
* update TypeScript configuration to use ESNext module and add skipLibCheck option ([0ffa53a](https://github.com/pirasanthan-jesugeevegan/temp-disposable-email/commit/0ffa53aa3851d622b3a0bad5b2024b797966be8d))
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
# [1.2.0](https://github.com/pirasanthan-jesugeevegan/temp-disposable-email/compare/v1.1.0...v1.2.0) (2024-11-28)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
### Features
|
|
41
|
+
|
|
42
|
+
* update main entry point to TypeScript source file ([3332c21](https://github.com/pirasanthan-jesugeevegan/temp-disposable-email/commit/3332c2147b825384ed52b068b95172f4fa3e94bd))
|
|
26
43
|
|
|
27
44
|
|
|
28
45
|
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getVerificationCode = exports.deleteMessage = exports.getRecentEmail = exports.deleteAccount = exports.createInbox = void 0;
|
|
4
|
+
var accountService_1 = require("./services/accountService");
|
|
5
|
+
Object.defineProperty(exports, "createInbox", { enumerable: true, get: function () { return accountService_1.createInbox; } });
|
|
6
|
+
Object.defineProperty(exports, "deleteAccount", { enumerable: true, get: function () { return accountService_1.deleteAccount; } });
|
|
7
|
+
var messageService_1 = require("./services/messageService");
|
|
8
|
+
Object.defineProperty(exports, "getRecentEmail", { enumerable: true, get: function () { return messageService_1.getRecentEmail; } });
|
|
9
|
+
Object.defineProperty(exports, "deleteMessage", { enumerable: true, get: function () { return messageService_1.deleteMessage; } });
|
|
10
|
+
var getVerificationCode_1 = require("./utils/getVerificationCode");
|
|
11
|
+
Object.defineProperty(exports, "getVerificationCode", { enumerable: true, get: function () { return getVerificationCode_1.getVerificationCode; } });
|
|
@@ -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,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.deleteAccount = exports.createInbox = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const axios_1 = tslib_1.__importDefault(require("axios"));
|
|
6
|
+
const authService_1 = require("./authService");
|
|
7
|
+
const generateRandomName_1 = require("../utils/generateRandomName");
|
|
8
|
+
const delay_1 = require("../utils/delay");
|
|
9
|
+
const constant_1 = require("../utils/constant");
|
|
10
|
+
let accountId = null;
|
|
11
|
+
/**
|
|
12
|
+
* Creates a new email inbox with a unique address.
|
|
13
|
+
*
|
|
14
|
+
* This function selects an available domain, generates an email
|
|
15
|
+
* address, and attempts to create an account on the Mail.tm service.
|
|
16
|
+
* If account creation is rate-limited, the function retries with a
|
|
17
|
+
* delay. Upon successful creation, it authenticates the account and
|
|
18
|
+
* stores the token and account ID for future API calls.
|
|
19
|
+
*
|
|
20
|
+
* @param {string} [username] - Optional username; a random one is generated if not provided.
|
|
21
|
+
* @returns {Promise<string>} The generated email address.
|
|
22
|
+
*
|
|
23
|
+
* @throws {Error} If no domains are available or account creation fails.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* const email = await createInbox("customUser");
|
|
27
|
+
* console.log(email); // Outputs: "customUser@mail.tm"
|
|
28
|
+
*/
|
|
29
|
+
const createInbox = async (username) => {
|
|
30
|
+
const domainsResponse = await axios_1.default.get(`${constant_1.BASE_URL}/domains`, {
|
|
31
|
+
headers: { accept: 'application/ld+json' },
|
|
32
|
+
});
|
|
33
|
+
const domains = domainsResponse.data['hydra:member'].map((domain) => domain.domain);
|
|
34
|
+
if (domains.length === 0)
|
|
35
|
+
throw new Error('No available domains.');
|
|
36
|
+
const emailAddress = `${username || (0, generateRandomName_1.generateRandomName)()}@${domains[0]}`;
|
|
37
|
+
const password = (0, generateRandomName_1.generateRandomName)();
|
|
38
|
+
let attempts = 0;
|
|
39
|
+
const maxRetries = 5;
|
|
40
|
+
while (attempts < maxRetries) {
|
|
41
|
+
try {
|
|
42
|
+
const accountResponse = await axios_1.default.post(`${constant_1.BASE_URL}/accounts`, { address: emailAddress, password }, {
|
|
43
|
+
headers: {
|
|
44
|
+
accept: 'application/json',
|
|
45
|
+
'Content-Type': 'application/json',
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
accountId = accountResponse.data.id;
|
|
49
|
+
await (0, authService_1.authenticate)(emailAddress, password);
|
|
50
|
+
return emailAddress;
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
if (error.response?.status === 429) {
|
|
54
|
+
attempts++;
|
|
55
|
+
const retryAfter = error.response.headers['retry-after']
|
|
56
|
+
? parseInt(error.response.headers['retry-after'])
|
|
57
|
+
: 5;
|
|
58
|
+
await (0, delay_1.delay)(retryAfter * 1000);
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
throw error;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
throw new Error('Failed to create account after multiple retries.');
|
|
66
|
+
};
|
|
67
|
+
exports.createInbox = createInbox;
|
|
68
|
+
/**
|
|
69
|
+
* Deletes the account created during `createInbox`.
|
|
70
|
+
*
|
|
71
|
+
* This function removes the email account from the Mail.tm service
|
|
72
|
+
* and clears the stored authentication token and account ID.
|
|
73
|
+
* Subsequent API calls requiring authentication will fail until a
|
|
74
|
+
* new account is created.
|
|
75
|
+
*
|
|
76
|
+
* @returns {Promise<void>} Resolves when the account is successfully deleted.
|
|
77
|
+
*
|
|
78
|
+
* @throws {Error} If no account is authenticated or deletion fails.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* await deleteAccount();
|
|
82
|
+
* console.log("Account deleted successfully.");
|
|
83
|
+
*/
|
|
84
|
+
const deleteAccount = async () => {
|
|
85
|
+
const token = (0, authService_1.getToken)();
|
|
86
|
+
if (!token || !accountId) {
|
|
87
|
+
throw new Error('Account information missing. Create and authenticate an account first.');
|
|
88
|
+
}
|
|
89
|
+
await axios_1.default.delete(`${constant_1.BASE_URL}/accounts/${accountId}`, {
|
|
90
|
+
headers: {
|
|
91
|
+
accept: '*/*',
|
|
92
|
+
Authorization: `Bearer ${token}`,
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
accountId = null;
|
|
96
|
+
};
|
|
97
|
+
exports.deleteAccount = deleteAccount;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getToken = exports.authenticate = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const axios_1 = tslib_1.__importDefault(require("axios"));
|
|
6
|
+
const constant_1 = require("../utils/constant");
|
|
7
|
+
let token = null;
|
|
8
|
+
const authenticate = async (email, password) => {
|
|
9
|
+
const response = await axios_1.default.post(`${constant_1.BASE_URL}/token`, { address: email, password }, {
|
|
10
|
+
headers: {
|
|
11
|
+
accept: 'application/json',
|
|
12
|
+
'Content-Type': 'application/json',
|
|
13
|
+
},
|
|
14
|
+
});
|
|
15
|
+
token = response.data.token;
|
|
16
|
+
};
|
|
17
|
+
exports.authenticate = authenticate;
|
|
18
|
+
const getToken = () => token;
|
|
19
|
+
exports.getToken = getToken;
|
|
@@ -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,119 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.deleteMessage = exports.getRecentEmail = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const axios_1 = tslib_1.__importDefault(require("axios"));
|
|
6
|
+
const authService_1 = require("./authService");
|
|
7
|
+
const constant_1 = require("../utils/constant");
|
|
8
|
+
const api_1 = require("../utils/api");
|
|
9
|
+
/**
|
|
10
|
+
* Retrieves the latest message from the inbox.
|
|
11
|
+
*
|
|
12
|
+
* If messages are available, this function fetches the first one and
|
|
13
|
+
* returns its details. Polling options can be specified to wait for
|
|
14
|
+
* messages if the inbox is empty. Optionally, the message can be
|
|
15
|
+
* deleted after reading.
|
|
16
|
+
*
|
|
17
|
+
* @param {GetEmailOptions} [options] - Optional settings for polling and deletion.
|
|
18
|
+
* @returns {Promise<MessageContent | null>} The email content (sender, recipient, subject, text, HTML), or `null` if no messages are found.
|
|
19
|
+
*
|
|
20
|
+
* @throws {Error} If no messages are available within the polling timeout or authentication fails.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* const message = await getRecentEmail({ maxWaitTime: 5000, waitInterval: 1000, logPolling: true });
|
|
24
|
+
* console.log(message.subject); // Outputs: "Hello!"
|
|
25
|
+
*/
|
|
26
|
+
const getRecentEmail = async (options) => {
|
|
27
|
+
const token = (0, authService_1.getToken)();
|
|
28
|
+
const { maxWaitTime = 30000, waitInterval = 2000, logPolling = false, deleteAfterRead = false, } = options || {};
|
|
29
|
+
if (!token)
|
|
30
|
+
throw new Error('Authentication required. Call createInbox() first.');
|
|
31
|
+
if (!options) {
|
|
32
|
+
const messages = await (0, api_1.fetchMessages)();
|
|
33
|
+
if (messages.length === 0)
|
|
34
|
+
throw new Error('No messages available');
|
|
35
|
+
const messageId = messages[0].id;
|
|
36
|
+
const { from, to, subject, intro, text, html } = await (0, api_1.fetchMessageContent)(messageId);
|
|
37
|
+
if (deleteAfterRead) {
|
|
38
|
+
await (0, exports.deleteMessage)(messageId);
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
from: from.address,
|
|
42
|
+
to: to[0].address,
|
|
43
|
+
subject,
|
|
44
|
+
intro,
|
|
45
|
+
text,
|
|
46
|
+
html,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
const startTime = Date.now();
|
|
50
|
+
if (logPolling) {
|
|
51
|
+
console.log(`Polling started with a timeout of ${maxWaitTime / 1000}sec and interval of ${waitInterval / 1000}sec.`);
|
|
52
|
+
}
|
|
53
|
+
while (Date.now() - startTime < maxWaitTime) {
|
|
54
|
+
const messages = await (0, api_1.fetchMessages)();
|
|
55
|
+
if (messages.length > 0) {
|
|
56
|
+
if (logPolling) {
|
|
57
|
+
console.log(`Found ${messages.length} message(s), fetching details...`);
|
|
58
|
+
}
|
|
59
|
+
const messageId = messages[0].id;
|
|
60
|
+
if (logPolling) {
|
|
61
|
+
console.log(`Message retrieved: ${messageId}`);
|
|
62
|
+
}
|
|
63
|
+
const { from, to, subject, intro, text, html } = await (0, api_1.fetchMessageContent)(messageId);
|
|
64
|
+
if (deleteAfterRead) {
|
|
65
|
+
await (0, exports.deleteMessage)(messageId);
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
from: from.address,
|
|
69
|
+
to: to[0].address,
|
|
70
|
+
subject,
|
|
71
|
+
intro,
|
|
72
|
+
text,
|
|
73
|
+
html,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
await new Promise((resolve) => setTimeout(resolve, waitInterval));
|
|
77
|
+
if (logPolling) {
|
|
78
|
+
console.log(`No messages found, waiting for ${waitInterval / 1000} seconds...`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (logPolling) {
|
|
82
|
+
console.log(`Waiting timeout of ${maxWaitTime / 1000} seconds reached. No messages found.`);
|
|
83
|
+
}
|
|
84
|
+
throw new Error(`No messages available within ${maxWaitTime / 1000} seconds timeout`);
|
|
85
|
+
};
|
|
86
|
+
exports.getRecentEmail = getRecentEmail;
|
|
87
|
+
/**
|
|
88
|
+
* Deletes a message by its unique ID from the Mail.tm inbox.
|
|
89
|
+
*
|
|
90
|
+
* This function requires a valid authentication token. If the
|
|
91
|
+
* token is not set, an error is thrown. The message is deleted
|
|
92
|
+
* using the Mail.tm API, and a success or failure message is logged.
|
|
93
|
+
*
|
|
94
|
+
* @param {string} messageId - The unique ID of the message to delete.
|
|
95
|
+
* @returns {Promise<void>} Resolves after the message is deleted.
|
|
96
|
+
*
|
|
97
|
+
* @throws {Error} If authentication is missing or the deletion fails.
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* await deleteMessage("12345");
|
|
101
|
+
* console.log("Message deleted successfully.");
|
|
102
|
+
*/
|
|
103
|
+
const deleteMessage = async (messageId) => {
|
|
104
|
+
const token = (0, authService_1.getToken)();
|
|
105
|
+
if (!token)
|
|
106
|
+
throw new Error('Authentication required. Call createInbox() first.');
|
|
107
|
+
try {
|
|
108
|
+
await axios_1.default.delete(`${constant_1.BASE_URL}/messages/${messageId}`, {
|
|
109
|
+
headers: {
|
|
110
|
+
accept: '*/*',
|
|
111
|
+
Authorization: `Bearer ${token}`,
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
console.error(`Failed to delete message with ID ${messageId}: ${error}`);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
exports.deleteMessage = deleteMessage;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.fetchMessageContent = exports.fetchMessages = exports.getAuthHeaders = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const axios_1 = tslib_1.__importDefault(require("axios"));
|
|
6
|
+
const constant_1 = require("./constant");
|
|
7
|
+
const authService_1 = require("../services/authService");
|
|
8
|
+
const getAuthHeaders = () => {
|
|
9
|
+
const token = (0, authService_1.getToken)();
|
|
10
|
+
if (!token) {
|
|
11
|
+
throw new Error('Authentication required. Token not found.');
|
|
12
|
+
}
|
|
13
|
+
return {
|
|
14
|
+
accept: 'application/json',
|
|
15
|
+
Authorization: `Bearer ${token}`,
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
exports.getAuthHeaders = getAuthHeaders;
|
|
19
|
+
const fetchMessages = async () => {
|
|
20
|
+
try {
|
|
21
|
+
const res = await axios_1.default.get(`${constant_1.BASE_URL}/messages`, {
|
|
22
|
+
headers: (0, exports.getAuthHeaders)(),
|
|
23
|
+
});
|
|
24
|
+
console.log(JSON.stringify(res.data));
|
|
25
|
+
return res.data;
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
console.error('Error fetching messages:', error);
|
|
29
|
+
throw new Error('Failed to fetch messages. Please try again later.');
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
exports.fetchMessages = fetchMessages;
|
|
33
|
+
const fetchMessageContent = async (messageId) => {
|
|
34
|
+
try {
|
|
35
|
+
const res = await axios_1.default.get(`${constant_1.BASE_URL}/messages/${messageId}`, {
|
|
36
|
+
headers: (0, exports.getAuthHeaders)(),
|
|
37
|
+
});
|
|
38
|
+
return res.data;
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
console.error(`Error fetching message content for ID ${messageId}:`, error);
|
|
42
|
+
throw new Error('Failed to fetch message content. Please try again later.');
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
exports.fetchMessageContent = fetchMessageContent;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const BASE_URL: string;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BASE_URL = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const dotenv = tslib_1.__importStar(require("dotenv"));
|
|
6
|
+
dotenv.config();
|
|
7
|
+
exports.BASE_URL = process.env?.['BASE_URL'] || '';
|
|
@@ -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>;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Extracts a verification code from the provided email content.
|
|
4
|
+
*
|
|
5
|
+
* This function scans the given text for a sequence of 5 or more
|
|
6
|
+
* consecutive digits and returns the first valid verification code.
|
|
7
|
+
* If no valid sequence is found, the function returns `null`.
|
|
8
|
+
*
|
|
9
|
+
* @param {string} text - The content of the email, typically the body.
|
|
10
|
+
* @returns {Promise<string | null>} The first verification code found, or `null` if no valid code exists.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const emailContent = "Your code is 123456.";
|
|
14
|
+
* const verificationCode = await getVerificationCode(emailContent);
|
|
15
|
+
* console.log(verificationCode); // Output: "123456"
|
|
16
|
+
*/
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.getVerificationCode = void 0;
|
|
19
|
+
const getVerificationCode = async (text) => {
|
|
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
|
+
};
|
|
28
|
+
exports.getVerificationCode = getVerificationCode;
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "temp-disposable-email",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.0",
|
|
4
4
|
"description": "",
|
|
5
|
-
"
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
6
7
|
"scripts": {
|
|
7
8
|
"build": "tsc",
|
|
8
9
|
"test": "jest",
|
|
@@ -31,7 +32,8 @@
|
|
|
31
32
|
"homepage": "https://github.com/pirasanthan-jesugeevegan/temp-disposable-email#readme",
|
|
32
33
|
"dependencies": {
|
|
33
34
|
"axios": "^1.7.8",
|
|
34
|
-
"dotenv": "^16.4.5"
|
|
35
|
+
"dotenv": "^16.4.5",
|
|
36
|
+
"tslib": "^2.8.1"
|
|
35
37
|
},
|
|
36
38
|
"devDependencies": {
|
|
37
39
|
"@types/jest": "^29.5.14",
|
package/src/utils/constant.ts
CHANGED
package/tsconfig.json
CHANGED
|
@@ -1,12 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"compilerOptions": {
|
|
3
|
-
"target": "
|
|
4
|
-
"module": "
|
|
3
|
+
"target": "ES2021",
|
|
4
|
+
"module": "commonjs",
|
|
5
5
|
"declaration": true,
|
|
6
6
|
"outDir": "./dist",
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
"
|
|
7
|
+
"importHelpers": true,
|
|
8
|
+
"preserveValueImports": false,
|
|
9
|
+
"allowSyntheticDefaultImports": true /* Allow 'import x from y' when a module doesn't have a default export. */,
|
|
10
|
+
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */,
|
|
11
|
+
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
|
12
|
+
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
|
|
13
|
+
/* Type Checking */
|
|
14
|
+
"strict": true /* Enable all strict type-checking options. */,
|
|
15
|
+
"noImplicitAny": false /* Enable error reporting for expressions and declarations with an implied `any` type.. */,
|
|
16
|
+
"strictNullChecks": true /* When type checking, take into account `null` and `undefined`. */,
|
|
17
|
+
"strictFunctionTypes": true /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */,
|
|
18
|
+
"strictBindCallApply": true /* Check that the arguments for `bind`, `call`, and `apply` methods match the original function. */,
|
|
19
|
+
"strictPropertyInitialization": true /* Check for class properties that are declared but not set in the constructor. */,
|
|
20
|
+
"noImplicitThis": true /* Enable error reporting when `this` is given the type `any`. */,
|
|
21
|
+
"useUnknownInCatchVariables": true /* Type catch clause variables as 'unknown' instead of 'any'. */,
|
|
22
|
+
"alwaysStrict": true /* Ensure 'use strict' is always emitted. */,
|
|
23
|
+
"noUnusedLocals": true /* Enable error reporting when a local variables aren't read. */,
|
|
24
|
+
"noUnusedParameters": true /* Raise an error when a function parameter isn't read */,
|
|
25
|
+
"exactOptionalPropertyTypes": true /* Interpret optional property types as written, rather than adding 'undefined'. */,
|
|
26
|
+
"noImplicitReturns": true /* Enable error reporting for codepaths that do not explicitly return in a function. */,
|
|
27
|
+
"noFallthroughCasesInSwitch": true /* Enable error reporting for fallthrough cases in switch statements. */,
|
|
28
|
+
"noUncheckedIndexedAccess": true /* Include 'undefined' in index signature results */,
|
|
29
|
+
"noImplicitOverride": true /* Ensure overriding members in derived classes are marked with an override modifier. */,
|
|
30
|
+
"noPropertyAccessFromIndexSignature": true /* Enforces using indexed accessors for keys declared using an indexed type */,
|
|
31
|
+
"allowUnusedLabels": false /* Disable error reporting for unused labels. */,
|
|
32
|
+
"allowUnreachableCode": false /* Disable error reporting for unreachable code. */,
|
|
33
|
+
|
|
34
|
+
/* Completeness */
|
|
35
|
+
"skipDefaultLibCheck": true /* Skip type checking .d.ts files that are included with TypeScript. */,
|
|
36
|
+
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
|
10
37
|
},
|
|
11
38
|
"include": ["src/**/*"],
|
|
12
39
|
"exclude": ["node_modules", "tests"]
|