@vroskus/library-email 1.0.0 → 1.0.1
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/.eslintrc +76 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +8 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +4 -5
- package/src/index.ts +156 -0
- package/src/types.ts +8 -0
- package/tsconfig.json +26 -0
package/.eslintrc
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
{
|
|
2
|
+
"root": true,
|
|
3
|
+
"parser": "@typescript-eslint/parser",
|
|
4
|
+
"parserOptions": {
|
|
5
|
+
"requireConfigFile": false,
|
|
6
|
+
"project": ["tsconfig.json"]
|
|
7
|
+
},
|
|
8
|
+
"extends": [
|
|
9
|
+
"airbnb-base",
|
|
10
|
+
"airbnb-typescript",
|
|
11
|
+
"plugin:import/recommended",
|
|
12
|
+
"plugin:@typescript-eslint/eslint-recommended",
|
|
13
|
+
"plugin:@typescript-eslint/recommended",
|
|
14
|
+
"plugin:sort/recommended"
|
|
15
|
+
],
|
|
16
|
+
"plugins": [
|
|
17
|
+
"@typescript-eslint",
|
|
18
|
+
"import",
|
|
19
|
+
"import-newlines",
|
|
20
|
+
"react",
|
|
21
|
+
"sort"
|
|
22
|
+
],
|
|
23
|
+
"rules": {
|
|
24
|
+
"complexity": ["error", 5],
|
|
25
|
+
"@typescript-eslint/ban-ts-comment": [1],
|
|
26
|
+
"@typescript-eslint/no-unused-vars": [2],
|
|
27
|
+
"curly": ["error", "all"],
|
|
28
|
+
"import/no-cycle": [0],
|
|
29
|
+
"no-await-in-loop": 0,
|
|
30
|
+
"no-console": "error",
|
|
31
|
+
"no-duplicate-imports": [0],
|
|
32
|
+
"no-loss-of-precision": [0],
|
|
33
|
+
"no-restricted-syntax": 0,
|
|
34
|
+
"no-unreachable-loop": [0],
|
|
35
|
+
"no-useless-backreference": [0],
|
|
36
|
+
"no-unused-private-class-members": [2],
|
|
37
|
+
"padding-line-between-statements": ["error", {
|
|
38
|
+
"blankLine": "always",
|
|
39
|
+
"prev": ["const", "let"],
|
|
40
|
+
"next": "*"
|
|
41
|
+
}, {
|
|
42
|
+
"blankLine": "any",
|
|
43
|
+
"prev": ["const", "let"],
|
|
44
|
+
"next": ["const", "let"]
|
|
45
|
+
}, {
|
|
46
|
+
"blankLine": "always",
|
|
47
|
+
"prev": ["if", "function", "for"],
|
|
48
|
+
"next": "*"
|
|
49
|
+
}, {
|
|
50
|
+
"blankLine": "always",
|
|
51
|
+
"prev": "*",
|
|
52
|
+
"next": "return"
|
|
53
|
+
}],
|
|
54
|
+
"object-curly-newline": ["error", {
|
|
55
|
+
"ObjectExpression": "always",
|
|
56
|
+
"ObjectPattern": "always",
|
|
57
|
+
"ImportDeclaration": "always",
|
|
58
|
+
"ExportDeclaration": "always"
|
|
59
|
+
}],
|
|
60
|
+
"function-call-argument-newline": ["error", "always"],
|
|
61
|
+
"object-property-newline": ["error", {
|
|
62
|
+
"allowMultiplePropertiesPerLine": false
|
|
63
|
+
}],
|
|
64
|
+
"import-newlines/enforce": ["error", {
|
|
65
|
+
"items": 1,
|
|
66
|
+
"forceSingleLine": false
|
|
67
|
+
}],
|
|
68
|
+
"sort/imports": [0],
|
|
69
|
+
"sort/exports": [0]
|
|
70
|
+
},
|
|
71
|
+
"env": {
|
|
72
|
+
"node": true,
|
|
73
|
+
"jest": true,
|
|
74
|
+
"es6": true
|
|
75
|
+
}
|
|
76
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { $Config as Config } from './types';
|
|
2
|
+
type $SendMailParams = {
|
|
3
|
+
bcc?: string | Array<string>;
|
|
4
|
+
from: string;
|
|
5
|
+
html: string;
|
|
6
|
+
replyTo?: string;
|
|
7
|
+
subject: string;
|
|
8
|
+
text: string;
|
|
9
|
+
to: string | Array<string>;
|
|
10
|
+
};
|
|
11
|
+
type $SendEmailResponse = {
|
|
12
|
+
accepted: Array<string>;
|
|
13
|
+
messageId: string;
|
|
14
|
+
pending: Array<string>;
|
|
15
|
+
rejected: Array<string>;
|
|
16
|
+
response: string;
|
|
17
|
+
};
|
|
18
|
+
type $Transporter = {
|
|
19
|
+
sendMail: (arg0: $SendMailParams) => Promise<$SendEmailResponse>;
|
|
20
|
+
};
|
|
21
|
+
export type $Config = Config;
|
|
22
|
+
declare class EmailService<C extends Config> {
|
|
23
|
+
#private;
|
|
24
|
+
from: string;
|
|
25
|
+
transporter: $Transporter;
|
|
26
|
+
constructor({ email, from, host, password, port, username, }: C);
|
|
27
|
+
sendMail({ bcc, content, replyTo, subject, to, }: {
|
|
28
|
+
bcc?: string | Array<string>;
|
|
29
|
+
content: string;
|
|
30
|
+
replyTo?: string;
|
|
31
|
+
subject: string;
|
|
32
|
+
to: string | Array<string>;
|
|
33
|
+
}): Promise<string>;
|
|
34
|
+
}
|
|
35
|
+
export default EmailService;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AACA,4DAAoC;AACpC,+CAEsB;AA+BtB,MAAM,YAAY;IAKhB,YAAY,EACV,KAAK,EACL,IAAI,EACJ,IAAI,EACJ,QAAQ,EACR,IAAI,EACJ,QAAQ,GACN;;QACF,IAAI,CAAC,IAAI,GAAG,IAAI,IAAI,MAAM,KAAK,GAAG,CAAC;QAEnC,uBAAA,IAAI,+DAAkB,MAAtB,IAAI,EAAmB;YACrB,IAAI;YACJ,QAAQ;YACR,IAAI,EAAE,IAAI,IAAI,IAAI;YAClB,QAAQ;SACT,CAAC,CAAC;IACL,CAAC;IAgEK,QAAQ,CAAC,EACb,GAAG,EACH,OAAO,EACP,OAAO,EACP,OAAO,EACP,EAAE,GAOH;;YACC,MAAM,MAAM,GAAG;gBACb,GAAG;gBACH,IAAI,EAAE,IAAI,CAAC,IAA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,EAAE,KAAK;SACd,CAAC;QAEF,IAAI,CAAC,WAAW,GAAG,oBAAU,CAAC,eAAe,CAAC,MAAM,CAAC,CAAC;KACvD;AACH,CAAC;AAqCH,kBAAe,YAAY,CAAC"}
|
package/dist/types.d.ts
ADDED
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vroskus/library-email",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Email",
|
|
5
5
|
"author": "Vilius Roškus <vilius@regattas.eu>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -9,9 +9,6 @@
|
|
|
9
9
|
"url": "git+https://github.com/vroskus/library-email.git"
|
|
10
10
|
},
|
|
11
11
|
"main": "dist/index.js",
|
|
12
|
-
"files": [
|
|
13
|
-
"dist/index.js"
|
|
14
|
-
],
|
|
15
12
|
"scripts": {
|
|
16
13
|
"postinstall": "npm run build",
|
|
17
14
|
"build": "tsc",
|
|
@@ -20,6 +17,8 @@
|
|
|
20
17
|
"test:e2e": "echo 'No tests'"
|
|
21
18
|
},
|
|
22
19
|
"dependencies": {
|
|
20
|
+
"@types/lodash": "4.14.192",
|
|
21
|
+
"@types/nodemailer": "6.4.7",
|
|
23
22
|
"html-to-text": "9.0.4",
|
|
24
23
|
"lodash": "4.17.21",
|
|
25
24
|
"nodemailer": "6.9.1"
|
|
@@ -39,4 +38,4 @@
|
|
|
39
38
|
"npm-check": "6.0.1",
|
|
40
39
|
"typescript": "4.9.5"
|
|
41
40
|
}
|
|
42
|
-
}
|
|
41
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
// Helpers
|
|
2
|
+
import nodemailer from 'nodemailer';
|
|
3
|
+
import {
|
|
4
|
+
htmlToText,
|
|
5
|
+
} from 'html-to-text';
|
|
6
|
+
|
|
7
|
+
// Types
|
|
8
|
+
import type {
|
|
9
|
+
$Config as Config,
|
|
10
|
+
} from './types';
|
|
11
|
+
|
|
12
|
+
type $SendMailParams = {
|
|
13
|
+
bcc?: string | Array<string>;
|
|
14
|
+
from: string;
|
|
15
|
+
html: string;
|
|
16
|
+
replyTo?: string;
|
|
17
|
+
subject: string;
|
|
18
|
+
text: string;
|
|
19
|
+
to: string | Array<string>;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
type $SendEmailResponse = {
|
|
23
|
+
accepted: Array<string>;
|
|
24
|
+
messageId: string;
|
|
25
|
+
pending: Array<string>;
|
|
26
|
+
rejected: Array<string>;
|
|
27
|
+
response: string;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
type $Transporter = {
|
|
31
|
+
sendMail: (arg0: $SendMailParams) => Promise<$SendEmailResponse>;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export type $Config = Config;
|
|
35
|
+
|
|
36
|
+
class EmailService<C extends Config> {
|
|
37
|
+
from: string;
|
|
38
|
+
|
|
39
|
+
transporter: $Transporter;
|
|
40
|
+
|
|
41
|
+
constructor({
|
|
42
|
+
email,
|
|
43
|
+
from,
|
|
44
|
+
host,
|
|
45
|
+
password,
|
|
46
|
+
port,
|
|
47
|
+
username,
|
|
48
|
+
}: C) {
|
|
49
|
+
this.from = `"${from}" <${email}>`;
|
|
50
|
+
|
|
51
|
+
this.#setupTransporter({
|
|
52
|
+
host,
|
|
53
|
+
password,
|
|
54
|
+
port: port || '25',
|
|
55
|
+
username,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
#setupTransporter({
|
|
60
|
+
host,
|
|
61
|
+
password,
|
|
62
|
+
port,
|
|
63
|
+
username,
|
|
64
|
+
}: {
|
|
65
|
+
host: string;
|
|
66
|
+
password?: string;
|
|
67
|
+
port: string;
|
|
68
|
+
username?: string;
|
|
69
|
+
}): void {
|
|
70
|
+
this.transporter = {
|
|
71
|
+
sendMail: async ({
|
|
72
|
+
bcc,
|
|
73
|
+
html,
|
|
74
|
+
subject,
|
|
75
|
+
text,
|
|
76
|
+
to,
|
|
77
|
+
}) => {
|
|
78
|
+
// eslint-disable-next-line no-console
|
|
79
|
+
console.log(
|
|
80
|
+
'\x1b[33m',
|
|
81
|
+
'EmailService.sendEmail',
|
|
82
|
+
'\x1b[0m\x1b[90m',
|
|
83
|
+
to,
|
|
84
|
+
bcc,
|
|
85
|
+
subject,
|
|
86
|
+
JSON.stringify(html),
|
|
87
|
+
JSON.stringify(text),
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
return {
|
|
91
|
+
accepted: Array.isArray(to) ? to : [to],
|
|
92
|
+
messageId: '0',
|
|
93
|
+
pending: [],
|
|
94
|
+
rejected: [],
|
|
95
|
+
response: 'Email sent by dummy service',
|
|
96
|
+
};
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
if (host !== '') {
|
|
101
|
+
let auth;
|
|
102
|
+
|
|
103
|
+
if (username && password) {
|
|
104
|
+
auth = {
|
|
105
|
+
pass: password,
|
|
106
|
+
user: username,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const params = {
|
|
111
|
+
auth,
|
|
112
|
+
host,
|
|
113
|
+
port,
|
|
114
|
+
secure: false,
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
this.transporter = nodemailer.createTransport(params);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async sendMail({
|
|
122
|
+
bcc,
|
|
123
|
+
content,
|
|
124
|
+
replyTo,
|
|
125
|
+
subject,
|
|
126
|
+
to,
|
|
127
|
+
}: {
|
|
128
|
+
bcc?: string | Array<string>;
|
|
129
|
+
content: string;
|
|
130
|
+
replyTo?: string;
|
|
131
|
+
subject: string;
|
|
132
|
+
to: string | Array<string>;
|
|
133
|
+
}): Promise<string> {
|
|
134
|
+
const params = {
|
|
135
|
+
bcc,
|
|
136
|
+
from: this.from,
|
|
137
|
+
html: content,
|
|
138
|
+
replyTo,
|
|
139
|
+
subject,
|
|
140
|
+
text: htmlToText(content),
|
|
141
|
+
to,
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
const info: $SendEmailResponse = await this.transporter.sendMail(params);
|
|
145
|
+
|
|
146
|
+
// eslint-disable-next-line no-console
|
|
147
|
+
console.log(
|
|
148
|
+
'info',
|
|
149
|
+
info,
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
return info.messageId;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export default EmailService;
|
package/src/types.ts
ADDED
package/tsconfig.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"outDir": "./dist/",
|
|
4
|
+
"types": ["node", "jest"],
|
|
5
|
+
"module": "commonjs",
|
|
6
|
+
"esModuleInterop": true,
|
|
7
|
+
"declaration": true,
|
|
8
|
+
"noImplicitAny": false,
|
|
9
|
+
"removeComments": true,
|
|
10
|
+
"noLib": false,
|
|
11
|
+
"emitDecoratorMetadata": true,
|
|
12
|
+
"experimentalDecorators": true,
|
|
13
|
+
"target": "es6",
|
|
14
|
+
"sourceMap": true,
|
|
15
|
+
"lib": [
|
|
16
|
+
"es6"
|
|
17
|
+
]
|
|
18
|
+
},
|
|
19
|
+
"exclude": [
|
|
20
|
+
"node_modules",
|
|
21
|
+
"./dist/**/*"
|
|
22
|
+
],
|
|
23
|
+
"include": [
|
|
24
|
+
"./src"
|
|
25
|
+
]
|
|
26
|
+
}
|