@flink-app/email-plugin 0.3.0 → 0.3.6

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) Frost Experience AB https://www.frost.se
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,17 @@
1
+ import { emailFlowmailer as email } from "./schemas/emailFlowmailer";
2
+ import { client } from "./schemas/client";
3
+ export interface flowmailerClientOptions {
4
+ client_id: string;
5
+ client_secrect: string;
6
+ account_id: string;
7
+ }
8
+ export declare class flowMailerClient implements client {
9
+ accessToken: string;
10
+ client_id: string;
11
+ client_secrect: string;
12
+ account_id: string;
13
+ constructor(options: flowmailerClientOptions);
14
+ getToken(): Promise<void>;
15
+ sendEmail(email: email, to: string): Promise<boolean>;
16
+ send(email: email): Promise<boolean>;
17
+ }
@@ -0,0 +1,166 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ 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;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ var __importDefault = (this && this.__importDefault) || function (mod) {
39
+ return (mod && mod.__esModule) ? mod : { "default": mod };
40
+ };
41
+ Object.defineProperty(exports, "__esModule", { value: true });
42
+ exports.flowMailerClient = void 0;
43
+ var axios_1 = __importDefault(require("axios"));
44
+ var querystring_1 = __importDefault(require("querystring"));
45
+ var flowMailerClient = /** @class */ (function () {
46
+ function flowMailerClient(options) {
47
+ this.accessToken = "";
48
+ this.client_id = options.client_id;
49
+ this.client_secrect = options.client_secrect;
50
+ this.account_id = options.account_id;
51
+ }
52
+ flowMailerClient.prototype.getToken = function () {
53
+ return __awaiter(this, void 0, void 0, function () {
54
+ var resp, ex_1;
55
+ return __generator(this, function (_a) {
56
+ switch (_a.label) {
57
+ case 0:
58
+ _a.trys.push([0, 2, , 3]);
59
+ return [4 /*yield*/, axios_1.default.post("https://login.flowmailer.net/oauth/token", querystring_1.default.stringify({
60
+ client_id: this.client_id,
61
+ client_secret: this.client_secrect,
62
+ grant_type: "client_credentials",
63
+ scope: "api",
64
+ }), {
65
+ headers: {
66
+ "Content-Type": "application/x-www-form-urlencoded",
67
+ },
68
+ })];
69
+ case 1:
70
+ resp = _a.sent();
71
+ if (resp.status == 200) {
72
+ this.accessToken = resp.data.access_token;
73
+ }
74
+ return [3 /*break*/, 3];
75
+ case 2:
76
+ ex_1 = _a.sent();
77
+ return [3 /*break*/, 3];
78
+ case 3: return [2 /*return*/];
79
+ }
80
+ });
81
+ });
82
+ };
83
+ flowMailerClient.prototype.sendEmail = function (email, to) {
84
+ var _a, _b, _c;
85
+ return __awaiter(this, void 0, void 0, function () {
86
+ var data, resp, ex_2;
87
+ return __generator(this, function (_d) {
88
+ switch (_d.label) {
89
+ case 0:
90
+ data = {
91
+ messageType: "EMAIL",
92
+ senderAddress: email.from,
93
+ recipientAddress: to,
94
+ subject: email.subject,
95
+ text: email.text,
96
+ html: email.html,
97
+ };
98
+ _d.label = 1;
99
+ case 1:
100
+ _d.trys.push([1, 3, , 4]);
101
+ return [4 /*yield*/, axios_1.default.post("https://api.flowmailer.net/" + this.account_id + "/messages/submit", data, {
102
+ headers: {
103
+ Accept: "application/vnd.flowmailer.v1.12+json",
104
+ "Content-Type": "application/vnd.flowmailer.v1.12+json;charset=UTF-8",
105
+ Authorization: "Bearer " + this.accessToken,
106
+ },
107
+ })];
108
+ case 2:
109
+ resp = _d.sent();
110
+ if (resp.status == 201)
111
+ return [2 /*return*/, true];
112
+ return [3 /*break*/, 4];
113
+ case 3:
114
+ ex_2 = _d.sent();
115
+ if (((_c = (_b = (_a = ex_2) === null || _a === void 0 ? void 0 : _a.response) === null || _b === void 0 ? void 0 : _b.data) === null || _c === void 0 ? void 0 : _c.error) != "invalid_token") {
116
+ }
117
+ return [3 /*break*/, 4];
118
+ case 4: return [2 /*return*/, false];
119
+ }
120
+ });
121
+ });
122
+ };
123
+ flowMailerClient.prototype.send = function (email) {
124
+ return __awaiter(this, void 0, void 0, function () {
125
+ var failure, _a, _b, _i, i, success;
126
+ return __generator(this, function (_c) {
127
+ switch (_c.label) {
128
+ case 0:
129
+ failure = false;
130
+ _a = [];
131
+ for (_b in email.to)
132
+ _a.push(_b);
133
+ _i = 0;
134
+ _c.label = 1;
135
+ case 1:
136
+ if (!(_i < _a.length)) return [3 /*break*/, 8];
137
+ i = _a[_i];
138
+ if (!this.client_secrect) {
139
+ this.getToken();
140
+ }
141
+ return [4 /*yield*/, this.sendEmail(email, email.to[i])];
142
+ case 2:
143
+ success = _c.sent();
144
+ if (!!success) return [3 /*break*/, 4];
145
+ return [4 /*yield*/, this.getToken()];
146
+ case 3:
147
+ _c.sent();
148
+ return [3 /*break*/, 5];
149
+ case 4: return [3 /*break*/, 7];
150
+ case 5: return [4 /*yield*/, this.sendEmail(email, email.to[i])];
151
+ case 6:
152
+ success = _c.sent();
153
+ if (!success)
154
+ failure = true;
155
+ _c.label = 7;
156
+ case 7:
157
+ _i++;
158
+ return [3 /*break*/, 1];
159
+ case 8: return [2 /*return*/, !failure];
160
+ }
161
+ });
162
+ });
163
+ };
164
+ return flowMailerClient;
165
+ }());
166
+ exports.flowMailerClient = flowMailerClient;
package/dist/index.d.ts CHANGED
@@ -2,6 +2,7 @@ import { FlinkPlugin } from "@flink-app/flink";
2
2
  import { client } from "./schemas/client";
