@adatechnology/notification-contracts 0.3.0 → 0.5.0

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.cjs CHANGED
@@ -31,6 +31,7 @@ __export(index_exports, {
31
31
  EMAIL_ATTACHMENT_MAX_COUNT: () => EMAIL_ATTACHMENT_MAX_COUNT,
32
32
  EMAIL_DRIVER: () => EMAIL_DRIVER,
33
33
  INTRUSIVE_CHANNELS: () => INTRUSIVE_CHANNELS,
34
+ InvalidVapidKeysError: () => InvalidVapidKeysError,
34
35
  InvalidWebhookSignatureError: () => InvalidWebhookSignatureError,
35
36
  NOTIFICATION_CATEGORY_HINT: () => NOTIFICATION_CATEGORY_HINT,
36
37
  NOTIFICATION_CHANNEL: () => NOTIFICATION_CHANNEL,
@@ -53,6 +54,7 @@ __export(index_exports, {
53
54
  STANDARD_EMAIL_THEME: () => STANDARD_EMAIL_THEME,
54
55
  STANDARD_EMAIL_VARIABLES: () => STANDARD_EMAIL_VARIABLES,
55
56
  SUPPRESSION_REASON: () => SUPPRESSION_REASON,
57
+ SubscriptionNotFoundError: () => SubscriptionNotFoundError,
56
58
  SuppressedTargetError: () => SuppressedTargetError,
57
59
  TEMPLATE_CONSTRAINTS_BY_CHANNEL: () => TEMPLATE_CONSTRAINTS_BY_CHANNEL,
58
60
  TEMPLATE_VARIABLE_KIND: () => TEMPLATE_VARIABLE_KIND,
@@ -60,7 +62,10 @@ __export(index_exports, {
60
62
  TemplateNotFoundError: () => TemplateNotFoundError,
61
63
  ThrottledError: () => ThrottledError,
62
64
  UnknownTemplateVariablesError: () => UnknownTemplateVariablesError,
65
+ WebPushEncryptionError: () => WebPushEncryptionError,
63
66
  assessStandardEmailTemplate: () => assessStandardEmailTemplate,
67
+ broadcastPushFilterSchema: () => broadcastPushFilterSchema,
68
+ broadcastPushSchema: () => broadcastPushSchema,
64
69
  buildInitials: () => buildInitials,
65
70
  buildPreviewPayload: () => buildPreviewPayload,
66
71
  categoryPolicyEntrySchema: () => categoryPolicyEntrySchema,
@@ -71,22 +76,36 @@ __export(index_exports, {
71
76
  diffTemplateVariables: () => diffTemplateVariables,
72
77
  escapeEmailHtml: () => escapeEmailHtml,
73
78
  escapeTemplateHtml: () => escapeTemplateHtml,
79
+ extractCalendarEvent: () => extractCalendarEvent,
74
80
  extractTemplatePlaceholders: () => extractTemplatePlaceholders,
75
81
  findUnsafeEmailHtml: () => findUnsafeEmailHtml,
82
+ generateCalendarLinks: () => generateCalendarLinks,
83
+ generateGoogleCalendarUrl: () => generateGoogleCalendarUrl,
84
+ generateIcsContent: () => generateIcsContent,
85
+ generateIcsDataUri: () => generateIcsDataUri,
86
+ generateOffice365CalendarUrl: () => generateOffice365CalendarUrl,
87
+ generateOutlookCalendarUrl: () => generateOutlookCalendarUrl,
88
+ generateYahooCalendarUrl: () => generateYahooCalendarUrl,
76
89
  htmlToPlainText: () => htmlToPlainText,
77
90
  interpolateEmailHtml: () => interpolateEmailHtml,
78
91
  interpolateTemplate: () => interpolateTemplate,
79
92
  listNotificationsQuerySchema: () => listNotificationsQuerySchema,
80
93
  preferenceEntrySchema: () => preferenceEntrySchema,
94
+ pushNotificationPayloadSchema: () => pushNotificationPayloadSchema,
81
95
  registerDeviceSchema: () => registerDeviceSchema,
96
+ registerPushSubscriptionSchema: () => registerPushSubscriptionSchema,
82
97
  renderEmailLogoHtml: () => renderEmailLogoHtml,
83
98
  renderEmailMarksHtml: () => renderEmailMarksHtml,
84
99
  renderStandardEmail: () => renderStandardEmail,
85
100
  renderTemplate: () => renderTemplate,
86
101
  sendNotificationSchema: () => sendNotificationSchema,
102
+ sendPushDirectSchema: () => sendPushDirectSchema,
103
+ sendPushDirectTargetSchema: () => sendPushDirectTargetSchema,
87
104
  updateCategoryPoliciesSchema: () => updateCategoryPoliciesSchema,
88
105
  updatePreferencesSchema: () => updatePreferencesSchema,
89
- upsertTemplateSchema: () => upsertTemplateSchema
106
+ upsertTemplateSchema: () => upsertTemplateSchema,
107
+ webPushKeysSchema: () => webPushKeysSchema,
108
+ webPushSubscriptionSchema: () => webPushSubscriptionSchema
90
109
  });
91
110
  module.exports = __toCommonJS(index_exports);
92
111
 
@@ -126,7 +145,8 @@ var DEVICE_PLATFORM = {
126
145
  };
127
146
  var PUSH_DRIVER = {
128
147
  EXPO: "expo",
129
- FCM: "fcm"
148
+ FCM: "fcm",
149
+ WEBPUSH: "webpush"
130
150
  };
131
151
  var EMAIL_DRIVER = {
132
152
  SMTP: "smtp",
@@ -248,6 +268,64 @@ var deliveryWebhookSchema = import_zod.z.object({
248
268
  occurredAt: import_zod.z.coerce.date(),
249
269
  suppressionReason: import_zod.z.nativeEnum(SUPPRESSION_REASON).optional()
250
270
  });
271
+ var webPushKeysSchema = import_zod.z.object({
272
+ p256dh: import_zod.z.string().min(1),
273
+ auth: import_zod.z.string().min(1)
274
+ });
275
+ var webPushSubscriptionSchema = import_zod.z.object({
276
+ endpoint: import_zod.z.string().url(),
277
+ keys: webPushKeysSchema,
278
+ expirationTime: import_zod.z.number().nullable().optional()
279
+ });
280
+ var pushNotificationPayloadSchema = import_zod.z.object({
281
+ title: import_zod.z.string().min(1).max(256),
282
+ body: import_zod.z.string().min(1).max(4096),
283
+ icon: import_zod.z.string().url().optional(),
284
+ badge: import_zod.z.number().int().nonnegative().optional(),
285
+ image: import_zod.z.string().url().optional(),
286
+ tag: import_zod.z.string().max(64).optional(),
287
+ url: import_zod.z.string().optional(),
288
+ data: import_zod.z.record(import_zod.z.unknown()).optional()
289
+ });
290
+ var sendPushDirectTargetSchema = import_zod.z.discriminatedUnion("type", [
291
+ import_zod.z.object({
292
+ type: import_zod.z.literal("token"),
293
+ token: import_zod.z.string().min(1),
294
+ platform: import_zod.z.nativeEnum(DEVICE_PLATFORM),
295
+ driver: import_zod.z.nativeEnum(PUSH_DRIVER).optional()
296
+ }),
297
+ import_zod.z.object({
298
+ type: import_zod.z.literal("user"),
299
+ userId: import_zod.z.string().min(1)
300
+ }),
301
+ import_zod.z.object({
302
+ type: import_zod.z.literal("subscription"),
303
+ subscription: webPushSubscriptionSchema
304
+ })
305
+ ]);
306
+ var sendPushDirectSchema = import_zod.z.object({
307
+ target: sendPushDirectTargetSchema,
308
+ notification: pushNotificationPayloadSchema,
309
+ dedupeKey: import_zod.z.string().max(256).optional()
310
+ });
311
+ var broadcastPushFilterSchema = import_zod.z.object({
312
+ topic: import_zod.z.string().max(128).optional(),
313
+ tags: import_zod.z.array(import_zod.z.string().max(64)).optional(),
314
+ platform: import_zod.z.nativeEnum(DEVICE_PLATFORM).optional()
315
+ });
316
+ var broadcastPushSchema = import_zod.z.object({
317
+ filter: broadcastPushFilterSchema.optional(),
318
+ notification: pushNotificationPayloadSchema
319
+ });
320
+ var registerPushSubscriptionSchema = import_zod.z.object({
321
+ platform: import_zod.z.nativeEnum(DEVICE_PLATFORM),
322
+ driver: import_zod.z.nativeEnum(PUSH_DRIVER),
323
+ subscription: webPushSubscriptionSchema,
324
+ appVersion: import_zod.z.string().max(32).optional(),
325
+ locale: import_zod.z.string().min(2).max(16).optional(),
326
+ timezone: import_zod.z.string().min(1).max(64).optional(),
327
+ tags: import_zod.z.array(import_zod.z.string().max(64)).optional()
328
+ });
251
329
 
252
330
  // src/events.ts
253
331
  var NOTIFICATION_EVENT = {
@@ -286,7 +364,10 @@ var NOTIFICATION_ERROR_CODES = {
286
364
  DEVICE_NOT_FOUND: "NOTIFICATION_DEVICE_NOT_FOUND",
287
365
  THROTTLED: "NOTIFICATION_THROTTLED",
288
366
  CONFIG_MISSING: "NOTIFICATION_CONFIG_MISSING",
289
- TEMPLATE_UNKNOWN_VARIABLE: "NOTIFICATION_TEMPLATE_UNKNOWN_VARIABLE"
367
+ TEMPLATE_UNKNOWN_VARIABLE: "NOTIFICATION_TEMPLATE_UNKNOWN_VARIABLE",
368
+ WEBPUSH_ENCRYPTION_FAILED: "NOTIFICATION_WEBPUSH_ENCRYPTION_FAILED",
369
+ INVALID_VAPID_KEYS: "NOTIFICATION_INVALID_VAPID_KEYS",
370
+ SUBSCRIPTION_NOT_FOUND: "NOTIFICATION_SUBSCRIPTION_NOT_FOUND"
290
371
  };
291
372
  var TemplateIdNotFoundError = class extends NotificationError {
292
373
  static {
@@ -435,6 +516,33 @@ var ConfigMissingError = class extends NotificationError {
435
516
  }), this.field = field;
436
517
  }
437
518
  };
519
+ var WebPushEncryptionError = class extends NotificationError {
520
+ static {
521
+ __name(this, "WebPushEncryptionError");
522
+ }
523
+ constructor(message, details) {
524
+ super(message, 500, NOTIFICATION_ERROR_CODES.WEBPUSH_ENCRYPTION_FAILED, details);
525
+ }
526
+ };
527
+ var InvalidVapidKeysError = class extends NotificationError {
528
+ static {
529
+ __name(this, "InvalidVapidKeysError");
530
+ }
531
+ constructor(message) {
532
+ super(message, 500, NOTIFICATION_ERROR_CODES.INVALID_VAPID_KEYS);
533
+ }
534
+ };
535
+ var SubscriptionNotFoundError = class extends NotificationError {
536
+ static {
537
+ __name(this, "SubscriptionNotFoundError");
538
+ }
539
+ endpoint;
540
+ constructor(endpoint) {
541
+ super(`Assinatura de push n\xE3o encontrada.`, 404, NOTIFICATION_ERROR_CODES.SUBSCRIPTION_NOT_FOUND, {
542
+ endpoint
543
+ }), this.endpoint = endpoint;
544
+ }
545
+ };
438
546
 
439
547
  // src/whatsappDriver.ts
440
548
  var INVALID_TARGET_CODES = /* @__PURE__ */ new Set([
@@ -1009,6 +1117,156 @@ function renderStandardEmail(params) {
1009
1117
  };
1010
1118
  }
1011
1119
  __name(renderStandardEmail, "renderStandardEmail");
1120
+
1121
+ // src/calendarEvent.ts
1122
+ function toIsoDate(value) {
1123
+ return value instanceof Date ? value : new Date(value);
1124
+ }
1125
+ __name(toIsoDate, "toIsoDate");
1126
+ function formatUtcCompact(date) {
1127
+ return date.toISOString().replace(/[-:]/g, "").replace(/\.\d{3}/, "");
1128
+ }
1129
+ __name(formatUtcCompact, "formatUtcCompact");
1130
+ function escapeIcsText(text) {
1131
+ return text.replace(/\\/g, "\\\\").replace(/;/g, "\\;").replace(/,/g, "\\,").replace(/\r\n/g, "\\n").replace(/\n/g, "\\n");
1132
+ }
1133
+ __name(escapeIcsText, "escapeIcsText");
1134
+ function generateGoogleCalendarUrl(event) {
1135
+ const start = toIsoDate(event.startsAt);
1136
+ const end = event.endsAt ? toIsoDate(event.endsAt) : new Date(start.getTime() + 60 * 60 * 1e3);
1137
+ const params = new URLSearchParams({
1138
+ action: "TEMPLATE",
1139
+ text: event.title,
1140
+ dates: `${formatUtcCompact(start)}/${formatUtcCompact(end)}`
1141
+ });
1142
+ if (event.description) params.set("details", event.description);
1143
+ if (event.location) params.set("location", event.location);
1144
+ return `https://calendar.google.com/calendar/render?${params.toString()}`;
1145
+ }
1146
+ __name(generateGoogleCalendarUrl, "generateGoogleCalendarUrl");
1147
+ function generateOutlookCalendarUrl(event) {
1148
+ const start = toIsoDate(event.startsAt);
1149
+ const end = event.endsAt ? toIsoDate(event.endsAt) : new Date(start.getTime() + 60 * 60 * 1e3);
1150
+ const params = new URLSearchParams({
1151
+ path: "/calendar/action/compose",
1152
+ rru: "addevent",
1153
+ subject: event.title,
1154
+ startdt: start.toISOString(),
1155
+ enddt: end.toISOString()
1156
+ });
1157
+ if (event.description) params.set("body", event.description);
1158
+ if (event.location) params.set("location", event.location);
1159
+ return `https://outlook.live.com/calendar/0/deeplink/compose?${params.toString()}`;
1160
+ }
1161
+ __name(generateOutlookCalendarUrl, "generateOutlookCalendarUrl");
1162
+ function generateOffice365CalendarUrl(event) {
1163
+ const start = toIsoDate(event.startsAt);
1164
+ const end = event.endsAt ? toIsoDate(event.endsAt) : new Date(start.getTime() + 60 * 60 * 1e3);
1165
+ const params = new URLSearchParams({
1166
+ path: "/calendar/action/compose",
1167
+ rru: "addevent",
1168
+ subject: event.title,
1169
+ startdt: start.toISOString(),
1170
+ enddt: end.toISOString()
1171
+ });
1172
+ if (event.description) params.set("body", event.description);
1173
+ if (event.location) params.set("location", event.location);
1174
+ return `https://outlook.office.com/calendar/0/deeplink/compose?${params.toString()}`;
1175
+ }
1176
+ __name(generateOffice365CalendarUrl, "generateOffice365CalendarUrl");
1177
+ function generateYahooCalendarUrl(event) {
1178
+ const start = toIsoDate(event.startsAt);
1179
+ const end = event.endsAt ? toIsoDate(event.endsAt) : new Date(start.getTime() + 60 * 60 * 1e3);
1180
+ const durationMs = Math.max(0, end.getTime() - start.getTime());
1181
+ const durationHours = Math.floor(durationMs / (60 * 60 * 1e3));
1182
+ const durationMinutes = Math.floor(durationMs % (60 * 60 * 1e3) / (60 * 1e3));
1183
+ const durStr = `${String(durationHours).padStart(2, "0")}${String(durationMinutes).padStart(2, "0")}`;
1184
+ const params = new URLSearchParams({
1185
+ v: "60",
1186
+ view: "d",
1187
+ type: "20",
1188
+ title: event.title,
1189
+ st: formatUtcCompact(start),
1190
+ dur: durStr
1191
+ });
1192
+ if (event.description) params.set("desc", event.description);
1193
+ if (event.location) params.set("in_loc", event.location);
1194
+ return `https://calendar.yahoo.com/?${params.toString()}`;
1195
+ }
1196
+ __name(generateYahooCalendarUrl, "generateYahooCalendarUrl");
1197
+ function generateIcsDataUri(event, now = /* @__PURE__ */ new Date()) {
1198
+ const ics = generateIcsContent(event, now);
1199
+ return `data:text/calendar;charset=utf-8,${encodeURIComponent(ics)}`;
1200
+ }
1201
+ __name(generateIcsDataUri, "generateIcsDataUri");
1202
+ function generateCalendarLinks(event) {
1203
+ return {
1204
+ google: generateGoogleCalendarUrl(event),
1205
+ outlook: generateOutlookCalendarUrl(event),
1206
+ office365: generateOffice365CalendarUrl(event),
1207
+ yahoo: generateYahooCalendarUrl(event),
1208
+ icsDataUri: generateIcsDataUri(event)
1209
+ };
1210
+ }
1211
+ __name(generateCalendarLinks, "generateCalendarLinks");
1212
+ function generateIcsContent(event, now = /* @__PURE__ */ new Date()) {
1213
+ const start = toIsoDate(event.startsAt);
1214
+ const end = event.endsAt ? toIsoDate(event.endsAt) : new Date(start.getTime() + 60 * 60 * 1e3);
1215
+ const uid = `${now.getTime()}-${Math.random().toString(36).substring(2, 9)}@adatechnology.com.br`;
1216
+ const lines = [
1217
+ "BEGIN:VCALENDAR",
1218
+ "VERSION:2.0",
1219
+ "PRODID:-//Ada Technology//Notification Calendar//PT",
1220
+ "CALSCALE:GREGORIAN",
1221
+ "METHOD:PUBLISH",
1222
+ "BEGIN:VEVENT",
1223
+ `UID:${uid}`,
1224
+ `DTSTAMP:${formatUtcCompact(now)}`,
1225
+ `DTSTART:${formatUtcCompact(start)}`,
1226
+ `DTEND:${formatUtcCompact(end)}`,
1227
+ `SUMMARY:${escapeIcsText(event.title)}`
1228
+ ];
1229
+ if (event.description) {
1230
+ lines.push(`DESCRIPTION:${escapeIcsText(event.description)}`);
1231
+ }
1232
+ if (event.location) {
1233
+ lines.push(`LOCATION:${escapeIcsText(event.location)}`);
1234
+ }
1235
+ if (event.url) {
1236
+ lines.push(`URL:${event.url}`);
1237
+ }
1238
+ lines.push("END:VEVENT", "END:VCALENDAR");
1239
+ return lines.join("\r\n");
1240
+ }
1241
+ __name(generateIcsContent, "generateIcsContent");
1242
+ function extractCalendarEvent(payload) {
1243
+ if (!payload || typeof payload !== "object") return void 0;
1244
+ const raw = payload.calendarEvent && typeof payload.calendarEvent === "object" ? payload.calendarEvent : payload;
1245
+ const title = typeof raw.calendarTitle === "string" ? raw.calendarTitle : typeof raw.title === "string" ? raw.title : void 0;
1246
+ const startsAt = typeof raw.calendarStartsAt === "string" ? raw.calendarStartsAt : raw.startsAt instanceof Date || typeof raw.startsAt === "string" ? raw.startsAt : typeof raw.activityDate === "string" ? typeof raw.activityTime === "string" ? `${raw.activityDate}T${raw.activityTime}:00` : `${raw.activityDate}T09:00:00` : void 0;
1247
+ if (!title || !startsAt) return void 0;
1248
+ const description = typeof raw.calendarDescription === "string" ? raw.calendarDescription : typeof raw.description === "string" ? raw.description : void 0;
1249
+ const endsAt = typeof raw.calendarEndsAt === "string" ? raw.calendarEndsAt : raw.endsAt instanceof Date || typeof raw.endsAt === "string" ? raw.endsAt : void 0;
1250
+ const location = typeof raw.location === "string" ? raw.location : void 0;
1251
+ const url = typeof raw.url === "string" ? raw.url : void 0;
1252
+ return {
1253
+ title,
1254
+ startsAt,
1255
+ ...description ? {
1256
+ description
1257
+ } : {},
1258
+ ...endsAt ? {
1259
+ endsAt
1260
+ } : {},
1261
+ ...location ? {
1262
+ location
1263
+ } : {},
1264
+ ...url ? {
1265
+ url
1266
+ } : {}
1267
+ };
1268
+ }
1269
+ __name(extractCalendarEvent, "extractCalendarEvent");
1012
1270
  // Annotate the CommonJS export names for ESM import in node:
1013
1271
  0 && (module.exports = {
1014
1272
  ChannelNotConfiguredError,
@@ -1021,6 +1279,7 @@ __name(renderStandardEmail, "renderStandardEmail");
1021
1279
  EMAIL_ATTACHMENT_MAX_COUNT,
1022
1280
  EMAIL_DRIVER,
1023
1281
  INTRUSIVE_CHANNELS,
1282
+ InvalidVapidKeysError,
1024
1283
  InvalidWebhookSignatureError,
1025
1284
  NOTIFICATION_CATEGORY_HINT,
1026
1285
  NOTIFICATION_CHANNEL,
@@ -1043,6 +1302,7 @@ __name(renderStandardEmail, "renderStandardEmail");
1043
1302
  STANDARD_EMAIL_THEME,
1044
1303
  STANDARD_EMAIL_VARIABLES,
1045
1304
  SUPPRESSION_REASON,
1305
+ SubscriptionNotFoundError,
1046
1306
  SuppressedTargetError,
1047
1307
  TEMPLATE_CONSTRAINTS_BY_CHANNEL,
1048
1308
  TEMPLATE_VARIABLE_KIND,
@@ -1050,7 +1310,10 @@ __name(renderStandardEmail, "renderStandardEmail");
1050
1310
  TemplateNotFoundError,
1051
1311
  ThrottledError,
1052
1312
  UnknownTemplateVariablesError,
1313
+ WebPushEncryptionError,
1053
1314
  assessStandardEmailTemplate,
1315
+ broadcastPushFilterSchema,
1316
+ broadcastPushSchema,
1054
1317
  buildInitials,
1055
1318
  buildPreviewPayload,
1056
1319
  categoryPolicyEntrySchema,
@@ -1061,21 +1324,35 @@ __name(renderStandardEmail, "renderStandardEmail");
1061
1324
  diffTemplateVariables,
1062
1325
  escapeEmailHtml,
1063
1326
  escapeTemplateHtml,
1327
+ extractCalendarEvent,
1064
1328
  extractTemplatePlaceholders,
1065
1329
  findUnsafeEmailHtml,
1330
+ generateCalendarLinks,
1331
+ generateGoogleCalendarUrl,
1332
+ generateIcsContent,
1333
+ generateIcsDataUri,
1334
+ generateOffice365CalendarUrl,
1335
+ generateOutlookCalendarUrl,
1336
+ generateYahooCalendarUrl,
1066
1337
  htmlToPlainText,
1067
1338
  interpolateEmailHtml,
1068
1339
  interpolateTemplate,
1069
1340
  listNotificationsQuerySchema,
1070
1341
  preferenceEntrySchema,
1342
+ pushNotificationPayloadSchema,
1071
1343
  registerDeviceSchema,
1344
+ registerPushSubscriptionSchema,
1072
1345
  renderEmailLogoHtml,
1073
1346
  renderEmailMarksHtml,
1074
1347
  renderStandardEmail,
1075
1348
  renderTemplate,
1076
1349
  sendNotificationSchema,
1350
+ sendPushDirectSchema,
1351
+ sendPushDirectTargetSchema,
1077
1352
  updateCategoryPoliciesSchema,
1078
1353
  updatePreferencesSchema,
1079
- upsertTemplateSchema
1354
+ upsertTemplateSchema,
1355
+ webPushKeysSchema,
1356
+ webPushSubscriptionSchema
1080
1357
  });
1081
1358
  //# sourceMappingURL=index.cjs.map