@terreno/api 0.26.0 → 0.28.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.
Files changed (122) hide show
  1. package/dist/__tests__/openApiCompat.test.js +8 -2
  2. package/dist/actions.openApi.test.js +1 -0
  3. package/dist/actions.test.js +3 -1
  4. package/dist/api.arrayOperations.test.js +1 -0
  5. package/dist/api.coverage.test.d.ts +1 -0
  6. package/dist/api.coverage.test.js +193 -0
  7. package/dist/api.errorPaths.test.d.ts +1 -0
  8. package/dist/api.errorPaths.test.js +280 -0
  9. package/dist/api.errors.test.js +7 -7
  10. package/dist/api.hooks.test.js +66 -8
  11. package/dist/api.js +245 -206
  12. package/dist/api.query.test.js +1 -0
  13. package/dist/api.test.js +11 -4
  14. package/dist/auth.js +1 -0
  15. package/dist/auth.test.js +123 -1
  16. package/dist/betterAuthSetup.js +1 -0
  17. package/dist/betterAuthSetup.test.js +1 -0
  18. package/dist/config.js +7 -4
  19. package/dist/config.test.js +28 -3
  20. package/dist/configuration.test.js +1 -0
  21. package/dist/configurationApp.js +1 -0
  22. package/dist/configurationPlugin.test.js +80 -0
  23. package/dist/consentApp.test.js +1 -0
  24. package/dist/docLoader.js +16 -54
  25. package/dist/docLoader.test.js +2 -1
  26. package/dist/envConfigurationPlugin.js +1 -0
  27. package/dist/errors.d.ts +72 -5
  28. package/dist/errors.js +265 -67
  29. package/dist/errors.test.js +163 -13
  30. package/dist/example.js +3 -2
  31. package/dist/expressServer.test.js +1 -0
  32. package/dist/githubAuth.js +2 -0
  33. package/dist/githubAuth.test.js +1 -0
  34. package/dist/httpClient.d.ts +2 -3
  35. package/dist/httpClient.js +4 -4
  36. package/dist/httpClient.test.js +3 -2
  37. package/dist/logger.test.js +107 -0
  38. package/dist/notifiers/googleChatNotifier.test.js +2 -1
  39. package/dist/notifiers/slackNotifier.test.js +1 -1
  40. package/dist/notifiers/zoomNotifier.test.js +2 -1
  41. package/dist/openApi.test.js +1 -0
  42. package/dist/openApiBuilder.js +2 -1
  43. package/dist/openApiBuilder.test.js +3 -2
  44. package/dist/openApiValidator.js +1 -0
  45. package/dist/openApiValidator.test.js +41 -0
  46. package/dist/permissions.js +0 -2
  47. package/dist/permissions.middleware.test.js +8 -42
  48. package/dist/permissions.test.js +1 -0
  49. package/dist/plugins.js +8 -0
  50. package/dist/plugins.test.js +5 -2
  51. package/dist/populate.js +4 -2
  52. package/dist/populate.test.js +1 -0
  53. package/dist/realtime/realtime.test.js +13 -3
  54. package/dist/realtime/types.d.ts +1 -1
  55. package/dist/requestContext.test.js +28 -0
  56. package/dist/terrenoApp.js +1 -0
  57. package/dist/tests/createTestData.js +11 -6
  58. package/dist/tests/models.js +1 -0
  59. package/dist/transformers.js +4 -2
  60. package/dist/vendor/wesleytodd-openapi/lib/generate-doc.js +3 -1
  61. package/package.json +7 -7
  62. package/src/__tests__/openApiCompat.test.ts +8 -2
  63. package/src/actions.openApi.test.ts +1 -0
  64. package/src/actions.test.ts +3 -1
  65. package/src/api.arrayOperations.test.ts +1 -0
  66. package/src/api.coverage.test.ts +123 -0
  67. package/src/api.errorPaths.test.ts +173 -0
  68. package/src/api.errors.test.ts +7 -7
  69. package/src/api.hooks.test.ts +25 -1
  70. package/src/api.query.test.ts +1 -0
  71. package/src/api.test.ts +11 -4
  72. package/src/api.ts +106 -57
  73. package/src/auth.test.ts +92 -3
  74. package/src/auth.ts +6 -0
  75. package/src/betterAuthSetup.test.ts +1 -0
  76. package/src/betterAuthSetup.ts +3 -0
  77. package/src/config.test.ts +25 -3
  78. package/src/config.ts +7 -4
  79. package/src/configuration.test.ts +1 -0
  80. package/src/configurationApp.ts +2 -0
  81. package/src/configurationPlugin.test.ts +46 -1
  82. package/src/consentApp.test.ts +1 -0
  83. package/src/docLoader.test.ts +2 -1
  84. package/src/docLoader.ts +17 -20
  85. package/src/envConfigurationPlugin.ts +2 -0
  86. package/src/errors.test.ts +178 -13
  87. package/src/errors.ts +278 -79
  88. package/src/example.ts +3 -2
  89. package/src/expressServer.test.ts +1 -0
  90. package/src/expressServer.ts +4 -2
  91. package/src/githubAuth.test.ts +1 -0
  92. package/src/githubAuth.ts +2 -0
  93. package/src/httpClient.test.ts +3 -2
  94. package/src/httpClient.ts +4 -4
  95. package/src/logger.test.ts +78 -0
  96. package/src/notifiers/googleChatNotifier.test.ts +2 -2
  97. package/src/notifiers/slackNotifier.test.ts +2 -2
  98. package/src/notifiers/zoomNotifier.test.ts +2 -2
  99. package/src/openApi.test.ts +1 -0
  100. package/src/openApiBuilder.test.ts +7 -6
  101. package/src/openApiBuilder.ts +2 -1
  102. package/src/openApiValidator.test.ts +64 -0
  103. package/src/openApiValidator.ts +1 -0
  104. package/src/permissions.middleware.test.ts +8 -11
  105. package/src/permissions.test.ts +1 -0
  106. package/src/permissions.ts +0 -2
  107. package/src/plugins.test.ts +5 -2
  108. package/src/plugins.ts +9 -0
  109. package/src/populate.test.ts +1 -0
  110. package/src/populate.ts +4 -2
  111. package/src/realtime/realtime.test.ts +13 -3
  112. package/src/realtime/realtimeApp.ts +1 -0
  113. package/src/realtime/registry.ts +1 -0
  114. package/src/realtime/types.ts +1 -2
  115. package/src/requestContext.test.ts +32 -0
  116. package/src/terrenoApp.ts +1 -0
  117. package/src/tests/createTestData.ts +11 -6
  118. package/src/tests/models.ts +1 -0
  119. package/src/transformers.ts +4 -2
  120. package/src/vendor/wesleytodd-openapi/lib/generate-doc.js +4 -1
  121. package/tsconfig.json +4 -4
  122. /package/{types.d.ts → src/types.d.ts} +0 -0
