@terreno/api 57.3.0 → 57.6.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 (412) hide show
  1. package/.ai/skills/mongoose-schema-safety/SKILL.md +23 -10
  2. package/README.md +11 -5
  3. package/bunfig.toml +1 -1
  4. package/dist/actions.js +197 -368
  5. package/dist/adminModelRouter.js +88 -173
  6. package/dist/adminTypes.d.ts +7 -0
  7. package/dist/adminTypes.js +6 -1
  8. package/dist/api.d.ts +10 -0
  9. package/dist/api.js +601 -792
  10. package/dist/audit/auditApp.d.ts +30 -0
  11. package/dist/audit/auditApp.js +114 -0
  12. package/dist/audit/auditEventModel.d.ts +29 -0
  13. package/dist/audit/auditEventModel.js +101 -0
  14. package/dist/audit/cloudTasksEnqueue.d.ts +32 -0
  15. package/dist/audit/cloudTasksEnqueue.js +69 -0
  16. package/dist/audit/diff.d.ts +10 -0
  17. package/dist/audit/diff.js +140 -0
  18. package/dist/audit/processQueue.d.ts +6 -0
  19. package/dist/audit/processQueue.js +56 -0
  20. package/dist/audit/rbacSink.d.ts +2 -0
  21. package/dist/audit/rbacSink.js +41 -0
  22. package/dist/audit/record.d.ts +56 -0
  23. package/dist/audit/record.js +251 -0
  24. package/dist/auth.js +432 -681
  25. package/dist/authRecovery.js +155 -269
  26. package/dist/authTokens.js +43 -118
  27. package/dist/betterAuth.d.ts +6 -0
  28. package/dist/betterAuthApp.js +25 -26
  29. package/dist/betterAuthSetup.js +258 -413
  30. package/dist/browserLogsRoute.d.ts +6 -0
  31. package/dist/browserLogsRoute.js +97 -0
  32. package/dist/collectionRegistry.d.ts +3 -1
  33. package/dist/collectionRegistry.js +43 -33
  34. package/dist/config.js +53 -122
  35. package/dist/configurationApp.js +180 -361
  36. package/dist/configurationPlugin.js +130 -302
  37. package/dist/consentApp.js +276 -421
  38. package/dist/docLoader.js +78 -126
  39. package/dist/envConfigurationPlugin.js +25 -147
  40. package/dist/errors.js +151 -247
  41. package/dist/example.js +28 -34
  42. package/dist/expressServer.d.ts +10 -0
  43. package/dist/expressServer.js +109 -255
  44. package/dist/frameworkModelNames.d.ts +24 -0
  45. package/dist/frameworkModelNames.js +31 -0
  46. package/dist/githubAuth.js +152 -216
  47. package/dist/httpClient.js +176 -264
  48. package/dist/index.d.ts +28 -1
  49. package/dist/index.js +53 -2
  50. package/dist/logger.js +145 -278
  51. package/dist/mcp/auth.js +76 -128
  52. package/dist/mcp/createMCPRequest.js +4 -5
  53. package/dist/mcp/handlers.js +268 -480
  54. package/dist/mcp/query.d.ts +0 -13
  55. package/dist/mcp/query.js +57 -151
  56. package/dist/mcp/registry.js +17 -30
  57. package/dist/mcp/schemaGenerator.js +95 -181
  58. package/dist/mcp/server.js +28 -100
  59. package/dist/mcp/serviceTokens.js +102 -191
  60. package/dist/mcp/toolGenerator.js +78 -191
  61. package/dist/middleware.js +15 -27
  62. package/dist/migrations/fixtures/all-down/20260910120000-one.d.ts +4 -0
  63. package/dist/migrations/fixtures/all-down/20260910120000-one.js +9 -0
  64. package/dist/migrations/fixtures/all-down/20260910120001-two.d.ts +4 -0
  65. package/dist/migrations/fixtures/all-down/20260910120001-two.js +9 -0
  66. package/dist/migrations/fixtures/bad-id/20260910120000-mismatch.d.ts +3 -0
  67. package/dist/migrations/fixtures/bad-id/20260910120000-mismatch.js +7 -0
  68. package/dist/migrations/fixtures/bad-name/not-a-migration.d.ts +3 -0
  69. package/dist/migrations/fixtures/bad-name/not-a-migration.js +7 -0
  70. package/dist/migrations/fixtures/missing-up/20260910120000-noup.d.ts +2 -0
  71. package/dist/migrations/fixtures/missing-up/20260910120000-noup.js +5 -0
  72. package/dist/migrations/fixtures/valid/20260910120000-alpha.d.ts +3 -0
  73. package/dist/migrations/fixtures/valid/20260910120000-alpha.js +7 -0
  74. package/dist/migrations/fixtures/valid/20260910120001-beta.d.ts +4 -0
  75. package/dist/migrations/fixtures/valid/20260910120001-beta.js +9 -0
  76. package/dist/migrations/generate.d.ts +17 -0
  77. package/dist/migrations/generate.js +155 -0
  78. package/dist/migrations/load.d.ts +20 -0
  79. package/dist/migrations/load.js +110 -0
  80. package/dist/migrations/lock.d.ts +13 -0
  81. package/dist/migrations/lock.js +73 -0
  82. package/dist/migrations/runner.d.ts +42 -0
  83. package/dist/migrations/runner.js +249 -0
  84. package/dist/migrations/schemaCatalog.d.ts +44 -0
  85. package/dist/migrations/schemaCatalog.js +185 -0
  86. package/dist/migrations/types.d.ts +87 -0
  87. package/dist/migrations/types.js +7 -0
  88. package/dist/models/consentForm.js +6 -6
  89. package/dist/models/consentResponse.js +5 -5
  90. package/dist/models/mcpServiceToken.js +66 -156
  91. package/dist/models/notification.d.ts +2 -0
  92. package/dist/models/notification.js +62 -0
  93. package/dist/models/notificationPreference.d.ts +2 -0
  94. package/dist/models/notificationPreference.js +49 -0
  95. package/dist/models/versionConfig.js +5 -5
  96. package/dist/notifications/notificationService.d.ts +40 -0
  97. package/dist/notifications/notificationService.js +183 -0
  98. package/dist/notifications/notificationsApp.d.ts +18 -0
  99. package/dist/notifications/notificationsApp.js +156 -0
  100. package/dist/notifications/notificationsBeforeSend.d.ts +14 -0
  101. package/dist/notifications/notificationsBeforeSend.js +32 -0
  102. package/dist/notifiers/googleChatNotifier.js +36 -117
  103. package/dist/notifiers/slackNotifier.d.ts +31 -2
  104. package/dist/notifiers/slackNotifier.js +170 -119
  105. package/dist/notifiers/zoomNotifier.js +60 -111
  106. package/dist/openApi.d.ts +1 -7
  107. package/dist/openApi.js +113 -127
  108. package/dist/openApiBuilder.d.ts +14 -3
  109. package/dist/openApiBuilder.js +117 -159
  110. package/dist/openApiCompat.js +81 -148
  111. package/dist/openApiEtag.js +7 -7
  112. package/dist/openApiValidator.d.ts +13 -1
  113. package/dist/openApiValidator.js +301 -246
  114. package/dist/orgs/orgContext.d.ts +23 -0
  115. package/dist/orgs/orgContext.js +128 -0
  116. package/dist/orgs/orgPermissions.d.ts +4 -0
  117. package/dist/orgs/orgPermissions.js +38 -0
  118. package/dist/orgs/orgPlugin.d.ts +13 -0
  119. package/dist/orgs/orgPlugin.js +66 -0
  120. package/dist/orgs/organizationModel.d.ts +13 -0
  121. package/dist/orgs/organizationModel.js +200 -0
  122. package/dist/orgs/organizationSettings.d.ts +19 -0
  123. package/dist/orgs/organizationSettings.js +113 -0
  124. package/dist/orgs/orgsApp.d.ts +28 -0
  125. package/dist/orgs/orgsApp.js +416 -0
  126. package/dist/permissions.d.ts +7 -13
  127. package/dist/permissions.js +95 -211
  128. package/dist/plugins.js +125 -218
  129. package/dist/populate.js +130 -240
  130. package/dist/rateLimit/applyTrustProxy.js +1 -1
  131. package/dist/rateLimit/createStore.js +7 -8
  132. package/dist/rateLimit/memoryStore.js +22 -100
  133. package/dist/rateLimit/middleware.d.ts +0 -8
  134. package/dist/rateLimit/middleware.js +49 -101
  135. package/dist/rateLimit/mongoStore.d.ts +1 -0
  136. package/dist/rateLimit/mongoStore.js +55 -112
  137. package/dist/rateLimit/policies.d.ts +0 -4
  138. package/dist/rateLimit/policies.js +44 -80
  139. package/dist/rateLimit/redisStore.d.ts +1 -0
  140. package/dist/rateLimit/redisStore.js +40 -133
  141. package/dist/rbac/access.js +122 -253
  142. package/dist/rbac/assertAllowed.js +95 -171
  143. package/dist/rbac/auditModel.js +14 -58
  144. package/dist/rbac/backfillAdmins.js +24 -130
  145. package/dist/rbac/fieldViews.js +46 -141
  146. package/dist/rbac/membershipOrgAdminSource.d.ts +6 -0
  147. package/dist/rbac/membershipOrgAdminSource.js +51 -0
  148. package/dist/rbac/middleware.js +30 -96
  149. package/dist/rbac/modelRouterAccess.d.ts +3 -0
  150. package/dist/rbac/modelRouterAccess.js +133 -307
  151. package/dist/rbac/permissionUtils.js +40 -179
  152. package/dist/rbac/realtimeAccess.js +81 -147
  153. package/dist/rbac/resolve.d.ts +1 -1
  154. package/dist/rbac/resolve.js +154 -319
  155. package/dist/rbac/roleManager.js +318 -700
  156. package/dist/rbac/roleModel.d.ts +2 -5
  157. package/dist/rbac/roleModel.js +60 -173
  158. package/dist/rbac/routes.js +93 -196
  159. package/dist/rbac/scopes.js +39 -133
  160. package/dist/rbac/statements.d.ts +10 -1
  161. package/dist/rbac/statements.js +42 -93
  162. package/dist/rbac/types.d.ts +5 -0
  163. package/dist/rbac/userPlugin.js +3 -29
  164. package/dist/realtime/changeStreamWatcher.d.ts +10 -4
  165. package/dist/realtime/changeStreamWatcher.js +499 -764
  166. package/dist/realtime/deprecation.d.ts +1 -0
  167. package/dist/realtime/deprecation.js +9 -10
  168. package/dist/realtime/queryMatcher.js +110 -191
  169. package/dist/realtime/queryStore.js +35 -118
  170. package/dist/realtime/realtimeApp.d.ts +1 -0
  171. package/dist/realtime/realtimeApp.js +248 -401
  172. package/dist/realtime/registry.js +19 -30
  173. package/dist/realtime/sessionRevalidation.js +132 -358
  174. package/dist/realtime/socketAuth.js +89 -208
  175. package/dist/realtime/socketUser.js +10 -56
  176. package/dist/requestContext.js +93 -139
  177. package/dist/schemaMetadata.js +181 -265
  178. package/dist/scriptRunner.js +96 -266
  179. package/dist/scrubAdminFields.js +36 -108
  180. package/dist/secretProviders.js +130 -299
  181. package/dist/seedRunner.js +221 -514
  182. package/dist/sync/adminBroadcastScope.d.ts +40 -0
  183. package/dist/sync/adminBroadcastScope.js +82 -0
  184. package/dist/sync/adminWindowAccess.d.ts +13 -0
  185. package/dist/sync/adminWindowAccess.js +25 -0
  186. package/dist/sync/adminWindowMutation.d.ts +88 -0
  187. package/dist/sync/adminWindowMutation.js +176 -0
  188. package/dist/sync/executors.d.ts +9 -3
  189. package/dist/sync/executors.js +425 -598
  190. package/dist/sync/loadHarness.js +392 -689
  191. package/dist/sync/models.js +181 -341
  192. package/dist/sync/mutationHandler.d.ts +5 -2
  193. package/dist/sync/mutationHandler.js +549 -697
  194. package/dist/sync/registrationSideEffects.js +41 -95
  195. package/dist/sync/registry.js +31 -47
  196. package/dist/sync/routes.d.ts +13 -0
  197. package/dist/sync/routes.js +484 -760
  198. package/dist/sync/scripts/compactTombstones.js +83 -228
  199. package/dist/sync/serialize.js +18 -58
  200. package/dist/sync/socketHandlers.d.ts +8 -2
  201. package/dist/sync/socketHandlers.js +271 -402
  202. package/dist/sync/streams.d.ts +2 -0
  203. package/dist/sync/streams.js +54 -116
  204. package/dist/sync/syncApp.d.ts +2 -1
  205. package/dist/sync/syncApp.js +14 -16
  206. package/dist/sync/syncBatch.d.ts +5 -2
  207. package/dist/sync/syncBatch.js +63 -168
  208. package/dist/sync/syncSeqPlugin.js +430 -702
  209. package/dist/sync/types.d.ts +18 -0
  210. package/dist/syncConsents.js +113 -253
  211. package/dist/terrenoApp.d.ts +29 -0
  212. package/dist/terrenoApp.js +184 -257
  213. package/dist/tests/createTestData.d.ts +1 -3
  214. package/dist/tests/createTestData.js +147 -259
  215. package/dist/tests/models.d.ts +1 -0
  216. package/dist/tests/models.js +23 -56
  217. package/dist/tests/mongoTestSetup.js +42 -103
  218. package/dist/tests/testHelper.d.ts +0 -5
  219. package/dist/tests/testHelper.js +32 -101
  220. package/dist/tests.js +12 -51
  221. package/dist/transformers.js +61 -164
  222. package/dist/types/authToken.d.ts +3 -3
  223. package/dist/types/mcpServiceToken.d.ts +2 -2
  224. package/dist/types/membership.d.ts +23 -0
  225. package/dist/types/membership.js +2 -0
  226. package/dist/types/notification.d.ts +28 -0
  227. package/dist/types/notification.js +2 -0
  228. package/dist/types/notificationPreference.d.ts +20 -0
  229. package/dist/types/notificationPreference.js +2 -0
  230. package/dist/types/organization.d.ts +36 -0
  231. package/dist/types/organization.js +2 -0
  232. package/dist/utils.js +31 -93
  233. package/dist/vendor/wesleytodd-openapi/index.js +37 -51
  234. package/dist/vendor/wesleytodd-openapi/lib/generate-doc.js +32 -40
  235. package/dist/vendor/wesleytodd-openapi/lib/layer-schema.js +3 -3
  236. package/dist/vendor/wesleytodd-openapi/lib/ui.js +50 -19
  237. package/dist/vendor/wesleytodd-openapi/lib/validate.js +19 -47
  238. package/dist/versionCheckPlugin.js +52 -100
  239. package/dist/webhooks/headerValue.js +2 -2
  240. package/dist/webhooks/idempotency/memoryStore.js +16 -59
  241. package/dist/webhooks/idempotency/mongoStore.js +55 -130
  242. package/dist/webhooks/idempotency/webhookReceipt.d.ts +4 -49
  243. package/dist/webhooks/idempotency/webhookReceipt.js +14 -14
  244. package/dist/webhooks/rawBody.d.ts +1 -0
  245. package/dist/webhooks/rawBody.js +2 -1
  246. package/dist/webhooks/timingSafeEqual.js +4 -5
  247. package/dist/webhooks/verifiers/hmac.js +17 -18
  248. package/dist/webhooks/verifiers/sendgrid.js +20 -22
  249. package/dist/webhooks/verifiers/stripe.js +31 -65
  250. package/dist/webhooks/verifiers/twilio.js +23 -73
  251. package/dist/webhooks/webhooksApp.js +78 -177
  252. package/dist/zodOpenApi.js +2 -2
  253. package/package.json +34 -28
  254. package/src/__snapshots__/openApi.test.ts.snap +192 -54
  255. package/src/__snapshots__/openApiBuilder.test.ts.snap +0 -18
  256. package/src/__snapshots__/schemaMetadata.test.ts.snap +0 -1
  257. package/src/__tests__/browserLogsRoute.test.ts +165 -0
  258. package/src/adminTypes.ts +8 -0
  259. package/src/api.test.ts +39 -2
  260. package/src/api.ts +68 -0
  261. package/src/audit/auditApp.test.ts +243 -0
  262. package/src/audit/auditApp.ts +139 -0
  263. package/src/audit/auditEventModel.ts +160 -0
  264. package/src/audit/auditRouter.test.ts +456 -0
  265. package/src/audit/cloudTasksEnqueue.test.ts +124 -0
  266. package/src/audit/cloudTasksEnqueue.ts +104 -0
  267. package/src/audit/diff.ts +163 -0
  268. package/src/audit/processQueue.ts +69 -0
  269. package/src/audit/rbacSink.test.ts +86 -0
  270. package/src/audit/rbacSink.ts +42 -0
  271. package/src/audit/record.test.ts +402 -0
  272. package/src/audit/record.ts +329 -0
  273. package/src/auth.test.ts +1 -1
  274. package/src/auth.ts +1 -1
  275. package/src/authRecovery.test.ts +43 -0
  276. package/src/authRecovery.ts +16 -10
  277. package/src/authTokens.ts +6 -2
  278. package/src/authVerification.test.ts +22 -0
  279. package/src/betterAuth.ts +7 -0
  280. package/src/betterAuthSetup.test.ts +27 -0
  281. package/src/betterAuthSetup.ts +1 -0
  282. package/src/browserLogsRoute.ts +110 -0
  283. package/src/collectionRegistry.test.ts +8 -9
  284. package/src/collectionRegistry.ts +21 -1
  285. package/src/configurationPlugin.test.ts +1 -11
  286. package/src/express.d.ts +2 -0
  287. package/src/expressServer.test.ts +50 -2
  288. package/src/expressServer.ts +10 -0
  289. package/src/frameworkModelNames.test.ts +62 -0
  290. package/src/frameworkModelNames.ts +33 -0
  291. package/src/index.ts +76 -0
  292. package/src/isolated/migrateCliConnect.isolated.ts +51 -0
  293. package/src/mcp/auth.test.ts +34 -1
  294. package/src/mcp/handlers.test.ts +275 -0
  295. package/src/mcp/query.ts +3 -3
  296. package/src/mcp/toolGenerator.test.ts +81 -0
  297. package/src/migrations/cli.test.ts +353 -0
  298. package/src/migrations/cli.ts +283 -0
  299. package/src/migrations/fixtures/all-down/20260910120000-one.ts +6 -0
  300. package/src/migrations/fixtures/all-down/20260910120001-two.ts +6 -0
  301. package/src/migrations/fixtures/bad-id/20260910120000-mismatch.ts +4 -0
  302. package/src/migrations/fixtures/bad-name/not-a-migration.ts +4 -0
  303. package/src/migrations/fixtures/missing-up/20260910120000-noup.ts +2 -0
  304. package/src/migrations/fixtures/valid/20260910120000-alpha.ts +4 -0
  305. package/src/migrations/fixtures/valid/20260910120001-beta.ts +6 -0
  306. package/src/migrations/generate.test.ts +333 -0
  307. package/src/migrations/generate.ts +216 -0
  308. package/src/migrations/load.test.ts +103 -0
  309. package/src/migrations/load.ts +124 -0
  310. package/src/migrations/lock.test.ts +133 -0
  311. package/src/migrations/lock.ts +118 -0
  312. package/src/migrations/runner.test.ts +504 -0
  313. package/src/migrations/runner.ts +358 -0
  314. package/src/migrations/schemaCatalog.test.ts +159 -0
  315. package/src/migrations/schemaCatalog.ts +262 -0
  316. package/src/migrations/types.ts +94 -0
  317. package/src/models/mcpServiceToken.ts +4 -3
  318. package/src/models/notification.ts +69 -0
  319. package/src/models/notificationPreference.ts +62 -0
  320. package/src/notifications/notificationService.ts +254 -0
  321. package/src/notifications/notificationsApp.test.ts +528 -0
  322. package/src/notifications/notificationsApp.ts +200 -0
  323. package/src/notifications/notificationsBeforeSend.ts +51 -0
  324. package/src/notifiers/slackNotifier.test.ts +186 -1
  325. package/src/notifiers/slackNotifier.ts +167 -8
  326. package/src/openApi.test.ts +0 -1
  327. package/src/openApi.ts +2 -1
  328. package/src/openApiBuilder.ts +14 -3
  329. package/src/openApiValidator.test.ts +221 -2
  330. package/src/openApiValidator.ts +218 -33
  331. package/src/orgs/orgAccessOptIn.test.ts +87 -0
  332. package/src/orgs/orgContext.test.ts +259 -0
  333. package/src/orgs/orgContext.ts +151 -0
  334. package/src/orgs/orgPermissions.test.ts +105 -0
  335. package/src/orgs/orgPermissions.ts +40 -0
  336. package/src/orgs/orgPlugin.test.ts +130 -0
  337. package/src/orgs/orgPlugin.ts +70 -0
  338. package/src/orgs/orgRbac.test.ts +267 -0
  339. package/src/orgs/orgSecurityDefects.test.ts +415 -0
  340. package/src/orgs/organizationModel.test.ts +205 -0
  341. package/src/orgs/organizationModel.ts +262 -0
  342. package/src/orgs/organizationSettings.test.ts +113 -0
  343. package/src/orgs/organizationSettings.ts +126 -0
  344. package/src/orgs/orgsApp.test.ts +382 -0
  345. package/src/orgs/orgsApp.ts +563 -0
  346. package/src/permissions.test.ts +62 -36
  347. package/src/permissions.ts +8 -30
  348. package/src/plugins.test.ts +5 -0
  349. package/src/plugins.ts +3 -0
  350. package/src/rateLimit/middleware.ts +1 -1
  351. package/src/rateLimit/mongoStore.ts +1 -0
  352. package/src/rateLimit/policies.ts +2 -2
  353. package/src/rateLimit/redisStore.ts +1 -0
  354. package/src/rbac/access.ts +11 -3
  355. package/src/rbac/membershipOrgAdminSource.ts +59 -0
  356. package/src/rbac/modelRouterAccess.ts +3 -0
  357. package/src/rbac/permissionUtils.test.ts +26 -5
  358. package/src/rbac/resolve.test.ts +34 -2
  359. package/src/rbac/resolve.ts +26 -7
  360. package/src/rbac/roleModel.test.ts +34 -1
  361. package/src/rbac/roleModel.ts +17 -1
  362. package/src/rbac/statements.test.ts +19 -0
  363. package/src/rbac/statements.ts +19 -1
  364. package/src/rbac/types.ts +5 -0
  365. package/src/realtime/changeStreamWatcher.ts +73 -33
  366. package/src/realtime/deprecation.ts +1 -0
  367. package/src/realtime/realtime.test.ts +357 -185
  368. package/src/realtime/realtimeApp.ts +2 -1
  369. package/src/schemaMetadata.test.ts +14 -0
  370. package/src/schemaMetadata.ts +5 -5
  371. package/src/sync/adminBroadcastConfig.test.ts +96 -0
  372. package/src/sync/adminBroadcastScope.test.ts +155 -0
  373. package/src/sync/adminBroadcastScope.ts +123 -0
  374. package/src/sync/adminWindowAccess.test.ts +57 -0
  375. package/src/sync/adminWindowAccess.ts +32 -0
  376. package/src/sync/adminWindowMutation.test.ts +450 -0
  377. package/src/sync/adminWindowMutation.ts +283 -0
  378. package/src/sync/executors.ts +33 -6
  379. package/src/sync/integration.test.ts +2 -2
  380. package/src/sync/mutationHandler.test.ts +277 -1
  381. package/src/sync/mutationHandler.ts +171 -29
  382. package/src/sync/registry.ts +4 -0
  383. package/src/sync/routes.ts +52 -8
  384. package/src/sync/socketHandlers.ts +154 -58
  385. package/src/sync/streams.ts +6 -0
  386. package/src/sync/sync.test.ts +3 -2
  387. package/src/sync/syncApp.ts +2 -1
  388. package/src/sync/syncBatch.ts +7 -2
  389. package/src/sync/syncRoutes.test.ts +297 -3
  390. package/src/sync/syncSocket.test.ts +229 -0
  391. package/src/sync/types.ts +20 -0
  392. package/src/terrenoApp.migrations.test.ts +114 -0
  393. package/src/terrenoApp.test.ts +41 -0
  394. package/src/terrenoApp.ts +97 -31
  395. package/src/tests/createTestData.ts +2 -2
  396. package/src/tests/models.ts +10 -2
  397. package/src/tests/testHelper.ts +1 -7
  398. package/src/types/authToken.ts +2 -4
  399. package/src/types/mcpServiceToken.ts +1 -7
  400. package/src/types/membership.ts +48 -0
  401. package/src/types/notification.ts +34 -0
  402. package/src/types/notificationPreference.ts +28 -0
  403. package/src/types/organization.ts +49 -0
  404. package/src/types.d.ts +1 -0
  405. package/src/webhooks/idempotency/webhookReceipt.ts +8 -10
  406. package/src/webhooks/rawBody.test.ts +33 -0
  407. package/src/webhooks/rawBody.ts +1 -0
  408. package/tsconfig.json +13 -54
  409. package/tsconfig.typedoc.json +4 -1
  410. package/dist/vendor/wesleytodd-openapi/lib/convert-yaml.d.ts +0 -2
  411. package/dist/vendor/wesleytodd-openapi/lib/convert-yaml.js +0 -13
  412. package/src/vendor/wesleytodd-openapi/lib/convert-yaml.js +0 -13
