UNPKG

4.16 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8var _lodash = _interopRequireDefault(require("lodash"));
9
10function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
12function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
13
14function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
15
16function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
17
18function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
19
20function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
21
22var views = require('koa-views');
23
24var path = require("path");
25
26var juice = require('juice');
27
28class Mail {
29 constructor() {
30 var args = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
31 var nunjucksOptions = {
32 map: {
33 html: 'nunjucks'
34 },
35 options: {
36 ext: spice.config.spice.view.ext || '.html',
37 noCache: true,
38 throwOnUndefined: false,
39 globals: spice.config.spice.view.globals,
40 filters: spice.config.spice.view.filters
41 }
42 };
43
44 if (spice.config.spice.view.custom_tags.enabled == true) {
45 nunjucksOptions.options.tags = {
46 variableStart: spice.config.spice.view.custom_tags.start || '${',
47 variableEnd: spice.config.spice.view.custom_tags.end || '}'
48 };
49 }
50
51 this.driver_name = args.driver_name || spice.config.mail.default_driver;
52 this.driver_options = _lodash.default.merge(spice.config.mail.drivers[this.driver_name], args.driver_options);
53 this.template = args.template;
54 this.Driver = spice.config.mail.providers[this.driver_name];
55 }
56
57 prepare(data) {
58 var _this = this;
59
60 return _asyncToGenerator(function* () {
61 //apply template here
62 var obj = {};
63 views(path.resolve('./src/views'), _this.nunjucksOptions)(obj, () => {});
64 yield obj.render(_this.template, data);
65 return obj.body;
66 })();
67 }
68
69 send(_ref) {
70 var _this2 = this;
71
72 var {
73 data,
74 options,
75 template,
76 content,
77 driver_options
78 } = _ref;
79 return _asyncToGenerator(function* () {
80 if (template) {
81 _this2.template = template;
82 }
83
84 if (driver_options) {
85 _this2.driver_options = _lodash.default.merge(_this2.driver_options, driver_options);
86 }
87
88 var sendable_text = "";
89
90 if (_this2.template) {
91 sendable_text = yield _this2.prepare(_objectSpread({}, data, {}, _this2));
92 }
93
94 if (content) {
95 sendable_text = content;
96 }
97
98 var driver = new _this2.Driver(_this2.driver_options);
99 return yield driver.send(juice(sendable_text), options);
100 })();
101 }
102
103}
104
105exports.default = Mail;
\No newline at end of file