@kne/fastify-message 1.0.2 → 1.0.3

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.
@@ -8,14 +8,9 @@ const { convert } = require('html-to-text');
8
8
  const pify = require('pify');
9
9
 
10
10
  module.exports = fp(async (fastify, options) => {
11
- const emailConfig = Object.assign(
12
- {},
13
- {
14
- port: 465,
15
- secure: true
16
- },
17
- options.emailConfig
18
- );
11
+ const emailConfig = Object.assign({}, {
12
+ port: 465, secure: true
13
+ }, options.emailConfig);
19
14
  const isTest = options.isTest;
20
15
  const { models, services } = fastify[options.name];
21
16
  const includeTemplate = async dir => {
@@ -30,14 +25,10 @@ module.exports = fp(async (fastify, options) => {
30
25
  const content = await fs.readFile(filePath, 'utf8');
31
26
  const filename = file.replace(path.extname(file), '');
32
27
  const tempArray = filename.split('_');
33
- const code = tempArray[0],
34
- type = tempArray[1] || 0,
35
- name = tempArray[2] || code;
28
+ const code = tempArray[0], type = tempArray[1] || 0, name = tempArray[2] || code;
36
29
  const codeTemplate = await models.template.findOne({
37
30
  where: {
38
- code,
39
- type,
40
- level: 0
31
+ code, type, level: 0
41
32
  }
42
33
  });
43
34
 
@@ -49,11 +40,7 @@ module.exports = fp(async (fastify, options) => {
49
40
  continue;
50
41
  }
51
42
  await models.template.create({
52
- code,
53
- type,
54
- name,
55
- content,
56
- level: 0
43
+ code, type, name, content, level: 0
57
44
  });
58
45
  console.log(`create template: ${code}`);
59
46
  }
@@ -76,9 +63,7 @@ module.exports = fp(async (fastify, options) => {
76
63
  const messageTemplate = async ({ code, type = 0, level = 0, props }) => {
77
64
  const codeTemplate = await models.template.findOne({
78
65
  where: {
79
- code,
80
- type,
81
- level
66
+ code, type, level
82
67
  }
83
68
  });
84
69
  if (!codeTemplate) {
@@ -88,11 +73,7 @@ module.exports = fp(async (fastify, options) => {
88
73
  const content = parseTemplate(template(codeTemplate.content)(props).split(/<!--(\s*)-->/g));
89
74
 
90
75
  return {
91
- content,
92
- props,
93
- code,
94
- type,
95
- templateId: codeTemplate.id
76
+ content, props, code, type, templateId: codeTemplate.id
96
77
  };
97
78
  };
98
79
 
@@ -100,22 +81,8 @@ module.exports = fp(async (fastify, options) => {
100
81
  targetOptions = Object.assign({}, targetOptions);
101
82
  const { content, templateId } = await messageTemplate({ code, type, level, props });
102
83
  const sendOptions = await (async () => {
84
+ const currentSender = options.senders?.[type];
103
85
  if (type === 0) {
104
- const currentClient = merge(
105
- {},
106
- {
107
- host: emailConfig.host,
108
- port: emailConfig.port,
109
- secure: emailConfig.secure,
110
- auth: {
111
- user: emailConfig.user,
112
- pass: emailConfig.pass
113
- }
114
- },
115
- client
116
- );
117
- const smtp = nodemailer.createTransport(currentClient);
118
-
119
86
  const mailOptions = {
120
87
  ...targetOptions,
121
88
  from: `"${targetOptions.title || emailConfig.user}" <${emailConfig.user}>`,
@@ -127,11 +94,20 @@ module.exports = fp(async (fastify, options) => {
127
94
  };
128
95
 
129
96
  if (!isTest) {
130
- await pify(smtp.sendMail.bind(smtp))(mailOptions);
97
+ if (typeof currentSender === 'function') {
98
+ await currentSender(mailOptions);
99
+ } else {
100
+ const currentClient = merge({}, {
101
+ host: emailConfig.host, port: emailConfig.port, secure: emailConfig.secure, auth: {
102
+ user: emailConfig.user, pass: emailConfig.pass
103
+ }
104
+ }, client);
105
+ const smtp = nodemailer.createTransport(currentClient);
106
+ await pify(smtp.sendMail.bind(smtp))(mailOptions);
107
+ }
131
108
  }
132
109
  return mailOptions;
133
110
  }
134
- const currentSender = options.senders?.[type];
135
111
  if (typeof currentSender === 'function') {
136
112
  const { content, templateId } = await messageTemplate({ code, type, level, props });
137
113
  if (!isTest) {
@@ -145,9 +121,6 @@ module.exports = fp(async (fastify, options) => {
145
121
  };
146
122
 
147
123
  Object.assign(fastify[options.name].services, {
148
- includeTemplate,
149
- messageTemplate,
150
- parseTemplate,
151
- sendMessage
124
+ includeTemplate, messageTemplate, parseTemplate, sendMessage
152
125
  });
153
126
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-message",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "管理消息发送,支持sms消息,email消息等.",
5
5
  "main": "index.js",
6
6
  "scripts": {