package/dist/api.js CHANGED
@@ -142,6 +142,7 @@ var registry_1 = require("./realtime/registry");
142
142
  var transformers_1 = require("./transformers");
143
143
  var utils_1 = require("./utils");
144
144
  var addPopulateToQuery = function (
145
+ // noExplicitAny: mongoose Query type parameters vary widely across populated/unpopulated documents — caller passes concrete types
145
146
  // biome-ignore lint/suspicious/noExplicitAny: mongoose Query type parameters vary widely across populated/unpopulated documents — caller passes concrete types
146
147
  builtQuery, populatePaths) {
147
148
  var e_1, _a;
@@ -206,7 +207,7 @@ var mergeDateRangeQueryParams = function (model, query) {
206
207
  }
207
208
  var baseField = match[1];
208
209
  var path = schema.path(baseField);
209
- if (!path || path.instance !== "Date") {
210
+ if ((path === null || path === void 0 ? void 0 : path.instance) !== "Date") {
210
211
  continue;
211
212
  }
212
213
  dateRangeBases.add(baseField);
@@ -450,10 +451,12 @@ function _buildModelRouter(model, options) {
450
451
  throw error;
451
452
  }
452
453
  throw new errors_1.APIError({
454
+ cause: error,
455
+ code: "transform-error",
456
+ detail: (0, errors_1.errorMessage)(error),
453
457
  disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error),
454
- error: error,
455
458
  status: 400,
456
- title: (0, errors_1.errorMessage)(error),
459
+ title: "Transform error",
457
460
  });
458
461
  }
459
462
  if (!options.preCreate) return [3 /*break*/, 5];
@@ -470,10 +473,12 @@ function _buildModelRouter(model, options) {
470
473
  throw error_1;
471
474
  }
472
475
  throw new errors_1.APIError({
476
+ cause: error_1,
477
+ code: "pre-create-hook-error",
478
+ detail: (0, errors_1.errorMessage)(error_1),
473
479
  disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_1),
474
- error: error_1,
475
480
  status: 400,
476
- title: "preCreate hook error: ".concat((0, errors_1.errorMessage)(error_1)),
481
+ title: "preCreate hook error",
477
482
  });
478
483
  case 4:
479
484
  if (body === undefined) {
@@ -509,10 +514,12 @@ function _buildModelRouter(model, options) {
509
514
  case 8:
510
515
  error_2 = _a.sent();
511
516
  throw new errors_1.APIError({
517
+ cause: error_2,
518
+ code: "create-error",
519
+ detail: (0, errors_1.errorMessage)(error_2),
512
520
  disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_2),
513
- error: error_2,
514
521
  status: 400,
515
- title: (0, errors_1.errorMessage)(error_2),
522
+ title: "Create error",
516
523
  });
517
524
  case 9:
518
525
  if (!options.populatePaths) return [3 /*break*/, 13];
@@ -528,10 +535,12 @@ function _buildModelRouter(model, options) {
528
535
  case 12:
529
536
  error_3 = _a.sent();
530
537
  throw new errors_1.APIError({
538
+ cause: error_3,
539
+ code: "populate-error",
540
+ detail: (0, errors_1.errorMessage)(error_3),
531
541
  disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_3),
532
- error: error_3,
533
542
  status: 400,
534
- title: "Populate error: ".concat((0, errors_1.errorMessage)(error_3)),
543
+ title: "Populate error",
535
544
  });
536
545
  case 13:
537
546
  if (!options.postCreate) return [3 /*break*/, 17];
@@ -545,10 +554,12 @@ function _buildModelRouter(model, options) {
545
554
  case 16:
546
555
  error_4 = _a.sent();
547
556
  throw new errors_1.APIError({
557
+ cause: error_4,
558
+ code: "post-create-hook-error",
559
+ detail: (0, errors_1.errorMessage)(error_4),
548
560
  disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_4),
549
- error: error_4,
550
561
  status: 400,
551
- title: "postCreate hook error: ".concat((0, errors_1.errorMessage)(error_4)),
562
+ title: "postCreate hook error",
552
563
  });
553
564
  case 17:
554
565
  _a.trys.push([17, 19, , 20]);
@@ -559,9 +570,11 @@ function _buildModelRouter(model, options) {
559
570
  case 19:
560
571
  error_5 = _a.sent();
561
572
  throw new errors_1.APIError({
573
+ cause: error_5,
574
+ code: "response-handler-error",
575
+ detail: (0, errors_1.errorMessage)(error_5),
562
576
  disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_5),
563
- error: error_5,
564
- title: "responseHandler error: ".concat((0, errors_1.errorMessage)(error_5)),
577
+ title: "responseHandler error",
565
578
  });
566
579
  case 20: return [2 /*return*/];
567
580
  }
@@ -640,10 +653,12 @@ function _buildModelRouter(model, options) {
640
653
  case 3:
641
654
  error_6 = _o.sent();
642
655
  throw new errors_1.APIError({
656
+ cause: error_6,
657
+ code: "query-filter-error",
658
+ detail: String(error_6),
643
659
  disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_6),
644
- error: error_6,
645
660
  status: 400,
646
- title: "Query filter error: ".concat(error_6),
661
+ title: "Query filter error",
647
662
  });
648
663
  case 4:
649
664
  // If the query filter returns null specifically, we know this is a query that shouldn't
@@ -694,9 +709,11 @@ function _buildModelRouter(model, options) {
694
709
  case 9:
695
710
  error_7 = _o.sent();
696
711
  throw new errors_1.APIError({
712
+ cause: error_7,
713
+ code: "list-error",
714
+ detail: (0, errors_1.errorMessage)(error_7),
697
715
  disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_7),
698
- error: error_7,
699
- title: "List error: ".concat((0, errors_1.errorStack)(error_7)),
716
+ title: "List error",
700
717
  });
701
718
  case 10:
702
719
  _o.trys.push([10, 12, , 13]);
@@ -707,9 +724,11 @@ function _buildModelRouter(model, options) {
707
724
  case 12:
708
725
  error_8 = _o.sent();
709
726
  throw new errors_1.APIError({
727
+ cause: error_8,
728
+ code: "response-handler-error",
729
+ detail: (0, errors_1.errorMessage)(error_8),
710
730
  disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_8),
711
- error: error_8,
712
- title: "responseHandler error: ".concat((0, errors_1.errorMessage)(error_8)),
731
+ title: "responseHandler error",
713
732
  });
714
733
  case 13:
715
734
  try {
@@ -741,9 +760,11 @@ function _buildModelRouter(model, options) {
741
760
  }
742
761
  catch (error) {
743
762
  throw new errors_1.APIError({
763
+ cause: error,
764
+ code: "serialization-error",
765
+ detail: (0, errors_1.errorMessage)(error),
744
766
  disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error),
745
- error: error,
746
- title: "Serialization error: ".concat((0, errors_1.errorMessage)(error)),
767
+ title: "Serialization error",
747
768
  });
748
769
  }