3
3
  export { sendgridClient } from "./sendgridClient";
4
4
  export { smtpClient } from "./smtpClient";
5
+ export { flowMailerClient } from "./flowmailerClient";
5
6
  export type { email } from "./schemas/email";
6
7
  export declare type emailPluginOptions = {
7
8
  /**
package/dist/index.js CHANGED
@@ -1,10 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.emailPlugin = exports.smtpClient = exports.sendgridClient = void 0;
3
+ exports.emailPlugin = exports.flowMailerClient = exports.smtpClient = exports.sendgridClient = void 0;
4
4
  var sendgridClient_1 = require("./sendgridClient");
5
5
  Object.defineProperty(exports, "sendgridClient", { enumerable: true, get: function () { return sendgridClient_1.sendgridClient; } });
6
6
  var smtpClient_1 = require("./smtpClient");
7
7
  Object.defineProperty(exports, "smtpClient", { enumerable: true, get: function () { return smtpClient_1.smtpClient; } });
8
+ var flowmailerClient_1 = require("./flowmailerClient");
9
+ Object.defineProperty(exports, "flowMailerClient", { enumerable: true, get: function () { return flowmailerClient_1.flowMailerClient; } });
8
10
  var emailPlugin = function (options) {
9
11
  return {
10
12
  id: "emailPlugin",
@@ -1,5 +1,6 @@
1
1
  import { email } from "./email";
2
+ import { emailFlowmailer } from "./emailFlowmailer";
2
3
  import { emailSendgrid } from "./emailSendgrid";
3
4
  export interface client {
4
- send(email: email | emailSendgrid): Promise<boolean>;
5
+ send(email: email | emailSendgrid | emailFlowmailer): Promise<boolean>;
5
6
  }
@@ -0,0 +1,24 @@
1
+ export declare type emailFlowmailer = {
2
+ /**
3
+ * From address used to send the email
4
+ */
5
+ from: string;
6
+ /**
7
+ * Email addresses to send to
8
+ */
9
+ to: string[];
10
+ /**
11
+ * Reply email
12
+ */
13
+ replyTo?: string;
14
+ /**
15
+ * Email addresses to add as BCC
16
+ */
17
+ bcc?: string[];
18
+ /**
19
+ * Subject of email
20
+ */
21
+ subject: string;
22
+ text?: string;
23
+ html?: string;
24
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flink-app/email-plugin",
3
- "version": "0.3.0",
3
+ "version": "0.3.6",
4
4
  "description": "Flink plugin that makes it possible to send email",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\"",
@@ -16,7 +16,9 @@
16
16
  "dependencies": {
17
17
  "@sendgrid/mail": "^7.4.5",
18
18
  "@types/nodemailer": "^6.4.4",
19
- "nodemailer": "^6.6.2"
19
+ "axios": "^0.27.2",
20
+ "nodemailer": "^6.6.2",
21
+ "querystring": "^0.2.1"
20
22
  },
21
23
  "devDependencies": {
22
24
  "@flink-app/flink": "^0.3.0",
@@ -24,5 +26,5 @@
24
26
  "ts-node": "^9.1.1",
25
27
  "typescript": "^4.2.4"
26
28
  },
27
- "gitHead": "21b4a23ed4d3eea816c38271bd37b5aa9d91cd5b"
29
+ "gitHead": "b58e0bd688be669532e4f0eda3d8b14b34266a28"
28
30
  }