package/dist/errors.js CHANGED
@@ -1,30 +1,4 @@
1
1
  "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
- var __assign = (this && this.__assign) || function () {
18
- __assign = Object.assign || function(t) {
19
- for (var s, i = 1, n = arguments.length; i < n; i++) {
20
- s = arguments[i];
21
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
22
- t[p] = s[p];
23
- }
24
- return t;
25
- };
26
- return __assign.apply(this, arguments);
27
- };
28
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
29
3
  if (k2 === undefined) k2 = k;
30
4
  var desc = Object.getOwnPropertyDescriptor(m, k);
@@ -58,42 +32,15 @@ var __importStar = (this && this.__importStar) || (function () {
58
32
  return result;
59
33
  };
60
34
  })();
61
- var __values = (this && this.__values) || function(o) {
62
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
63
- if (m) return m.call(o);
64
- if (o && typeof o.length === "number") return {
65
- next: function () {
66
- if (o && i >= o.length) o = void 0;
67
- return { value: o && o[i++], done: !o };
68
- }
69
- };
70
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
71
- };
72
- var __read = (this && this.__read) || function (o, n) {
73
- var m = typeof Symbol === "function" && o[Symbol.iterator];
74
- if (!m) return o;
75
- var i = m.call(o), r, ar = [], e;
76
- try {
77
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
78
- }
79
- catch (error) { e = { error: error }; }
80
- finally {
81
- try {
82
- if (r && !r.done && (m = i["return"])) m.call(i);
83
- }
84
- finally { if (e) throw e.error; }
85
- }
86
- return ar;
87
- };
88
35
  Object.defineProperty(exports, "__esModule", { value: true });