749
770
  return [2 /*return*/];
@@ -770,9 +791,11 @@ function _buildModelRouter(model, options) {
770
791
  case 3:
771
792
  error_9 = _a.sent();
772
793
  throw new errors_1.APIError({
794
+ cause: error_9,
795
+ code: "response-handler-error",
796
+ detail: (0, errors_1.errorMessage)(error_9),
773
797
  disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_9),
774
- error: error_9,
775
- title: "responseHandler error: ".concat((0, errors_1.errorMessage)(error_9)),
798
+ title: "responseHandler error",
776
799
  });
777
800
  case 4: return [2 /*return*/];
778
801
  }
@@ -806,10 +829,12 @@ function _buildModelRouter(model, options) {
806
829
  throw error;
807
830
  }
808
831
  throw new errors_1.APIError({
832
+ cause: error,
833
+ code: "transform-error",
834
+ detail: "PATCH failed on ".concat(req.params.id, " for user ").concat((_a = req.user) === null || _a === void 0 ? void 0 : _a.id, ": ").concat((0, errors_1.errorMessage)(error)),
809
835
  disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error),
810
- error: error,
811
836
  status: 403,
812
- title: "PATCH failed on ".concat(req.params.id, " for user ").concat((_a = req.user) === null || _a === void 0 ? void 0 : _a.id, ": ").concat((0, errors_1.errorMessage)(error)),
837
+ title: "PATCH failed",
813
838
  });
814
839
  }
815
840
  bodyUpdatedAt = req.body._updatedAt;
@@ -831,10 +856,12 @@ function _buildModelRouter(model, options) {
831
856
  throw error_10;
832
857
  }
833
858
  throw new errors_1.APIError({
859
+ cause: error_10,
860
+ code: "pre-update-hook-error",
861
+ detail: "preUpdate hook error on ".concat(req.params.id, ": ").concat((0, errors_1.errorMessage)(error_10)),
834
862
  disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_10),
835
- error: error_10,
836
863
  status: 400,
837
- title: "preUpdate hook error on ".concat(req.params.id, ": ").concat((0, errors_1.errorMessage)(error_10)),
864
+ title: "preUpdate hook error",
838
865
  });
839
866
  case 4:
840
867
  if (body === undefined) {
@@ -918,10 +945,12 @@ function _buildModelRouter(model, options) {
918
945
  case 10:
919
946
  error_11 = _c.sent();
920
947
  throw new errors_1.APIError({
948
+ cause: error_11,
949
+ code: "update-save-error",
950
+ detail: "preUpdate hook save error on ".concat(req.params.id, ": ").concat((0, errors_1.errorMessage)(error_11)),
921
951
  disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_11),
922
- error: error_11,
923
952
  status: 400,
924
- title: "preUpdate hook save error on ".concat(req.params.id, ": ").concat((0, errors_1.errorMessage)(error_11)),
953
+ title: "preUpdate hook save error",
925
954
  });
926
955
  case 11:
927
956
  if (!options.populatePaths) return [3 /*break*/, 13];
@@ -943,10 +972,12 @@ function _buildModelRouter(model, options) {
943
972
  case 16:
944
973
  error_12 = _c.sent();
945
974
  throw new errors_1.APIError({
975
+ cause: error_12,
976
+ code: "post-update-hook-error",
977
+ detail: "postUpdate hook error on ".concat(req.params.id, ": ").concat((0, errors_1.errorMessage)(error_12)),
946
978
  disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_12),
947
- error: error_12,
948
979
  status: 400,
949
- title: "postUpdate hook error on ".concat(req.params.id, ": ").concat((0, errors_1.errorMessage)(error_12)),
980
+ title: "postUpdate hook error",
950
981
  });
951
982
  case 17:
952
983
  _c.trys.push([17, 19, , 20]);
@@ -957,9 +988,11 @@ function _buildModelRouter(model, options) {
957
988
  case 19:
958
989
  error_13 = _c.sent();
959
990
  throw new errors_1.APIError({
991
+ cause: error_13,
992
+ code: "response-handler-error",
993
+ detail: (0, errors_1.errorMessage)(error_13),
960
994
  disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_13),
961
- error: error_13,
962
- title: "responseHandler error: ".concat((0, errors_1.errorMessage)(error_13)),
995
+ title: "responseHandler error",
963
996
  });
964
997
  case 20: return [2 /*return*/];
965
998
  }
@@ -990,10 +1023,12 @@ function _buildModelRouter(model, options) {
990
1023
  throw error_14;
991
1024
  }
