@creator.co/wapi 1.0.13 → 1.1.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/dist/index.d.ts +2 -1
- package/dist/index.js +3 -5
- package/dist/index.js.map +1 -1
- package/dist/src/Mailer/Mailer.d.ts +9 -0
- package/dist/src/Mailer/Mailer.js +166 -0
- package/dist/src/Mailer/Mailer.js.map +1 -0
- package/index.ts +3 -5
- package/package.json +7 -2
- package/src/Mailer/Mailer.ts +96 -0
package/dist/index.d.ts
CHANGED
|
@@ -3,5 +3,6 @@ import Utils from "./src/API/Utils";
|
|
|
3
3
|
import EventProcessor from "./src/BaseEvent/EventProcessor";
|
|
4
4
|
import Process from "./src/BaseEvent/Process";
|
|
5
5
|
import Transaction from "./src/BaseEvent/Transaction";
|
|
6
|
+
import Mailer from "./src/Mailer/Mailer";
|
|
6
7
|
import Publisher from "./src/Publisher/Publisher";
|
|
7
|
-
export { Transaction, Process, EventProcessor, Publisher, Response, Utils, };
|
|
8
|
+
export { Transaction, Process, EventProcessor, Publisher, Mailer, Response, Utils, };
|
package/dist/index.js
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Utils = exports.Response = exports.Publisher = exports.EventProcessor = exports.Process = exports.Transaction = void 0;
|
|
4
|
-
// Base Events
|
|
5
|
-
//
|
|
3
|
+
exports.Utils = exports.Response = exports.Mailer = exports.Publisher = exports.EventProcessor = exports.Process = exports.Transaction = void 0;
|
|
6
4
|
var Response_1 = require("./src/API/Response");
|
|
7
5
|
exports.Response = Response_1.default;
|
|
8
|
-
//Helpers
|
|
9
6
|
var Utils_1 = require("./src/API/Utils");
|
|
10
7
|
exports.Utils = Utils_1.default;
|
|
11
8
|
var EventProcessor_1 = require("./src/BaseEvent/EventProcessor");
|
|
@@ -14,7 +11,8 @@ var Process_1 = require("./src/BaseEvent/Process");
|
|
|
14
11
|
exports.Process = Process_1.default;
|
|
15
12
|
var Transaction_1 = require("./src/BaseEvent/Transaction");
|
|
16
13
|
exports.Transaction = Transaction_1.default;
|
|
17
|
-
|
|
14
|
+
var Mailer_1 = require("./src/Mailer/Mailer");
|
|
15
|
+
exports.Mailer = Mailer_1.default;
|
|
18
16
|
var Publisher_1 = require("./src/Publisher/Publisher");
|
|
19
17
|
exports.Publisher = Publisher_1.default;
|
|
20
18
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;AAAA
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;AAAA,+CAAyC;AAiBvC,mBAjBK,kBAAQ,CAiBL;AAhBV,yCAAmC;AAkBjC,gBAlBK,eAAK,CAkBL;AAjBP,iEAA2D;AAUzD,yBAVK,wBAAc,CAUL;AAThB,mDAA6C;AAQ3C,kBARK,iBAAO,CAQL;AAPT,2DAAqD;AAMnD,sBANK,qBAAW,CAML;AALb,8CAAwC;AAUtC,iBAVK,gBAAM,CAUL;AATR,uDAAiD;AAQ/C,oBARK,mBAAS,CAQL"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export default class Mailer {
|
|
2
|
+
private readonly from;
|
|
3
|
+
private readonly templateDefaultFile;
|
|
4
|
+
private readonly transporter;
|
|
5
|
+
constructor(defaultFrom: string, region: string);
|
|
6
|
+
sendTemplatedEmail(to: string | Array<string>, templates: string | Array<string>, data: object, optionalCC?: string | Array<string>, optionalAttachments?: any[]): Promise<any>;
|
|
7
|
+
private chooseTemplate;
|
|
8
|
+
private canRenderTemplate;
|
|
9
|
+
}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
24
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
+
function step(op) {
|
|
27
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
+
switch (op[0]) {
|
|
32
|
+
case 0: case 1: t = op; break;
|
|
33
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
+
default:
|
|
37
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
+
if (t[2]) _.ops.pop();
|
|
42
|
+
_.trys.pop(); continue;
|
|
43
|
+
}
|
|
44
|
+
op = body.call(thisArg, _);
|
|
45
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
|
+
var SES = require("@aws-sdk/client-ses");
|
|
51
|
+
var credential_provider_node_1 = require("@aws-sdk/credential-provider-node");
|
|
52
|
+
var Email = require("email-templates");
|
|
53
|
+
var nodemailer = require("nodemailer");
|
|
54
|
+
var Mailer = /** @class */ (function () {
|
|
55
|
+
function Mailer(defaultFrom, region) {
|
|
56
|
+
this.templateDefaultFile = "html";
|
|
57
|
+
this.from = defaultFrom;
|
|
58
|
+
this.transporter = nodemailer.createTransport({
|
|
59
|
+
SES: {
|
|
60
|
+
ses: new SES.SESClient({
|
|
61
|
+
credentials: (0, credential_provider_node_1.defaultProvider)(),
|
|
62
|
+
apiVersion: "2010-12-01",
|
|
63
|
+
region: region,
|
|
64
|
+
}),
|
|
65
|
+
aws: SES,
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
//to can be an array or just one address
|
|
70
|
+
Mailer.prototype.sendTemplatedEmail = function (to, templates, data, optionalCC,
|
|
71
|
+
// TODO: improve attachment type -> Attachment
|
|
72
|
+
optionalAttachments) {
|
|
73
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
74
|
+
var email, resp, chosenTemplate, e_1;
|
|
75
|
+
return __generator(this, function (_a) {
|
|
76
|
+
switch (_a.label) {
|
|
77
|
+
case 0:
|
|
78
|
+
email = new Email({
|
|
79
|
+
message: __assign(__assign({ from: this.from, to: to }, (optionalAttachments ? { attachments: optionalAttachments } : {})), (optionalCC ? { cc: optionalCC } : {})),
|
|
80
|
+
transport: this.transporter,
|
|
81
|
+
send: true,
|
|
82
|
+
});
|
|
83
|
+
resp = null;
|
|
84
|
+
_a.label = 1;
|
|
85
|
+
case 1:
|
|
86
|
+
_a.trys.push([1, 4, , 5]);
|
|
87
|
+
return [4 /*yield*/, this.chooseTemplate(templates, data)];
|
|
88
|
+
case 2:
|
|
89
|
+
chosenTemplate = _a.sent();
|
|
90
|
+
return [4 /*yield*/, email.send({ template: chosenTemplate, locals: data })];
|
|
91
|
+
case 3:
|
|
92
|
+
resp = _a.sent();
|
|
93
|
+
console.debug("Mailer resp:", resp);
|
|
94
|
+
return [3 /*break*/, 5];
|
|
95
|
+
case 4:
|
|
96
|
+
e_1 = _a.sent();
|
|
97
|
+
console.error("Mailer error:", e_1);
|
|
98
|
+
return [3 /*break*/, 5];
|
|
99
|
+
case 5: return [2 /*return*/, resp];
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
};
|
|
104
|
+
/* private */
|
|
105
|
+
Mailer.prototype.chooseTemplate = function (templates, data) {
|
|
106
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
107
|
+
var _i, templates_1, template;
|
|
108
|
+
return __generator(this, function (_a) {
|
|
109
|
+
switch (_a.label) {
|
|
110
|
+
case 0:
|
|
111
|
+
if (!Array.isArray(templates)) return [3 /*break*/, 5];
|
|
112
|
+
_i = 0, templates_1 = templates;
|
|
113
|
+
_a.label = 1;
|
|
114
|
+
case 1:
|
|
115
|
+
if (!(_i < templates_1.length)) return [3 /*break*/, 4];
|
|
116
|
+
template = templates_1[_i];
|
|
117
|
+
return [4 /*yield*/, this.canRenderTemplate(template, data)];
|
|
118
|
+
case 2:
|
|
119
|
+
if (_a.sent())
|
|
120
|
+
return [2 /*return*/, template];
|
|
121
|
+
_a.label = 3;
|
|
122
|
+
case 3:
|
|
123
|
+
_i++;
|
|
124
|
+
return [3 /*break*/, 1];
|
|
125
|
+
case 4: return [3 /*break*/, 7];
|
|
126
|
+
case 5:
|
|
127
|
+
if (!templates) return [3 /*break*/, 7];
|
|
128
|
+
return [4 /*yield*/, this.canRenderTemplate(templates, data)];
|
|
129
|
+
case 6:
|
|
130
|
+
if (_a.sent())
|
|
131
|
+
return [2 /*return*/, templates];
|
|
132
|
+
_a.label = 7;
|
|
133
|
+
case 7: throw new Error("Could not render email with template ".concat(templates, " and following data. Please, check logs above! ") +
|
|
134
|
+
JSON.stringify(data, null, 2));
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
};
|
|
139
|
+
Mailer.prototype.canRenderTemplate = function (template, data) {
|
|
140
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
141
|
+
var validRenderResp, email, e_2;
|
|
142
|
+
return __generator(this, function (_a) {
|
|
143
|
+
switch (_a.label) {
|
|
144
|
+
case 0:
|
|
145
|
+
validRenderResp = null;
|
|
146
|
+
_a.label = 1;
|
|
147
|
+
case 1:
|
|
148
|
+
_a.trys.push([1, 3, , 4]);
|
|
149
|
+
email = new Email();
|
|
150
|
+
return [4 /*yield*/, email.render("".concat(template, "/").concat(this.templateDefaultFile), data)];
|
|
151
|
+
case 2:
|
|
152
|
+
validRenderResp = _a.sent();
|
|
153
|
+
return [3 /*break*/, 4];
|
|
154
|
+
case 3:
|
|
155
|
+
e_2 = _a.sent();
|
|
156
|
+
console.log("Error while checking renderability of email template ".concat(template), e_2);
|
|
157
|
+
return [3 /*break*/, 4];
|
|
158
|
+
case 4: return [2 /*return*/, validRenderResp != null];
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
};
|
|
163
|
+
return Mailer;
|
|
164
|
+
}());
|
|
165
|
+
exports.default = Mailer;
|
|
166
|
+
//# sourceMappingURL=Mailer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Mailer.js","sourceRoot":"","sources":["../../../src/Mailer/Mailer.ts"],"names":[],"mappings":"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"}
|
package/index.ts
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
// Base Events
|
|
2
|
-
//
|
|
3
1
|
import Response from "./src/API/Response"
|
|
4
|
-
//Helpers
|
|
5
2
|
import Utils from "./src/API/Utils"
|
|
6
3
|
import EventProcessor from "./src/BaseEvent/EventProcessor"
|
|
7
4
|
import Process from "./src/BaseEvent/Process"
|
|
8
5
|
import Transaction from "./src/BaseEvent/Transaction"
|
|
9
|
-
|
|
6
|
+
import Mailer from "./src/Mailer/Mailer"
|
|
10
7
|
import Publisher from "./src/Publisher/Publisher"
|
|
11
|
-
|
|
8
|
+
|
|
12
9
|
export {
|
|
13
10
|
//Base Events
|
|
14
11
|
Transaction,
|
|
@@ -16,6 +13,7 @@ export {
|
|
|
16
13
|
EventProcessor,
|
|
17
14
|
//Client
|
|
18
15
|
Publisher,
|
|
16
|
+
Mailer,
|
|
19
17
|
//Responses
|
|
20
18
|
Response,
|
|
21
19
|
//Helpers
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@creator.co/wapi",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -14,19 +14,24 @@
|
|
|
14
14
|
"author": "",
|
|
15
15
|
"license": "ISC",
|
|
16
16
|
"dependencies": {
|
|
17
|
+
"@aws-sdk/client-ses": "^3.414.0",
|
|
17
18
|
"@aws-sdk/client-sns": "^3.398.0",
|
|
19
|
+
"@aws-sdk/credential-provider-node": "^3.414.0",
|
|
20
|
+
"@types/email-templates": "^10.0.1",
|
|
21
|
+
"@types/nodemailer": "^6.4.10",
|
|
18
22
|
"abind": "^1.0.5",
|
|
23
|
+
"email-templates": "^11.1.1",
|
|
19
24
|
"json-stringify-safe": "^5.0.1",
|
|
20
25
|
"sha1": "^1.1.1",
|
|
21
26
|
"stack-trace": "0.0.10"
|
|
22
27
|
},
|
|
23
28
|
"devDependencies": {
|
|
24
29
|
"@types/aws-lambda": "^8.10.119",
|
|
25
|
-
"eslint-config-prettier": "^9.0.0",
|
|
26
30
|
"@types/node": "^20.5.7",
|
|
27
31
|
"@typescript-eslint/eslint-plugin": "^6.5.0",
|
|
28
32
|
"@typescript-eslint/parser": "^6.5.0",
|
|
29
33
|
"eslint": "^8.48.0",
|
|
34
|
+
"eslint-config-prettier": "^9.0.0",
|
|
30
35
|
"eslint-plugin-import": "^2.28.1",
|
|
31
36
|
"eslint-plugin-prettier": "^5.0.0",
|
|
32
37
|
"prettier": "^3.0.3"
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import * as SES from "@aws-sdk/client-ses"
|
|
2
|
+
import { defaultProvider } from "@aws-sdk/credential-provider-node"
|
|
3
|
+
import * as Email from "email-templates"
|
|
4
|
+
import * as nodemailer from "nodemailer"
|
|
5
|
+
import type * as SESTransport from "nodemailer/lib/ses-transport"
|
|
6
|
+
|
|
7
|
+
export default class Mailer {
|
|
8
|
+
private readonly from: string
|
|
9
|
+
private readonly templateDefaultFile: string = "html"
|
|
10
|
+
private readonly transporter: ReturnType<
|
|
11
|
+
typeof nodemailer.createTransport<SESTransport.SentMessageInfo>
|
|
12
|
+
>
|
|
13
|
+
constructor(defaultFrom: string, region: string) {
|
|
14
|
+
this.from = defaultFrom
|
|
15
|
+
this.transporter = nodemailer.createTransport({
|
|
16
|
+
SES: {
|
|
17
|
+
ses: new SES.SESClient({
|
|
18
|
+
credentials: defaultProvider(),
|
|
19
|
+
apiVersion: "2010-12-01",
|
|
20
|
+
region,
|
|
21
|
+
}),
|
|
22
|
+
aws: SES,
|
|
23
|
+
},
|
|
24
|
+
})
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
//to can be an array or just one address
|
|
28
|
+
async sendTemplatedEmail(
|
|
29
|
+
to: string | Array<string>,
|
|
30
|
+
templates: string | Array<string>,
|
|
31
|
+
data: object,
|
|
32
|
+
optionalCC?: string | Array<string>,
|
|
33
|
+
// TODO: improve attachment type -> Attachment
|
|
34
|
+
optionalAttachments?: any[],
|
|
35
|
+
) {
|
|
36
|
+
//Generate emails
|
|
37
|
+
const email = new Email({
|
|
38
|
+
message: {
|
|
39
|
+
from: this.from,
|
|
40
|
+
to: to,
|
|
41
|
+
...(optionalAttachments ? { attachments: optionalAttachments } : {}),
|
|
42
|
+
...(optionalCC ? { cc: optionalCC } : {}),
|
|
43
|
+
},
|
|
44
|
+
transport: this.transporter,
|
|
45
|
+
send: true,
|
|
46
|
+
})
|
|
47
|
+
//
|
|
48
|
+
let resp = null
|
|
49
|
+
try {
|
|
50
|
+
const chosenTemplate = await this.chooseTemplate(templates, data)
|
|
51
|
+
resp = await email.send({ template: chosenTemplate, locals: data })
|
|
52
|
+
console.debug("Mailer resp:", resp)
|
|
53
|
+
} catch (e) {
|
|
54
|
+
console.error("Mailer error:", e)
|
|
55
|
+
}
|
|
56
|
+
return resp
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/* private */
|
|
60
|
+
private async chooseTemplate(
|
|
61
|
+
templates: string | Array<string>,
|
|
62
|
+
data: object,
|
|
63
|
+
): Promise<string> {
|
|
64
|
+
if (Array.isArray(templates)) {
|
|
65
|
+
//For each template check if can render it
|
|
66
|
+
for (const template of templates) {
|
|
67
|
+
if (await this.canRenderTemplate(template, data)) return template
|
|
68
|
+
}
|
|
69
|
+
} else if (templates) {
|
|
70
|
+
if (await this.canRenderTemplate(templates, data)) return templates
|
|
71
|
+
}
|
|
72
|
+
throw new Error(
|
|
73
|
+
`Could not render email with template ${templates} and following data. Please, check logs above! ` +
|
|
74
|
+
JSON.stringify(data, null, 2),
|
|
75
|
+
)
|
|
76
|
+
}
|
|
77
|
+
private async canRenderTemplate(
|
|
78
|
+
template: string,
|
|
79
|
+
data: object,
|
|
80
|
+
): Promise<boolean> {
|
|
81
|
+
let validRenderResp = null
|
|
82
|
+
try {
|
|
83
|
+
const email = new Email()
|
|
84
|
+
validRenderResp = await email.render(
|
|
85
|
+
`${template}/${this.templateDefaultFile}`,
|
|
86
|
+
data,
|
|
87
|
+
)
|
|
88
|
+
} catch (e) {
|
|
89
|
+
console.log(
|
|
90
|
+
`Error while checking renderability of email template ${template}`,
|
|
91
|
+
e,
|
|
92
|
+
)
|
|
93
|
+
}
|
|
94
|
+
return validRenderResp != null
|
|
95
|
+
}
|
|
96
|
+
}
|