@terreno/api 0.31.0 → 56.0.0-beta.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 (250) hide show
  1. package/CHANGELOG.md +122 -2
  2. package/LICENSE +202 -21
  3. package/dist/api.d.ts +12 -3
  4. package/dist/api.js +110 -361
  5. package/dist/auth.d.ts +26 -0
  6. package/dist/auth.js +100 -1
  7. package/dist/betterAuth.d.ts +5 -0
  8. package/dist/betterAuthSetup.d.ts +1 -15
  9. package/dist/betterAuthSetup.js +29 -8
  10. package/dist/configurationPlugin.js +2 -2
  11. package/dist/docLoader.js +12 -9
  12. package/dist/errors.d.ts +13 -0
  13. package/dist/errors.js +29 -1
  14. package/dist/expressServer.js +5 -11
  15. package/dist/githubAuth.js +4 -1
  16. package/dist/index.d.ts +17 -1
  17. package/dist/index.js +29 -1
  18. package/dist/middleware.d.ts +7 -0
  19. package/dist/middleware.js +16 -1
  20. package/dist/openApiCompat.d.ts +10 -1
  21. package/dist/openApiCompat.js +115 -47
  22. package/dist/permissions.d.ts +27 -0
  23. package/dist/permissions.js +42 -1
  24. package/dist/plugins.d.ts +15 -3
  25. package/dist/plugins.js +36 -6
  26. package/dist/realtime/changeStreamWatcher.d.ts +100 -3
  27. package/dist/realtime/changeStreamWatcher.js +542 -137
  28. package/dist/realtime/realtimeApp.d.ts +3 -1
  29. package/dist/realtime/realtimeApp.js +75 -27
  30. package/dist/realtime/sessionRevalidation.d.ts +90 -0
  31. package/dist/realtime/sessionRevalidation.js +438 -0
  32. package/dist/realtime/socketAuth.d.ts +97 -0
  33. package/dist/realtime/socketAuth.js +272 -0
  34. package/dist/realtime/socketUser.d.ts +54 -0
  35. package/dist/realtime/socketUser.js +75 -5
  36. package/dist/realtime/types.d.ts +40 -0
  37. package/dist/secretProviders.d.ts +23 -0
  38. package/dist/secretProviders.js +9 -3
  39. package/dist/sync/executors.d.ts +198 -0
  40. package/dist/sync/executors.js +670 -0
  41. package/dist/sync/loadHarness.js +807 -0
  42. package/dist/sync/models.d.ts +216 -0
  43. package/dist/sync/models.js +644 -0
  44. package/dist/sync/mutationHandler.d.ts +97 -0
  45. package/dist/sync/mutationHandler.js +886 -0
  46. package/dist/sync/registry.d.ts +73 -0
  47. package/dist/sync/registry.js +223 -0
  48. package/dist/sync/routes.d.ts +62 -0
  49. package/dist/sync/routes.js +936 -0
  50. package/dist/sync/scripts/compactTombstones.d.ts +47 -0
  51. package/dist/sync/scripts/compactTombstones.js +270 -0
  52. package/dist/sync/serialize.d.ts +20 -0
  53. package/dist/{tests/bunSetup.js → sync/serialize.js} +35 -68
  54. package/dist/sync/socketHandlers.d.ts +54 -0
  55. package/dist/sync/socketHandlers.js +470 -0
  56. package/dist/sync/streams.d.ts +85 -0
  57. package/dist/sync/streams.js +221 -0
  58. package/dist/sync/syncApp.d.ts +29 -0
  59. package/dist/sync/syncApp.js +44 -0
  60. package/dist/sync/syncBatch.d.ts +71 -0
  61. package/dist/sync/syncBatch.js +252 -0
  62. package/dist/sync/syncSeqPlugin.d.ts +8 -0
  63. package/dist/sync/syncSeqPlugin.js +903 -0
  64. package/dist/sync/types.d.ts +254 -0
  65. package/dist/sync/types.js +9 -0
  66. package/dist/terrenoApp.js +90 -27
  67. package/dist/tests/models.d.ts +11 -27
  68. package/dist/tests/models.js +3 -5
  69. package/dist/tests/testHelper.d.ts +3 -9
  70. package/dist/vendor/wesleytodd-openapi/index.d.ts +1 -1
  71. package/dist/vendor/wesleytodd-openapi/lib/ui.d.ts +2 -1
  72. package/package.json +10 -8
  73. package/src/__tests__/openApiCompat.test.ts +20 -0
  74. package/src/actions.test.ts +12 -19
  75. package/src/actions.ts +2 -2
  76. package/src/api.coverage.test.ts +9 -7
  77. package/src/api.errorPaths.test.ts +1 -47
  78. package/src/api.test.ts +12 -0
  79. package/src/api.ts +98 -317
  80. package/src/auth.test.ts +128 -176
  81. package/src/auth.ts +109 -0
  82. package/src/betterAuth.ts +6 -0
  83. package/src/betterAuthSetup.test.ts +34 -0
  84. package/src/betterAuthSetup.ts +27 -8
  85. package/src/configurationPlugin.ts +2 -2
  86. package/src/consentApp.test.ts +12 -12
  87. package/src/docLoader.ts +3 -1
  88. package/src/errors.ts +33 -0
  89. package/src/expressServer.test.ts +7 -1
  90. package/src/expressServer.ts +5 -11
  91. package/src/githubAuth.ts +3 -1
  92. package/src/index.ts +42 -0
  93. package/src/middleware.test.ts +40 -1
  94. package/src/middleware.ts +15 -0
  95. package/src/models/versionConfig.ts +1 -1
  96. package/src/openApiBuilder.test.ts +9 -24
  97. package/src/openApiCompat.ts +99 -45
  98. package/src/openApiValidator.test.ts +0 -41
  99. package/src/permissions.test.ts +75 -1
  100. package/src/permissions.ts +49 -0
  101. package/src/plugins.test.ts +54 -14
  102. package/src/plugins.ts +47 -9
  103. package/src/populate.test.ts +16 -25
  104. package/src/realtime/changeStreamWatcher.ts +552 -114
  105. package/src/realtime/realtime.test.ts +157 -239
  106. package/src/realtime/realtimeApp.ts +80 -19
  107. package/src/realtime/sessionRevalidation.test.ts +423 -0
  108. package/src/realtime/sessionRevalidation.ts +273 -0
  109. package/src/realtime/socketAuth.test.ts +355 -0
  110. package/src/realtime/socketAuth.ts +228 -0
  111. package/src/realtime/socketUser.ts +69 -0
  112. package/src/realtime/types.ts +40 -0
  113. package/src/secretProviders.test.ts +7 -1
  114. package/src/secretProviders.ts +15 -3
  115. package/src/sync/ensureSyncIndexes.test.ts +144 -0
  116. package/src/sync/executors.test.ts +882 -0
  117. package/src/sync/executors.ts +703 -0
  118. package/src/sync/integration.test.ts +1160 -0
  119. package/src/sync/loadHarness.ts +618 -0
  120. package/src/sync/models.ts +640 -0
  121. package/src/sync/mutationHandler.test.ts +1252 -0
  122. package/src/sync/mutationHandler.ts +850 -0
  123. package/src/sync/registry.ts +232 -0
  124. package/src/sync/routes.ts +812 -0
  125. package/src/sync/scripts/compactTombstones.test.ts +249 -0
  126. package/src/sync/scripts/compactTombstones.ts +145 -0
  127. package/src/sync/serialize.ts +48 -0
  128. package/src/sync/sessionRevalidationIntegration.test.ts +281 -0
  129. package/src/sync/socketHandlers.ts +320 -0
  130. package/src/sync/streams.ts +188 -0
  131. package/src/sync/sync.test.ts +582 -0
  132. package/src/sync/syncApp.ts +44 -0
  133. package/src/sync/syncBatch.ts +219 -0
  134. package/src/sync/syncFrontier.test.ts +494 -0
  135. package/src/sync/syncPhaseC.test.ts +718 -0
  136. package/src/sync/syncRoutes.test.ts +865 -0
  137. package/src/sync/syncSeqPlugin.ts +738 -0
  138. package/src/sync/syncSocket.test.ts +1440 -0
  139. package/src/sync/types.ts +270 -0
  140. package/src/terrenoApp.ts +37 -17
  141. package/src/tests/bunSetup.ts +8 -0
  142. package/src/tests/models.ts +6 -8
  143. package/tsconfig.json +4 -4
  144. package/tsconfig.typedoc.json +7 -1
  145. package/dist/__tests__/openApiCompat.test.js +0 -317
  146. package/dist/__tests__/versionCheckPlugin.test.d.ts +0 -1
  147. package/dist/__tests__/versionCheckPlugin.test.js +0 -597
  148. package/dist/actions.openApi.test.d.ts +0 -1
  149. package/dist/actions.openApi.test.js +0 -253
  150. package/dist/actions.test.d.ts +0 -1
  151. package/dist/actions.test.js +0 -1087
  152. package/dist/api.arrayOperations.test.d.ts +0 -1
  153. package/dist/api.arrayOperations.test.js +0 -878
  154. package/dist/api.asyncHandler.test.d.ts +0 -1
  155. package/dist/api.asyncHandler.test.js +0 -236
  156. package/dist/api.coverage.test.d.ts +0 -1
  157. package/dist/api.coverage.test.js +0 -192
  158. package/dist/api.errorPaths.test.d.ts +0 -1
  159. package/dist/api.errorPaths.test.js +0 -887
  160. package/dist/api.errors.test.d.ts +0 -1
  161. package/dist/api.errors.test.js +0 -185
  162. package/dist/api.hooks.test.d.ts +0 -1
  163. package/dist/api.hooks.test.js +0 -950
  164. package/dist/api.query.test.d.ts +0 -1
  165. package/dist/api.query.test.js +0 -948
  166. package/dist/api.test.d.ts +0 -1
  167. package/dist/api.test.js +0 -2820
  168. package/dist/auth.compile.test.d.ts +0 -1
  169. package/dist/auth.compile.test.js +0 -201
  170. package/dist/auth.coverage.test.d.ts +0 -1
  171. package/dist/auth.coverage.test.js +0 -202
  172. package/dist/auth.test.d.ts +0 -1
  173. package/dist/auth.test.js +0 -2286
  174. package/dist/betterAuth.test.d.ts +0 -1
  175. package/dist/betterAuth.test.js +0 -181
  176. package/dist/betterAuthApp.test.d.ts +0 -1
  177. package/dist/betterAuthApp.test.js +0 -242
  178. package/dist/betterAuthSetup.test.d.ts +0 -1
  179. package/dist/betterAuthSetup.test.js +0 -762
  180. package/dist/config.test.d.ts +0 -1
  181. package/dist/config.test.js +0 -353
  182. package/dist/configuration.test.d.ts +0 -1
  183. package/dist/configuration.test.js +0 -980
  184. package/dist/configurationPlugin.test.d.ts +0 -1
  185. package/dist/configurationPlugin.test.js +0 -827
  186. package/dist/consentApp.test.d.ts +0 -1
  187. package/dist/consentApp.test.js +0 -1411
  188. package/dist/docLoader.test.d.ts +0 -1
  189. package/dist/docLoader.test.js +0 -137
  190. package/dist/envConfigurationPlugin.test.d.ts +0 -1
  191. package/dist/envConfigurationPlugin.test.js +0 -369
  192. package/dist/errors.test.d.ts +0 -1
  193. package/dist/errors.test.js +0 -607
  194. package/dist/expressServer.test.d.ts +0 -1
  195. package/dist/expressServer.test.js +0 -1249
  196. package/dist/githubAuth.test.d.ts +0 -1
  197. package/dist/githubAuth.test.js +0 -1142
  198. package/dist/httpClient.test.d.ts +0 -1
  199. package/dist/httpClient.test.js +0 -1471
  200. package/dist/logger.test.d.ts +0 -1
  201. package/dist/logger.test.js +0 -685
  202. package/dist/middleware.test.d.ts +0 -1
  203. package/dist/middleware.test.js +0 -241
  204. package/dist/notifiers/googleChatNotifier.test.d.ts +0 -1
  205. package/dist/notifiers/googleChatNotifier.test.js +0 -298
  206. package/dist/notifiers/slackNotifier.test.d.ts +0 -1
  207. package/dist/notifiers/slackNotifier.test.js +0 -297
  208. package/dist/notifiers/zoomNotifier.test.d.ts +0 -1
  209. package/dist/notifiers/zoomNotifier.test.js +0 -373
  210. package/dist/openApi.test.d.ts +0 -1
  211. package/dist/openApi.test.js +0 -620
  212. package/dist/openApiBuilder.test.d.ts +0 -1
  213. package/dist/openApiBuilder.test.js +0 -599
  214. package/dist/openApiEtag.test.d.ts +0 -1
  215. package/dist/openApiEtag.test.js +0 -86
  216. package/dist/openApiValidator.test.d.ts +0 -1
  217. package/dist/openApiValidator.test.js +0 -795
  218. package/dist/permissions.middleware.test.d.ts +0 -1
  219. package/dist/permissions.middleware.test.js +0 -308
  220. package/dist/permissions.test.d.ts +0 -1
  221. package/dist/permissions.test.js +0 -439
  222. package/dist/plugins.test.d.ts +0 -1
  223. package/dist/plugins.test.js +0 -902
  224. package/dist/populate.test.d.ts +0 -1
  225. package/dist/populate.test.js +0 -370
  226. package/dist/realtime/realtime.test.d.ts +0 -10
  227. package/dist/realtime/realtime.test.js +0 -4213
  228. package/dist/realtime/realtimeAppSetup.test.d.ts +0 -1
  229. package/dist/realtime/realtimeAppSetup.test.js +0 -213
  230. package/dist/requestContext.test.d.ts +0 -1
  231. package/dist/requestContext.test.js +0 -412
  232. package/dist/scriptRunner.test.d.ts +0 -1
  233. package/dist/scriptRunner.test.js +0 -386
  234. package/dist/secretProviders.test.d.ts +0 -1
  235. package/dist/secretProviders.test.js +0 -726
  236. package/dist/syncConsents.test.d.ts +0 -1
  237. package/dist/syncConsents.test.js +0 -522
  238. package/dist/terrenoApp.test.d.ts +0 -1
  239. package/dist/terrenoApp.test.js +0 -489
  240. package/dist/tests/bunSetup.d.ts +0 -1
  241. package/dist/tests/fixtures/compileAuthEntry.d.ts +0 -1
  242. package/dist/tests/fixtures/compileAuthEntry.js +0 -37
  243. package/dist/tests/testEnv.d.ts +0 -0
  244. package/dist/tests/testEnv.js +0 -6
  245. package/dist/transformers.test.d.ts +0 -1
  246. package/dist/transformers.test.js +0 -423
  247. package/dist/utils.test.d.ts +0 -1
  248. package/dist/utils.test.js +0 -242
  249. package/src/realtime/realtimeAppSetup.test.ts +0 -161
  250. /package/dist/{__tests__/openApiCompat.test.d.ts → sync/loadHarness.d.ts} +0 -0
