Skip to content

Commit a077ca9

Browse files
author
Sergey Khomushin
committed
refactor keys options
1 parent 3c5dd93 commit a077ca9

File tree

7 files changed

+51
-19
lines changed

7 files changed

+51
-19
lines changed

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
"publishConfig": {
2020
"access": "public"
2121
},
22+
"exports": {
23+
".": {
24+
"import": "./mjs/emailjs.js",
25+
"require": "./cjs/emailjs.js"
26+
}
27+
},
2228
"engines": {
2329
"node": ">=14.0.0"
2430
},

src/api/send_json.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { EmailJSResponseStatus } from '../models/emailjs_response_status';
55

66
export const sendJSON = (params: string): Promise<EmailJSResponseStatus> => {
77
const options: RequestOptions = {
8-
host: store._origin,
8+
host: store._host,
99
path: 'api/v1.0/email/send',
1010
port: 443,
1111
method: 'POST',

src/emailjs.spec.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,28 @@ describe('required params validator', () => {
2828
});
2929

3030
it('should send method and fail on the service ID', () => {
31-
emailjs.init('user_LC2JWGTestKeySomething');
31+
emailjs.init({
32+
publicKey: 'LC2JWGTestKeySomething',
33+
});
3234

3335
expect(() => emailjs.send('', 'my_test_template')).toThrow('The service ID is required');
3436
});
3537

3638
it('should send method and fail on the template ID', () => {
37-
emailjs.init('user_LC2JWGTestKeySomething');
39+
emailjs.init({
40+
publicKey: 'LC2JWGTestKeySomething',
41+
});
3842

3943
expect(() => emailjs.send('default_service', '')).toThrow('The template ID is required');
4044
});
4145
});
4246

4347
describe('emailjs.send method', () => {
4448
it('should init and send method successfully', async () => {
45-
emailjs.init('user_LC2JWGTestKeySomething');
49+
emailjs.init({
50+
publicKey: 'LC2JWGTestKeySomething',
51+
privateKey: 'PrKeyTestKeySomething',
52+
});
4653

4754
try {
4855
const result = await emailjs.send('default_service', 'my_test_template');
@@ -58,7 +65,10 @@ describe('emailjs.send method', () => {
5865
'default_service',
5966
'my_test_template',
6067
{},
61-
'user_LC2JWGTestKeySomething',
68+
{
69+
publicKey: 'LC2JWGTestKeySomething',
70+
privateKey: 'PrKeyTestKeySomething',
71+
},
6272
);
6373
expect(result).toEqual({ status: 200, text: 'OK' });
6474
} catch (error) {
@@ -80,7 +90,10 @@ describe('emailjs.send method', () => {
8090
'default_service',
8191
'my_test_template',
8292
{},
83-
'user_LC2JWGTestKeySomething',
93+
{
94+
publicKey: 'LC2JWGTestKeySomething',
95+
privateKey: 'PrKeyTestKeySomething',
96+
},
8497
);
8598
expect(result).toBeUndefined();
8699
} catch (error) {

src/methods/init/init.ts

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

38
/**
49
* Initiation
5-
* @param {string} publicKey - set the EmailJS public key
6-
* @param {string} origin - set the EmailJS origin
10+
* @param {InitOptions} options - set emailjs options
711
*/
812

9-
export const init = (publicKey: string, origin = 'api.emailjs.com'): void => {
10-
store._userID = publicKey;
11-
store._origin = origin;
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';
1217
};

src/methods/send/send.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { EmailJSResponseStatus } from '../../models/emailjs_response_status';
2+
import type { Options } from '../../types/Options';
23

34
import { store } from '../../store/store';
45
import { validateParams } from '../../utils/validate_params';
@@ -9,24 +10,26 @@ import { sendJSON } from '../../api/send_json';
910
* @param {string} serviceID - the EmailJS service ID
1011
* @param {string} templateID - the EmailJS template ID
1112
* @param {object} templatePrams - the template params, what will be set to the EmailJS template
12-
* @param {string} publicKey - the EmailJS public key
13+
* @param {Options} options - the EmailJS options
1314
* @returns {Promise<string>}
1415
*/
1516
export const send = (
1617
serviceID: string,
1718
templateID: string,
1819
templatePrams?: Record<string, unknown>,
19-
publicKey?: string,
20+
options?: Options,
2021
): Promise<EmailJSResponseStatus> => {
21-
const pubKey = publicKey || store._userID;
22+
const pubKey = options?.publicKey || store._publicKey;
23+
const prKey = options?.privateKey || store._privateKey;
2224

2325
validateParams(pubKey, serviceID, templateID);
2426

2527
const params = {
2628
lib_version: process.env.npm_package_version,
27-
user_id: pubKey,
2829
service_id: serviceID,
2930
template_id: templateID,
31+
user_id: prKey,
32+
accessToken: options?.privateKey,
3033
template_params: templatePrams,
3134
};
3235

src/store/store.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
interface Store {
2-
_userID: string;
3-
_origin: string;
2+
_publicKey: string;
3+
_host: string;
4+
_privateKey?: string;
45
}
56

67
export const store: Store = {
7-
_userID: '',
8-
_origin: 'api.emailjs.com',
8+
_publicKey: '',
9+
_host: 'api.emailjs.com',
910
};

src/types/Options.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface Options {
2+
publicKey: string;
3+
privateKey?: string;
4+
}

0 commit comments

Comments
 (0)