@svkruik/sk-dispatch-connector 0.0.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/README.md ADDED
@@ -0,0 +1,12 @@
1
+ # SK Dispatch - Connector
2
+
3
+ [![Publish Package](https://github.com/SVKruik-Organization/Dispatch/actions/workflows/publish.yml/badge.svg)](https://github.com/SVKruik-Organization/Dispatch/actions/workflows/publish.yml)
4
+ [![NPM Version](https://img.shields.io/npm/v/%40svkruik%2Fsk-dispatch-connector?label=%40svkruik%2Fsk-dispatch-connector&color=green)](https://www.npmjs.com/package/@svkruik/sk-dispatch-connector)
5
+
6
+ Making dispatching mails a breeze. Simplifies communication with SK Dispatch for other apps by removing boilerplate and providing stronger Intellisense.
7
+
8
+ This package is developed along-side the [server](https://github.com/SVKruik-Organization/Dispatch/tree/main/server) for tight typings and validations.
9
+
10
+ ---
11
+
12
+ Because a simple `README.md` can only do so much, I have also made an documentation platform, [SK Docs](https://platform.stefankruik.com/documentation). If you are serious about using my products, I highly recommend reading the relevant articles. Don't have time for that? Don't hesitate to [reach out](https://skpvt.io/r/support). I am always open to have a chat and answer questions to your heart's content, unless I am debugging deployment 😄.
package/dist/index.js ADDED
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sendMail = sendMail;
4
+ const sk_platform_formatters_1 = require("@svkruik/sk-platform-formatters");
5
+ const sk_uplink_connector_1 = require("@svkruik/sk-uplink-connector");
6
+ const path_1 = require("path");
7
+ async function sendMail(mailDetails, protocol, appName) {
8
+ if (!process.env.DISPATCH_HTTP_API_URL && protocol === "http")
9
+ return (0, sk_platform_formatters_1.logError)(new Error("DISPATCH_HTTP_API_URL is not defined in environment variables."));
10
+ try {
11
+ const callerFile = (0, path_1.basename)((new Error().stack?.split("\n")[2] || "").trim().split(" ").pop() || "unknown").replaceAll(")", "");
12
+ // Validate Email
13
+ if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(mailDetails.to))
14
+ throw new Error(`Invalid email address provided: ${mailDetails.to}`);
15
+ const payload = JSON.stringify({
16
+ mailDetails
17
+ });
18
+ if (protocol === "http") {
19
+ await fetch(process.env.DISPATCH_HTTP_API_URL, {
20
+ method: "POST",
21
+ headers: { "Content-Type": "application/json" },
22
+ body: payload
23
+ });
24
+ }
25
+ else
26
+ return await (0, sk_uplink_connector_1.sendUplink)({
27
+ "name": "unicast-services",
28
+ "router": "Dispatch",
29
+ "type": "direct"
30
+ }, {
31
+ "content": payload,
32
+ "reason": `Sending '${mailDetails.fileName}' mail via Dispatch Connector.`,
33
+ "recipient": "Dispatch",
34
+ "sender": appName,
35
+ "triggerSource": callerFile,
36
+ "task": "sendMail",
37
+ "timestamp": new Date()
38
+ });
39
+ }
40
+ catch (error) {
41
+ (0, sk_platform_formatters_1.logError)(error);
42
+ }
43
+ }
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@svkruik/sk-dispatch-connector",
3
+ "description": "Making dispatching mails a breeze.",
4
+ "keywords": [
5
+ "sk-platform",
6
+ "sk-dispatch",
7
+ "sk-uplink",
8
+ "amqp",
9
+ "rabbitmq"
10
+ ],
11
+ "version": "0.0.1",
12
+ "author": "Stefan Kruik",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/SVKruik-Organization/Dispatch.git"
16
+ },
17
+ "bugs": {
18
+ "url": "https://github.com/SVKruik-Organization/Dispatch/issues"
19
+ },
20
+ "homepage": "https://github.com/SVKruik-Organization/Dispatch#readme",
21
+ "license": "MIT",
22
+ "scripts": {
23
+ "build": "npx tsc",
24
+ "start": "ts-node index.ts"
25
+ },
26
+ "types": "types.d.ts",
27
+ "main": "dist/index.js",
28
+ "publishConfig": {
29
+ "access": "public"
30
+ },
31
+ "files": [
32
+ "dist",
33
+ "types.d.ts"
34
+ ],
35
+ "dependencies": {
36
+ "@svkruik/sk-platform-formatters": "^0.0.5",
37
+ "@svkruik/sk-uplink-connector": "0.1.3"
38
+ },
39
+ "devDependencies": {
40
+ "@types/node": "^20.12.7",
41
+ "ts-node": "^10.9.2",
42
+ "typescript": "^5.4.5"
43
+ }
44
+ }
package/types.d.ts ADDED
@@ -0,0 +1,28 @@
1
+ // Functions
2
+ export function sendMail<T extends validFileNames>(
3
+ mailDetails: MailDetails<T>,
4
+ protocol: validProtocols
5
+ ): Promise<void>;
6
+
7
+ // Types
8
+ export type validProtocols = "amqp" | "http";
9
+ export type validFileNames = "2fa-code" | "new-guest-login" | "new-login"
10
+ export type ReplacementMap = {
11
+ "2fa-code": Array<
12
+ { key: "firstName"; value: string, },
13
+ { key: "platformName"; value: string },
14
+ { key: "verificationPin"; value: string | number }>,
15
+ "new-guest-login": Array<
16
+ { key: "adminName"; value: string },
17
+ { key: "guestName"; value: string },
18
+ { key: "platformName"; value: string }>,
19
+ "new-login": Array<
20
+ { key: "firstName"; value: string, },
21
+ { key: "platformName"; value: string }>,
22
+ };
23
+
24
+ export type MailDetails<T extends validFileNames> = {
25
+ to: string; // Email will be validated
26
+ replacements: ReplacementMap[T];
27
+ fileName: T;
28
+ };