@develit-services/notification 0.0.19 → 0.0.20

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.
@@ -0,0 +1,205 @@
1
+ import z$1, { z } from 'zod';
2
+ import { useFetch, createInternalError } from '@develit-io/backend-sdk';
3
+ import { z as z$2 } from 'zod/v4';
4
+ import twilio from 'twilio';
5
+
6
+ const iContactSchema = z.union([
7
+ z.string(),
8
+ z.object({
9
+ email: z.string(),
10
+ name: z.union([z.string(), z.undefined()])
11
+ })
12
+ ]);
13
+ const iEmailSchema = z.object({
14
+ to: z.union([iContactSchema, z.array(iContactSchema)]),
15
+ replyTo: z.union([iContactSchema, z.array(iContactSchema)]).optional(),
16
+ cc: z.union([iContactSchema, z.array(iContactSchema)]).optional(),
17
+ bcc: z.union([iContactSchema, z.array(iContactSchema)]).optional(),
18
+ from: iContactSchema.optional(),
19
+ subject: z.string(),
20
+ text: z.string().optional(),
21
+ html: z.string().optional(),
22
+ templateId: z.number().optional(),
23
+ templateVariables: z.record(z.string(), z.string()).optional()
24
+ });
25
+
26
+ class IEmailConnector {
27
+ constructor({
28
+ API_KEY,
29
+ SMTP_HOST,
30
+ SENDER
31
+ }) {
32
+ this.API_KEY = API_KEY;
33
+ this.SMTP_HOST = SMTP_HOST;
34
+ this.SENDER = SENDER;
35
+ }
36
+ }
37
+
38
+ class EcomailConnector extends IEmailConnector {
39
+ static {
40
+ this.providerName = "ecomail";
41
+ }
42
+ constructor({
43
+ API_KEY,
44
+ SMTP_HOST,
45
+ SENDER
46
+ }) {
47
+ super({ API_KEY, SMTP_HOST, SENDER });
48
+ }
49
+ async sendEmail(email) {
50
+ if (email.templateVariables) {
51
+ for (const [key, value] of Object.entries(email.templateVariables)) {
52
+ if (typeof value === "string" && String(value).includes("localhost") && key in email.templateVariables) {
53
+ email.templateVariables[key] = String(value).replace("localhost", "origin");
54
+ }
55
+ }
56
+ }
57
+ const emEmail = this.convertEmail(email);
58
+ const uri = emEmail.message.template_id ? "https://api2.ecomailapp.cz/transactional/send-template" : "https://api2.ecomailapp.cz/transactional/send-message";
59
+ const [_, error] = await useFetch(uri, {
60
+ method: "POST",
61
+ headers: {
62
+ "Content-Type": "application/json",
63
+ key: this.API_KEY
64
+ },
65
+ body: JSON.stringify(emEmail)
66
+ });
67
+ if (error) throw error;
68
+ }
69
+ convertEmail(email) {
70
+ const toContacts = this.convertContacts(email.to);
71
+ const ccContacts = email.cc ? this.convertContacts(email.cc) : [];
72
+ const bccContacts = email.bcc ? this.convertContacts(email.bcc) : [];
73
+ const replyTo = email.replyTo ? this.convertContacts(email.replyTo)[0] : void 0;
74
+ const from = this.convertContact(email.from || this.SENDER);
75
+ const subject = email.subject;
76
+ const textAttachments = email.text ? [{ name: "plain", type: "text/plain", content: email.text }] : [];
77
+ const htmlAttachments = email.html ? [{ type: "text/html", content: email.html, name: "email_body.html" }] : [];
78
+ const attachments = [...textAttachments, ...htmlAttachments];
79
+ const contacts = toContacts.flatMap((to) => {
80
+ const entries = [];
81
+ if (ccContacts.length > 0) {
82
+ ccContacts.forEach((cc) => {
83
+ entries.push({ email: cc?.email, name: cc?.name, cc: cc?.email });
84
+ });
85
+ }
86
+ if (bccContacts.length > 0) {
87
+ bccContacts.forEach((bcc) => {
88
+ entries.push({ email: bcc?.email, name: bcc?.name, bcc: bcc?.email });
89
+ });
90
+ }
91
+ return entries.length > 0 ? entries : [to];
92
+ });
93
+ return {
94
+ message: {
95
+ from_email: from?.email,
96
+ from_name: from.name || "",
97
+ subject,
98
+ attachments,
99
+ to: contacts,
100
+ text: email.text,
101
+ html: email.html,
102
+ reply_to: replyTo?.email,
103
+ template_id: email.templateId,
104
+ global_merge_vars: email.templateVariables ? Object.keys(email.templateVariables).map((key) => ({
105
+ name: key,
106
+ content: email.templateVariables[key]
107
+ })) : null
108
+ }
109
+ };
110
+ }
111
+ convertContacts(contacts) {
112
+ if (!contacts) {
113
+ return [];
114
+ }
115
+ const contactArray = Array.isArray(contacts) ? contacts : [contacts];
116
+ return contactArray.map(this.convertContact);
117
+ }
118
+ convertContact(contact) {
119
+ if (typeof contact === "string") {
120
+ return { email: contact, name: void 0 };
121
+ }
122
+ return { email: contact?.email, name: contact?.name };
123
+ }
124
+ }
125
+
126
+ const sendEmailInputSchema = z$1.object({
127
+ email: iEmailSchema,
128
+ metadata: z$1.object({
129
+ userAgent: z$1.string().optional(),
130
+ ip: z$1.ipv4().or(z$1.ipv6()).optional(),
131
+ initiator: z$1.object({
132
+ service: z$1.string(),
133
+ userId: z$1.string().optional()
134
+ })
135
+ })
136
+ });
137
+
138
+ const sendSlackInputSchema = z$2.object({
139
+ slack: z$2.object({
140
+ message: z$2.string()
141
+ }),
142
+ metadata: z$2.object({
143
+ userAgent: z$2.string().optional(),
144
+ ip: z$2.ipv4().or(z$2.ipv6()).optional(),
145
+ initiator: z$2.object({
146
+ service: z$2.string(),
147
+ userId: z$2.string().optional()
148
+ })
149
+ })
150
+ });
151
+
152
+ const sendSmsInputSchema = z.object({
153
+ sms: z.object({
154
+ message: z.string(),
155
+ to: z.string()
156
+ }),
157
+ metadata: z.object({
158
+ userAgent: z.string().optional(),
159
+ ip: z.ipv4().or(z.ipv6()).optional(),
160
+ initiator: z.object({
161
+ service: z.string(),
162
+ userId: z.string().optional()
163
+ })
164
+ })
165
+ });
166
+
167
+ class ISmsConnector {
168
+ constructor({
169
+ ACCOUNT_ID,
170
+ AUTH_TOKEN,
171
+ SERVICE_ID
172
+ }) {
173
+ this.ACCOUNT_ID = ACCOUNT_ID;
174
+ this.AUTH_TOKEN = AUTH_TOKEN;
175
+ this.SERVICE_ID = SERVICE_ID;
176
+ }
177
+ }
178
+
179
+ class TwilioConnector extends ISmsConnector {
180
+ static {
181
+ this.providerName = "twilio";
182
+ }
183
+ constructor({
184
+ ACCOUNT_ID,
185
+ AUTH_TOKEN,
186
+ SERVICE_ID
187
+ }) {
188
+ super({ ACCOUNT_ID, AUTH_TOKEN, SERVICE_ID });
189
+ this.twilioClient = twilio(ACCOUNT_ID, AUTH_TOKEN);
190
+ }
191
+ async sendSms(sms) {
192
+ const message = await this.twilioClient.messages.create({
193
+ body: sms.message,
194
+ messagingServiceSid: this.SERVICE_ID,
195
+ to: sms.to
196
+ });
197
+ if (message.errorMessage)
198
+ return createInternalError(null, {
199
+ message: message.errorMessage,
200
+ status: message.errorCode
201
+ });
202
+ }
203
+ }
204
+
205
+ export { EcomailConnector as E, IEmailConnector as I, TwilioConnector as T, sendSmsInputSchema as a, sendSlackInputSchema as b, iEmailSchema as c, ISmsConnector as d, iContactSchema as i, sendEmailInputSchema as s };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@develit-services/notification",
3
- "version": "0.0.19",
3
+ "version": "0.0.20",
4
4
  "author": "Develit.io s.r.o.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -19,6 +19,11 @@
19
19
  "import": "./dist/database/schema.mjs",
20
20
  "require": "./dist/database/schema.cjs"
21
21
  },
22
+ "./@types": {
23
+ "types": "./dist/@types.d.ts",
24
+ "import": "./dist/@types.mjs",
25
+ "require": "./dist/@types.cjs"
26
+ },
22
27
  "./package.json": "./package.json"
23
28
  },
24
29
  "files": [