992
1025
  throw new errors_1.APIError({
1026
+ cause: error_14,
1027
+ code: "pre-delete-hook-error",
1028
+ detail: "preDelete hook error on ".concat(req.params.id, ": ").concat((0, errors_1.errorMessage)(error_14)),
993
1029
  disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_14),
994
- error: error_14,
995
1030
  status: 403,
996
- title: "preDelete hook error on ".concat(req.params.id, ": ").concat((0, errors_1.errorMessage)(error_14)),
1031
+ title: "preDelete hook error",
997
1032
  });
998
1033
  case 4:
999
1034
  if (body === undefined) {
@@ -1028,10 +1063,12 @@ function _buildModelRouter(model, options) {
1028
1063
  case 9:
1029
1064
  error_15 = _a.sent();
1030
1065
  throw new errors_1.APIError({
1066
+ cause: error_15,
1067
+ code: "delete-error",
1068
+ detail: (0, errors_1.errorMessage)(error_15),
1031
1069
  disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_15),
1032
- error: error_15,
1033
1070
  status: 400,
1034
- title: (0, errors_1.errorMessage)(error_15),
1071
+ title: "Delete error",
1035
1072
  });
1036
1073
  case 10:
1037
1074
  if (!options.postDelete) return [3 /*break*/, 14];
@@ -1045,203 +1082,205 @@ function _buildModelRouter(model, options) {
1045
1082
  case 13:
1046
1083
  error_16 = _a.sent();
1047
1084
  throw new errors_1.APIError({
1085
+ cause: error_16,
1086
+ code: "post-delete-hook-error",
1087
+ detail: (0, errors_1.errorMessage)(error_16),
1048
1088
  disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_16),
1049
- error: error_16,
1050
1089
  status: 400,
1051
- title: "postDelete hook error: ".concat((0, errors_1.errorMessage)(error_16)),
1090
+ title: "postDelete hook error",
1052
1091
  });
1053
1092
  case 14: return [2 /*return*/, res.status(204).json({})];
1054
1093
  }
1055
1094
  });
1056
1095
  }); }));
