@flink-app/sms-plugin 0.3.8

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;
@@ -0,0 +1,16 @@
1
+ import { FlinkPlugin } from "@flink-app/flink";
2
+ import { client } from "./schemas/client";
3
+ export type { sms } from "./schemas/sms";
4
+ export { sms46elksClient } from "./sms46elksClient";
5
+ export declare type smsPluginOptions = {
6
+ /**
7
+ * Path for request
8
+ */
9
+ client: client;
10
+ };
11
+ export interface smsPluginContext {
12
+ smsPlugin: {
13
+ client: client;
14
+ };
15
+ }
16
+ export declare const smsPlugin: (options: smsPluginOptions) => FlinkPlugin;
package/dist/index.js ADDED
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.smsPlugin = exports.sms46elksClient = void 0;
4
+ var sms46elksClient_1 = require("./sms46elksClient");
5
+ Object.defineProperty(exports, "sms46elksClient", { enumerable: true, get: function () { return sms46elksClient_1.sms46elksClient; } });
6
+ var smsPlugin = function (options) {
7
+ return {
8
+ id: "smsPlugin",
9
+ init: function (app) { return init(app, options); },
10
+ ctx: {
11
+ client: options.client,
12
+ },
13
+ };
14
+ };
15
+ exports.smsPlugin = smsPlugin;
16
+ function init(app, options) { }
@@ -0,0 +1,4 @@
1
+ import { sms } from "./sms";
2
+ export interface client {
3
+ send(sms: sms): Promise<boolean>;
4
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,90 @@
1
+ /// <reference types="node" />
2
+ import { Readable } from "stream";
3
+ import { Url } from "url";
4
+ export declare type email = {
5
+ /**
6
+ * From address used to send the email
7
+ */
8
+ from: string;
9
+ /**
10
+ * Email addresses to send to
11
+ */
12
+ to: string[];
13
+ /**
14
+ * Reply email
15
+ */
16
+ replyTo?: string;
17
+ /**
18
+ * Email addresses to add as BCC
19
+ */
20
+ bcc?: string[];
21
+ /**
22
+ * Subject of email
23
+ */
24
+ subject: string;
25
+ /**
26
+ * attached file object
27
+ */
28
+ attachments?: Attachment[];
29
+ } & ({
30
+ text: string;
31
+ } | {
32
+ html: string;
33
+ });
34
+ interface Attachment {
35
+ /**
36
+ * String, Buffer or a Stream contents for the attachment
37
+ */
38
+ content?: string | Buffer | Readable;
39
+ /**
40
+ * filename to be reported as the name of the attached file. Use of unicode is allowed
41
+ */
42
+ filename?: string | false;
43
+ /**
44
+ * path to the file if you want to stream the file instead of including it (better for larger attachments)
45
+ */
46
+ path?: string | Url;
47
+ /**
48
+ * an URL to the file (data uris are allowed as well)
49
+ */
50
+ href?: string;
51
+ /**
52
+ * optional HTTP headers to pass on with the href request, eg. {authorization: "bearer ..."}
53
+ */
54
+ httpHeaders?: string;
55
+ /**
56
+ * optional content type for the attachment, if not set will be derived from the filename property
57
+ */
58
+ contentType?: string;
59
+ /**
60
+ * optional content disposition type for the attachment, defaults to ‘attachment’
61
+ */
62
+ contentDisposition?: 'attachment' | 'inline';
63
+ /**
64
+ * optional content id for using inline images in HTML message source
65
+ */
66
+ cid?: string;
67
+ /**
68
+ * If set and content is string, then encodes the content to a Buffer using the specified encoding. Example values: ‘base64’, ‘hex’, ‘binary’ etc.
69
+ * Useful if you want to use binary attachments in a JSON formatted email object
70
+ */
71
+ encoding?: string;
72
+ /**
73
+ * custom headers for the attachment node. Same usage as with message headers
74
+ */
75
+ headers?: {
76
+ [key: string]: string | string[] | {
77
+ prepared: boolean;
78
+ value: string;
79
+ };
80
+ } | Array<{
81
+ key: string;
82
+ value: string;
83
+ }>;
84
+ /**
85
+ * is an optional special value that overrides entire contents of current mime node including mime headers.
86
+ * Useful if you want to prepare node contents yourself
87
+ */
88
+ raw?: string | Buffer;
89
+ }
90
+ export {};
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -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 });
@@ -0,0 +1,40 @@
1
+ export declare type emailSendgrid = {
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
+ /**
23
+ * attached file object
24
+ */
25
+ attachments?: Attachment[];
26
+ } & ({
27
+ text: string;
28
+ } | {
29
+ html: string;
30
+ });
31
+ interface Attachment {
32
+ /**
33
+ * base64 encoded string
34
+ */
35
+ content: string;
36
+ filename: string;
37
+ type?: string;
38
+ disposition?: string;
39
+ }
40
+ export {};
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,6 @@
1
+ export interface sendgridClientOptions {
2
+ /**
3
+ * API-key to use when sending email
4
+ */
5
+ apiKey: string;
6
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,5 @@
1
+ export interface sms {
2
+ from: string;
3
+ to: string[];
4
+ message: string;
5
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,9 @@
1
+ export interface smtpClientOptions {
2
+ host: string;
3
+ port: number;
4
+ secure: boolean;
5
+ auth?: {
6
+ user: string;
7
+ pass: string;
8
+ };
9
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,14 @@
1
+ import { MailService } from "@sendgrid/mail";
2
+ import { emailSendgrid as email } from "./schemas/emailSendgrid";
3
+ import { client } from "./schemas/client";
4
+ export interface sendgridClientOptions {
5
+ /**
6
+ * API-key to use when sending email
7
+ */
8
+ apiKey: string;
9
+ }
10
+ export declare class sendgridClient implements client {
11
+ sendgrid: MailService;
12
+ constructor(options: sendgridClientOptions);
13
+ send(email: email): Promise<boolean>;
14
+ }
@@ -0,0 +1,68 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.sendgridClient = void 0;
40
+ var mail_1 = require("@sendgrid/mail");
41
+ var sendgridClient = /** @class */ (function () {
42
+ function sendgridClient(options) {
43
+ this.sendgrid = new mail_1.MailService();
44
+ this.sendgrid.setApiKey(options.apiKey);
45
+ }
46
+ sendgridClient.prototype.send = function (email) {
47
+ return __awaiter(this, void 0, void 0, function () {
48
+ var ex_1;
49
+ return __generator(this, function (_a) {
50
+ switch (_a.label) {
51
+ case 0:
52
+ _a.trys.push([0, 2, , 3]);
53
+ return [4 /*yield*/, this.sendgrid.send(email)];
54
+ case 1:
55
+ _a.sent();
56
+ return [3 /*break*/, 3];
57
+ case 2:
58
+ ex_1 = _a.sent();
59
+ console.log(JSON.stringify(ex_1));
60
+ return [2 /*return*/, false];
61
+ case 3: return [2 /*return*/, true];
62
+ }
63
+ });
64
+ });
65
+ };
66
+ return sendgridClient;
67
+ }());
68
+ exports.sendgridClient = sendgridClient;
@@ -0,0 +1,13 @@
1
+ import { sms } from "./schemas/sms";
2
+ import { client } from "./schemas/client";
3
+ export interface sms46elksClientOptions {
4
+ username: string;
5
+ password: string;
6
+ }
7
+ export declare class sms46elksClient implements client {
8
+ username: string;
9
+ password: string;
10
+ constructor(options: sms46elksClientOptions);
11
+ sendSMS(sms: sms): Promise<void>;
12
+ send(sms: sms): Promise<boolean>;
13
+ }
@@ -0,0 +1,119 @@
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 (_) 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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
50
+ return (mod && mod.__esModule) ? mod : { "default": mod };
51
+ };
52
+ Object.defineProperty(exports, "__esModule", { value: true });
53
+ exports.sms46elksClient = void 0;
54
+ var axios_1 = __importDefault(require("axios"));
55
+ var url_1 = require("url");
56
+ var sms46elksClient = /** @class */ (function () {
57
+ function sms46elksClient(options) {
58
+ this.username = options.username;
59
+ this.password = options.password;
60
+ }
61
+ sms46elksClient.prototype.sendSMS = function (sms) {
62
+ return __awaiter(this, void 0, void 0, function () {
63
+ var authKey, _a, _b, _i, t, to, url, data, postData, config, res;
64
+ return __generator(this, function (_c) {
65
+ switch (_c.label) {
66
+ case 0:
67
+ authKey = Buffer.from(this.username + ":" + this.password).toString("base64");
68
+ _a = [];
69
+ for (_b in sms.to)
70
+ _a.push(_b);
71
+ _i = 0;
72
+ _c.label = 1;
73
+ case 1:
74
+ if (!(_i < _a.length)) return [3 /*break*/, 4];
75
+ t = _a[_i];
76
+ to = sms.to[t];
77
+ url = "https://api.46elks.com/a1/sms";
78
+ data = __assign(__assign({}, sms), { to: to });
79
+ postData = new url_1.URLSearchParams(data).toString();
80
+ config = {
81
+ headers: {
82
+ Authorization: "Basic " + authKey,
83
+ },
84
+ };
85
+ return [4 /*yield*/, axios_1.default.post(url, postData, config)];
86
+ case 2:
87
+ res = _c.sent();
88
+ _c.label = 3;
89
+ case 3:
90
+ _i++;
91
+ return [3 /*break*/, 1];
92
+ case 4: return [2 /*return*/];
93
+ }
94
+ });
95
+ });
96
+ };
97
+ sms46elksClient.prototype.send = function (sms) {
98
+ return __awaiter(this, void 0, void 0, function () {
99
+ var ex_1;
100
+ return __generator(this, function (_a) {
101
+ switch (_a.label) {
102
+ case 0:
103
+ _a.trys.push([0, 2, , 3]);
104
+ return [4 /*yield*/, this.sendSMS(sms)];
105
+ case 1:
106
+ _a.sent();
107
+ return [3 /*break*/, 3];
108
+ case 2:
109
+ ex_1 = _a.sent();
110
+ console.log(JSON.stringify(ex_1));
111
+ return [2 /*return*/, false];
112
+ case 3: return [2 /*return*/, true];
113
+ }
114
+ });
115
+ });
116
+ };
117
+ return sms46elksClient;
118
+ }());
119
+ exports.sms46elksClient = sms46elksClient;
@@ -0,0 +1,17 @@
1
+ import nodemailer from "nodemailer";
2
+ import { email } from "./schemas/email";
3
+ import { client } from "./schemas/client";
4
+ export interface smtpClientOptions {
5
+ host: string;
6
+ port: number;
7
+ secure: boolean;
8
+ auth?: {
9
+ user: string;
10
+ pass: string;
11
+ };
12
+ }
13
+ export declare class smtpClient implements client {
14
+ transporter: nodemailer.Transporter;
15
+ constructor(options: smtpClientOptions);
16
+ send(email: email): Promise<boolean>;
17
+ }
@@ -0,0 +1,70 @@
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.smtpClient = void 0;
43
+ var nodemailer_1 = __importDefault(require("nodemailer"));
44
+ var smtpClient = /** @class */ (function () {
45
+ function smtpClient(options) {
46
+ this.transporter = nodemailer_1.default.createTransport(options);
47
+ }
48
+ smtpClient.prototype.send = function (email) {
49
+ return __awaiter(this, void 0, void 0, function () {
50
+ var ex_1;
51
+ return __generator(this, function (_a) {
52
+ switch (_a.label) {
53
+ case 0:
54
+ _a.trys.push([0, 2, , 3]);
55
+ return [4 /*yield*/, this.transporter.sendMail(email)];
56
+ case 1:
57
+ _a.sent();
58
+ return [3 /*break*/, 3];
59
+ case 2:
60
+ ex_1 = _a.sent();
61
+ console.log(JSON.stringify(ex_1));
62
+ return [2 /*return*/, false];
63
+ case 3: return [2 /*return*/, true];
64
+ }
65
+ });
66
+ });
67
+ };
68
+ return smtpClient;
69
+ }());
70
+ exports.smtpClient = smtpClient;
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@flink-app/sms-plugin",
3
+ "version": "0.3.8",
4
+ "description": "Flink plugin that makes it possible to send sms",
5
+ "scripts": {
6
+ "test": "echo \"Error: no test specified\"",
7
+ "prepublish": "tsc"
8
+ },
9
+ "author": "johan@frost.se",
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "license": "MIT",
14
+ "types": "dist/index.d.ts",
15
+ "main": "dist/index.js",
16
+ "dependencies": {
17
+ "axios": "^0.27.2"
18
+ },
19
+ "devDependencies": {
20
+ "@flink-app/flink": "^0.3.8",
21
+ "@types/node": "^15.6.2",
22
+ "ts-node": "^9.1.1",
23
+ "typescript": "^4.2.4"
24
+ },
25
+ "gitHead": "9c45e69ffff300482f9945074c5ecc3e1d661467"
26
+ }
package/readme.md ADDED
@@ -0,0 +1,59 @@
1
+ # Flink API Docs
2
+
3
+ A FLINK plugin that makes it possible to send sms
4
+
5
+ ## Usage
6
+
7
+ Install plugin to your flink app project:
8
+
9
+ ```
10
+ npm i -S @flink-app/sms-plugin
11
+ ```
12
+
13
+ ## Setup
14
+
15
+ ### With 46 elks
16
+
17
+ Add and configure plugin in your app startup (probable the `index.ts` in root project):
18
+
19
+ ```
20
+ import { emailPlugin, sendgridClient } from "@flink-app/email-plugin";
21
+
22
+ function start() {
23
+ new FlinkApp<AppContext>({
24
+ name: "My app",
25
+ plugins: [
26
+ // Register plugin
27
+ emailPlugin({
28
+ client : new sendgridClient({
29
+ apiKey : process.env.SENDGRID_API_KEY
30
+ })
31
+ })
32
+ ],
33
+ }).start();
34
+ }
35
+
36
+ ```
37
+
38
+ Add plugin ctx to `Ctx.ts` in root project
39
+
40
+ ```
41
+ import { smsPluginContext } from "@flink-app/sms-plugin";
42
+
43
+ export interface Ctx extends FlinkContext<smsPluginContext> {
44
+
45
+ }
46
+
47
+ ```
48
+ ## Send sms
49
+ Send email from your handlers by using the the context
50
+ ```
51
+
52
+ await ctx.plugins.smsPlugin.client.send({
53
+ to : ["+4612345678"],
54
+ from : "Sender",
55
+ message : "Hello world"
56
+
57
+ })
58
+
59
+ ```
package/src/index.ts ADDED
@@ -0,0 +1,29 @@
1
+ import { FlinkApp, FlinkPlugin } from "@flink-app/flink";
2
+ import { client } from "./schemas/client";
3
+ export type { sms } from "./schemas/sms";
4
+ export { sms46elksClient } from "./sms46elksClient";
5
+
6
+ export type smsPluginOptions = {
7
+ /**
8
+ * Path for request
9
+ */
10
+ client: client;
11
+ };
12
+
13
+ export interface smsPluginContext {
14
+ smsPlugin: {
15
+ client: client;
16
+ };
17
+ }
18
+
19
+ export const smsPlugin = (options: smsPluginOptions): FlinkPlugin => {
20
+ return {
21
+ id: "smsPlugin",
22
+ init: (app) => init(app, options),
23
+ ctx: {
24
+ client: options.client,
25
+ },
26
+ };
27
+ };
28
+
29
+ function init(app: FlinkApp<any>, options: smsPluginOptions) {}
@@ -0,0 +1,5 @@
1
+ import { sms } from "./sms";
2
+
3
+ export interface client {
4
+ send(sms: sms): Promise<boolean>;
5
+ }
@@ -0,0 +1,10 @@
1
+ import { Readable } from "stream";
2
+ import { Url } from "url";
3
+
4
+ export interface sms {
5
+ from: string;
6
+
7
+ to: string[];
8
+
9
+ message: string;
10
+ }
@@ -0,0 +1,56 @@
1
+ import { sms } from "./schemas/sms";
2
+ import { client } from "./schemas/client";
3
+ import axios from "axios";
4
+ import { URLSearchParams } from "url";
5
+
6
+ export interface sms46elksClientOptions {
7
+ username: string;
8
+ password: string;
9
+ }
10
+
11
+ export class sms46elksClient implements client {
12
+ username: string;
13
+ password: string;
14
+ constructor(options: sms46elksClientOptions) {
15
+ this.username = options.username;
16
+ this.password = options.password;
17
+ }
18
+ async sendSMS(sms: sms) {
19
+ const authKey = Buffer.from(this.username + ":" + this.password).toString("base64");
20
+
21
+ for (let t in sms.to) {
22
+ const to = sms.to[t];
23
+
24
+ // Set the SMS endpoint
25
+ const url = "https://api.46elks.com/a1/sms";
26
+
27
+ // Request data object
28
+ var data = {
29
+ ...sms,
30
+ to,
31
+ };
32
+
33
+ const postData = new URLSearchParams(data).toString();
34
+
35
+ // Set the headers
36
+ const config = {
37
+ headers: {
38
+ Authorization: "Basic " + authKey,
39
+ },
40
+ };
41
+
42
+ // Send request
43
+ const res = await axios.post(url, postData, config);
44
+ }
45
+ }
46
+
47
+ async send(sms: sms) {
48
+ try {
49
+ await this.sendSMS(sms);
50
+ } catch (ex) {
51
+ console.log(JSON.stringify(ex));
52
+ return false;
53
+ }
54
+ return true;
55
+ }
56
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es5",
4
+ "lib": ["esnext", "es2016"],
5
+ "allowJs": true,
6
+ "skipLibCheck": true,
7
+ "esModuleInterop": true,
8
+ "allowSyntheticDefaultImports": true,
9
+ "strict": true,
10
+ "forceConsistentCasingInFileNames": true,
11
+ "module": "commonjs",
12
+ "moduleResolution": "node",
13
+ "resolveJsonModule": true,
14
+ "isolatedModules": true,
15
+ "noEmit": false,
16
+ "declaration": true,
17
+ "experimentalDecorators": true,
18
+ "checkJs": true,
19
+ "outDir": "dist"
20
+ },
21
+ "include": ["./src/*"],
22
+ "exclude": ["./node_modules/*"]
23
+ }