89
36
  exports.apiFallthroughErrorMiddleware = exports.apiErrorMiddleware = exports.passthroughOrWrapWrite = exports.passthroughOrWrap = exports.mongooseErrorToAPIError = exports.apiUnauthorizedMiddleware = exports.getAPIErrorBody = exports.getDisableExternalErrorTracking = exports.errorStack = exports.errorDetail = exports.errorMessage = exports.isAPIError = exports.errorsPlugin = exports.InternalServerError = exports.ValidationError = exports.ConflictError = exports.NotFoundError = exports.ForbiddenError = exports.UnauthorizedError = exports.BadRequestError = exports.APIError = void 0;
90
37
  // https://jsonapi.org/format/#errors
91
- var Sentry = __importStar(require("@sentry/bun"));
92
- var mongoose_1 = __importStar(require("mongoose"));
93
- var logger_1 = require("./logger");
38
+ const Sentry = __importStar(require("@sentry/bun"));
39
+ const mongoose_1 = __importStar(require("mongoose"));
40
+ const logger_1 = require("./logger");
94
41
  // Maps common HTTP statuses to a standard Error name so external tools (Sentry, log explorers)
95
42
  // show a meaningful error type instead of a generic "APIError".
96
- var STATUS_ERROR_NAMES = {
43
+ const STATUS_ERROR_NAMES = {
97
44
  400: "BadRequestError",
98
45
  401: "UnauthorizedError",
99
46
  403: "ForbiddenError",
@@ -114,18 +61,18 @@ var STATUS_ERROR_NAMES = {
114
61
  };
115
62
  // Converts an application error code like "update-admin-error" into a PascalCase Error name
116
63
  // like "UpdateAdminError" so each logical error gets its own type in external tools.
117
- var errorNameFromCode = function (code) {
118
- var words = code.split(/[^a-zA-Z0-9]+/).filter(Boolean);
64
+ const errorNameFromCode = (code) => {
65
+ const words = code.split(/[^a-zA-Z0-9]+/).filter(Boolean);
119
66
  if (words.length === 0) {
120
67
  return undefined;
121
68
  }
122
- return words.map(function (word) { return word.charAt(0).toUpperCase() + word.slice(1); }).join("");
69
+ return words.map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join("");
123
70
  };
124
- var normalizeStatus = function (status) {
71
+ const normalizeStatus = (status) => {
125
72
  if (status === undefined || status === null) {
126
73
  return 500;
127
74
  }
128
- var truncated = Math.trunc(status);
75
+ const truncated = Math.trunc(status);
129
76
  if (!Number.isFinite(truncated) || truncated < 400 || truncated > 599) {
130
77
  return 500;
131
78
  }
@@ -151,153 +98,129 @@ var normalizeStatus = function (status) {
151
98
  * });
152
99
  * ```
153
100
  */
154
- var APIError = /** @class */ (function (_super) {
155
- __extends(APIError, _super);
156
- function APIError(options) {
157
- var _newTarget = this.constructor;
158
- var _this = this;
159
- var _a, _b, _c, _d;
160
- var cause = (_a = options.cause) !== null && _a !== void 0 ? _a : options.error;
101
+ class APIError extends Error {
102
+ // Brand for detection across duplicate copies of @terreno/api in node_modules, where
103
+ // `instanceof` checks fail. See isAPIError.
104
+ isTerrenoAPIError = true;
105
+ id;
106
+ links;
107
+ status;
108
+ code;
109
+ detail;
110
+ source;
111
+ meta;
112
+ disableExternalErrorTracking;
113
+ constructor(options) {
114
+ const cause = options.cause ?? options.error;
161
115
  // message is exactly the stable title; detail and cause stacks are separate fields.
162
- _this = _super.call(this, options.title, cause === undefined ? undefined : { cause: cause }) || this;
163
- // Brand for detection across duplicate copies of @terreno/api in node_modules, where
164
- // `instanceof` checks fail. See isAPIError.
165
- _this.isTerrenoAPIError = true;
116
+ super(options.title, cause === undefined ? undefined : { cause });
166
117
  // The package compiles to ES5, where extending built-ins loses the prototype chain (the
167
118
  // original reason isAPIError checked `name` instead of using `instanceof`). Repair it so
168
119
  // getters, methods, and instanceof work on compiled output too.
169
- Object.setPrototypeOf(_this, _newTarget.prototype);
170
- _this.status = normalizeStatus(options.status);
171
- _this.code = options.code;
120
+ Object.setPrototypeOf(this, new.target.prototype);
121
+ this.status = normalizeStatus(options.status);
122
+ this.code = options.code;
172
123
  // name precedence: subclass name (from new.target) > code > status-derived > "APIError".
173
- var subclassName = _newTarget !== APIError && _newTarget.name && _newTarget.name !== "APIError"
174
- ? _newTarget.name
124
+ const subclassName = new.target !== APIError && new.target.name && new.target.name !== "APIError"
125
+ ? new.target.name
175
126
  : undefined;
176
- _this.name =
177
- (_c = (_b = subclassName !== null && subclassName !== void 0 ? subclassName : (options.code ? errorNameFromCode(options.code) : undefined)) !== null && _b !== void 0 ? _b : STATUS_ERROR_NAMES[_this.status]) !== null && _c !== void 0 ? _c : "APIError";
178
- _this.id = options.id;
179
- _this.links = options.links;
180
- _this.detail = options.detail;
181
- _this.source = options.source;
182
- _this.meta = (_d = options.meta) !== null && _d !== void 0 ? _d : {};
183
- _this.disableExternalErrorTracking = options.disableExternalErrorTracking;
127
+ this.name =
128
+ subclassName ??
129
+ (options.code ? errorNameFromCode(options.code) : undefined) ??
130
+ STATUS_ERROR_NAMES[this.status] ??
131
+ "APIError";
132
+ this.id = options.id;
133
+ this.links = options.links;
134
+ this.detail = options.detail;
135
+ this.source = options.source;
136
+ this.meta = options.meta ?? {};
137
+ this.disableExternalErrorTracking = options.disableExternalErrorTracking;
184
138
  if (options.fields) {
185
- _this.meta.fields = options.fields;
139
+ this.meta.fields = options.fields;
186
140
  }
187
- return _this;
188
141
  }
189
- Object.defineProperty(APIError.prototype, "error", {
190
- /** @deprecated Use the standard `cause` field instead. */
191
- get: function () {
192
- return this.cause;
193
- },
194
- enumerable: false,
195
- configurable: true
196
- });
197
- Object.defineProperty(APIError.prototype, "title", {
198
- // JSONAPI title is the same stable summary as the standard Error message. A getter keeps the
199
- // two from ever drifting.
200
- get: function () {
201
- return this.message;
202
- },
203
- enumerable: false,
204
- configurable: true
205
- });
142
+ /** @deprecated Use the standard `cause` field instead. */
143
+ get error() {
144
+ return this.cause;
145
+ }
146
+ // JSONAPI title is the same stable summary as the standard Error message. A getter keeps the
147
+ // two from ever drifting.
148
+ get title() {
149
+ return this.message;
150
+ }
206
151
  // Client-facing JSONAPI body. Excludes name/stack/cause; includes
207
152
  // disableExternalErrorTracking only when true (client Sentry suppression).
208
- APIError.prototype.toJSON = function () {
153
+ toJSON() {
209
154
  return serializeAPIError(this);
210
- };
211
- return APIError;
212
- }(Error));
155
+ }
156
+ }
213
157
  exports.APIError = APIError;
214
- var withStatus = function (options, status) {
215
- return typeof options === "string" ? { status: status, title: options } : __assign(__assign({}, options), { status: status });
158
+ const withStatus = (options, status) => {
159
+ return typeof options === "string" ? { status, title: options } : { ...options, status };
216
160
  };
217
161
  /** 400 Bad Request. */
218
- var BadRequestError = /** @class */ (function (_super) {
219
- __extends(BadRequestError, _super);
220
- function BadRequestError(options) {
221
- var _this = _super.call(this, withStatus(options, 400)) || this;
222
- _this.name = "BadRequestError";
223
- return _this;
224
- }
225
- return BadRequestError;
226
- }(APIError));
162
+ class BadRequestError extends APIError {
163
+ constructor(options) {
164
+ super(withStatus(options, 400));
165
+ this.name = "BadRequestError";
166
+ }
167
+ }
227
168
  exports.BadRequestError = BadRequestError;
228
169
  /** 401 Unauthorized. */
229
- var UnauthorizedError = /** @class */ (function (_super) {
230
- __extends(UnauthorizedError, _super);
231
- function UnauthorizedError(options) {
232
- var _this = _super.call(this, withStatus(options, 401)) || this;
233
- _this.name = "UnauthorizedError";
234
- return _this;
235
- }
236
- return UnauthorizedError;
237
- }(APIError));
170
+ class UnauthorizedError extends APIError {
171
+ constructor(options) {
172
+ super(withStatus(options, 401));
173
+ this.name = "UnauthorizedError";
174
+ }
175
+ }
238
176
  exports.UnauthorizedError = UnauthorizedError;
239
177
  /** 403 Forbidden. */
240
- var ForbiddenError = /** @class */ (function (_super) {
241
- __extends(ForbiddenError, _super);
242
- function ForbiddenError(options) {
243
- var _this = _super.call(this, withStatus(options, 403)) || this;
244
- _this.name = "ForbiddenError";
245
- return _this;
246
- }
247
- return ForbiddenError;
248
- }(APIError));
178
+ class ForbiddenError extends APIError {
179
+ constructor(options) {
180
+ super(withStatus(options, 403));
181
+ this.name = "ForbiddenError";
182
+ }
183
+ }
249
184
  exports.ForbiddenError = ForbiddenError;
250
185
  /** 404 Not Found. */
251
- var NotFoundError = /** @class */ (function (_super) {
252
- __extends(NotFoundError, _super);
253
- function NotFoundError(options) {
254
- var _this = _super.call(this, withStatus(options, 404)) || this;
255
- _this.name = "NotFoundError";
256
- return _this;
257
- }
258
- return NotFoundError;
259
- }(APIError));
186
+ class NotFoundError extends APIError {
187
+ constructor(options) {
188
+ super(withStatus(options, 404));
189
+ this.name = "NotFoundError";
190
+ }
191
+ }
260
192
  exports.NotFoundError = NotFoundError;
261
193
  /** 409 Conflict. */
262
- var ConflictError = /** @class */ (function (_super) {
263
- __extends(ConflictError, _super);
264
- function ConflictError(options) {
265
- var _this = _super.call(this, withStatus(options, 409)) || this;
266
- _this.name = "ConflictError";
267
- return _this;
268
- }
269
- return ConflictError;
270
- }(APIError));
194
+ class ConflictError extends APIError {
195
+ constructor(options) {
196
+ super(withStatus(options, 409));
197
+ this.name = "ConflictError";
198
+ }
199
+ }
271
200
  exports.ConflictError = ConflictError;
272
201
  /** 400 Bad Request with a "ValidationError" type, for request/schema validation failures. */
273
- var ValidationError = /** @class */ (function (_super) {
274
- __extends(ValidationError, _super);
275
- function ValidationError(options) {
276
- var _this = _super.call(this, withStatus(options, 400)) || this;
277
- _this.name = "ValidationError";
278
- return _this;
279
- }
280
- return ValidationError;
281
- }(APIError));
202
+ class ValidationError extends APIError {
203
+ constructor(options) {
204
+ super(withStatus(options, 400));
205
+ this.name = "ValidationError";
206
+ }
207
+ }
282
208
  exports.ValidationError = ValidationError;
283
209
  /** 500 Internal Server Error. */
284
- var InternalServerError = /** @class */ (function (_super) {
285
- __extends(InternalServerError, _super);
286
- function InternalServerError(options) {
287
- var _this = _super.call(this, withStatus(options, 500)) || this;
288
- _this.name = "InternalServerError";
289
- return _this;
290
- }
291
- return InternalServerError;
292
- }(APIError));
210
+ class InternalServerError extends APIError {
211
+ constructor(options) {
212
+ super(withStatus(options, 500));
213
+ this.name = "InternalServerError";
214
+ }
215
+ }
293
216
  exports.InternalServerError = InternalServerError;
294
217
  // This can be attached to any schema to store errors compatible with the JSONAPI spec.
295
218
  // Lazily initialize to avoid module loading order issues with Bun where mongoose
296
219
  // may not be fully initialized when this module loads.
297
220
  // Create an errors field for storing error information in a JSONAPI compatible form directly on a
298
221
  // model.
299
- var errorsPlugin = function (schema) {
300
- var errorSchema = new mongoose_1.Schema({
222
+ const errorsPlugin = (schema) => {
223
+ const errorSchema = new mongoose_1.Schema({
301
224
  code: { description: "Application-specific error code", type: String },
302
225
  detail: { description: "Human-readable explanation of the error", type: String },
303
226
  id: { description: "Unique identifier for this error occurrence", type: String },
@@ -323,7 +246,7 @@ var errorsPlugin = function (schema) {
323
246
  schema.add({ apiErrors: errorSchema });
324
247
  };
325
248
  exports.errorsPlugin = errorsPlugin;
326
- var isAPIError = function (error) {
249
+ const isAPIError = (error) => {
327
250
  if (error instanceof APIError) {
328
251
  return true;
329
252
  }
@@ -337,7 +260,7 @@ var isAPIError = function (error) {
337
260
  };
338
261
  exports.isAPIError = isAPIError;
339
262
  /** Extract a human-readable message from an unknown error. */
340
- var errorMessage = function (error) {
263
+ const errorMessage = (error) => {
341
264
  if (error instanceof Error) {
342
265
  return error.message;
343
266
  }
@@ -349,15 +272,15 @@ exports.errorMessage = errorMessage;
349
272
  * wrapper error. An `APIError`'s `message` is exactly its `title`, so `errorMessage` alone would
350
273
  * drop the per-occurrence text the caller put in `detail`.
351
274
  */
352
- var errorDetail = function (error) {
275
+ const errorDetail = (error) => {
353
276
  if ((0, exports.isAPIError)(error) && error.detail) {
354
- return "".concat(error.title, ": ").concat(error.detail);
277
+ return `${error.title}: ${error.detail}`;
355
278
  }
356
279
  return (0, exports.errorMessage)(error);
357
280
  };
358
281
  exports.errorDetail = errorDetail;
359
282
  /** Extract a stack trace string from an unknown error. */
360
- var errorStack = function (error) {
283
+ const errorStack = (error) => {
361
284
  if (error instanceof Error && error.stack) {
362
285
  return error.stack;
363
286
  }
@@ -369,7 +292,7 @@ exports.errorStack = errorStack;
369
292
  * Works with both APIError instances and regular Error objects that may have
370
293
  * this property attached.
371
294
  */
372
- var getDisableExternalErrorTracking = function (error) {
295
+ const getDisableExternalErrorTracking = (error) => {
373
296
  if (error instanceof Error) {
374
297
  if ((0, exports.isAPIError)(error)) {
375
298
  return error.disableExternalErrorTracking;
@@ -383,39 +306,28 @@ var getDisableExternalErrorTracking = function (error) {
383
306
  exports.getDisableExternalErrorTracking = getDisableExternalErrorTracking;
384
307
  // Builds the client-facing JSONAPI body from any APIError-shaped error, including instances from
385
308
  // older/duplicate copies of @terreno/api that may not have toJSON.
386
- var serializeAPIError = function (error) {
387
- var e_1, _a;
388
- var indexable = error;
309
+ const serializeAPIError = (error) => {
310
+ const indexable = error;
389
311
  // Prefer an explicit title property for legacy/duplicate-package instances whose message was
390
312
  // polluted with detail/cause stacks but still carry a clean title field.
391
- var title = typeof indexable.title === "string" && indexable.title.length > 0
313
+ const title = typeof indexable.title === "string" && indexable.title.length > 0
392
314
  ? indexable.title
393
315
  : error.message;
394
- var body = {
316
+ const body = {
395
317
  status: typeof indexable.status === "number" ? indexable.status : 500,
396
- title: title,
318
+ title,
397
319
  };
398
- try {
399
- for (var _b = __values(["id", "links", "code", "detail", "source", "meta"]), _c = _b.next(); !_c.done; _c = _b.next()) {
400
- var key = _c.value;
401
- var value = indexable[key];
402
- if (key === "meta" &&
403
- value &&
404
- typeof value === "object" &&
405
- Object.keys(value).length === 0) {
406
- continue;
407
- }
408
- if (value) {
409
- body[key] = value;
410
- }
320
+ for (const key of ["id", "links", "code", "detail", "source", "meta"]) {
321
+ const value = indexable[key];
322
+ if (key === "meta" &&
323
+ value &&
324
+ typeof value === "object" &&
325
+ Object.keys(value).length === 0) {
326
+ continue;
411
327
  }
412
- }
413
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
414
- finally {
415
- try {
416
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
328
+ if (value) {
329
+ body[key] = value;
417
330
  }
418
- finally { if (e_1) throw e_1.error; }
419
331
  }
420
332
  if (indexable.disableExternalErrorTracking === true) {
421
333
  body.disableExternalErrorTracking = true;
@@ -426,7 +338,7 @@ var serializeAPIError = function (error) {
426
338
  * Creates an APIError body to send to clients as JSON.
427
339
  * @deprecated Use `error.toJSON()` instead.
428
340
  */
429
- var getAPIErrorBody = function (error) {
341
+ const getAPIErrorBody = (error) => {
430
342
  return serializeAPIError(error);
431
343
  };
432
344
  exports.getAPIErrorBody = getAPIErrorBody;
@@ -438,7 +350,7 @@ exports.getAPIErrorBody = getAPIErrorBody;
438
350
  * `apiErrorMiddleware`. A domain-specific `Error` subclass with the same message also falls
439
351
  * through so its own handler can respond.
440
352
  */
441
- var apiUnauthorizedMiddleware = function (err, _req, res, next) {
353
+ const apiUnauthorizedMiddleware = (err, _req, res, next) => {
442
354
  if (!(0, exports.isAPIError)(err) &&
443
355
  err.message === "Unauthorized" &&
444
356
  Object.getPrototypeOf(err) === Error.prototype) {
@@ -453,41 +365,29 @@ exports.apiUnauthorizedMiddleware = apiUnauthorizedMiddleware;
453
365
  /**
454
366
  * Converts Mongoose validation/cast errors into client-friendly APIErrors.
455
367
  */
456
- var mongooseErrorToAPIError = function (err) {
457
- var e_2, _a, _b;
458
- var _c, _d;
368
+ const mongooseErrorToAPIError = (err) => {
459
369
  if (err instanceof mongoose_1.default.Error.ValidationError) {
460
- var fields = {};
461
- try {
462
- for (var _e = __values(Object.entries(err.errors)), _f = _e.next(); !_f.done; _f = _e.next()) {
463
- var _g = __read(_f.value, 2), path = _g[0], subErr = _g[1];
464
- fields[path] = subErr.message;
465
- }
466
- }
467
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
468
- finally {
469
- try {
470
- if (_f && !_f.done && (_a = _e.return)) _a.call(_e);
471
- }
472
- finally { if (e_2) throw e_2.error; }
370
+ const fields = {};
371
+ for (const [path, subErr] of Object.entries(err.errors)) {
372
+ fields[path] = subErr.message;
473
373
  }
474
374
  return new ValidationError({
475
375
  cause: err,
476
376
  detail: err.message,
477
377
  disableExternalErrorTracking: true,
478
- fields: fields,
378
+ fields,
479
379
  title: "Validation failed",
480
380
  });
481
381
  }
482
382
  if (err instanceof mongoose_1.default.Error.CastError) {
483
- var path = (_c = err.path) !== null && _c !== void 0 ? _c : "field";
383
+ const path = err.path ?? "field";
484
384
  return new ValidationError({
485
385
  cause: err,
486
- detail: "Invalid value for ".concat(path),
386
+ detail: `Invalid value for ${path}`,
487
387
  disableExternalErrorTracking: true,
488
- fields: (_b = {},
489
- _b[path] = "Expected ".concat((_d = err.kind) !== null && _d !== void 0 ? _d : "a valid value", ", got ").concat(JSON.stringify(err.value)),
490
- _b),
388
+ fields: {
389
+ [path]: `Expected ${err.kind ?? "a valid value"}, got ${JSON.stringify(err.value)}`,
390
+ },
491
391
  title: "Validation failed",
492
392
  });
493
393
  }
@@ -500,11 +400,16 @@ exports.mongooseErrorToAPIError = mongooseErrorToAPIError;
500
400
  * anything else is wrapped in the given framework error. `wrapper` may override any field,
501
401
  * including `detail`, which defaults to the caught error's text.
502
402
  */
503
- var passthroughOrWrap = function (error, wrapper) {
403
+ const passthroughOrWrap = (error, wrapper) => {
504
404
  if ((0, exports.isAPIError)(error)) {
505
405
  return error;
506
406
  }
507
- return new APIError(__assign({ cause: error, detail: (0, exports.errorDetail)(error), disableExternalErrorTracking: (0, exports.getDisableExternalErrorTracking)(error) }, wrapper));
407
+ return new APIError({
408
+ cause: error,
409
+ detail: (0, exports.errorDetail)(error),
410
+ disableExternalErrorTracking: (0, exports.getDisableExternalErrorTracking)(error),
411
+ ...wrapper,
412
+ });
508
413
  };
509
414
  exports.passthroughOrWrap = passthroughOrWrap;
510
415
  /**
@@ -512,9 +417,9 @@ exports.passthroughOrWrap = passthroughOrWrap;
512
417
  * errors are converted first so their per-field messages survive as `meta.fields` instead of being
513
418
  * flattened into the wrapper's `detail`.
514
419
  */
515
- var passthroughOrWrapWrite = function (error, wrapper) {
420
+ const passthroughOrWrapWrite = (error, wrapper) => {
516
421
  if (!(0, exports.isAPIError)(error) && error instanceof Error) {
517
- var converted = (0, exports.mongooseErrorToAPIError)(error);
422
+ const converted = (0, exports.mongooseErrorToAPIError)(error);
518
423
  if (converted) {
519
424
  return converted;
520
425
  }
@@ -526,19 +431,18 @@ exports.passthroughOrWrapWrite = passthroughOrWrapWrite;
526
431
  // Sentry unless external tracking is disabled. Fingerprints on the logical error type
527
432
  // (name + code/title + status) so unrelated errors constructed at the same framework lines don't
528
433
  // group together, while the `cause` chain preserves each occurrence's real origin stack.
529
- var logAndCaptureAPIError = function (err) {
530
- var cause = err.cause;
531
- var causeStack = cause instanceof Error && cause.stack ? "\n".concat(cause.stack) : "";
532
- var logMessage = "".concat(err.name, "(").concat(err.status, "): ").concat(err.message).concat(err.detail ? " \u2014 ".concat(err.detail) : "").concat(causeStack);
533
- var logFn = err.status >= 500 ? logger_1.logger.error.bind(logger_1.logger) : logger_1.logger.warn.bind(logger_1.logger);
434
+ const logAndCaptureAPIError = (err) => {
435
+ const cause = err.cause;
436
+ const causeStack = cause instanceof Error && cause.stack ? `\n${cause.stack}` : "";
437
+ const logMessage = `${err.name}(${err.status}): ${err.message}${err.detail ? ` — ${err.detail}` : ""}${causeStack}`;
438
+ const logFn = err.status >= 500 ? logger_1.logger.error.bind(logger_1.logger) : logger_1.logger.warn.bind(logger_1.logger);
534
439
  if (err.disableExternalErrorTracking) {
535
440
  logFn(logMessage);
536
441
  return;
537
442
  }
538
443
  logFn(logMessage);
539
- Sentry.withScope(function (scope) {
540
- var _a;
541
- scope.setFingerprint([err.name, (_a = err.code) !== null && _a !== void 0 ? _a : err.name, String(err.status)]);
444
+ Sentry.withScope((scope) => {
445
+ scope.setFingerprint([err.name, err.code ?? err.name, String(err.status)]);
542
446
  scope.setTag("http.status_code", String(err.status));
543
447
  if (err.code) {
544
448
  scope.setTag("api_error.code", err.code);
@@ -547,8 +451,8 @@ var logAndCaptureAPIError = function (err) {
547
451
  Sentry.captureException(err);
548
452
  });
549
453
  };
550
- var apiErrorMiddleware = function (err, _req, res, next) {
551
- var apiError = (0, exports.isAPIError)(err) ? err : (0, exports.mongooseErrorToAPIError)(err);
454
+ const apiErrorMiddleware = (err, _req, res, next) => {
455
+ const apiError = (0, exports.isAPIError)(err) ? err : (0, exports.mongooseErrorToAPIError)(err);
552
456
  if (!apiError) {
553
457
  next(err);
554
458
  return;
@@ -561,8 +465,8 @@ exports.apiErrorMiddleware = apiErrorMiddleware;
561
465
  * Final Express error handler for unexpected errors. Always returns JSON so
562
466
  * clients (e.g. RTK Query) can parse the response.
563
467
  */
564
- var apiFallthroughErrorMiddleware = function (err, _req, res, _next) {
565
- logger_1.logger.error("Fallthrough error: ".concat(err).concat(err.stack ? "\n".concat(err.stack) : ""));
468
+ const apiFallthroughErrorMiddleware = (err, _req, res, _next) => {
469
+ logger_1.logger.error(`Fallthrough error: ${err}${err.stack ? `\n${err.stack}` : ""}`);
566
470
  Sentry.captureException(err);
567
471
  if (res.headersSent) {
568
472
  return;