temp-disposable-email 0.0.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/.nvmrc +1 -0
- package/LICENSE +21 -0
- package/README.md +196 -0
- package/cypress.config.ts +12 -0
- package/dist/cjs/index.d.ts +4 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +47 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/services/accountService.d.ts +37 -0
- package/dist/cjs/services/accountService.d.ts.map +1 -0
- package/dist/cjs/services/accountService.js +100 -0
- package/dist/cjs/services/accountService.js.map +1 -0
- package/dist/cjs/services/authService.d.ts +3 -0
- package/dist/cjs/services/authService.d.ts.map +1 -0
- package/dist/cjs/services/authService.js +22 -0
- package/dist/cjs/services/authService.js.map +1 -0
- package/dist/cjs/services/messageService.d.ts +54 -0
- package/dist/cjs/services/messageService.d.ts.map +1 -0
- package/dist/cjs/services/messageService.js +122 -0
- package/dist/cjs/services/messageService.js.map +1 -0
- package/dist/cjs/utils/api.d.ts +7 -0
- package/dist/cjs/utils/api.d.ts.map +1 -0
- package/dist/cjs/utils/api.js +47 -0
- package/dist/cjs/utils/api.js.map +1 -0
- package/dist/cjs/utils/constant.d.ts +2 -0
- package/dist/cjs/utils/constant.d.ts.map +1 -0
- package/dist/cjs/utils/constant.js +5 -0
- package/dist/cjs/utils/constant.js.map +1 -0
- package/dist/cjs/utils/delay.d.ts +2 -0
- package/dist/cjs/utils/delay.d.ts.map +1 -0
- package/dist/cjs/utils/delay.js +6 -0
- package/dist/cjs/utils/delay.js.map +1 -0
- package/dist/cjs/utils/generateRandomName.d.ts +2 -0
- package/dist/cjs/utils/generateRandomName.d.ts.map +1 -0
- package/dist/cjs/utils/generateRandomName.js +6 -0
- package/dist/cjs/utils/generateRandomName.js.map +1 -0
- package/dist/cjs/utils/getVerificationCode.d.ts +17 -0
- package/dist/cjs/utils/getVerificationCode.d.ts.map +1 -0
- package/dist/cjs/utils/getVerificationCode.js +29 -0
- package/dist/cjs/utils/getVerificationCode.js.map +1 -0
- package/dist/esm/index.d.ts +4 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +6 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/services/accountService.d.ts +37 -0
- package/dist/esm/services/accountService.d.ts.map +1 -0
- package/dist/esm/services/accountService.js +92 -0
- package/dist/esm/services/accountService.js.map +1 -0
- package/dist/esm/services/authService.d.ts +3 -0
- package/dist/esm/services/authService.d.ts.map +1 -0
- package/dist/esm/services/authService.js +14 -0
- package/dist/esm/services/authService.js.map +1 -0
- package/dist/esm/services/messageService.d.ts +54 -0
- package/dist/esm/services/messageService.d.ts.map +1 -0
- package/dist/esm/services/messageService.js +114 -0
- package/dist/esm/services/messageService.js.map +1 -0
- package/dist/esm/utils/api.d.ts +7 -0
- package/dist/esm/utils/api.d.ts.map +1 -0
- package/dist/esm/utils/api.js +38 -0
- package/dist/esm/utils/api.js.map +1 -0
- package/dist/esm/utils/constant.d.ts +2 -0
- package/dist/esm/utils/constant.d.ts.map +1 -0
- package/dist/esm/utils/constant.js +2 -0
- package/dist/esm/utils/constant.js.map +1 -0
- package/dist/esm/utils/delay.d.ts +2 -0
- package/dist/esm/utils/delay.d.ts.map +1 -0
- package/dist/esm/utils/delay.js +2 -0
- package/dist/esm/utils/delay.js.map +1 -0
- package/dist/esm/utils/generateRandomName.d.ts +2 -0
- package/dist/esm/utils/generateRandomName.d.ts.map +1 -0
- package/dist/esm/utils/generateRandomName.js +2 -0
- package/dist/esm/utils/generateRandomName.js.map +1 -0
- package/dist/esm/utils/getVerificationCode.d.ts +17 -0
- package/dist/esm/utils/getVerificationCode.d.ts.map +1 -0
- package/dist/esm/utils/getVerificationCode.js +25 -0
- package/dist/esm/utils/getVerificationCode.js.map +1 -0
- package/jest.config.js +8 -0
- package/package.json +60 -0
- package/playwright.config.ts +78 -0
- package/src/index.ts +11 -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 +38 -0
- package/src/utils/constant.ts +1 -0
- package/src/utils/delay.ts +2 -0
- package/src/utils/generateRandomName.ts +2 -0
- package/src/utils/getVerificationCode.ts +27 -0
- package/tsconfig.cjs.json +16 -0
- package/tsconfig.json +17 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"delay.d.ts","sourceRoot":"","sources":["../../../src/utils/delay.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,KAAK,OAAQ,MAAM,KAAG,OAAO,CAAC,IAAI,CACI,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"delay.js","sourceRoot":"","sources":["../../../src/utils/delay.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,KAAK,GAAG,CAAC,EAAU,EAAiB,EAAE,CACjD,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generateRandomName.d.ts","sourceRoot":"","sources":["../../../src/utils/generateRandomName.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,kBAAkB,QAAO,MACO,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generateRandomName.js","sourceRoot":"","sources":["../../../src/utils/generateRandomName.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,kBAAkB,GAAG,GAAW,EAAE,CAC7C,IAAI,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
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>;
|
|
17
|
+
//# sourceMappingURL=getVerificationCode.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getVerificationCode.d.ts","sourceRoot":"","sources":["../../../src/utils/getVerificationCode.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,eAAO,MAAM,mBAAmB,SACxB,MAAM,KACX,OAAO,CAAC,MAAM,GAAG,IAAI,CAQvB,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
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 const getVerificationCode = async (text) => {
|
|
17
|
+
console.log('Extracting the verification code from the email content...');
|
|
18
|
+
const matches = text.match(/\b\d{5,}\b/);
|
|
19
|
+
if (matches) {
|
|
20
|
+
return matches[0];
|
|
21
|
+
}
|
|
22
|
+
console.warn('No verification code found in the provided email content.');
|
|
23
|
+
return null;
|
|
24
|
+
};
|
|
25
|
+
//# sourceMappingURL=getVerificationCode.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getVerificationCode.js","sourceRoot":"","sources":["../../../src/utils/getVerificationCode.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,MAAM,CAAC,MAAM,mBAAmB,GAAG,KAAK,EACtC,IAAY,EACY,EAAE;IAC1B,OAAO,CAAC,GAAG,CAAC,4DAA4D,CAAC,CAAC;IAC1E,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;IACzC,IAAI,OAAO,EAAE,CAAC;QACZ,OAAO,OAAO,CAAC,CAAC,CAAC,CAAC;IACpB,CAAC;IACD,OAAO,CAAC,IAAI,CAAC,2DAA2D,CAAC,CAAC;IAC1E,OAAO,IAAI,CAAC;AACd,CAAC,CAAC"}
|
package/jest.config.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "temp-disposable-email",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Generating Temporary email address for testing, retrieving email from the temporary email address",
|
|
5
|
+
"main": "dist/cjs/index.js",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"require": "./dist/cjs/index.js",
|
|
9
|
+
"import": "./dist/esm/index.js"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"types": "dist/esm/index.d.ts",
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "tsc -p tsconfig.json && tsc -p tsconfig.cjs.json",
|
|
15
|
+
"test": "jest",
|
|
16
|
+
"test:cypress": "cypress run",
|
|
17
|
+
"test:playwright": "playwright test",
|
|
18
|
+
"lint": "eslint src --fix",
|
|
19
|
+
"prepare": "npm run build"
|
|
20
|
+
},
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/pirasanthan-jesugeevegan/temp-disposable-email.git"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"temp-email",
|
|
27
|
+
"disposable-email",
|
|
28
|
+
"email",
|
|
29
|
+
"email-testing",
|
|
30
|
+
"automation",
|
|
31
|
+
"polling",
|
|
32
|
+
"cypress",
|
|
33
|
+
"playwright"
|
|
34
|
+
],
|
|
35
|
+
"author": "Pirasanthan Jesugeevegan",
|
|
36
|
+
"license": "MIT",
|
|
37
|
+
"bugs": {
|
|
38
|
+
"url": "https://github.com/pirasanthan-jesugeevegan/temp-disposable-email/issues"
|
|
39
|
+
},
|
|
40
|
+
"homepage": "https://github.com/pirasanthan-jesugeevegan/temp-disposable-email#readme",
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"axios": "^1.7.8",
|
|
43
|
+
"dotenv": "^16.4.5",
|
|
44
|
+
"temp-disposable-email": "^1.6.4",
|
|
45
|
+
"tslib": "^2.8.1"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@playwright/test": "^1.49.0",
|
|
49
|
+
"@types/cypress": "^1.1.6",
|
|
50
|
+
"@types/jest": "^29.5.14",
|
|
51
|
+
"@types/node": "^22.10.0",
|
|
52
|
+
"axios-mock-adapter": "^2.1.0",
|
|
53
|
+
"jest": "^29.7.0",
|
|
54
|
+
"ts-jest": "^29.2.5",
|
|
55
|
+
"typescript": "^5.7.2"
|
|
56
|
+
},
|
|
57
|
+
"publishConfig": {
|
|
58
|
+
"registry": "https://registry.npmjs.org/"
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { defineConfig, devices } from '@playwright/test';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Read environment variables from file.
|
|
5
|
+
* https://github.com/motdotla/dotenv
|
|
6
|
+
*/
|
|
7
|
+
// import dotenv from 'dotenv';
|
|
8
|
+
// import path from 'path';
|
|
9
|
+
// dotenv.config({ path: path.resolve(__dirname, '.env') });
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* See https://playwright.dev/docs/test-configuration.
|
|
13
|
+
*/
|
|
14
|
+
export default defineConfig({
|
|
15
|
+
testDir: './examples/playwright',
|
|
16
|
+
/* Run tests in files in parallel */
|
|
17
|
+
fullyParallel: true,
|
|
18
|
+
/* Fail the build on CI if you accidentally left test.only in the source code. */
|
|
19
|
+
forbidOnly: !!process.env.CI,
|
|
20
|
+
/* Retry on CI only */
|
|
21
|
+
retries: process.env.CI ? 2 : 0,
|
|
22
|
+
/* Opt out of parallel tests on CI. */
|
|
23
|
+
workers: process.env.CI ? 1 : undefined,
|
|
24
|
+
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
|
|
25
|
+
reporter: 'html',
|
|
26
|
+
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
|
|
27
|
+
use: {
|
|
28
|
+
/* Base URL to use in actions like `await page.goto('/')`. */
|
|
29
|
+
// baseURL: 'http://127.0.0.1:3000',
|
|
30
|
+
|
|
31
|
+
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
|
|
32
|
+
trace: 'on-first-retry',
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
/* Configure projects for major browsers */
|
|
36
|
+
projects: [
|
|
37
|
+
{
|
|
38
|
+
name: 'chromium',
|
|
39
|
+
use: { ...devices['Desktop Chrome'] },
|
|
40
|
+
},
|
|
41
|
+
|
|
42
|
+
// {
|
|
43
|
+
// name: 'firefox',
|
|
44
|
+
// use: { ...devices['Desktop Firefox'] },
|
|
45
|
+
// },
|
|
46
|
+
|
|
47
|
+
// {
|
|
48
|
+
// name: 'webkit',
|
|
49
|
+
// use: { ...devices['Desktop Safari'] },
|
|
50
|
+
// },
|
|
51
|
+
/* Test against mobile viewports. */
|
|
52
|
+
// {
|
|
53
|
+
// name: 'Mobile Chrome',
|
|
54
|
+
// use: { ...devices['Pixel 5'] },
|
|
55
|
+
// },
|
|
56
|
+
// {
|
|
57
|
+
// name: 'Mobile Safari',
|
|
58
|
+
// use: { ...devices['iPhone 12'] },
|
|
59
|
+
// },
|
|
60
|
+
|
|
61
|
+
/* Test against branded browsers. */
|
|
62
|
+
// {
|
|
63
|
+
// name: 'Microsoft Edge',
|
|
64
|
+
// use: { ...devices['Desktop Edge'], channel: 'msedge' },
|
|
65
|
+
// },
|
|
66
|
+
// {
|
|
67
|
+
// name: 'Google Chrome',
|
|
68
|
+
// use: { ...devices['Desktop Chrome'], channel: 'chrome' },
|
|
69
|
+
// },
|
|
70
|
+
],
|
|
71
|
+
|
|
72
|
+
/* Run your local dev server before starting the tests */
|
|
73
|
+
// webServer: {
|
|
74
|
+
// command: 'npm run start',
|
|
75
|
+
// url: 'http://127.0.0.1:3000',
|
|
76
|
+
// reuseExistingServer: !process.env.CI,
|
|
77
|
+
// },
|
|
78
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import * as dotenv from 'dotenv';
|
|
2
|
+
dotenv.config();
|
|
3
|
+
|
|
4
|
+
export { createInbox, deleteAccount } from './services/accountService';
|
|
5
|
+
export {
|
|
6
|
+
getRecentEmail,
|
|
7
|
+
deleteMessage,
|
|
8
|
+
MessageContent,
|
|
9
|
+
GetEmailOptions,
|
|
10
|
+
} from './services/messageService';
|
|
11
|
+
export { getVerificationCode } from './utils/getVerificationCode';
|
|
@@ -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`);
|
|
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,38 @@
|
|
|
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
|
+
return res.data;
|
|
22
|
+
} catch (error) {
|
|
23
|
+
console.error('Error fetching messages:', error);
|
|
24
|
+
throw new Error('Failed to fetch messages. Please try again later.');
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export const fetchMessageContent = async (messageId: string): Promise<any> => {
|
|
29
|
+
try {
|
|
30
|
+
const res = await axios.get(`${BASE_URL}/messages/${messageId}`, {
|
|
31
|
+
headers: getAuthHeaders(),
|
|
32
|
+
});
|
|
33
|
+
return res.data;
|
|
34
|
+
} catch (error) {
|
|
35
|
+
console.error(`Error fetching message content for ID ${messageId}:`, error);
|
|
36
|
+
throw new Error('Failed to fetch message content. Please try again later.');
|
|
37
|
+
}
|
|
38
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const BASE_URL = process.env.BASE_URL;
|
|
@@ -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
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"module": "CommonJS",
|
|
4
|
+
"target": "ESNext",
|
|
5
|
+
"moduleResolution": "node",
|
|
6
|
+
"esModuleInterop": true,
|
|
7
|
+
"allowSyntheticDefaultImports": true,
|
|
8
|
+
"outDir": "./dist/cjs",
|
|
9
|
+
"declaration": true,
|
|
10
|
+
"declarationMap": true,
|
|
11
|
+
"sourceMap": true,
|
|
12
|
+
"skipLibCheck": true
|
|
13
|
+
},
|
|
14
|
+
"include": ["src/**/*"],
|
|
15
|
+
"exclude": ["node_modules", "examples"]
|
|
16
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"module": "ESNext",
|
|
4
|
+
"target": "ESNext",
|
|
5
|
+
"moduleResolution": "node",
|
|
6
|
+
"esModuleInterop": true,
|
|
7
|
+
"allowSyntheticDefaultImports": true,
|
|
8
|
+
"outDir": "./dist/esm",
|
|
9
|
+
"declaration": true,
|
|
10
|
+
"declarationMap": true,
|
|
11
|
+
"sourceMap": true,
|
|
12
|
+
"skipLibCheck": true,
|
|
13
|
+
"types": ["jest", "node"]
|
|
14
|
+
},
|
|
15
|
+
"include": ["src/**/*"],
|
|
16
|
+
"exclude": ["node_modules", "examples"]
|
|
17
|
+
}
|