@dereekb/nestjs 13.31.0 → 13.32.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.
@@ -5,15 +5,15 @@ import Mailgun from 'mailgun.js';
5
5
  import FormData from 'form-data';
6
6
  import { ConfigService, ConfigModule } from '@nestjs/config';
7
7
 
8
- function _array_like_to_array$1(arr, len) {
8
+ function _array_like_to_array$2(arr, len) {
9
9
  if (len == null || len > arr.length) len = arr.length;
10
10
  for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
11
11
  return arr2;
12
12
  }
13
- function _array_with_holes(arr) {
13
+ function _array_with_holes$1(arr) {
14
14
  if (Array.isArray(arr)) return arr;
15
15
  }
16
- function _define_property$4(obj, key, value) {
16
+ function _define_property$5(obj, key, value) {
17
17
  if (key in obj) {
18
18
  Object.defineProperty(obj, key, {
19
19
  value: value,
@@ -34,7 +34,7 @@ function _instanceof(left, right) {
34
34
  return left instanceof right;
35
35
  }
36
36
  }
37
- function _iterable_to_array_limit(arr, i) {
37
+ function _iterable_to_array_limit$1(arr, i) {
38
38
  var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
39
39
  if (_i == null) return;
40
40
  var _arr = [];
@@ -58,10 +58,10 @@ function _iterable_to_array_limit(arr, i) {
58
58
  }
59
59
  return _arr;
60
60
  }
61
- function _non_iterable_rest() {
61
+ function _non_iterable_rest$1() {
62
62
  throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
63
63
  }
64
- function _object_spread$2(target) {
64
+ function _object_spread$3(target) {
65
65
  for(var i = 1; i < arguments.length; i++){
66
66
  var source = arguments[i] != null ? arguments[i] : {};
67
67
  var ownKeys = Object.keys(source);
@@ -71,7 +71,7 @@ function _object_spread$2(target) {
71
71
  }));
72
72
  }
73
73
  ownKeys.forEach(function(key) {
74
- _define_property$4(target, key, source[key]);
74
+ _define_property$5(target, key, source[key]);
75
75
  });
76
76
  }
77
77
  return target;
@@ -95,20 +95,20 @@ function _object_spread_props$1(target, source) {
95
95
  }
96
96
  return target;
97
97
  }
98
- function _sliced_to_array(arr, i) {
99
- return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array$1(arr, i) || _non_iterable_rest();
98
+ function _sliced_to_array$1(arr, i) {
99
+ return _array_with_holes$1(arr) || _iterable_to_array_limit$1(arr, i) || _unsupported_iterable_to_array$2(arr, i) || _non_iterable_rest$1();
100
100
  }
101
101
  function _type_of$1(obj) {
102
102
  "@swc/helpers - typeof";
103
103
  return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
104
104
  }
105
- function _unsupported_iterable_to_array$1(o, minLen) {
105
+ function _unsupported_iterable_to_array$2(o, minLen) {
106
106
  if (!o) return;
107
- if (typeof o === "string") return _array_like_to_array$1(o, minLen);
107
+ if (typeof o === "string") return _array_like_to_array$2(o, minLen);
108
108
  var n = Object.prototype.toString.call(o).slice(8, -1);
109
109
  if (n === "Object" && o.constructor) n = o.constructor.name;
110
110
  if (n === "Map" || n === "Set") return Array.from(n);
111
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array$1(o, minLen);
111
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array$2(o, minLen);
112
112
  }
113
113
  var DEFAULT_RECIPIENT_VARIABLE_PREFIX = 'recipient-';
114
114
  var MAILGUN_REPLY_TO_EMAIL_HEADER_DATA_VARIABLE_KEY = "h:Reply-To";
@@ -134,7 +134,7 @@ var MAILGUN_REPLY_TO_EMAIL_HEADER_DATA_VARIABLE_KEY = "h:Reply-To";
134
134
  var allowBatchSending = (_request_batchSend = request.batchSend) !== null && _request_batchSend !== void 0 ? _request_batchSend : true;
135
135
  var mergeRecipientVariablesIntoGlobalVariable = !allowBatchSending;
136
136
  function mapEmailToLowercase(x) {
137
- return _object_spread_props$1(_object_spread$2({}, x), {
137
+ return _object_spread_props$1(_object_spread$3({}, x), {
138
138
  email: x.email.toLowerCase()
139
139
  });
140
140
  }
@@ -152,7 +152,7 @@ var MAILGUN_REPLY_TO_EMAIL_HEADER_DATA_VARIABLE_KEY = "h:Reply-To";
152
152
  if (allowBatchSending && to.length > MAX_BATCH_SEND_RECIPIENTS) {
153
153
  throw new Error("convertMailgunTemplateEmailRequestToMailgunMessageData(): Can only batch send to a maximum of ".concat(MAX_BATCH_SEND_RECIPIENTS, " recipients."));
154
154
  }
155
- var data = _object_spread$2({
155
+ var data = _object_spread$3({
156
156
  from: from,
157
157
  to: to,
158
158
  cc: cc,
@@ -169,7 +169,7 @@ var MAILGUN_REPLY_TO_EMAIL_HEADER_DATA_VARIABLE_KEY = "h:Reply-To";
169
169
  if (request.templateVariables) {
170
170
  forEachKeyValue(request.templateVariables, {
171
171
  forEach: function forEach(x) {
172
- var _x = _sliced_to_array(x, 2), key = _x[0], value = _x[1];
172
+ var _x = _sliced_to_array$1(x, 2), key = _x[0], value = _x[1];
173
173
  var encodedValue = encodeMailgunTemplateVariableValue(value);
174
174
  if (encodedValue != null) {
175
175
  data["v:".concat(key)] = encodedValue;
@@ -198,7 +198,7 @@ var MAILGUN_REPLY_TO_EMAIL_HEADER_DATA_VARIABLE_KEY = "h:Reply-To";
198
198
  var decoded = JSON.parse(data['recipient-variables']);
199
199
  forEachKeyValue(decoded, {
200
200
  forEach: function forEach(x) {
201
- var _x = _sliced_to_array(x, 2), recipientEmail = _x[0], userVariables = _x[1];
201
+ var _x = _sliced_to_array$1(x, 2), recipientEmail = _x[0], userVariables = _x[1];
202
202
  var email = recipientEmail.toLowerCase();
203
203
  if (email in recipientVariables) {
204
204
  overrideInObject(recipientVariables[email], {
@@ -226,10 +226,10 @@ var MAILGUN_REPLY_TO_EMAIL_HEADER_DATA_VARIABLE_KEY = "h:Reply-To";
226
226
  // iterate all recipient variables and merge them into the global variables
227
227
  forEachKeyValue(recipientVariables, {
228
228
  forEach: function forEach(x) {
229
- var _x = _sliced_to_array(x, 2); _x[0]; var userVariables = _x[1];
229
+ var _x = _sliced_to_array$1(x, 2); _x[0]; var userVariables = _x[1];
230
230
  forEachKeyValue(userVariables, {
231
231
  forEach: function forEach(y) {
232
- var _y = _sliced_to_array(y, 2), key = _y[0], value = _y[1];
232
+ var _y = _sliced_to_array$1(y, 2), key = _y[0], value = _y[1];
233
233
  var encodedValue = encodeMailgunTemplateVariableValue(value);
234
234
  if (encodedValue != null) {
235
235
  if (addRecipientPrefixVariable) {
@@ -373,7 +373,7 @@ function _create_class$2(Constructor, protoProps, staticProps) {
373
373
  if (staticProps) _defineProperties$2(Constructor, staticProps);
374
374
  return Constructor;
375
375
  }
376
- function _define_property$3(obj, key, value) {
376
+ function _define_property$4(obj, key, value) {
377
377
  if (key in obj) {
378
378
  Object.defineProperty(obj, key, {
379
379
  value: value,
@@ -390,19 +390,19 @@ var MailgunServiceConfig = /*#__PURE__*/ function() {
390
390
  function MailgunServiceConfig() {
391
391
  _class_call_check$3(this, MailgunServiceConfig);
392
392
  // Mailgun Config
393
- _define_property$3(this, "mailgun", void 0);
393
+ _define_property$4(this, "mailgun", void 0);
394
394
  /**
395
395
  * Base URL to the client.
396
- */ _define_property$3(this, "clientUrl", void 0);
396
+ */ _define_property$4(this, "clientUrl", void 0);
397
397
  /**
398
398
  * Main domain to send emails from.
399
- */ _define_property$3(this, "domain", void 0);
399
+ */ _define_property$4(this, "domain", void 0);
400
400
  /**
401
401
  * Mailgun sender string.
402
- */ _define_property$3(this, "sender", void 0);
402
+ */ _define_property$4(this, "sender", void 0);
403
403
  /**
404
404
  * Additional messages config
405
- */ _define_property$3(this, "messages", void 0);
405
+ */ _define_property$4(this, "messages", void 0);
406
406
  }
407
407
  _create_class$2(MailgunServiceConfig, null, [
408
408
  {
@@ -444,7 +444,7 @@ function _create_class$1(Constructor, protoProps, staticProps) {
444
444
  if (protoProps) _defineProperties$1(Constructor.prototype, protoProps);
445
445
  return Constructor;
446
446
  }
447
- function _define_property$2(obj, key, value) {
447
+ function _define_property$3(obj, key, value) {
448
448
  if (key in obj) {
449
449
  Object.defineProperty(obj, key, {
450
450
  value: value,
@@ -457,7 +457,7 @@ function _define_property$2(obj, key, value) {
457
457
  }
458
458
  return obj;
459
459
  }
460
- function _object_spread$1(target) {
460
+ function _object_spread$2(target) {
461
461
  for(var i = 1; i < arguments.length; i++){
462
462
  var source = arguments[i] != null ? arguments[i] : {};
463
463
  var ownKeys = Object.keys(source);
@@ -467,7 +467,7 @@ function _object_spread$1(target) {
467
467
  }));
468
468
  }
469
469
  ownKeys.forEach(function(key) {
470
- _define_property$2(target, key, source[key]);
470
+ _define_property$3(target, key, source[key]);
471
471
  });
472
472
  }
473
473
  return target;
@@ -475,10 +475,10 @@ function _object_spread$1(target) {
475
475
  var MailgunApi = /*#__PURE__*/ function() {
476
476
  function MailgunApi(config) {
477
477
  _class_call_check$2(this, MailgunApi);
478
- _define_property$2(this, "config", void 0);
479
- _define_property$2(this, "client", void 0);
478
+ _define_property$3(this, "config", void 0);
479
+ _define_property$3(this, "client", void 0);
480
480
  this.config = config;
481
- this.client = new Mailgun(FormData).client(_object_spread$1({}, config.mailgun));
481
+ this.client = new Mailgun(FormData).client(_object_spread$2({}, config.mailgun));
482
482
  }
483
483
  _create_class$1(MailgunApi, [
484
484
  {
@@ -487,6 +487,50 @@ var MailgunApi = /*#__PURE__*/ function() {
487
487
  return this.client.messages;
488
488
  }
489
489
  },
490
+ {
491
+ key: "suppressions",
492
+ get: /**
493
+ * The domain's suppression lists (bounces, spam complaints, unsubscribes, whitelists).
494
+ *
495
+ * An address on the bounce or complaint list is silently dropped by Mailgun on every subsequent send,
496
+ * which makes this the first thing to check when diagnosing "this recipient stopped receiving email".
497
+ *
498
+ * @returns The suppressions client.
499
+ */ function get() {
500
+ return this.client.suppressions;
501
+ }
502
+ },
503
+ {
504
+ key: "events",
505
+ get: /**
506
+ * The domain's event log, used to inspect what actually happened to sent messages
507
+ * (accepted/delivered/failed/rejected/complained/unsubscribed).
508
+ *
509
+ * @returns The events client.
510
+ */ function get() {
511
+ return this.client.events;
512
+ }
513
+ },
514
+ {
515
+ key: "validate",
516
+ get: /**
517
+ * Single-address validation.
518
+ *
519
+ * @returns The validation client.
520
+ */ function get() {
521
+ return this.client.validate;
522
+ }
523
+ },
524
+ {
525
+ key: "domains",
526
+ get: /**
527
+ * Domain administration, used to read the sending domain's verification state.
528
+ *
529
+ * @returns The domains client.
530
+ */ function get() {
531
+ return this.client.domains;
532
+ }
533
+ },
490
534
  {
491
535
  key: "clientUrl",
492
536
  get: function get() {
@@ -513,6 +557,604 @@ MailgunApi = __decorate([
513
557
  __param(0, Inject(MailgunServiceConfig))
514
558
  ], MailgunApi);
515
559
 
560
+ function _array_like_to_array$1(arr, len) {
561
+ if (len == null || len > arr.length) len = arr.length;
562
+ for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
563
+ return arr2;
564
+ }
565
+ function _array_with_holes(arr) {
566
+ if (Array.isArray(arr)) return arr;
567
+ }
568
+ function asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, key, arg) {
569
+ try {
570
+ var info = gen[key](arg);
571
+ var value = info.value;
572
+ } catch (error) {
573
+ reject(error);
574
+ return;
575
+ }
576
+ if (info.done) {
577
+ resolve(value);
578
+ } else {
579
+ Promise.resolve(value).then(_next, _throw);
580
+ }
581
+ }
582
+ function _async_to_generator$1(fn) {
583
+ return function() {
584
+ var self = this, args = arguments;
585
+ return new Promise(function(resolve, reject) {
586
+ var gen = fn.apply(self, args);
587
+ function _next(value) {
588
+ asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, "next", value);
589
+ }
590
+ function _throw(err) {
591
+ asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, "throw", err);
592
+ }
593
+ _next(undefined);
594
+ });
595
+ };
596
+ }
597
+ function _define_property$2(obj, key, value) {
598
+ if (key in obj) {
599
+ Object.defineProperty(obj, key, {
600
+ value: value,
601
+ enumerable: true,
602
+ configurable: true,
603
+ writable: true
604
+ });
605
+ } else {
606
+ obj[key] = value;
607
+ }
608
+ return obj;
609
+ }
610
+ function _iterable_to_array_limit(arr, i) {
611
+ var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
612
+ if (_i == null) return;
613
+ var _arr = [];
614
+ var _n = true;
615
+ var _d = false;
616
+ var _s, _e;
617
+ try {
618
+ for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
619
+ _arr.push(_s.value);
620
+ if (i && _arr.length === i) break;
621
+ }
622
+ } catch (err) {
623
+ _d = true;
624
+ _e = err;
625
+ } finally{
626
+ try {
627
+ if (!_n && _i["return"] != null) _i["return"]();
628
+ } finally{
629
+ if (_d) throw _e;
630
+ }
631
+ }
632
+ return _arr;
633
+ }
634
+ function _non_iterable_rest() {
635
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
636
+ }
637
+ function _object_spread$1(target) {
638
+ for(var i = 1; i < arguments.length; i++){
639
+ var source = arguments[i] != null ? arguments[i] : {};
640
+ var ownKeys = Object.keys(source);
641
+ if (typeof Object.getOwnPropertySymbols === "function") {
642
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
643
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
644
+ }));
645
+ }
646
+ ownKeys.forEach(function(key) {
647
+ _define_property$2(target, key, source[key]);
648
+ });
649
+ }
650
+ return target;
651
+ }
652
+ function _sliced_to_array(arr, i) {
653
+ return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array$1(arr, i) || _non_iterable_rest();
654
+ }
655
+ function _unsupported_iterable_to_array$1(o, minLen) {
656
+ if (!o) return;
657
+ if (typeof o === "string") return _array_like_to_array$1(o, minLen);
658
+ var n = Object.prototype.toString.call(o).slice(8, -1);
659
+ if (n === "Object" && o.constructor) n = o.constructor.name;
660
+ if (n === "Map" || n === "Set") return Array.from(n);
661
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array$1(o, minLen);
662
+ }
663
+ function _ts_generator$1(thisArg, body) {
664
+ var f, y, t, _ = {
665
+ label: 0,
666
+ sent: function() {
667
+ if (t[0] & 1) throw t[1];
668
+ return t[1];
669
+ },
670
+ trys: [],
671
+ ops: []
672
+ }, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype), d = Object.defineProperty;
673
+ return d(g, "next", {
674
+ value: verb(0)
675
+ }), d(g, "throw", {
676
+ value: verb(1)
677
+ }), d(g, "return", {
678
+ value: verb(2)
679
+ }), typeof Symbol === "function" && d(g, Symbol.iterator, {
680
+ value: function() {
681
+ return this;
682
+ }
683
+ }), g;
684
+ function verb(n) {
685
+ return function(v) {
686
+ return step([
687
+ n,
688
+ v
689
+ ]);
690
+ };
691
+ }
692
+ function step(op) {
693
+ if (f) throw new TypeError("Generator is already executing.");
694
+ while(g && (g = 0, op[0] && (_ = 0)), _)try {
695
+ 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;
696
+ if (y = 0, t) op = [
697
+ op[0] & 2,
698
+ t.value
699
+ ];
700
+ switch(op[0]){
701
+ case 0:
702
+ case 1:
703
+ t = op;
704
+ break;
705
+ case 4:
706
+ _.label++;
707
+ return {
708
+ value: op[1],
709
+ done: false
710
+ };
711
+ case 5:
712
+ _.label++;
713
+ y = op[1];
714
+ op = [
715
+ 0
716
+ ];
717
+ continue;
718
+ case 7:
719
+ op = _.ops.pop();
720
+ _.trys.pop();
721
+ continue;
722
+ default:
723
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
724
+ _ = 0;
725
+ continue;
726
+ }
727
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
728
+ _.label = op[1];
729
+ break;
730
+ }
731
+ if (op[0] === 6 && _.label < t[1]) {
732
+ _.label = t[1];
733
+ t = op;
734
+ break;
735
+ }
736
+ if (t && _.label < t[2]) {
737
+ _.label = t[2];
738
+ _.ops.push(op);
739
+ break;
740
+ }
741
+ if (t[2]) _.ops.pop();
742
+ _.trys.pop();
743
+ continue;
744
+ }
745
+ op = body.call(thisArg, _);
746
+ } catch (e) {
747
+ op = [
748
+ 6,
749
+ e
750
+ ];
751
+ y = 0;
752
+ } finally{
753
+ f = t = 0;
754
+ }
755
+ if (op[0] & 5) throw op[1];
756
+ return {
757
+ value: op[0] ? op[1] : void 0,
758
+ done: true
759
+ };
760
+ }
761
+ }
762
+ /**
763
+ * The default number of events to read back when inspecting a recipient's recent activity.
764
+ */ var DEFAULT_MAILGUN_RECENT_EVENTS_LIMIT = 25;
765
+ /**
766
+ * The default window of history to inspect when reading a recipient's recent events.
767
+ */ var DEFAULT_MAILGUN_RECENT_EVENTS_WINDOW_DAYS = 30;
768
+ /**
769
+ * Mailgun event names relevant to delivery diagnosis.
770
+ *
771
+ * Mailgun returns these as free-form strings; this enum names the ones that carry
772
+ * delivery meaning. Unknown values are passed through untouched.
773
+ */ var MailgunEventName;
774
+ (function(MailgunEventName) {
775
+ MailgunEventName["ACCEPTED"] = "accepted";
776
+ MailgunEventName["DELIVERED"] = "delivered";
777
+ MailgunEventName["FAILED"] = "failed";
778
+ MailgunEventName["REJECTED"] = "rejected";
779
+ MailgunEventName["COMPLAINED"] = "complained";
780
+ MailgunEventName["UNSUBSCRIBED"] = "unsubscribed";
781
+ MailgunEventName["OPENED"] = "opened";
782
+ MailgunEventName["CLICKED"] = "clicked";
783
+ MailgunEventName["STORED"] = "stored";
784
+ })(MailgunEventName || (MailgunEventName = {}));
785
+ /**
786
+ * Mailgun failure severities. A `permanent` failure will never succeed on retry;
787
+ * a `temporary` one may.
788
+ */ var MailgunEventSeverity;
789
+ (function(MailgunEventSeverity) {
790
+ MailgunEventSeverity["PERMANENT"] = "permanent";
791
+ MailgunEventSeverity["TEMPORARY"] = "temporary";
792
+ })(MailgunEventSeverity || (MailgunEventSeverity = {}));
793
+ /**
794
+ * True if any suppression record was found for the address.
795
+ *
796
+ * @param suppressions - The suppression records to check.
797
+ * @returns True if the address appears on any of the domain's suppression lists.
798
+ */ function hasAnyMailgunRecipientSuppression(suppressions) {
799
+ return suppressions.bounce != null || suppressions.complaint != null || suppressions.unsubscribe != null;
800
+ }
801
+ /**
802
+ * Reads a single suppression entry, treating "not found" as an absent value.
803
+ *
804
+ * The Mailgun API responds 404 when the address is not on the requested list, which is the common case
805
+ * and not an error condition here.
806
+ *
807
+ * @param read - Performs the suppression lookup.
808
+ * @returns The suppression record, or undefined if the address is not on the list or the lookup failed.
809
+ */ function readSuppression(read) {
810
+ return _async_to_generator$1(function() {
811
+ var result;
812
+ return _ts_generator$1(this, function(_state) {
813
+ switch(_state.label){
814
+ case 0:
815
+ _state.trys.push([
816
+ 0,
817
+ 2,
818
+ ,
819
+ 3
820
+ ]);
821
+ return [
822
+ 4,
823
+ read()
824
+ ];
825
+ case 1:
826
+ result = _state.sent();
827
+ return [
828
+ 3,
829
+ 3
830
+ ];
831
+ case 2:
832
+ _state.sent();
833
+ result = undefined;
834
+ return [
835
+ 3,
836
+ 3
837
+ ];
838
+ case 3:
839
+ return [
840
+ 2,
841
+ result
842
+ ];
843
+ }
844
+ });
845
+ })();
846
+ }
847
+ /**
848
+ * Looks up an address across the domain's bounce, complaint, and unsubscribe lists.
849
+ *
850
+ * @param api - The Mailgun API.
851
+ * @param email - The address to look up.
852
+ * @returns The suppression records found for the address. Absent fields mean the address is not on that list.
853
+ *
854
+ * @example
855
+ * ```ts
856
+ * const suppressions = await mailgunSuppressionsForRecipient(api, 'user@example.com');
857
+ *
858
+ * if (suppressions.bounce) {
859
+ * console.log(`suppressed by bounce: ${suppressions.bounce.error}`);
860
+ * }
861
+ * ```
862
+ */ function mailgunSuppressionsForRecipient(api, email) {
863
+ return _async_to_generator$1(function() {
864
+ var domain, suppressions, _ref, bounce, complaint, unsubscribe;
865
+ return _ts_generator$1(this, function(_state) {
866
+ switch(_state.label){
867
+ case 0:
868
+ domain = api.domain, suppressions = api.suppressions;
869
+ return [
870
+ 4,
871
+ Promise.all([
872
+ //
873
+ readSuppression(function() {
874
+ return suppressions.get(domain, 'bounces', email);
875
+ }),
876
+ readSuppression(function() {
877
+ return suppressions.get(domain, 'complaints', email);
878
+ }),
879
+ readSuppression(function() {
880
+ return suppressions.get(domain, 'unsubscribes', email);
881
+ })
882
+ ])
883
+ ];
884
+ case 1:
885
+ _ref = _sliced_to_array.apply(void 0, [
886
+ _state.sent(),
887
+ 3
888
+ ]), bounce = _ref[0], complaint = _ref[1], unsubscribe = _ref[2];
889
+ return [
890
+ 2,
891
+ {
892
+ bounce: bounce,
893
+ complaint: complaint,
894
+ unsubscribe: unsubscribe
895
+ }
896
+ ];
897
+ }
898
+ });
899
+ })();
900
+ }
901
+ /**
902
+ * Reads the most recent Mailgun events for a recipient address, newest first.
903
+ *
904
+ * @param api - The Mailgun API.
905
+ * @param email - The recipient address to filter on.
906
+ * @param config - Optional limit/window/event filters.
907
+ * @returns The matching events, newest first. Empty if there is no recent activity or the lookup failed.
908
+ *
909
+ * @example
910
+ * ```ts
911
+ * const events = await mailgunRecentEventsForRecipient(api, 'user@example.com', { limit: 10 });
912
+ * const lastDelivered = events.find((x) => x.event === MailgunEventName.DELIVERED);
913
+ * ```
914
+ */ function mailgunRecentEventsForRecipient(api, email, config) {
915
+ return _async_to_generator$1(function() {
916
+ var _ref, _ref1, limit, begin, query;
917
+ return _ts_generator$1(this, function(_state) {
918
+ limit = (_ref = config === null || config === void 0 ? void 0 : config.limit) !== null && _ref !== void 0 ? _ref : DEFAULT_MAILGUN_RECENT_EVENTS_LIMIT;
919
+ begin = (_ref1 = config === null || config === void 0 ? void 0 : config.begin) !== null && _ref1 !== void 0 ? _ref1 : new Date(Date.now() - DEFAULT_MAILGUN_RECENT_EVENTS_WINDOW_DAYS * 24 * 60 * 60 * 1000);
920
+ query = {
921
+ recipient: email,
922
+ begin: begin.toUTCString(),
923
+ ascending: 'no',
924
+ limit: limit
925
+ };
926
+ if (config === null || config === void 0 ? void 0 : config.event) {
927
+ query.event = config.event;
928
+ }
929
+ return [
930
+ 2,
931
+ mailgunEventsForQuery(api, query)
932
+ ];
933
+ });
934
+ })();
935
+ }
936
+ /**
937
+ * Strips the angle brackets Mailgun's send response wraps a message id in.
938
+ *
939
+ * `messages.create()` returns an id shaped like `<20240101120000.1.abc@domain>`, while the
940
+ * Events API `message-id` filter expects the bare `20240101120000.1.abc@domain`. Passing the
941
+ * bracketed form matches nothing.
942
+ *
943
+ * @param messageId - A message id in either form.
944
+ * @returns The message id without surrounding angle brackets.
945
+ */ function bareMailgunMessageId(messageId) {
946
+ return messageId.replaceAll(/^<|>$/g, '');
947
+ }
948
+ /**
949
+ * Reads all Mailgun events recorded for a specific message id.
950
+ *
951
+ * Used to resolve the outcome of a message that was already sent — the Events API lags the
952
+ * send by seconds to minutes, so an empty result means "not known yet", not "not delivered".
953
+ *
954
+ * @param api - The Mailgun API.
955
+ * @param messageId - The message id, with or without angle brackets.
956
+ * @param config - Optional additional filters (e.g. `recipient` to scope the lookup).
957
+ * @returns The events recorded for the message. Empty if none are recorded yet or the lookup failed.
958
+ */ function mailgunEventsForMessageId(api, messageId, config) {
959
+ return _async_to_generator$1(function() {
960
+ var query;
961
+ return _ts_generator$1(this, function(_state) {
962
+ query = _object_spread$1({
963
+ 'message-id': bareMailgunMessageId(messageId),
964
+ ascending: 'no'
965
+ }, config);
966
+ return [
967
+ 2,
968
+ mailgunEventsForQuery(api, query)
969
+ ];
970
+ });
971
+ })();
972
+ }
973
+ /**
974
+ * Runs an arbitrary Events API query, returning an empty list if the lookup fails.
975
+ *
976
+ * @param api - The Mailgun API.
977
+ * @param query - The events query.
978
+ * @returns The matching events, or an empty array on failure.
979
+ */ function mailgunEventsForQuery(api, query) {
980
+ return _async_to_generator$1(function() {
981
+ var events, _result_items, result;
982
+ return _ts_generator$1(this, function(_state) {
983
+ switch(_state.label){
984
+ case 0:
985
+ _state.trys.push([
986
+ 0,
987
+ 2,
988
+ ,
989
+ 3
990
+ ]);
991
+ return [
992
+ 4,
993
+ api.events.get(api.domain, query)
994
+ ];
995
+ case 1:
996
+ result = _state.sent();
997
+ events = (_result_items = result.items) !== null && _result_items !== void 0 ? _result_items : [];
998
+ return [
999
+ 3,
1000
+ 3
1001
+ ];
1002
+ case 2:
1003
+ _state.sent();
1004
+ events = [];
1005
+ return [
1006
+ 3,
1007
+ 3
1008
+ ];
1009
+ case 3:
1010
+ return [
1011
+ 2,
1012
+ events
1013
+ ];
1014
+ }
1015
+ });
1016
+ })();
1017
+ }
1018
+ /**
1019
+ * Reads the sending domain's state.
1020
+ *
1021
+ * A domain that is not `active` will fail to deliver regardless of recipient configuration,
1022
+ * so this distinguishes a system-wide outage from a per-recipient problem.
1023
+ *
1024
+ * @param api - The Mailgun API.
1025
+ * @returns The domain's state, with `unknown` set if it could not be read.
1026
+ */ function mailgunDomainState(api) {
1027
+ return _async_to_generator$1(function() {
1028
+ var domain, state, result;
1029
+ return _ts_generator$1(this, function(_state) {
1030
+ switch(_state.label){
1031
+ case 0:
1032
+ domain = api.domain;
1033
+ _state.label = 1;
1034
+ case 1:
1035
+ _state.trys.push([
1036
+ 1,
1037
+ 3,
1038
+ ,
1039
+ 4
1040
+ ]);
1041
+ return [
1042
+ 4,
1043
+ api.domains.get(domain)
1044
+ ];
1045
+ case 2:
1046
+ result = _state.sent();
1047
+ state = {
1048
+ domain: domain,
1049
+ state: result === null || result === void 0 ? void 0 : result.state,
1050
+ disabled: result === null || result === void 0 ? void 0 : result.is_disabled
1051
+ };
1052
+ return [
1053
+ 3,
1054
+ 4
1055
+ ];
1056
+ case 3:
1057
+ _state.sent();
1058
+ state = {
1059
+ domain: domain,
1060
+ unknown: true
1061
+ };
1062
+ return [
1063
+ 3,
1064
+ 4
1065
+ ];
1066
+ case 4:
1067
+ return [
1068
+ 2,
1069
+ state
1070
+ ];
1071
+ }
1072
+ });
1073
+ })();
1074
+ }
1075
+ /**
1076
+ * Validates a single email address via the Mailgun Validation API.
1077
+ *
1078
+ * Note that validation consumes Mailgun validation quota, so callers should treat this as
1079
+ * an opt-in check rather than something to run on every request.
1080
+ *
1081
+ * @param api - The Mailgun API.
1082
+ * @param email - The address to validate.
1083
+ * @returns The validation result, or undefined if validation is unavailable or failed.
1084
+ */ function mailgunValidateEmail(api, email) {
1085
+ return _async_to_generator$1(function() {
1086
+ var result;
1087
+ return _ts_generator$1(this, function(_state) {
1088
+ switch(_state.label){
1089
+ case 0:
1090
+ _state.trys.push([
1091
+ 0,
1092
+ 2,
1093
+ ,
1094
+ 3
1095
+ ]);
1096
+ return [
1097
+ 4,
1098
+ api.validate.get(email)
1099
+ ];
1100
+ case 1:
1101
+ result = _state.sent();
1102
+ return [
1103
+ 3,
1104
+ 3
1105
+ ];
1106
+ case 2:
1107
+ _state.sent();
1108
+ result = undefined;
1109
+ return [
1110
+ 3,
1111
+ 3
1112
+ ];
1113
+ case 3:
1114
+ return [
1115
+ 2,
1116
+ result
1117
+ ];
1118
+ }
1119
+ });
1120
+ })();
1121
+ }
1122
+ /**
1123
+ * Converts a Mailgun event's unix-seconds timestamp to a Date.
1124
+ *
1125
+ * @param event - The event.
1126
+ * @returns The event's timestamp as a Date.
1127
+ */ function mailgunDomainEventDate(event) {
1128
+ return new Date(event.timestamp * 1000);
1129
+ }
1130
+ /**
1131
+ * The age of a Mailgun event in milliseconds relative to `now`.
1132
+ *
1133
+ * @param event - The event.
1134
+ * @param now - The reference time. Defaults to the current time.
1135
+ * @returns The event's age in milliseconds.
1136
+ */ function mailgunDomainEventAge(event) {
1137
+ var now = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : new Date();
1138
+ return now.getTime() - mailgunDomainEventDate(event).getTime();
1139
+ }
1140
+ /**
1141
+ * Extracts the most useful human-readable reason from a failed/rejected Mailgun event.
1142
+ *
1143
+ * Mailgun spreads the explanation across `reason`, `delivery-status.description`, and
1144
+ * `delivery-status.message` depending on how the failure occurred, and any of them may be
1145
+ * empty.
1146
+ *
1147
+ * @param event - The event.
1148
+ * @returns The best available description, or undefined if the event carries none.
1149
+ */ function mailgunDomainEventFailureReason(event) {
1150
+ var _ref;
1151
+ var deliveryStatus = event['delivery-status'];
1152
+ var description = (deliveryStatus === null || deliveryStatus === void 0 ? void 0 : deliveryStatus.description) || undefined;
1153
+ var message = (deliveryStatus === null || deliveryStatus === void 0 ? void 0 : deliveryStatus.message) || undefined;
1154
+ var reason = event.reason || undefined;
1155
+ return (_ref = description !== null && description !== void 0 ? description : message) !== null && _ref !== void 0 ? _ref : reason;
1156
+ }
1157
+
516
1158
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
517
1159
  try {
518
1160
  var info = gen[key](arg);
@@ -1131,4 +1773,4 @@ function _unsupported_iterable_to_array(o, minLen) {
1131
1773
  };
1132
1774
  }
1133
1775
 
1134
- export { DEFAULT_RECIPIENT_VARIABLE_PREFIX, MAILGUN_BATCH_SEND_RECIPIENT_SUBJECT_TEMPLATE, MAILGUN_REPLY_TO_EMAIL_HEADER_DATA_VARIABLE_KEY, MAX_BATCH_SEND_RECIPIENTS, MailgunApi, MailgunService, MailgunServiceModule, convertMailgunRecipientToString, convertMailgunRecipientsToStrings, convertMailgunTemplateEmailRequestToMailgunMessageData, encodeMailgunTemplateVariableValue, expandMailgunRecipientBatchSendTargetRequestFactory, mailgunRecipientBatchSendTargetEntityKeyRecipientLookup, mailgunRecipientBatchSendTargetFromReplyToBatchGroupKey, mailgunServiceConfigFactory };
1776
+ export { DEFAULT_MAILGUN_RECENT_EVENTS_LIMIT, DEFAULT_MAILGUN_RECENT_EVENTS_WINDOW_DAYS, DEFAULT_RECIPIENT_VARIABLE_PREFIX, MAILGUN_BATCH_SEND_RECIPIENT_SUBJECT_TEMPLATE, MAILGUN_REPLY_TO_EMAIL_HEADER_DATA_VARIABLE_KEY, MAX_BATCH_SEND_RECIPIENTS, MailgunApi, MailgunEventName, MailgunEventSeverity, MailgunService, MailgunServiceModule, bareMailgunMessageId, convertMailgunRecipientToString, convertMailgunRecipientsToStrings, convertMailgunTemplateEmailRequestToMailgunMessageData, encodeMailgunTemplateVariableValue, expandMailgunRecipientBatchSendTargetRequestFactory, hasAnyMailgunRecipientSuppression, mailgunDomainEventAge, mailgunDomainEventDate, mailgunDomainEventFailureReason, mailgunDomainState, mailgunEventsForMessageId, mailgunEventsForQuery, mailgunRecentEventsForRecipient, mailgunRecipientBatchSendTargetEntityKeyRecipientLookup, mailgunRecipientBatchSendTargetFromReplyToBatchGroupKey, mailgunServiceConfigFactory, mailgunSuppressionsForRecipient, mailgunValidateEmail };