Skip to content

Commit b30b94c

Browse files
committed
refactor the code for v4
1 parent 48a0f6d commit b30b94c

28 files changed

+830
-172
lines changed

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
{
22
"name": "@emailjs/nodejs",
33
"version": "2.2.0",
4-
"description": "EmailJS helps to send emails directly from your code. Official EmailJS SDK for NodeJS",
4+
"description": "Official EmailJS SDK for NodeJS",
55
"private": false,
66
"author": "EmailJS",
77
"contributors": [
88
"Sergey Khomushin <[email protected]> (https://www.emailjs.com)"
99
],
1010
"homepage": "https://www.emailjs.com",
1111
"license": "BSD-3-Clause",
12-
"main": "cjs/emailjs.js",
13-
"types": "mjs/emailjs.d.ts",
14-
"module": "mjs/emailjs.js",
12+
"main": "cjs/index.js",
13+
"types": "es/index.d.ts",
14+
"module": "es/index.js",
1515
"repository": {
1616
"type": "git",
1717
"url": "https://github.com/emailjs-com/emailjs-nodejs.git"
@@ -21,8 +21,8 @@
2121
},
2222
"exports": {
2323
".": {
24-
"import": "./mjs/emailjs.js",
25-
"require": "./cjs/emailjs.js"
24+
"import": "./mjs/index.js",
25+
"require": "./cjs/index.js"
2626
}
2727
},
2828
"engines": {

src/emailjs.spec.ts

Lines changed: 0 additions & 107 deletions
This file was deleted.

src/emailjs.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { StorageProvider } from './types/StorageProvider';
2+
import { EmailJSResponseStatus } from './models/EmailJSResponseStatus';
3+
import { init } from './methods/init/init';
4+
import { send } from './methods/send/send';
5+
6+
export type { StorageProvider };
7+
8+
export { init, send, EmailJSResponseStatus };
9+
10+
export default {
11+
init,
12+
send,
13+
EmailJSResponseStatus,
14+
};

src/it.spec.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { it, expect, describe, jest } from '@jest/globals';
2+
import { type RequestOptions } from 'https';
3+
import emailjs, { send, init, EmailJSResponseStatus } from './index';
4+
5+
jest.mock('https', () => ({
6+
...jest.requireActual<typeof import('https')>('https'),
7+
request: jest.fn((_: RequestOptions, cb: (res: any) => void) =>
8+
cb({
9+
on: (_: string, cb: (chunk: any) => void) => cb(Buffer.from('OK', 'utf8')),
10+
statusCode: 200,
11+
}),
12+
),
13+
on: jest.fn(),
14+
write: jest.fn(),
15+
end: jest.fn(),
16+
}));
17+
18+
it('should export necessary methods and models', () => {
19+
expect(init).toBeDefined();
20+
expect(send).toBeDefined();
21+
expect(EmailJSResponseStatus).toBeDefined();
22+
});
23+
24+
describe('send method', () => {
25+
it('should call the init and the send method successfully', async () => {
26+
emailjs.init({
27+
publicKey: 'LC2JWGTestKeySomething',
28+
privateKey: 'PrKeyTestKeySomething',
29+
});
30+
31+
try {
32+
const result = await emailjs.send('default_service', 'my_test_template');
33+
expect(result).toEqual({ status: 200, text: 'OK' });
34+
} catch (error) {
35+
expect(error).toBeUndefined();
36+
}
37+
});
38+
39+
it('should call the init and the send method successfully as promise', () => {
40+
emailjs.init({
41+
publicKey: 'LC2JWGTestKeySomething',
42+
privateKey: 'PrKeyTestKeySomething',
43+
});
44+
45+
return emailjs.send('default_service', 'my_test_template').then(
46+
(result) => {
47+
expect(result).toEqual({ status: 200, text: 'OK' });
48+
},
49+
(error) => {
50+
expect(error).toBeUndefined();
51+
},
52+
);
53+
});
54+
});

src/methods/init/init.spec.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { it, expect, beforeEach } from '@jest/globals';
2+
3+
import { init } from './init';
4+
import { store } from '../../store/store';
5+
6+
beforeEach(() => {
7+
store.origin = 'https://api.emailjs.com';
8+
store.publicKey = undefined;
9+
store.storageProvider = undefined;
10+
});
11+
12+
it('should call the init method with empty options and get default values', () => {
13+
init({});
14+
15+
expect(store).toEqual({
16+
origin: 'api.emailjs.com',
17+
});
18+
});
19+
20+
it('should call the init method with custom options', () => {
21+
init({
22+
publicKey: 'C2JWGTestKeySomething',
23+
privateKey: 'privateC2JWGTestKeySomething',
24+
blockList: {
25+
list: ['[email protected]'],
26+
},
27+
limitRate: {
28+
throttle: 10000,
29+
},
30+
});
31+
32+
expect(store).toEqual({
33+
origin: 'api.emailjs.com',
34+
publicKey: 'C2JWGTestKeySomething',
35+
privateKey: 'privateC2JWGTestKeySomething',
36+
blockList: {
37+
list: ['[email protected]'],
38+
},
39+
limitRate: {
40+
throttle: 10000,
41+
},
42+
storageProvider: undefined,
43+
});
44+
});

src/methods/init/init.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
import type { Options } from '../../types/Options.js';
2-
import { store } from '../../store/store.js';
3-
4-
export interface InitOptions extends Options {
5-
host?: string;
6-
}
1+
import { store } from '../../store/store';
2+
import type { Options } from '../../types/Options';
73

84
/**
9-
* Initiation
10-
* @param {InitOptions} options - set emailjs options
5+
* EmailJS global SDK config
6+
* @param {object} options - the EmailJS global SDK config options
7+
* @param {string} origin - the non-default EmailJS origin
118
*/
9+
export const init = (options: Options, origin: string = 'api.emailjs.com'): void => {
10+
if (!options) return;
1211

13-
export const init = (options: InitOptions): void => {
14-
store._publicKey = options.publicKey;
15-
store._privateKey = options.privateKey;
16-
store._host = options.host || 'api.emailjs.com';
12+
store.publicKey = options.publicKey;
13+
store.privateKey = options.privateKey;
14+
store.storageProvider = options.storageProvider;
15+
store.blockList = options.blockList;
16+
store.limitRate = options.limitRate;
17+
store.origin = options.origin || origin;
1718
};

0 commit comments

Comments
 (0)