@@ -1,1087 +0,0 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
- return new (P || (P = Promise))(function (resolve, reject) {
16
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
- step((generator = generator.apply(thisArg, _arguments || [])).next());
20
- });
21
- };
22
- var __generator = (this && this.__generator) || function (thisArg, body) {
23
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
24
- return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
- function verb(n) { return function (v) { return step([n, v]); }; }
26
- function step(op) {
27
- if (f) throw new TypeError("Generator is already executing.");
28
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
- 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;
30
- if (y = 0, t) op = [op[0] & 2, t.value];
31
- switch (op[0]) {
32
- case 0: case 1: t = op; break;
33
- case 4: _.label++; return { value: op[1], done: false };
34
- case 5: _.label++; y = op[1]; op = [0]; continue;
35
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
- default:
37
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
- if (t[2]) _.ops.pop();
42
- _.trys.pop(); continue;
43
- }
44
- op = body.call(thisArg, _);
45
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
- }
48
- };
49
- var __read = (this && this.__read) || function (o, n) {
50
- var m = typeof Symbol === "function" && o[Symbol.iterator];
51
- if (!m) return o;
52
- var i = m.call(o), r, ar = [], e;
53
- try {
54
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
55
- }
56
- catch (error) { e = { error: error }; }
57
- finally {
58
- try {
59
- if (r && !r.done && (m = i["return"])) m.call(i);
60
- }
61
- finally { if (e) throw e.error; }
62
- }
63
- return ar;
64
- };
65
- var __importDefault = (this && this.__importDefault) || function (mod) {
66
- return (mod && mod.__esModule) ? mod : { "default": mod };
67
- };
68
- Object.defineProperty(exports, "__esModule", { value: true });
69
- var bun_test_1 = require("bun:test");
70
- var mongoose_1 = require("mongoose");
71
- var supertest_1 = __importDefault(require("supertest"));
72
- var actions_1 = require("./actions");
73
- var api_1 = require("./api");
74
- var auth_1 = require("./auth");
75
- var errors_1 = require("./errors");
76
- var permissions_1 = require("./permissions");
77
- var plugins_1 = require("./plugins");
78
- var tests_1 = require("./tests");
79
- var zodOpenApi_1 = require("./zodOpenApi");
80
- var stuffSchema = new mongoose_1.Schema({
81
- name: { type: String },
82
- ownerId: { type: String },
83
- });
84
- stuffSchema.plugin(plugins_1.isDeletedPlugin);
85
- var StuffModel = (0, mongoose_1.model)("ActionStuff", stuffSchema);
86
- var allPermissions = {
87
- create: [permissions_1.Permissions.IsAny],
88
- delete: [permissions_1.Permissions.IsAny],
89
- list: [permissions_1.Permissions.IsAny],
90
- read: [permissions_1.Permissions.IsAny],
91
- update: [permissions_1.Permissions.IsAny],
92
- };
93
- (0, bun_test_1.describe)("modelRouter actions", function () {
94
- (0, bun_test_1.describe)("registration validation", function () {
95
- (0, bun_test_1.it)("throws when permissions are missing", function () {
96
- (0, bun_test_1.expect)(function () {
97
- return (0, api_1.modelRouter)(tests_1.FoodModel, {
98
- collectionActions: {
99
- broken: {
100
- handler: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
101
- return [2 /*return*/, ({ ok: true })];
102
- }); }); },
103
- method: "POST",
104
- },
105
- },
106
- permissions: allPermissions,
107
- });
108
- }).toThrow(/missing required "permissions"/);
109
- });
110
- (0, bun_test_1.it)("rejects single-character action names by design", function () {
111
- (0, bun_test_1.expect)(function () {
112
- return (0, api_1.modelRouter)(tests_1.FoodModel, {
113
- collectionActions: {
114
- a: {
115
- handler: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
116
- return [2 /*return*/, ({})];
117
- }); }); },
118
- method: "GET",
119
- permissions: [permissions_1.Permissions.IsAny],
120
- },
121
- },
122
- permissions: allPermissions,
123
- });
124
- }).toThrow(actions_1.ACTION_NAME_PATTERN.toString());
125
- });
126
- (0, bun_test_1.it)("throws on invalid action name", function () {
127
- (0, bun_test_1.expect)(function () {
128
- return (0, api_1.modelRouter)(tests_1.FoodModel, {
129
- collectionActions: {
130
- "foo*bar": {
131
- handler: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
132
- return [2 /*return*/, ({})];
133
- }); }); },
134
- method: "GET",
135
- permissions: [permissions_1.Permissions.IsAny],
136
- },
137
- },
138
- permissions: allPermissions,
139
- });
140
- }).toThrow(actions_1.ACTION_NAME_PATTERN.toString());
141
- });
142
- (0, bun_test_1.it)("throws on empty action name", function () {
143
- (0, bun_test_1.expect)(function () {
144
- return (0, api_1.modelRouter)(tests_1.FoodModel, {
145
- collectionActions: {
146
- "": {
147
- handler: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
148
- return [2 /*return*/, ({})];
149
- }); }); },
150
- method: "GET",
151
- permissions: [permissions_1.Permissions.IsAny],
152
- },
153
- },
154
- permissions: allPermissions,
155
- });
156
- }).toThrow("Action name cannot be empty");
157
- });
158
- (0, bun_test_1.it)("throws when instance action collides with array field", function () {
159
- (0, bun_test_1.expect)(function () {
160
- return (0, api_1.modelRouter)(tests_1.FoodModel, {
161
- instanceActions: {
162
- tags: {
163
- handler: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
164
- return [2 /*return*/, ({})];
165
- }); }); },
166
- method: "GET",
167
- permissions: [permissions_1.Permissions.IsAny],
168
- },
169
- },
170
- permissions: allPermissions,
171
- });
172
- }).toThrow(/collides with array field/);
173
- });
174
- (0, bun_test_1.it)("allows same action name on instance and collection scopes", function () {
175
- (0, bun_test_1.expect)(function () {
176
- return (0, api_1.modelRouter)(tests_1.FoodModel, {
177
- collectionActions: {
178
- sync: {
179
- handler: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
180
- return [2 /*return*/, ({ scope: "collection" })];
181
- }); }); },
182
- method: "POST",
183
- permissions: [permissions_1.Permissions.IsAny],
184
- },
185
- },
186
- instanceActions: {
187
- sync: {
188
- handler: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
189
- return [2 /*return*/, ({ scope: "instance" })];
190
- }); }); },
191
- method: "POST",
192
- permissions: [permissions_1.Permissions.IsAny],
193
- },
194
- },
195
- permissions: allPermissions,
196
- });
197
- }).not.toThrow();
198
- });
199
- });
200
- (0, bun_test_1.describe)("integration", function () {
201
- var app;
202
- var server;
203
- var admin;
204
- var notAdmin;
205
- var spinach;
206
- var mountFoodRouter = function (options) {
207
- app.use("/food", (0, api_1.modelRouter)(tests_1.FoodModel, __assign({ allowAnonymous: true }, options)));
208
- if (!app.get("terrenoUnauthorizedMiddleware")) {
209
- app.use(errors_1.apiUnauthorizedMiddleware);
210
- app.set("terrenoUnauthorizedMiddleware", true);
211
- }
212
- };
213
- (0, bun_test_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
214
- var _a, _b;
215
- return __generator(this, function (_c) {
216
- switch (_c.label) {
217
- case 0:
218
- process.env.REFRESH_TOKEN_SECRET = "testsecret1234";
219
- return [4 /*yield*/, (0, tests_1.setupDb)()];
220
- case 1:
221
- _a = __read.apply(void 0, [_c.sent(), 2]), admin = _a[0], notAdmin = _a[1];
222
- return [4 /*yield*/, Promise.all([
223
- tests_1.FoodModel.create({
224
- calories: 1,
225
- created: new Date(),
226
- hidden: false,
227
- name: "Spinach",
228
- ownerId: notAdmin._id,
229
- source: { name: "test" },
230
- }),
231
- ])];
232
- case 2:
233
- _b = __read.apply(void 0, [_c.sent(), 1]), spinach = _b[0];
234
- app = (0, tests_1.getBaseServer)();
235
- (0, auth_1.setupAuth)(app, tests_1.UserModel);
236
- (0, auth_1.addAuthRoutes)(app, tests_1.UserModel);
237
- server = (0, supertest_1.default)(app);
238
- return [2 /*return*/];
239
- }
240
- });
241
- }); });
242
- (0, bun_test_1.describe)("routing and permissions", function () {
243
- (0, bun_test_1.it)("allows empty permissions array and returns 405 at runtime", function () { return __awaiter(void 0, void 0, void 0, function () {
244
- var agent, res;
245
- return __generator(this, function (_a) {
246
- switch (_a.label) {
247
- case 0:
248
- mountFoodRouter({
249
- collectionActions: {
250
- disabled: {
251
- handler: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
252
- return [2 /*return*/, ({ ok: true })];
253
- }); }); },
254
- method: "POST",
255
- permissions: [],
256
- },
257
- },
258
- permissions: allPermissions,
259
- });
260
- return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
261
- case 1:
262
- agent = _a.sent();
263
- return [4 /*yield*/, agent.post("/food/disabled").send({}).expect(405)];
264
- case 2:
265
- res = _a.sent();
266
- (0, bun_test_1.expect)(res.body.title).toBe("Access denied");
267
- (0, bun_test_1.expect)(res.body.detail).toContain("Access to CREATE on Food denied");
268
- return [2 /*return*/];
269
- }
270
- });
271
- }); });
272
- (0, bun_test_1.it)("runs instance POST action with ctx.doc and req.obj", function () { return __awaiter(void 0, void 0, void 0, function () {
273
- var seenDoc, seenObj, res;
274
- return __generator(this, function (_a) {
275
- switch (_a.label) {
276
- case 0:
277
- mountFoodRouter({
278
- instanceActions: {
279
- mark: {
280
- handler: function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
281
- var doc = _b.doc, req = _b.req;
282
- return __generator(this, function (_c) {
283
- seenDoc = doc;
284
- seenObj = req.obj;
285
- return [2 /*return*/, { marked: true }];
286
- });
287
- }); },
288
- method: "POST",
289
- permissions: [permissions_1.Permissions.IsAny],
290
- },
291
- },
292
- permissions: allPermissions,
293
- });
294
- return [4 /*yield*/, server.post("/food/".concat(spinach._id, "/mark")).send({}).expect(200)];
295
- case 1:
296
- res = _a.sent();
297
- (0, bun_test_1.expect)(res.body.data).toEqual({ marked: true });
298
- (0, bun_test_1.expect)(seenDoc === null || seenDoc === void 0 ? void 0 : seenDoc._id.toString()).toBe(spinach._id.toString());
299
- (0, bun_test_1.expect)(seenObj === null || seenObj === void 0 ? void 0 : seenObj._id.toString()).toBe(spinach._id.toString());
300
- return [2 /*return*/];
301
- }
302
- });
303
- }); });
304
- (0, bun_test_1.it)("runs collection POST action without doc", function () { return __awaiter(void 0, void 0, void 0, function () {
305
- var res;
306
- return __generator(this, function (_a) {
307
- switch (_a.label) {
308
- case 0:
309
- mountFoodRouter({
310
- collectionActions: {
311
- bulk: {
312
- handler: function (ctx) { return __awaiter(void 0, void 0, void 0, function () {
313
- return __generator(this, function (_a) {
314
- (0, bun_test_1.expect)(ctx.doc).toBeUndefined();
315
- return [2 /*return*/, { count: 1 }];
316
- });
317
- }); },
318
- method: "POST",
319
- permissions: [permissions_1.Permissions.IsAny],
320
- },
321
- },
322
- permissions: allPermissions,
323
- });
324
- return [4 /*yield*/, server.post("/food/bulk").send({}).expect(200)];
325
- case 1:
326
- res = _a.sent();
327
- (0, bun_test_1.expect)(res.body.data).toEqual({ count: 1 });
328
- return [2 /*return*/];
329
- }
330
- });
331
- }); });
332
- (0, bun_test_1.it)("runs GET instance and collection actions", function () { return __awaiter(void 0, void 0, void 0, function () {
333
- var collectionRes, instanceRes;
334
- return __generator(this, function (_a) {
335
- switch (_a.label) {
336
- case 0:
337
- mountFoodRouter({
338
- collectionActions: {
339
- stats: {
340
- handler: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
341
- return [2 /*return*/, ({ total: 1 })];
342
- }); }); },
343
- method: "GET",
344
- permissions: [permissions_1.Permissions.IsAny],
345
- },
346
- },
347
- instanceActions: {
348
- peek: {
349
- handler: function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
350
- var doc = _b.doc;
351
- return __generator(this, function (_c) {
352
- return [2 /*return*/, ({ name: doc === null || doc === void 0 ? void 0 : doc.name })];
353
- });
354
- }); },
355
- method: "GET",
356
- permissions: [permissions_1.Permissions.IsAny],
357
- },
358
- },
359
- permissions: allPermissions,
360
- });
361
- return [4 /*yield*/, server.get("/food/stats").expect(200)];
362
- case 1:
363
- collectionRes = _a.sent();
364
- (0, bun_test_1.expect)(collectionRes.body.data).toEqual({ total: 1 });
365
- return [4 /*yield*/, server.get("/food/".concat(spinach._id, "/peek")).expect(200)];
366
- case 2:
367
- instanceRes = _a.sent();
368
- (0, bun_test_1.expect)(instanceRes.body.data).toEqual({ name: "Spinach" });
369
- return [2 /*return*/];
370
- }
371
- });
372
- }); });
373
- (0, bun_test_1.it)("returns 404 for missing instance doc", function () { return __awaiter(void 0, void 0, void 0, function () {
374
- var missingId, res;
375
- return __generator(this, function (_a) {
376
- switch (_a.label) {
377
- case 0:
378
- mountFoodRouter({
379
- instanceActions: {
380
- peek: {
381
- handler: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
382
- return [2 /*return*/, ({})];
383
- }); }); },
384
- method: "GET",
385
- permissions: [permissions_1.Permissions.IsAny],
386
- },
387
- },
388
- permissions: allPermissions,
389
- });
390
- missingId = "507f1f77bcf86cd799439011";
391
- return [4 /*yield*/, server.get("/food/".concat(missingId, "/peek")).expect(404)];
392
- case 1:
393
- res = _a.sent();
394
- (0, bun_test_1.expect)(res.body.title).toBe("Document not found");
395
- (0, bun_test_1.expect)(res.body.detail).toContain(missingId);
396
- (0, bun_test_1.expect)(res.body.meta).toBeUndefined();
397
- return [2 /*return*/];
398
- }
399
- });
400
- }); });
401
- (0, bun_test_1.it)("returns 404 with soft-delete metadata on instance action", function () { return __awaiter(void 0, void 0, void 0, function () {
402
- var doc, agent, res;
403
- return __generator(this, function (_a) {
404
- switch (_a.label) {
405
- case 0: return [4 /*yield*/, StuffModel.deleteMany({})];
406
- case 1:
407
- _a.sent();
408
- return [4 /*yield*/, StuffModel.create({ deleted: true, name: "hidden", ownerId: "1" })];
409
- case 2:
410
- doc = _a.sent();
411
- app = (0, tests_1.getBaseServer)();
412
- (0, auth_1.setupAuth)(app, tests_1.UserModel);
413
- (0, auth_1.addAuthRoutes)(app, tests_1.UserModel);
414
- app.use("/stuff", (0, api_1.modelRouter)(StuffModel, {
415
- allowAnonymous: true,
416
- instanceActions: {
417
- peek: {
418
- handler: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
419
- return [2 /*return*/, ({})];
420
- }); }); },
421
- method: "GET",
422
- permissions: [permissions_1.Permissions.IsAny],
423
- },
424
- },
425
- permissions: allPermissions,
426
- }));
427
- return [4 /*yield*/, (0, tests_1.authAsUser)(app, "notAdmin")];
428
- case 3:
429
- agent = _a.sent();
430
- return [4 /*yield*/, agent.get("/stuff/".concat(doc._id, "/peek")).expect(404)];
431
- case 4:
432
- res = _a.sent();
433
- (0, bun_test_1.expect)(res.body.meta).toEqual({ deleted: "true" });
434
- return [2 /*return*/];
435
- }
436
- });
437
- }); });
438
- (0, bun_test_1.it)("returns 401 when unauthenticated and IsAuthenticated required", function () { return __awaiter(void 0, void 0, void 0, function () {
439
- var res;
440
- return __generator(this, function (_a) {
441
- switch (_a.label) {
442
- case 0:
443
- mountFoodRouter({
444
- allowAnonymous: false,
445
- collectionActions: {
446
- secure: {
447
- handler: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
448
- return [2 /*return*/, ({ ok: true })];
449
- }); }); },
450
- method: "POST",
451
- permissions: [permissions_1.Permissions.IsAuthenticated],
452
- },
453
- },
454
- permissions: allPermissions,
455
- });
456
- return [4 /*yield*/, server.post("/food/secure").send({}).expect(401)];
457
- case 1:
458
- res = _a.sent();
459
- (0, bun_test_1.expect)(res.body.title).toBe("Unauthorized");
460
- return [2 /*return*/];
461
- }
462
- });
463
- }); });
464
- (0, bun_test_1.it)("returns 405 for collection action when pre-doc permission denied", function () { return __awaiter(void 0, void 0, void 0, function () {
465
- var agent, res;
466
- return __generator(this, function (_a) {
467
- switch (_a.label) {
468
- case 0:
469
- mountFoodRouter({
470
- collectionActions: {
471
- adminOnly: {
472
- handler: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
473
- return [2 /*return*/, ({ ok: true })];
474
- }); }); },
475
- method: "POST",
476
- permissions: [permissions_1.Permissions.IsAdmin],
477
- },
478
- },
479
- permissions: allPermissions,
480
- });
481
- return [4 /*yield*/, (0, tests_1.authAsUser)(app, "notAdmin")];
482
- case 1:
483
- agent = _a.sent();
484
- return [4 /*yield*/, agent.post("/food/adminOnly").send({}).expect(405)];
485
- case 2:
486
- res = _a.sent();
487
- (0, bun_test_1.expect)(res.body.title).toBe("Access denied");
488
- (0, bun_test_1.expect)(res.body.detail).toContain("Access to CREATE on Food denied");
489
- return [2 /*return*/];
490
- }
491
- });
492
- }); });
493
- (0, bun_test_1.it)("returns 403 for instance action when post-doc permission denied", function () { return __awaiter(void 0, void 0, void 0, function () {
494
- var adminFood, agent, res;
495
- return __generator(this, function (_a) {
496
- switch (_a.label) {
497
- case 0: return [4 /*yield*/, tests_1.FoodModel.create({
498
- calories: 2,
499
- created: new Date(),
500
- hidden: false,
501
- name: "AdminApple",
502
- ownerId: admin._id,
503
- source: { name: "test" },
504
- })];
505
- case 1:
506
- adminFood = _a.sent();
507
- mountFoodRouter({
508
- instanceActions: {
509
- ownerOnly: {
510
- handler: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
511
- return [2 /*return*/, ({ ok: true })];
512
- }); }); },
513
- method: "POST",
514
- permissions: [permissions_1.Permissions.IsOwner],
515
- },
516
- },
517
- permissions: allPermissions,
518
- });
519
- return [4 /*yield*/, (0, tests_1.authAsUser)(app, "notAdmin")];
520
- case 2:
521
- agent = _a.sent();
522
- return [4 /*yield*/, agent.post("/food/".concat(adminFood._id, "/ownerOnly")).send({}).expect(403)];
523
- case 3:
524
- res = _a.sent();
525
- (0, bun_test_1.expect)(res.body.title).toBe("Access denied");
526
- (0, bun_test_1.expect)(res.body.detail).toContain("Access to UPDATE on Food:".concat(adminFood._id, " denied"));
527
- return [2 /*return*/];
528
- }
529
- });
530
- }); });
531
- (0, bun_test_1.it)("allows IsAuthenticatedOrReadOnly on GET with allowAnonymous", function () { return __awaiter(void 0, void 0, void 0, function () {
532
- var res;
533
- return __generator(this, function (_a) {
534
- switch (_a.label) {
535
- case 0:
536
- mountFoodRouter({
537
- allowAnonymous: true,
538
- instanceActions: {
539
- publicRead: {
540
- handler: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
541
- return [2 /*return*/, ({ ok: true })];
542
- }); }); },
543
- method: "GET",
544
- permissions: [permissions_1.Permissions.IsAuthenticatedOrReadOnly],
545
- },
546
- },
547
- permissions: allPermissions,
548
- });
549
- return [4 /*yield*/, server.get("/food/".concat(spinach._id, "/publicRead")).expect(200)];
550
- case 1:
551
- res = _a.sent();
552
- (0, bun_test_1.expect)(res.body.data).toEqual({ ok: true });
553
- return [2 /*return*/];
554
- }
555
- });
556
- }); });
557
- });
558
- (0, bun_test_1.describe)("validation", function () {
559
- (0, bun_test_1.it)("passes valid body through ctx", function () { return __awaiter(void 0, void 0, void 0, function () {
560
- var seenEmail;
561
- return __generator(this, function (_a) {
562
- switch (_a.label) {
563
- case 0:
564
- mountFoodRouter({
565
- collectionActions: {
566
- notify: {
567
- body: zodOpenApi_1.z.object({ email: zodOpenApi_1.z.string().email() }),
568
- handler: function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
569
- var body = _b.body;
570
- return __generator(this, function (_c) {
571
- seenEmail = body.email;
572
- return [2 /*return*/, { sent: true }];
573
- });
574
- }); },
575
- method: "POST",
576
- permissions: [permissions_1.Permissions.IsAny],
577
- },
578
- },
579
- permissions: allPermissions,
580
- });
581
- return [4 /*yield*/, server.post("/food/notify").send({ email: "a@b.com" }).expect(200)];
582
- case 1:
583
- _a.sent();
584
- (0, bun_test_1.expect)(seenEmail).toBe("a@b.com");
585
- return [2 /*return*/];
586
- }
587
- });
588
- }); });
589
- (0, bun_test_1.it)("returns 400 with meta.fields for invalid body", function () { return __awaiter(void 0, void 0, void 0, function () {
590
- var res;
591
- return __generator(this, function (_a) {
592
- switch (_a.label) {
593
- case 0:
594
- mountFoodRouter({
595
- collectionActions: {
596
- notify: {
597
- body: zodOpenApi_1.z.object({ email: zodOpenApi_1.z.string().email() }),
598
- handler: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
599
- return [2 /*return*/, ({ sent: true })];
600
- }); }); },
601
- method: "POST",
602
- permissions: [permissions_1.Permissions.IsAny],
603
- },
604
- },
605
- permissions: allPermissions,
606
- });
607
- return [4 /*yield*/, server.post("/food/notify").send({ email: "not-an-email" }).expect(400)];
608
- case 1:
609
- res = _a.sent();
610
- (0, bun_test_1.expect)(res.body.title).toBe("Validation failed");
611
- (0, bun_test_1.expect)(res.body.code).toBe("action-body-validation-failed");
612
- (0, bun_test_1.expect)(res.body.meta.fields.email).toBeDefined();
613
- return [2 /*return*/];
614
- }
615
- });
616
- }); });
617
- (0, bun_test_1.it)("validates query schema into ctx without mutating req.query", function () { return __awaiter(void 0, void 0, void 0, function () {
618
- var seenQ, originalQ;
619
- return __generator(this, function (_a) {
620
- switch (_a.label) {
621
- case 0:
622
- mountFoodRouter({
623
- collectionActions: {
624
- search: {
625
- handler: function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
626
- var query = _b.query, req = _b.req;
627
- return __generator(this, function (_c) {
628
- seenQ = query.count;
629
- originalQ = req.query.count;
630
- return [2 /*return*/];
631
- });
632
- }); },
633
- method: "GET",
634
- permissions: [permissions_1.Permissions.IsAny],
635
- query: zodOpenApi_1.z.object({ count: zodOpenApi_1.z.coerce.number() }),
636
- },
637
- },
638
- permissions: allPermissions,
639
- });
640
- return [4 /*yield*/, server.get("/food/search?count=5").expect(200)];
641
- case 1:
642
- _a.sent();
643
- (0, bun_test_1.expect)(seenQ).toBe(5);
644
- (0, bun_test_1.expect)(originalQ).toBe("5");
645
- return [2 /*return*/];
646
- }
647
- });
648
- }); });
649
- (0, bun_test_1.it)("returns 400 with meta.fields for invalid query", function () { return __awaiter(void 0, void 0, void 0, function () {
650
- var res;
651
- return __generator(this, function (_a) {
652
- switch (_a.label) {
653
- case 0:
654
- mountFoodRouter({
655
- collectionActions: {
656
- lookup: {
657
- handler: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
658
- return [2 /*return*/, ({ ok: true })];
659
- }); }); },
660
- method: "GET",
661
- permissions: [permissions_1.Permissions.IsAny],
662
- query: zodOpenApi_1.z.object({ email: zodOpenApi_1.z.string().email() }),
663
- },
664
- },
665
- permissions: allPermissions,
666
- });
667
- return [4 /*yield*/, server.get("/food/lookup?email=bad").expect(400)];
668
- case 1:
669
- res = _a.sent();
670
- (0, bun_test_1.expect)(res.body.title).toBe("Validation failed");
671
- (0, bun_test_1.expect)(res.body.code).toBe("action-query-validation-failed");
672
- (0, bun_test_1.expect)(res.body.meta.fields.email).toBeDefined();
673
- return [2 /*return*/];
674
- }
675
- });
676
- }); });
677
- (0, bun_test_1.it)("coerces body values via zod in ctx", function () { return __awaiter(void 0, void 0, void 0, function () {
678
- var seenCount, res;
679
- return __generator(this, function (_a) {
680
- switch (_a.label) {
681
- case 0:
682
- mountFoodRouter({
683
- collectionActions: {
684
- tally: {
685
- body: zodOpenApi_1.z.object({ count: zodOpenApi_1.z.coerce.number() }),
686
- handler: function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
687
- var parsed;
688
- var body = _b.body;
689
- return __generator(this, function (_c) {
690
- parsed = body;
691
- seenCount = parsed.count;
692
- return [2 /*return*/, { count: parsed.count }];
693
- });
694
- }); },
695
- method: "POST",
696
- permissions: [permissions_1.Permissions.IsAny],
697
- },
698
- },
699
- permissions: allPermissions,
700
- });
701
- return [4 /*yield*/, server.post("/food/tally").send({ count: "5" }).expect(200)];
702
- case 1:
703
- res = _a.sent();
704
- (0, bun_test_1.expect)(seenCount).toBe(5);
705
- (0, bun_test_1.expect)(res.body.data).toEqual({ count: 5 });
706
- return [2 /*return*/];
707
- }
708
- });
709
- }); });
710
- (0, bun_test_1.it)("strips unknown body fields by default", function () { return __awaiter(void 0, void 0, void 0, function () {
711
- var seenBody;
712
- return __generator(this, function (_a) {
713
- switch (_a.label) {
714
- case 0:
715
- seenBody = {};
716
- mountFoodRouter({
717
- collectionActions: {
718
- strictish: {
719
- body: zodOpenApi_1.z.object({ known: zodOpenApi_1.z.string() }),
720
- handler: function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
721
- var body = _b.body;
722
- return __generator(this, function (_c) {
723
- seenBody = body;
724
- return [2 /*return*/, { ok: true }];
725
- });
726
- }); },
727
- method: "POST",
728
- permissions: [permissions_1.Permissions.IsAny],
729
- },
730
- },
731
- permissions: allPermissions,
732
- });
733
- return [4 /*yield*/, server.post("/food/strictish").send({ extra: "x", known: "y" }).expect(200)];
734
- case 1:
735
- _a.sent();
736
- (0, bun_test_1.expect)(seenBody).toEqual({ known: "y" });
737
- return [2 /*return*/];
738
- }
739
- });
740
- }); });
741
- });
742
- (0, bun_test_1.describe)("response shape", function () {
743
- (0, bun_test_1.it)("wraps handler return in data envelope", function () { return __awaiter(void 0, void 0, void 0, function () {
744
- var res;
745
- return __generator(this, function (_a) {
746
- switch (_a.label) {
747
- case 0:
748
- mountFoodRouter({
749
- collectionActions: {
750
- echo: {
751
- handler: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
752
- return [2 /*return*/, ({ x: 1 })];
753
- }); }); },
754
- method: "POST",
755
- permissions: [permissions_1.Permissions.IsAny],
756
- },
757
- },
758
- permissions: allPermissions,
759
- });
760
- return [4 /*yield*/, server.post("/food/echo").send({}).expect(200)];
761
- case 1:
762
- res = _a.sent();
763
- (0, bun_test_1.expect)(res.body).toEqual({ data: { x: 1 } });
764
- return [2 /*return*/];
765
- }
766
- });
767
- }); });
768
- (0, bun_test_1.it)("respects custom status code", function () { return __awaiter(void 0, void 0, void 0, function () {
769
- var res;
770
- return __generator(this, function (_a) {
771
- switch (_a.label) {
772
- case 0:
773
- mountFoodRouter({
774
- collectionActions: {
775
- queue: {
776
- handler: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
777
- return [2 /*return*/, ({ queued: true })];
778
- }); }); },
779
- method: "POST",
780
- permissions: [permissions_1.Permissions.IsAny],
781
- status: 202,
782
- },
783
- },
784
- permissions: allPermissions,
785
- });
786
- return [4 /*yield*/, server.post("/food/queue").send({}).expect(202)];
787
- case 1:
788
- res = _a.sent();
789
- (0, bun_test_1.expect)(res.body).toEqual({ data: { queued: true } });
790
- return [2 /*return*/];
791
- }
792
- });
793
- }); });
794
- (0, bun_test_1.it)("returns data null for undefined handler return", function () { return __awaiter(void 0, void 0, void 0, function () {
795
- var res;
796
- return __generator(this, function (_a) {
797
- switch (_a.label) {
798
- case 0:
799
- mountFoodRouter({
800
- collectionActions: {
801
- noop: {
802
- handler: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
803
- return [2 /*return*/, undefined];
804
- }); }); },
805
- method: "POST",
806
- permissions: [permissions_1.Permissions.IsAny],
807
- },
808
- },
809
- permissions: allPermissions,
810
- });
811
- return [4 /*yield*/, server.post("/food/noop").send({}).expect(200)];
812
- case 1:
813
- res = _a.sent();
814
- (0, bun_test_1.expect)(res.body).toEqual({ data: null });
815
- return [2 /*return*/];
816
- }
817
- });
818
- }); });
819
- (0, bun_test_1.it)("skips auto-wrap when res.headersSent", function () { return __awaiter(void 0, void 0, void 0, function () {
820
- var res;
821
- return __generator(this, function (_a) {
822
- switch (_a.label) {
823
- case 0:
824
- mountFoodRouter({
825
- collectionActions: {
826
- custom: {
827
- handler: function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
828
- var res = _b.res;
829
- return __generator(this, function (_c) {
830
- res.json({ custom: 1 });
831
- return [2 /*return*/];
832
- });
833
- }); },
834
- method: "POST",
835
- permissions: [permissions_1.Permissions.IsAny],
836
- },
837
- },
838
- permissions: allPermissions,
839
- });
840
- return [4 /*yield*/, server.post("/food/custom").send({}).expect(200)];
841
- case 1:
842
- res = _a.sent();
843
- (0, bun_test_1.expect)(res.body).toEqual({ custom: 1 });
844
- return [2 /*return*/];
845
- }
846
- });
847
- }); });
848
- (0, bun_test_1.it)("allows custom list-style envelope via res.json", function () { return __awaiter(void 0, void 0, void 0, function () {
849
- var res;
850
- return __generator(this, function (_a) {
851
- switch (_a.label) {
852
- case 0:
853
- mountFoodRouter({
854
- collectionActions: {
855
- paged: {
856
- handler: function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
857
- var res = _b.res;
858
- return __generator(this, function (_c) {
859
- res.json({ data: [{ id: 1 }], more: false, page: 1, total: 1 });
860
- return [2 /*return*/];
861
- });
862
- }); },
863
- method: "GET",
864
- permissions: [permissions_1.Permissions.IsAny],
865
- },
866
- },
867
- permissions: allPermissions,
868
- });
869
- return [4 /*yield*/, server.get("/food/paged").expect(200)];
870
- case 1:
871
- res = _a.sent();
872
- (0, bun_test_1.expect)(res.body).toEqual({ data: [{ id: 1 }], more: false, page: 1, total: 1 });
873
- (0, bun_test_1.expect)(res.body.data).not.toHaveProperty("data");
874
- return [2 /*return*/];
875
- }
876
- });
877
- }); });
878
- });
879
- (0, bun_test_1.describe)("co-registration precedence", function () {
880
- (0, bun_test_1.it)("instance action wins over endpoints route on same path", function () { return __awaiter(void 0, void 0, void 0, function () {
881
- var res;
882
- return __generator(this, function (_a) {
883
- switch (_a.label) {
884
- case 0:
885
- mountFoodRouter({
886
- endpoints: function (router) {
887
- router.get("/:id/foo", function (_req, res) {
888
- res.json({ data: { from: "endpoints" } });
889
- });
890
- },
891
- instanceActions: {
892
- foo: {
893
- handler: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
894
- return [2 /*return*/, ({ from: "action" })];
895
- }); }); },
896
- method: "GET",
897
- permissions: [permissions_1.Permissions.IsAny],
898
- },
899
- },
900
- permissions: allPermissions,
901
- });
902
- return [4 /*yield*/, server.get("/food/".concat(spinach._id, "/foo")).expect(200)];
903
- case 1:
904
- res = _a.sent();
905
- (0, bun_test_1.expect)(res.body.data).toEqual({ from: "action" });
906
- return [2 /*return*/];
907
- }
908
- });
909
- }); });
910
- (0, bun_test_1.it)("collection action wins over endpoints route on same path", function () { return __awaiter(void 0, void 0, void 0, function () {
911
- var res;
912
- return __generator(this, function (_a) {
913
- switch (_a.label) {
914
- case 0:
915
- mountFoodRouter({
916
- collectionActions: {
917
- report: {
918
- handler: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
919
- return [2 /*return*/, ({ from: "action" })];
920
- }); }); },
921
- method: "GET",
922
- permissions: [permissions_1.Permissions.IsAny],
923
- },
924
- },
925
- endpoints: function (router) {
926
- router.get("/report", function (_req, res) {
927
- res.json({ data: { from: "endpoints" } });
928
- });
929
- },
930
- permissions: allPermissions,
931
- });
932
- return [4 /*yield*/, server.get("/food/report").expect(200)];
933
- case 1:
934
- res = _a.sent();
935
- (0, bun_test_1.expect)(res.body.data).toEqual({ from: "action" });
936
- return [2 /*return*/];
937
- }
938
- });
939
- }); });
940
- });
941
- });
942
- (0, bun_test_1.describe)("defineInstanceAction type ergonomics", function () {
943
- (0, bun_test_1.it)("preserves handler types at compile time", function () {
944
- var action = (0, actions_1.defineInstanceAction)({
945
- body: zodOpenApi_1.z.object({ notifyUsers: zodOpenApi_1.z.boolean() }),
946
- handler: function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
947
- var _doc, _notify;
948
- var _c, _d;
949
- var body = _b.body, doc = _b.doc;
950
- return __generator(this, function (_e) {
951
- _doc = doc;
952
- _notify = body.notifyUsers;
953
- return [2 /*return*/, { notify: _notify, publishedAt: (_d = (_c = _doc.publishedAt) === null || _c === void 0 ? void 0 : _c.toISOString()) !== null && _d !== void 0 ? _d : null }];
954
- });
955
- }); },
956
- method: "POST",
957
- permissions: [permissions_1.Permissions.IsAny],
958
- });
959
- (0, bun_test_1.expect)(action.method).toBe("POST");
960
- });
961
- (0, bun_test_1.it)("defineCollectionAction preserves body types", function () {
962
- var action = (0, actions_1.defineCollectionAction)({
963
- body: zodOpenApi_1.z.object({ ids: zodOpenApi_1.z.array(zodOpenApi_1.z.string()) }),
964
- handler: function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
965
- var _ids;
966
- var body = _b.body;
967
- return __generator(this, function (_c) {
968
- _ids = body.ids;
969
- return [2 /*return*/, { count: _ids.length }];
970
- });
971
- }); },
972
- method: "POST",
973
- permissions: [permissions_1.Permissions.IsAny],
974
- });
975
- (0, bun_test_1.expect)(action.method).toBe("POST");
976
- });
977
- });
978
- (0, bun_test_1.describe)("createActionOpenApiMiddleware", function () {
979
- var createMockOpenApi = function () {
980
- var captured;
981
- var pathFn = function (op) {
982
- captured = op;
983
- return function (_req, _res, next) {
984
- return next();
985
- };
986
- };
987
- return {
988
- get captured() {
989
- return captured;
990
- },
991
- middleware: { path: pathFn },
992
- };
993
- };
994
- (0, bun_test_1.it)("returns no-op middleware when openApi is not configured", function () {
995
- var handler = (0, actions_1.createActionOpenApiMiddleware)({
996
- action: { method: "GET", permissions: [] },
997
- actionName: "test",
998
- model: tests_1.FoodModel,
999
- options: {},
1000
- scope: "collection",
1001
- });
1002
- (0, bun_test_1.expect)(typeof handler).toBe("function");
1003
- });
1004
- (0, bun_test_1.it)("generates query parameters from action.query schema", function () {
1005
- var _a;
1006
- var mock = createMockOpenApi();
1007
- (0, actions_1.createActionOpenApiMiddleware)({
1008
- action: {
1009
- method: "GET",
1010
- permissions: [],
1011
- query: zodOpenApi_1.z.object({ page: zodOpenApi_1.z.coerce.number(), search: zodOpenApi_1.z.string() }),
1012
- },
1013
- actionName: "search",
1014
- model: tests_1.FoodModel,
1015
- options: { openApi: mock.middleware },
1016
- scope: "collection",
1017
- });
1018
- var params = (_a = mock.captured) === null || _a === void 0 ? void 0 : _a.parameters;
1019
- (0, bun_test_1.expect)(params.length).toBe(2);
1020
- (0, bun_test_1.expect)(params.some(function (p) { return p.name === "search"; })).toBe(true);
1021
- (0, bun_test_1.expect)(params.some(function (p) { return p.name === "page"; })).toBe(true);
1022
- });
1023
- (0, bun_test_1.it)("includes id path parameter for instance-scoped actions", function () {
1024
- var _a;
1025
- var mock = createMockOpenApi();
1026
- (0, actions_1.createActionOpenApiMiddleware)({
1027
- action: { method: "GET", permissions: [] },
1028
- actionName: "peek",
1029
- model: tests_1.FoodModel,
1030
- options: { openApi: mock.middleware },
1031
- scope: "instance",
1032
- });
1033
- var params = (_a = mock.captured) === null || _a === void 0 ? void 0 : _a.parameters;
1034
- (0, bun_test_1.expect)(params.some(function (p) { return p.name === "id" && p.in === "path"; })).toBe(true);
1035
- });
1036
- (0, bun_test_1.it)("uses fallback data schema when response is not provided", function () {
1037
- var _a;
1038
- var mock = createMockOpenApi();
1039
- (0, actions_1.createActionOpenApiMiddleware)({
1040
- action: { method: "POST", permissions: [] },
1041
- actionName: "noop",
1042
- model: tests_1.FoodModel,
1043
- options: { openApi: mock.middleware },
1044
- scope: "collection",
1045
- });
1046
- var responses = (_a = mock.captured) === null || _a === void 0 ? void 0 : _a.responses;
1047
- var ok = responses["200"];
1048
- var schema = ok.content["application/json"].schema;
1049
- (0, bun_test_1.expect)(schema.properties.data).toEqual({ type: "object" });
1050
- });
1051
- (0, bun_test_1.it)("uses zod response schema when provided", function () {
1052
- var _a;
1053
- var mock = createMockOpenApi();
1054
- (0, actions_1.createActionOpenApiMiddleware)({
1055
- action: {
1056
- method: "POST",
1057
- permissions: [],
1058
- response: zodOpenApi_1.z.object({ count: zodOpenApi_1.z.number() }),
1059
- },
1060
- actionName: "tally",
1061
- model: tests_1.FoodModel,
1062
- options: { openApi: mock.middleware },
1063
- scope: "collection",
1064
- });
1065
- var responses = (_a = mock.captured) === null || _a === void 0 ? void 0 : _a.responses;
1066
- var ok = responses["200"];
1067
- var schema = ok.content["application/json"].schema;
1068
- (0, bun_test_1.expect)(schema.properties.data.properties).toBeDefined();
1069
- });
1070
- (0, bun_test_1.it)("includes requestBody when action.body is defined", function () {
1071
- var _a;
1072
- var mock = createMockOpenApi();
1073
- (0, actions_1.createActionOpenApiMiddleware)({
1074
- action: {
1075
- body: zodOpenApi_1.z.object({ name: zodOpenApi_1.z.string() }),
1076
- method: "POST",
1077
- permissions: [],
1078
- },
1079
- actionName: "create",
1080
- model: tests_1.FoodModel,
1081
- options: { openApi: mock.middleware },
1082
- scope: "collection",
1083
- });
1084
- (0, bun_test_1.expect)((_a = mock.captured) === null || _a === void 0 ? void 0 : _a.requestBody).toBeDefined();
1085
- });
1086
- });
1087
- });