@@ -0,0 +1,97 @@
1
+ import { emailFlowmailer as email } from "./schemas/emailFlowmailer";
2
+ import { client } from "./schemas/client";
3
+ import axios from "axios";
4
+ import querystring from "querystring";
5
+
6
+ export interface flowmailerClientOptions {
7
+ client_id: string;
8
+ client_secrect: string;
9
+ account_id: string;
10
+ }
11
+
12
+ type SubmitMessage = {
13
+ messageType: "EMAIL";
14
+ senderAddress: string;
15
+ recipientAddress: string;
16
+ subject: string;
17
+ text?: string;
18
+ html?: string;
19
+ };
20
+
21
+ export class flowMailerClient implements client {
22
+ accessToken: string;
23
+ client_id: string;
24
+ client_secrect: string;
25
+ account_id: string;
26
+
27
+ constructor(options: flowmailerClientOptions) {
28
+ this.accessToken = "";
29
+ this.client_id = options.client_id;
30
+ this.client_secrect = options.client_secrect;
31
+ this.account_id = options.account_id;
32
+ }
33
+
34
+ async getToken() {
35
+ try {
36
+ const resp = await axios.post(
37
+ "https://login.flowmailer.net/oauth/token",
38
+ querystring.stringify({
39
+ client_id: this.client_id,
40
+ client_secret: this.client_secrect,
41
+ grant_type: "client_credentials",
42
+ scope: "api",
43
+ }),
44
+ {
45
+ headers: {
46
+ "Content-Type": "application/x-www-form-urlencoded",
47
+ },
48
+ }
49
+ );
50
+ if (resp.status == 200) {
51
+ this.accessToken = resp.data.access_token;
52
+ }
53
+ } catch (ex) {}
54
+ }
55
+ async sendEmail(email: email, to: string): Promise<boolean> {
56
+ const data = {
57
+ messageType: "EMAIL",
58
+ senderAddress: email.from,
59
+ recipientAddress: to,
60
+ subject: email.subject,
61
+ text: email.text,
62
+ html: email.html,
63
+ };
64
+ try {
65
+ const resp = await axios.post(`https://api.flowmailer.net/${this.account_id}/messages/submit`, data, {
66
+ headers: {
67
+ Accept: "application/vnd.flowmailer.v1.12+json",
68
+ "Content-Type": "application/vnd.flowmailer.v1.12+json;charset=UTF-8",
69
+ Authorization: `Bearer ${this.accessToken}`,
70
+ },
71
+ });
72
+ if (resp.status == 201) return true;
73
+ } catch (ex) {
74
+ if ((ex as any)?.response?.data?.error != "invalid_token") {
75
+
76
+ }
77
+ }
78
+ return false;
79
+ }
80
+ async send(email: email) {
81
+ let failure = false;
82
+ for (const i in email.to) {
83
+ if (!this.client_secrect) {
84
+ this.getToken();
85
+ }
86
+ let success = await this.sendEmail(email, email.to[i]);
87
+ if (!success) {
88
+ await this.getToken();
89
+ } else {
90
+ continue;
91
+ }
92
+ success = await this.sendEmail(email, email.to[i]);
93
+ if (!success) failure = true;
94
+ }
95
+ return !failure;
96
+ }
97
+ }
package/src/index.ts CHANGED
@@ -2,6 +2,7 @@ import { FlinkApp, FlinkPlugin } from "@flink-app/flink";
2
2
  import { client } from "./schemas/client";
3
3
  export { sendgridClient } from "./sendgridClient";
4
4
  export { smtpClient } from "./smtpClient";
5
+ export { flowMailerClient } from "./flowmailerClient"
5
6
  export type { email } from "./schemas/email";
6
7
 
7
8
  export type emailPluginOptions = {
@@ -1,6 +1,7 @@
1
1
  import { email } from "./email";
2
+ import { emailFlowmailer } from "./emailFlowmailer";
2
3
  import { emailSendgrid } from "./emailSendgrid";
3
4
 
4
5
  export interface client {
5
- send(email: email | emailSendgrid): Promise<boolean>;
6
+ send(email: email | emailSendgrid | emailFlowmailer): Promise<boolean>;
6
7
  }
@@ -0,0 +1,34 @@
1
+ import { Readable } from "stream";
2
+ import { Url } from "url";
3
+
4
+ export type emailFlowmailer = {
5
+ /**
6
+ * From address used to send the email
7
+ */
8
+ from: string;
9
+
10
+ /**
11
+ * Email addresses to send to
12
+ */
13
+ to: string[];
14
+
15
+ /**
16
+ * Reply email
17
+ */
18
+ replyTo?: string;
19
+
20
+ /**
21
+ * Email addresses to add as BCC
22
+ */
23
+ bcc?: string[];
24
+
25
+ /**
26
+ * Subject of email
27
+ */
28
+ subject: string;
29
+
30
+ text? : string
31
+
32
+ html? : string
33
+
34
+ };