@kne/fastify-message 1.0.1 → 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.
package/index.js CHANGED
@@ -12,7 +12,8 @@ module.exports = fp(async function (fastify, options) {
12
12
  throw new Error('fastify-account plugin must be registered before fastify-trtc-conference,or set options.getUserModel');
13
13
  }
14
14
  return fastify.account.models.user;
15
- }
15
+ },
16
+ senders: {}
16
17
  },
17
18
  options
18
19
  );
@@ -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,56 +73,54 @@ 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
 
99
80
  const sendMessage = async ({ type = 0, name, props, code, level = 0, client, options: targetOptions }) => {
100
81
  targetOptions = Object.assign({}, targetOptions);
101
- if (type === 0) {
102
- const currentClient = merge(
103
- {},
104
- {
105
- host: emailConfig.host,
106
- port: emailConfig.port,
107
- secure: emailConfig.secure,
108
- auth: {
109
- user: emailConfig.user,
110
- pass: emailConfig.pass
111
- }
112
- },
113
- client
114
- );
115
- const smtp = nodemailer.createTransport(currentClient);
116
-
117
- const { content, templateId } = await messageTemplate({ code, type, level, props });
82
+ const { content, templateId } = await messageTemplate({ code, type, level, props });
83
+ const sendOptions = await (async () => {
84
+ const currentSender = options.senders?.[type];
85
+ if (type === 0) {
86
+ const mailOptions = {
87
+ ...targetOptions,
88
+ from: `"${targetOptions.title || emailConfig.user}" <${emailConfig.user}>`,
89
+ to: name,
90
+ subject: content.subject || options.subject || emailConfig.defaultSubject || 'Message reminder',
91
+ text: content.text || convert(content.html),
92
+ html: content.html,
93
+ attachments: options.attachments || []
94
+ };
118
95
 
119
- const mailOptions = {
120
- ...targetOptions,
121
- from: `"${targetOptions.title || emailConfig.user}" <${emailConfig.user}>`,
122
- to: name,
123
- subject: content.subject || options.subject || emailConfig.defaultSubject || 'Message reminder',
124
- text: content.text || convert(content.html),
125
- html: content.html,
126
- attachments: options.attachments || []
127
- };
128
-
129
- if (!isTest) {
130
- await pify(smtp.sendMail.bind(smtp))(mailOptions);
96
+ if (!isTest) {
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
+ }
108
+ }
109
+ return mailOptions;
131
110
  }
111
+ if (typeof currentSender === 'function') {
112
+ const { content, templateId } = await messageTemplate({ code, type, level, props });
113
+ if (!isTest) {
114
+ return await currentSender({ code, templateId, content, props, name, type, level, options: targetOptions });
115
+ }
132
116
 
133
- await models.record.create({ type, code, templateId, props, name, content: mailOptions });
134
- }
117
+ return { content, props, level, options: targetOptions };
118
+ }
119
+ })(type);
120
+ await models.record.create({ type, code, templateId, props, name, content: sendOptions });
135
121
  };
136
122
 
137
123
  Object.assign(fastify[options.name].services, {
138
- includeTemplate,
139
- messageTemplate,
140
- parseTemplate,
141
- sendMessage
124
+ includeTemplate, messageTemplate, parseTemplate, sendMessage
142
125
  });
143
126
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-message",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "管理消息发送,支持sms消息,email消息等.",
5
5
  "main": "index.js",
6
6
  "scripts": {