@kne/fastify-message 1.0.0-alpha.0 → 1.0.0-alpha.2

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
@@ -33,6 +33,7 @@ module.exports = fp(async function (fastify, options) {
33
33
  });
34
34
 
35
35
  fastify.register(async (fastify, options) => {
36
+ await fastify.sequelize.syncPromiseResolve;
36
37
  await fastify[options.name].services.includeTemplate(options.templateDir);
37
38
  }, options);
38
39
  });
@@ -11,8 +11,8 @@ module.exports = ({ DataTypes, options }) => {
11
11
  comment: '发送类型: 0:邮件'
12
12
  },
13
13
  code: {
14
- type: DataTypes.INTEGER,
15
- comment: '消息类型: 1:账号验证码'
14
+ type: DataTypes.STRING,
15
+ comment: '模版编码'
16
16
  },
17
17
  props: {
18
18
  type: DataTypes.JSON,
@@ -5,7 +5,7 @@ const template = require('lodash/template');
5
5
  const merge = require('lodash/merge');
6
6
  const nodemailer = require('nodemailer');
7
7
  const { convert } = require('html-to-text');
8
- const pify = require('pify');
8
+ const pify = require('pify').default;
9
9
 
10
10
  module.exports = fp(async (fastify, options) => {
11
11
  const emailConfig = Object.assign(
@@ -25,7 +25,7 @@ module.exports = fp(async (fastify, options) => {
25
25
  const list = await fs.readdir(dir);
26
26
  for (const file of list) {
27
27
  const filePath = path.join(dir, file);
28
- const content = fs.readFile(filePath, 'utf8');
28
+ const content = await fs.readFile(filePath, 'utf8');
29
29
  const filename = file.replace(path.extname(file), '');
30
30
  const tempArray = filename.split('_');
31
31
  const code = tempArray[0],
@@ -38,12 +38,14 @@ module.exports = fp(async (fastify, options) => {
38
38
  level: 0
39
39
  }
40
40
  });
41
+
41
42
  if (codeTemplate) {
42
43
  codeTemplate.content = content;
43
44
  codeTemplate.name = name;
44
45
  await codeTemplate.save();
45
46
  continue;
46
47
  }
48
+
47
49
  await models.template.create({
48
50
  code,
49
51
  type,
@@ -61,8 +63,7 @@ module.exports = fp(async (fastify, options) => {
61
63
 
62
64
  while ((match = regex.exec(text)) !== null) {
63
65
  const fieldName = match[1].trim();
64
- const fieldValue = match[2].trim();
65
- result[fieldName] = fieldValue.replace(/\n/g, '\\n');
66
+ result[fieldName] = match[2].trim();
66
67
  }
67
68
 
68
69
  return result;
@@ -91,7 +92,8 @@ module.exports = fp(async (fastify, options) => {
91
92
  };
92
93
  };
93
94
 
94
- const sendMessage = async ({ type = 0, name, props, code, level = 0, client, options }) => {
95
+ const sendMessage = async ({ type = 0, name, props, code, level = 0, client, options: targetOptions }) => {
96
+ targetOptions = Object.assign({}, targetOptions);
95
97
  if (type === 0) {
96
98
  const currentClient = merge(
97
99
  {},
@@ -108,19 +110,19 @@ module.exports = fp(async (fastify, options) => {
108
110
  );
109
111
  const smtp = nodemailer.createTransport(currentClient);
110
112
 
111
- const { content, type, templateId } = await messageTemplate({ code, type, level, props });
113
+ const { content, templateId } = await messageTemplate({ code, type, level, props });
112
114
 
113
115
  const mailOptions = {
114
- ...options,
115
- from: `"${options.title || emailConfig.user}" <${emailConfig.user}>`,
116
+ ...targetOptions,
117
+ from: `"${targetOptions.title || emailConfig.user}" <${emailConfig.user}>`,
116
118
  to: name,
117
119
  subject: content.subject || options.subject || emailConfig.defaultSubject || 'Message reminder',
118
- html: content.html,
119
120
  text: content.text || convert(content.html),
121
+ html: content.html,
120
122
  attachments: options.attachments || []
121
123
  };
122
124
 
123
- if (isTest) {
125
+ if (!isTest) {
124
126
  await pify(smtp.sendMail.bind(smtp))(mailOptions);
125
127
  }
126
128
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-message",
3
- "version": "1.0.0-alpha.0",
3
+ "version": "1.0.0-alpha.2",
4
4
  "description": "管理消息发送,支持sms消息,email消息等.",
5
5
  "main": "index.js",
6
6
  "scripts": {