1057
- function arrayOperation(req, res, operation) {
1058
- return __awaiter(this, void 0, void 0, function () {
1059
- var doc, prevDoc, field, itemId, array, index, body, error_17, error_18, error_19;
1060
- var _a;
1061
- var _b, _c;
1062
- return __generator(this, function (_d) {
1063
- switch (_d.label) {
1064
- case 0: return [4 /*yield*/, (0, permissions_1.checkPermissions)("update", options.permissions.update, req.user)];
1065
- case 1:
1066
- // TODO Combine array operations and .patch(), as they are very similar.
1067
- if (!(_d.sent())) {
1068
- throw new errors_1.APIError({
1069
- status: 405,
1070
- title: "Access to PATCH on ".concat(model.modelName, " denied for ").concat((_b = req.user) === null || _b === void 0 ? void 0 : _b.id),
1071
- });
1072
- }
1073
- return [4 /*yield*/, model.findById(req.params.id)];
1074
- case 2:
1075
- doc = _d.sent();
1076
- prevDoc = (0, cloneDeep_1.default)(doc);
1077
- if (!doc) {
1078
- throw new errors_1.APIError({
1079
- status: 404,
1080
- title: "Could not find document to PATCH: ".concat(req.params.id),
1081
- });
1096
+ var arrayOperation = function (req, res, operation) { return __awaiter(_this, void 0, void 0, function () {
1097
+ var doc, prevDoc, field, itemId, array, index, body, error_17, error_18, error_19;
1098
+ var _a;
1099
+ var _b, _c;
1100
+ return __generator(this, function (_d) {
1101
+ switch (_d.label) {
1102
+ case 0: return [4 /*yield*/, (0, permissions_1.checkPermissions)("update", options.permissions.update, req.user)];
1103
+ case 1:
1104
+ // TODO Combine array operations and .patch(), as they are very similar.
1105
+ if (!(_d.sent())) {
1106
+ throw new errors_1.APIError({
1107
+ status: 405,
1108
+ title: "Access to PATCH on ".concat(model.modelName, " denied for ").concat((_b = req.user) === null || _b === void 0 ? void 0 : _b.id),
1109
+ });
1110
+ }
1111
+ return [4 /*yield*/, model.findById(req.params.id)];
1112
+ case 2:
1113
+ doc = _d.sent();
1114
+ prevDoc = (0, cloneDeep_1.default)(doc);
1115
+ if (!doc) {
1116
+ throw new errors_1.APIError({
1117
+ status: 404,
1118
+ title: "Could not find document to PATCH: ".concat(req.params.id),
1119
+ });
1120
+ }
1121
+ return [4 /*yield*/, (0, permissions_1.checkPermissions)("update", options.permissions.update, req.user, doc)];
1122
+ case 3:
1123
+ if (!(_d.sent())) {
1124
+ throw new errors_1.APIError({
1125
+ status: 403,
1126
+ title: "Patch not allowed for user ".concat((_c = req.user) === null || _c === void 0 ? void 0 : _c.id, " on doc ").concat(doc._id),
1127
+ });
1128
+ }
1129
+ field = req.params.field;
1130
+ itemId = req.params.itemId;
1131
+ // We apply the operation *before* the hooks. As far as the callers are concerned, this should
1132
+ // be like PATCHing the field and replacing the whole thing.
1133
+ if (operation !== "DELETE" && req.body[field] === undefined) {
1134
+ throw new errors_1.APIError({
1135
+ status: 400,
1136
+ title: "Malformed body, array operations should have a single, top level key, got: ".concat(Object.keys(req.body).join(",")),
1137
+ });
1138
+ }
1139
+ array = __spreadArray([], __read(doc[field]), false);
1140
+ if (operation === "POST") {
1141
+ array.push(req.body[field]);
1142
+ }
1143
+ else if (operation === "PATCH" || operation === "DELETE") {
1144
+ index = void 0;
1145
+ if ((0, utils_1.isValidObjectId)(itemId)) {
1146
+ index = array.findIndex(function (x) { return (x === null || x === void 0 ? void 0 : x.id) === itemId; });
1082
1147
  }
1083
- return [4 /*yield*/, (0, permissions_1.checkPermissions)("update", options.permissions.update, req.user, doc)];
1084
- case 3:
1085
- if (!(_d.sent())) {
1086
- throw new errors_1.APIError({
1087
- status: 403,
1088
- title: "Patch not allowed for user ".concat((_c = req.user) === null || _c === void 0 ? void 0 : _c.id, " on doc ").concat(doc._id),
1089
- });
1148
+ else {
1149
+ index = array.indexOf(itemId);
1090
1150
  }
1091
- field = req.params.field;
1092
- itemId = req.params.itemId;
1093
- // We apply the operation *before* the hooks. As far as the callers are concerned, this should
1094
- // be like PATCHing the field and replacing the whole thing.
1095
- if (operation !== "DELETE" && req.body[field] === undefined) {
1151
+ if (index === -1) {
1096
1152
  throw new errors_1.APIError({
1097
- status: 400,
1098
- title: "Malformed body, array operations should have a single, top level key, got: ".concat(Object.keys(req.body).join(",")),
1153
+ status: 404,
1154
+ title: "Could not find ".concat(field, "/").concat(itemId),
1099
1155
  });
1100
1156
  }
1101
- array = __spreadArray([], __read(doc[field]), false);
1102
- if (operation === "POST") {
1103
- array.push(req.body[field]);
1157
+ // For PATCHing an item by ID, we need to merge the objects so we don't override the _id or
1158
+ // other parts of the subdocument.
1159
+ if (operation === "PATCH" && (0, utils_1.isValidObjectId)(itemId)) {
1160
+ Object.assign(array[index], req.body[field]);
1104
1161
  }
1105
- else if (operation === "PATCH" || operation === "DELETE") {
1106
- index = void 0;
1107
- if ((0, utils_1.isValidObjectId)(itemId)) {
1108
- index = array.findIndex(function (x) { return (x === null || x === void 0 ? void 0 : x.id) === itemId; });
1109
- }
1110
- else {
1111
- index = array.indexOf(itemId);
1112
- }
1113
- if (index === -1) {
1114
- throw new errors_1.APIError({
1115
- status: 404,
1116
- title: "Could not find ".concat(field, "/").concat(itemId),
1117
- });
1118
- }
1119
- // For PATCHing an item by ID, we need to merge the objects so we don't override the _id or
1120
- // other parts of the subdocument.
1121
- if (operation === "PATCH" && (0, utils_1.isValidObjectId)(itemId)) {
1122
- Object.assign(array[index], req.body[field]);
1123
- }
1124
- else if (operation === "PATCH") {
1125
- // For PATCHing a string array, we can replace the whole object.
1126
- array[index] = req.body[field];
1127
- }
1128
- else {
1129
- array.splice(index, 1);
1130
- }
1162
+ else if (operation === "PATCH") {
1163
+ // For PATCHing a string array, we can replace the whole object.
1164
+ array[index] = req.body[field];
1131
1165
  }
1132
1166
  else {
1133
- throw new errors_1.APIError({
1134
- status: 400,
1135
- title: "Invalid array operation: ".concat(operation),
1136
- });
1137
- }
1138
- body = (_a = {}, _a[field] = array, _a);
1139
- try {
1140
- body = (0, transformers_1.transform)(options, body, "update", req.user);
1167
+ array.splice(index, 1);
1141
1168
  }
1142
- catch (error) {
1143
- if ((0, errors_1.isAPIError)(error)) {
1144
- throw error;
1145
- }
1146
- throw new errors_1.APIError({
1147
- disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error),
1148
- error: error,
1149
- status: 403,
1150
- title: (0, errors_1.errorMessage)(error),
1151
- });
1152
- }
1153
- if (!options.preUpdate) return [3 /*break*/, 8];
1154
- _d.label = 4;
1155
- case 4:
1156
- _d.trys.push([4, 6, , 7]);
1157
- return [4 /*yield*/, options.preUpdate(body, req)];
1158
- case 5:
1159
- body = _d.sent();
1160
- return [3 /*break*/, 7];
1161
- case 6:
1162
- error_17 = _d.sent();
1169
+ }
1170
+ else {
1163
1171
  throw new errors_1.APIError({
1164
- disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_17),
1165
- error: error_17,
1166
1172
  status: 400,
1167
- title: "preUpdate hook error on ".concat(req.params.id, ": ").concat((0, errors_1.errorMessage)(error_17)),
1173
+ title: "Invalid array operation: ".concat(operation),
1168
1174
  });
1169
- case 7:
1170
- if (body === undefined) {
1171
- throw new errors_1.APIError({
1172
- detail: "A body must be returned from preUpdate",
1173
- status: 403,
1174
- title: "Update not allowed",
1175
- });
1176
- }
1177
- if (body === null) {
1178
- throw new errors_1.APIError({
1179
- detail: "preUpdate hook on ".concat(req.params.id, " returned null"),
1180
- status: 403,
1181
- title: "Update not allowed",
1182
- });
1175
+ }
1176
+ body = (_a = {}, _a[field] = array, _a);
1177
+ try {
1178
+ body = (0, transformers_1.transform)(options, body, "update", req.user);
1179
+ }
1180
+ catch (error) {
1181
+ if ((0, errors_1.isAPIError)(error)) {
1182
+ throw error;
1183
1183
  }
1184
- _d.label = 8;
1185
- case 8:
1186
- _d.trys.push([8, 10, , 11]);
1187
- Object.assign(doc, body);
1188
- return [4 /*yield*/, doc.save()];
1189
- case 9:
1190
- _d.sent();
1191
- return [3 /*break*/, 11];
1192
- case 10:
1193
- error_18 = _d.sent();
1194
1184
  throw new errors_1.APIError({
1195
- disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_18),
1196
- error: error_18,
1197
- status: 400,
1198
- title: "PATCH Pre Update error on ".concat(req.params.id, ": ").concat((0, errors_1.errorMessage)(error_18)),
1185
+ cause: error,
1186
+ code: "transform-error",
1187
+ detail: (0, errors_1.errorMessage)(error),
1188
+ disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error),
1189
+ status: 403,
1190
+ title: "Transform error",
1199
1191
  });
1200
- case 11:
1201
- if (!options.postUpdate) return [3 /*break*/, 15];
1202
- _d.label = 12;
1203
- case 12:
1204
- _d.trys.push([12, 14, , 15]);
1205
- return [4 /*yield*/, options.postUpdate(doc, body, req, prevDoc)];
1206
- case 13:
1207
- _d.sent();
1208
- return [3 /*break*/, 15];
1209
- case 14:
1210
- error_19 = _d.sent();
1192
+ }
1193
+ if (!options.preUpdate) return [3 /*break*/, 8];
1194
+ _d.label = 4;
1195
+ case 4:
1196
+ _d.trys.push([4, 6, , 7]);
1197
+ return [4 /*yield*/, options.preUpdate(body, req)];
1198
+ case 5:
1199
+ body = _d.sent();
1200
+ return [3 /*break*/, 7];
1201
+ case 6:
1202
+ error_17 = _d.sent();
1203
+ throw new errors_1.APIError({
1204
+ cause: error_17,
1205
+ code: "pre-update-hook-error",
1206
+ detail: "preUpdate hook error on ".concat(req.params.id, ": ").concat((0, errors_1.errorMessage)(error_17)),
1207
+ disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_17),
1208
+ status: 400,
1209
+ title: "preUpdate hook error",
1210
+ });
1211
+ case 7:
1212
+ if (body === undefined) {
1211
1213
  throw new errors_1.APIError({
1212
- disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_19),
1213
- error: error_19,
1214
- status: 400,
1215
- title: "PATCH Post Update error on ".concat(req.params.id, ": ").concat((0, errors_1.errorMessage)(error_19)),
1214
+ detail: "A body must be returned from preUpdate",
1215
+ status: 403,
1216
+ title: "Update not allowed",
1216
1217
  });
1217
- case 15: return [2 /*return*/, res.json({
1218
- data: (0, transformers_1.serialize)(req, options, doc),
1219
- })];
1220
- }
1221
- });
1218
+ }
1219
+ if (body === null) {
1220
+ throw new errors_1.APIError({
1221
+ detail: "preUpdate hook on ".concat(req.params.id, " returned null"),
1222
+ status: 403,
1223
+ title: "Update not allowed",
1224
+ });
1225
+ }
1226
+ _d.label = 8;
1227
+ case 8:
1228
+ _d.trys.push([8, 10, , 11]);
1229
+ Object.assign(doc, body);
1230
+ return [4 /*yield*/, doc.save()];
1231
+ case 9:
1232
+ _d.sent();
1233
+ return [3 /*break*/, 11];
1234
+ case 10:
1235
+ error_18 = _d.sent();
1236
+ throw new errors_1.APIError({
1237
+ cause: error_18,
1238
+ code: "update-save-error",
1239
+ detail: "PATCH Pre Update error on ".concat(req.params.id, ": ").concat((0, errors_1.errorMessage)(error_18)),
1240
+ disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_18),
1241
+ status: 400,
1242
+ title: "PATCH Pre Update error",
1243
+ });
1244
+ case 11:
1245
+ if (!options.postUpdate) return [3 /*break*/, 15];
1246
+ _d.label = 12;
1247
+ case 12:
1248
+ _d.trys.push([12, 14, , 15]);
1249
+ return [4 /*yield*/, options.postUpdate(doc, body, req, prevDoc)];
1250
+ case 13:
1251
+ _d.sent();
1252
+ return [3 /*break*/, 15];
1253
+ case 14:
1254
+ error_19 = _d.sent();
1255
+ throw new errors_1.APIError({
1256
+ cause: error_19,
1257
+ code: "post-update-hook-error",
1258
+ detail: "PATCH Post Update error on ".concat(req.params.id, ": ").concat((0, errors_1.errorMessage)(error_19)),
1259
+ disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_19),
1260
+ status: 400,
1261
+ title: "PATCH Post Update error",
1262
+ });
1263
+ case 15: return [2 /*return*/, res.json({
1264
+ data: (0, transformers_1.serialize)(req, options, doc),
1265
+ })];
1266
+ }
1222
1267
  });
1223
- }
1224
- function arrayPost(req, res) {
1225
- return __awaiter(this, void 0, void 0, function () {
1226
- return __generator(this, function (_a) {
1227
- return [2 /*return*/, arrayOperation(req, res, "POST")];
1228
- });
1268
+ }); };
1269
+ var arrayPost = function (req, res) { return __awaiter(_this, void 0, void 0, function () {
1270
+ return __generator(this, function (_a) {
1271
+ return [2 /*return*/, arrayOperation(req, res, "POST")];
1229
1272
  });
1230
- }
1231
- function arrayPatch(req, res) {
1232
- return __awaiter(this, void 0, void 0, function () {
1233
- return __generator(this, function (_a) {
1234
- return [2 /*return*/, arrayOperation(req, res, "PATCH")];
1235
- });
1273
+ }); };
1274
+ var arrayPatch = function (req, res) { return __awaiter(_this, void 0, void 0, function () {
1275
+ return __generator(this, function (_a) {
1276
+ return [2 /*return*/, arrayOperation(req, res, "PATCH")];
1236
1277
  });
1237
- }
1238
- function arrayDelete(req, res) {
1239
- return __awaiter(this, void 0, void 0, function () {
1240
- return __generator(this, function (_a) {
1241
- return [2 /*return*/, arrayOperation(req, res, "DELETE")];
1242
- });
1278
+ }); };
1279
+ var arrayDelete = function (req, res) { return __awaiter(_this, void 0, void 0, function () {
1280
+ return __generator(this, function (_a) {
1281
+ return [2 /*return*/, arrayOperation(req, res, "DELETE")];
1243
1282
  });
1244
- }
1283
+ }); };
1245
1284
  // Set up routes for managing array fields. Check if there any array fields to add this for.
1246
1285
  if (Object.values(model.schema.paths).find(function (config) { return config.instance === "Array"; })) {
1247
1286
  router.post("/:id/:field", (0, auth_1.authenticateMiddleware)(options.allowAnonymous), (0, exports.asyncHandler)(arrayPost));