@terreno/api 57.4.0 → 57.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -5
- package/bunfig.toml +1 -1
- package/dist/actions.js +197 -368
- package/dist/adminModelRouter.js +88 -173
- package/dist/api.js +617 -852
- package/dist/audit/auditApp.js +48 -89
- package/dist/audit/auditEventModel.js +15 -16
- package/dist/audit/cloudTasksEnqueue.js +56 -149
- package/dist/audit/diff.js +70 -160
- package/dist/audit/processQueue.js +25 -70
- package/dist/audit/rbacSink.js +25 -66
- package/dist/audit/record.js +129 -251
- package/dist/auth.js +432 -681
- package/dist/authRecovery.js +155 -269
- package/dist/authTokens.js +43 -118
- package/dist/betterAuthApp.js +25 -26
- package/dist/betterAuthSetup.js +258 -413
- package/dist/browserLogsRoute.d.ts +6 -0
- package/dist/browserLogsRoute.js +97 -0
- package/dist/collectionRegistry.js +32 -47
- package/dist/config.js +53 -122
- package/dist/configurationApp.js +180 -361
- package/dist/configurationPlugin.js +130 -302
- package/dist/consentApp.js +276 -421
- package/dist/docLoader.js +78 -126
- package/dist/envConfigurationPlugin.js +25 -147
- package/dist/errors.js +151 -247
- package/dist/example.js +28 -34
- package/dist/expressServer.d.ts +10 -0
- package/dist/expressServer.js +109 -255
- package/dist/frameworkModelNames.d.ts +24 -0
- package/dist/frameworkModelNames.js +31 -0
- package/dist/githubAuth.js +152 -216
- package/dist/httpClient.js +176 -264
- package/dist/index.d.ts +2 -0
- package/dist/index.js +7 -2
- package/dist/logger.js +145 -278
- package/dist/mcp/auth.js +76 -128
- package/dist/mcp/createMCPRequest.js +4 -5
- package/dist/mcp/handlers.js +268 -480
- package/dist/mcp/query.js +56 -150
- package/dist/mcp/registry.js +17 -30
- package/dist/mcp/schemaGenerator.js +95 -181
- package/dist/mcp/server.js +28 -100
- package/dist/mcp/serviceTokens.js +102 -191
- package/dist/mcp/toolGenerator.js +78 -191
- package/dist/middleware.js +15 -27
- package/dist/migrations/fixtures/all-down/20260910120000-one.js +2 -42
- package/dist/migrations/fixtures/all-down/20260910120001-two.js +2 -42
- package/dist/migrations/fixtures/bad-id/20260910120000-mismatch.js +1 -39
- package/dist/migrations/fixtures/bad-name/not-a-migration.js +1 -39
- package/dist/migrations/fixtures/valid/20260910120000-alpha.js +1 -39
- package/dist/migrations/fixtures/valid/20260910120001-beta.js +2 -42
- package/dist/migrations/generate.js +107 -229
- package/dist/migrations/load.js +75 -201
- package/dist/migrations/lock.js +54 -126
- package/dist/migrations/runner.js +192 -446
- package/dist/migrations/schemaCatalog.js +122 -263
- package/dist/models/consentForm.js +6 -6
- package/dist/models/consentResponse.js +5 -5
- package/dist/models/mcpServiceToken.js +66 -156
- package/dist/models/notification.js +6 -6
- package/dist/models/notificationPreference.js +6 -6
- package/dist/models/versionConfig.js +5 -5
- package/dist/notifications/notificationService.js +133 -273
- package/dist/notifications/notificationsApp.js +85 -179
- package/dist/notifications/notificationsBeforeSend.js +19 -64
- package/dist/notifiers/googleChatNotifier.js +36 -117
- package/dist/notifiers/slackNotifier.d.ts +31 -2
- package/dist/notifiers/slackNotifier.js +170 -119
- package/dist/notifiers/zoomNotifier.js +60 -111
- package/dist/openApi.js +107 -122
- package/dist/openApiBuilder.js +117 -159
- package/dist/openApiCompat.js +81 -148
- package/dist/openApiEtag.js +7 -7
- package/dist/openApiValidator.js +177 -286
- package/dist/orgs/orgContext.js +87 -157
- package/dist/orgs/orgPermissions.js +29 -68
- package/dist/orgs/orgPlugin.js +9 -27
- package/dist/orgs/organizationModel.js +65 -166
- package/dist/orgs/organizationSettings.js +29 -36
- package/dist/orgs/orgsApp.js +346 -608
- package/dist/permissions.js +88 -181
- package/dist/plugins.js +125 -218
- package/dist/populate.js +130 -240
- package/dist/rateLimit/applyTrustProxy.js +1 -1
- package/dist/rateLimit/createStore.js +7 -8
- package/dist/rateLimit/memoryStore.js +22 -100
- package/dist/rateLimit/middleware.js +48 -99
- package/dist/rateLimit/mongoStore.js +54 -112
- package/dist/rateLimit/policies.js +41 -75
- package/dist/rateLimit/redisStore.js +39 -133
- package/dist/rbac/access.js +119 -267
- package/dist/rbac/assertAllowed.js +95 -171
- package/dist/rbac/auditModel.js +14 -58
- package/dist/rbac/backfillAdmins.js +24 -130
- package/dist/rbac/fieldViews.js +46 -141
- package/dist/rbac/membershipOrgAdminSource.js +28 -99
- package/dist/rbac/middleware.js +30 -96
- package/dist/rbac/modelRouterAccess.js +130 -307
- package/dist/rbac/permissionUtils.js +40 -179
- package/dist/rbac/realtimeAccess.js +81 -147
- package/dist/rbac/resolve.d.ts +1 -1
- package/dist/rbac/resolve.js +148 -342
- package/dist/rbac/roleManager.js +318 -700
- package/dist/rbac/roleModel.js +44 -171
- package/dist/rbac/routes.js +93 -196
- package/dist/rbac/scopes.js +39 -133
- package/dist/rbac/statements.js +23 -91
- package/dist/rbac/userPlugin.js +3 -29
- package/dist/realtime/changeStreamWatcher.js +489 -800
- package/dist/realtime/deprecation.js +8 -10
- package/dist/realtime/queryMatcher.js +110 -191
- package/dist/realtime/queryStore.js +35 -118
- package/dist/realtime/realtimeApp.js +247 -401
- package/dist/realtime/registry.js +19 -30
- package/dist/realtime/sessionRevalidation.js +132 -358
- package/dist/realtime/socketAuth.js +89 -208
- package/dist/realtime/socketUser.js +10 -56
- package/dist/requestContext.js +93 -139
- package/dist/schemaMetadata.js +179 -274
- package/dist/scriptRunner.js +96 -266
- package/dist/scrubAdminFields.js +36 -108
- package/dist/secretProviders.js +130 -299
- package/dist/seedRunner.js +221 -514
- package/dist/sync/adminBroadcastScope.js +58 -170
- package/dist/sync/adminWindowAccess.js +15 -86
- package/dist/sync/adminWindowMutation.js +110 -181
- package/dist/sync/executors.js +425 -629
- package/dist/sync/loadHarness.js +392 -689
- package/dist/sync/models.js +181 -341
- package/dist/sync/mutationHandler.js +533 -801
- package/dist/sync/registrationSideEffects.js +41 -95
- package/dist/sync/registry.js +29 -49
- package/dist/sync/routes.js +484 -817
- package/dist/sync/scripts/compactTombstones.js +83 -228
- package/dist/sync/serialize.js +18 -58
- package/dist/sync/socketHandlers.js +253 -478
- package/dist/sync/streams.js +49 -117
- package/dist/sync/syncApp.js +12 -15
- package/dist/sync/syncBatch.js +63 -168
- package/dist/sync/syncSeqPlugin.js +430 -702
- package/dist/syncConsents.js +113 -253
- package/dist/terrenoApp.js +166 -276
- package/dist/tests/createTestData.js +146 -256
- package/dist/tests/models.js +14 -54
- package/dist/tests/mongoTestSetup.js +42 -103
- package/dist/tests/testHelper.js +31 -93
- package/dist/tests.js +12 -51
- package/dist/transformers.js +61 -164
- package/dist/utils.js +31 -93
- package/dist/vendor/wesleytodd-openapi/index.js +37 -51
- package/dist/vendor/wesleytodd-openapi/lib/generate-doc.js +32 -40
- package/dist/vendor/wesleytodd-openapi/lib/layer-schema.js +3 -3
- package/dist/vendor/wesleytodd-openapi/lib/ui.js +50 -19
- package/dist/vendor/wesleytodd-openapi/lib/validate.js +19 -47
- package/dist/versionCheckPlugin.js +52 -100
- package/dist/webhooks/headerValue.js +2 -2
- package/dist/webhooks/idempotency/memoryStore.js +16 -59
- package/dist/webhooks/idempotency/mongoStore.js +55 -130
- package/dist/webhooks/idempotency/webhookReceipt.js +9 -9
- package/dist/webhooks/rawBody.js +1 -1
- package/dist/webhooks/timingSafeEqual.js +4 -5
- package/dist/webhooks/verifiers/hmac.js +17 -18
- package/dist/webhooks/verifiers/sendgrid.js +20 -22
- package/dist/webhooks/verifiers/stripe.js +31 -65
- package/dist/webhooks/verifiers/twilio.js +23 -73
- package/dist/webhooks/webhooksApp.js +78 -177
- package/dist/zodOpenApi.js +2 -2
- package/package.json +6 -6
- package/src/__tests__/browserLogsRoute.test.ts +165 -0
- package/src/api.arrayOperations.test.ts +44 -0
- package/src/api.ts +64 -39
- package/src/audit/auditApp.test.ts +5 -5
- package/src/audit/auditEventModel.ts +7 -3
- package/src/audit/auditRouter.test.ts +5 -5
- package/src/audit/rbacSink.test.ts +2 -2
- package/src/audit/record.test.ts +2 -2
- package/src/audit/record.ts +3 -2
- package/src/auth.ts +1 -1
- package/src/authRecovery.test.ts +43 -0
- package/src/authRecovery.ts +16 -10
- package/src/authTokens.ts +6 -2
- package/src/authVerification.test.ts +22 -0
- package/src/browserLogsRoute.ts +110 -0
- package/src/configurationPlugin.test.ts +1 -11
- package/src/expressServer.ts +10 -0
- package/src/frameworkModelNames.test.ts +62 -0
- package/src/frameworkModelNames.ts +33 -0
- package/src/index.ts +7 -0
- package/src/isolated/migrateCliConnect.isolated.ts +51 -0
- package/src/mcp/auth.test.ts +34 -1
- package/src/migrations/cli.test.ts +0 -23
- package/src/models/mcpServiceToken.ts +4 -3
- package/src/models/notification.ts +3 -2
- package/src/models/notificationPreference.ts +1 -1
- package/src/notifications/notificationsApp.test.ts +24 -1
- package/src/notifiers/slackNotifier.test.ts +186 -1
- package/src/notifiers/slackNotifier.ts +167 -8
- package/src/orgs/orgPlugin.ts +1 -1
- package/src/orgs/organizationModel.ts +11 -6
- package/src/plugins.test.ts +5 -0
- package/src/plugins.ts +3 -0
- package/src/realtime/realtimeApp.ts +1 -1
- package/src/sync/integration.test.ts +2 -2
- package/src/terrenoApp.ts +3 -0
- package/src/types.d.ts +1 -0
- package/src/webhooks/idempotency/webhookReceipt.ts +4 -3
- package/tsconfig.json +13 -54
- package/tsconfig.typedoc.json +2 -0
package/dist/actions.js
CHANGED
|
@@ -1,204 +1,98 @@
|
|
|
1
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 __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
66
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
67
|
-
if (ar || !(i in from)) {
|
|
68
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
69
|
-
ar[i] = from[i];
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
73
|
-
};
|
|
74
|
-
var __values = (this && this.__values) || function(o) {
|
|
75
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
76
|
-
if (m) return m.call(o);
|
|
77
|
-
if (o && typeof o.length === "number") return {
|
|
78
|
-
next: function () {
|
|
79
|
-
if (o && i >= o.length) o = void 0;
|
|
80
|
-
return { value: o && o[i++], done: !o };
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
84
|
-
};
|
|
85
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
86
3
|
exports.registerActionRoutes = exports.assertNoActionCollisions = exports.createActionOpenApiMiddleware = exports.wrapActionResponse = exports.validateActionRequest = exports.runActionPermissions = exports.defineCollectionAction = exports.defineInstanceAction = exports.ACTION_NAME_PATTERN = void 0;
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
4
|
+
const zod_to_openapi_1 = require("@asteasolutions/zod-to-openapi");
|
|
5
|
+
const api_1 = require("./api");
|
|
6
|
+
const auth_1 = require("./auth");
|
|
7
|
+
const docLoader_1 = require("./docLoader");
|
|
8
|
+
const errors_1 = require("./errors");
|
|
9
|
+
const openApi_1 = require("./openApi");
|
|
10
|
+
const permissions_1 = require("./permissions");
|
|
11
|
+
const middleware_1 = require("./rbac/middleware");
|
|
95
12
|
// At least two characters: leading letter plus one or more alphanumeric/_/- chars.
|
|
96
13
|
exports.ACTION_NAME_PATTERN = /^[A-Za-z][A-Za-z0-9_-]+$/;
|
|
97
|
-
|
|
14
|
+
const defineInstanceAction = (config) => {
|
|
98
15
|
return config;
|
|
99
16
|
};
|
|
100
17
|
exports.defineInstanceAction = defineInstanceAction;
|
|
101
|
-
|
|
18
|
+
const defineCollectionAction = (config) => {
|
|
102
19
|
return config;
|
|
103
20
|
};
|
|
104
21
|
exports.defineCollectionAction = defineCollectionAction;
|
|
105
|
-
|
|
22
|
+
const mapActionToCrudMethod = (scope, httpMethod) => {
|
|
106
23
|
if (scope === "instance") {
|
|
107
24
|
return httpMethod === "GET" ? "read" : "update";
|
|
108
25
|
}
|
|
109
26
|
return httpMethod === "GET" ? "list" : "create";
|
|
110
27
|
};
|
|
111
|
-
|
|
112
|
-
var _a;
|
|
28
|
+
const resolveActionAccess = (action, scope, routerAccess) => {
|
|
113
29
|
if (action.access) {
|
|
114
30
|
return action.access;
|
|
115
31
|
}
|
|
116
|
-
if (!
|
|
32
|
+
if (!routerAccess?.resource) {
|
|
117
33
|
return undefined;
|
|
118
34
|
}
|
|
119
|
-
|
|
120
|
-
|
|
35
|
+
const crud = mapActionToCrudMethod(scope, action.method);
|
|
36
|
+
const mapped = routerAccess.actions?.[crud];
|
|
121
37
|
if (mapped === null) {
|
|
122
38
|
return null;
|
|
123
39
|
}
|
|
124
|
-
return { action: mapped
|
|
40
|
+
return { action: mapped ?? crud, resource: routerAccess.resource };
|
|
125
41
|
};
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
});
|
|
165
|
-
}
|
|
166
|
-
throw new errors_1.ForbiddenError({
|
|
167
|
-
code: "action-access-denied",
|
|
168
|
-
detail: "Access to ".concat(method.toUpperCase(), " on ").concat(model.modelName, ":").concat(req.params.id, " ") +
|
|
169
|
-
"denied for ".concat((_d = req.user) === null || _d === void 0 ? void 0 : _d.id),
|
|
170
|
-
meta: { action: method.toUpperCase(), model: model.modelName },
|
|
171
|
-
title: "Access denied",
|
|
172
|
-
});
|
|
173
|
-
}
|
|
42
|
+
const runActionPermissions = async (action, scope, model, req, doc, accessControl, routerAccess) => {
|
|
43
|
+
const method = mapActionToCrudMethod(scope, action.method);
|
|
44
|
+
let permissions = action.permissions;
|
|
45
|
+
const rbacAccess = resolveActionAccess(action, scope, routerAccess);
|
|
46
|
+
if (rbacAccess === null || permissions?.length === 0) {
|
|
47
|
+
permissions = [];
|
|
48
|
+
}
|
|
49
|
+
else if (rbacAccess && accessControl) {
|
|
50
|
+
const isPermitted = (0, middleware_1.createIsPermitted)({ can: accessControl.can });
|
|
51
|
+
const rbacCheck = isPermitted({
|
|
52
|
+
[rbacAccess.resource]: [rbacAccess.action],
|
|
53
|
+
});
|
|
54
|
+
permissions = [...(action.permissions ?? []), rbacCheck];
|
|
55
|
+
}
|
|
56
|
+
if (!permissions) {
|
|
57
|
+
throw new errors_1.APIError({ status: 500, title: "Action missing permissions configuration" });
|
|
58
|
+
}
|
|
59
|
+
const allowed = await (0, permissions_1.checkPermissions)(method, permissions, req.user, doc);
|
|
60
|
+
if (allowed) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (!doc) {
|
|
64
|
+
// Status kept at 405 for backwards compatibility with the same pre-document permission denial
|
|
65
|
+
// in modelRouter's array operations; changing it would break consumers matching on it.
|
|
66
|
+
throw new errors_1.APIError({
|
|
67
|
+
code: "action-access-denied",
|
|
68
|
+
detail: `Access to ${method.toUpperCase()} on ${model.modelName} ` + `denied for ${req.user?.id}`,
|
|
69
|
+
meta: { action: method.toUpperCase(), model: model.modelName },
|
|
70
|
+
status: 405,
|
|
71
|
+
title: "Access denied",
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
throw new errors_1.ForbiddenError({
|
|
75
|
+
code: "action-access-denied",
|
|
76
|
+
detail: `Access to ${method.toUpperCase()} on ${model.modelName}:${req.params.id} ` +
|
|
77
|
+
`denied for ${req.user?.id}`,
|
|
78
|
+
meta: { action: method.toUpperCase(), model: model.modelName },
|
|
79
|
+
title: "Access denied",
|
|
174
80
|
});
|
|
175
|
-
}
|
|
81
|
+
};
|
|
176
82
|
exports.runActionPermissions = runActionPermissions;
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
var _d = __read(_c.value, 2), key = _d[0], msgs = _d[1];
|
|
183
|
-
if (msgs && msgs.length > 0) {
|
|
184
|
-
fields[key] = msgs[0];
|
|
185
|
-
}
|
|
83
|
+
const flattenZodFieldErrors = (fieldErrors) => {
|
|
84
|
+
const fields = {};
|
|
85
|
+
for (const [key, msgs] of Object.entries(fieldErrors)) {
|
|
86
|
+
if (msgs && msgs.length > 0) {
|
|
87
|
+
fields[key] = msgs[0];
|
|
186
88
|
}
|
|
187
89
|
}
|
|
188
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
189
|
-
finally {
|
|
190
|
-
try {
|
|
191
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
192
|
-
}
|
|
193
|
-
finally { if (e_1) throw e_1.error; }
|
|
194
|
-
}
|
|
195
90
|
return fields;
|
|
196
91
|
};
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
var body;
|
|
92
|
+
const validateActionRequest = ({ action, req, }) => {
|
|
93
|
+
let body;
|
|
200
94
|
if (action.body) {
|
|
201
|
-
|
|
95
|
+
const parsedBody = action.body.safeParse(req.body);
|
|
202
96
|
if (!parsedBody.success) {
|
|
203
97
|
throw new errors_1.ValidationError({
|
|
204
98
|
code: "action-body-validation-failed",
|
|
@@ -212,9 +106,9 @@ var validateActionRequest = function (_a) {
|
|
|
212
106
|
else {
|
|
213
107
|
body = req.body;
|
|
214
108
|
}
|
|
215
|
-
|
|
109
|
+
let query;
|
|
216
110
|
if (action.query) {
|
|
217
|
-
|
|
111
|
+
const parsedQuery = action.query.safeParse(req.query);
|
|
218
112
|
if (!parsedQuery.success) {
|
|
219
113
|
throw new errors_1.ValidationError({
|
|
220
114
|
code: "action-query-validation-failed",
|
|
@@ -228,59 +122,50 @@ var validateActionRequest = function (_a) {
|
|
|
228
122
|
else {
|
|
229
123
|
query = req.query;
|
|
230
124
|
}
|
|
231
|
-
return { body
|
|
125
|
+
return { body, query };
|
|
232
126
|
};
|
|
233
127
|
exports.validateActionRequest = validateActionRequest;
|
|
234
|
-
|
|
235
|
-
var _a;
|
|
128
|
+
const wrapActionResponse = (handlerResult, action, res) => {
|
|
236
129
|
if (res.headersSent) {
|
|
237
130
|
return;
|
|
238
131
|
}
|
|
239
|
-
res.status(
|
|
132
|
+
res.status(action.status ?? 200).json({ data: handlerResult ?? null });
|
|
240
133
|
};
|
|
241
134
|
exports.wrapActionResponse = wrapActionResponse;
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
var refId = "ActionInlineSchema".concat(inlineOpenApiSchemaCounter++);
|
|
135
|
+
let inlineOpenApiSchemaCounter = 0;
|
|
136
|
+
const zodToJsonSchema = (zodSchema) => {
|
|
137
|
+
const registry = new zod_to_openapi_1.OpenAPIRegistry();
|
|
138
|
+
const refId = `ActionInlineSchema${inlineOpenApiSchemaCounter++}`;
|
|
247
139
|
registry.register(refId, zodSchema);
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
140
|
+
const generator = new zod_to_openapi_1.OpenApiGeneratorV3(registry.definitions);
|
|
141
|
+
const { components } = generator.generateComponents();
|
|
142
|
+
const schema = components?.schemas?.[refId];
|
|
251
143
|
if (schema && typeof schema === "object") {
|
|
252
144
|
return schema;
|
|
253
145
|
}
|
|
254
146
|
return { type: "object" };
|
|
255
147
|
};
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
var properties = jsonSchema.properties;
|
|
148
|
+
const queryParametersFromSchema = (querySchema) => {
|
|
149
|
+
const jsonSchema = zodToJsonSchema(querySchema);
|
|
150
|
+
const properties = jsonSchema.properties;
|
|
260
151
|
if (!properties) {
|
|
261
152
|
return [];
|
|
262
153
|
}
|
|
263
|
-
|
|
264
|
-
return Object.entries(properties).map(
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
schema: schema,
|
|
271
|
-
});
|
|
272
|
-
});
|
|
154
|
+
const requiredFields = jsonSchema.required ?? [];
|
|
155
|
+
return Object.entries(properties).map(([name, schema]) => ({
|
|
156
|
+
in: "query",
|
|
157
|
+
name,
|
|
158
|
+
required: requiredFields.includes(name),
|
|
159
|
+
schema,
|
|
160
|
+
}));
|
|
273
161
|
};
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
var action = _a.action, scope = _a.scope, actionName = _a.actionName, model = _a.model, options = _a.options;
|
|
278
|
-
if (!((_c = options.openApi) === null || _c === void 0 ? void 0 : _c.path)) {
|
|
279
|
-
return function (_req, _res, next) { return next(); };
|
|
162
|
+
const createActionOpenApiMiddleware = ({ action, scope, actionName, model, options, }) => {
|
|
163
|
+
if (!options.openApi?.path) {
|
|
164
|
+
return (_req, _res, next) => next();
|
|
280
165
|
}
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
166
|
+
const tag = action.tag ?? model.collection.collectionName;
|
|
167
|
+
const statusCode = String(action.status ?? 200);
|
|
168
|
+
const parameters = [];
|
|
284
169
|
if (scope === "instance") {
|
|
285
170
|
parameters.push({
|
|
286
171
|
in: "path",
|
|
@@ -290,34 +175,37 @@ var createActionOpenApiMiddleware = function (_a) {
|
|
|
290
175
|
});
|
|
291
176
|
}
|
|
292
177
|
if (action.query) {
|
|
293
|
-
parameters.push
|
|
178
|
+
parameters.push(...queryParametersFromSchema(action.query));
|
|
294
179
|
}
|
|
295
|
-
|
|
180
|
+
const operation = {
|
|
296
181
|
description: action.description,
|
|
297
|
-
operationId:
|
|
298
|
-
parameters
|
|
299
|
-
responses:
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
182
|
+
operationId: `${tag}_${actionName}`,
|
|
183
|
+
parameters,
|
|
184
|
+
responses: {
|
|
185
|
+
[statusCode]: {
|
|
186
|
+
content: {
|
|
187
|
+
"application/json": {
|
|
188
|
+
schema: action.response
|
|
189
|
+
? {
|
|
190
|
+
properties: {
|
|
191
|
+
data: zodToJsonSchema(action.response),
|
|
192
|
+
},
|
|
193
|
+
required: ["data"],
|
|
194
|
+
type: "object",
|
|
195
|
+
}
|
|
196
|
+
: {
|
|
197
|
+
properties: {
|
|
198
|
+
data: { type: "object" },
|
|
199
|
+
},
|
|
200
|
+
type: "object",
|
|
306
201
|
},
|
|
307
|
-
|
|
308
|
-
type: "object",
|
|
309
|
-
}
|
|
310
|
-
: {
|
|
311
|
-
properties: {
|
|
312
|
-
data: { type: "object" },
|
|
313
|
-
},
|
|
314
|
-
type: "object",
|
|
315
|
-
},
|
|
202
|
+
},
|
|
316
203
|
},
|
|
204
|
+
description: "Successful response",
|
|
317
205
|
},
|
|
318
|
-
|
|
319
|
-
},
|
|
320
|
-
summary:
|
|
206
|
+
...openApi_1.defaultOpenApiErrorResponses,
|
|
207
|
+
},
|
|
208
|
+
summary: action.summary ?? `${actionName} ${scope} action`,
|
|
321
209
|
tags: [tag],
|
|
322
210
|
};
|
|
323
211
|
if (action.body) {
|
|
@@ -333,26 +221,26 @@ var createActionOpenApiMiddleware = function (_a) {
|
|
|
333
221
|
return options.openApi.path(operation);
|
|
334
222
|
};
|
|
335
223
|
exports.createActionOpenApiMiddleware = createActionOpenApiMiddleware;
|
|
336
|
-
|
|
224
|
+
const getArrayFieldNames = (model) => {
|
|
337
225
|
return Object.values(model.schema.paths)
|
|
338
|
-
.filter(
|
|
339
|
-
.map(
|
|
226
|
+
.filter((config) => config.instance === "Array")
|
|
227
|
+
.map((config) => config.path);
|
|
340
228
|
};
|
|
341
229
|
// Registration-time validation throws APIError so misconfiguration fails at app boot.
|
|
342
|
-
|
|
230
|
+
const validateActionConfig = (scope, name, config) => {
|
|
343
231
|
if (!name) {
|
|
344
232
|
throw new errors_1.APIError({ status: 500, title: "Action name cannot be empty" });
|
|
345
233
|
}
|
|
346
234
|
if (!exports.ACTION_NAME_PATTERN.test(name)) {
|
|
347
235
|
throw new errors_1.APIError({
|
|
348
236
|
status: 500,
|
|
349
|
-
title:
|
|
237
|
+
title: `Invalid action name "${name}". Action names must match ${exports.ACTION_NAME_PATTERN.toString()}`,
|
|
350
238
|
});
|
|
351
239
|
}
|
|
352
240
|
if (config.permissions === undefined && !config.access) {
|
|
353
241
|
throw new errors_1.APIError({
|
|
354
242
|
status: 500,
|
|
355
|
-
title:
|
|
243
|
+
title: `Action "${name}" (${scope}) is missing required "permissions" or "access". ` +
|
|
356
244
|
"Provide at least one permission function, an access descriptor, or [] to disable the action.",
|
|
357
245
|
});
|
|
358
246
|
}
|
|
@@ -362,119 +250,75 @@ var validateActionConfig = function (scope, name, config) {
|
|
|
362
250
|
if (config.method !== "GET" && config.method !== "POST") {
|
|
363
251
|
throw new errors_1.APIError({
|
|
364
252
|
status: 500,
|
|
365
|
-
title:
|
|
253
|
+
title: `Action "${name}" (${scope}) only supports GET and POST methods`,
|
|
366
254
|
});
|
|
367
255
|
}
|
|
368
256
|
};
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
var e_2, _a;
|
|
257
|
+
const assertNoActionCollisions = (model, options) => {
|
|
258
|
+
const arrayFields = new Set(getArrayFieldNames(model));
|
|
259
|
+
const validateMap = (actions, scope) => {
|
|
373
260
|
if (!actions) {
|
|
374
261
|
return;
|
|
375
262
|
}
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
title: "instanceAction '".concat(name_1, "' collides with array field operations on /:id/").concat(name_1),
|
|
384
|
-
});
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
389
|
-
finally {
|
|
390
|
-
try {
|
|
391
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
263
|
+
for (const [name, config] of Object.entries(actions)) {
|
|
264
|
+
validateActionConfig(scope, name, config);
|
|
265
|
+
if (scope === "instance" && arrayFields.has(name)) {
|
|
266
|
+
throw new errors_1.APIError({
|
|
267
|
+
status: 500,
|
|
268
|
+
title: `instanceAction '${name}' collides with array field operations on /:id/${name}`,
|
|
269
|
+
});
|
|
392
270
|
}
|
|
393
|
-
finally { if (e_2) throw e_2.error; }
|
|
394
271
|
}
|
|
395
272
|
};
|
|
396
273
|
validateMap(options.instanceActions, "instance");
|
|
397
274
|
validateMap(options.collectionActions, "collection");
|
|
398
275
|
};
|
|
399
276
|
exports.assertNoActionCollisions = assertNoActionCollisions;
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
277
|
+
const buildActionMiddleware = (model, options, registered) => {
|
|
278
|
+
const action = registered.config;
|
|
279
|
+
const scope = registered.scope;
|
|
280
|
+
const actionName = registered.name;
|
|
281
|
+
const preDocPermissions = async (req, _res, next) => {
|
|
282
|
+
try {
|
|
283
|
+
await (0, exports.runActionPermissions)(action, scope, model, req, undefined, options.accessControl, options.access);
|
|
284
|
+
return next();
|
|
285
|
+
}
|
|
286
|
+
catch (error) {
|
|
287
|
+
return next(error);
|
|
288
|
+
}
|
|
289
|
+
};
|
|
290
|
+
const loadDocAndPostPermissions = scope === "instance"
|
|
291
|
+
? async (req, _res, next) => {
|
|
292
|
+
try {
|
|
293
|
+
const doc = await (0, docLoader_1.loadDocOr404)(model, req.params.id, options.populatePaths);
|
|
294
|
+
req.obj = doc;
|
|
295
|
+
await (0, exports.runActionPermissions)(action, scope, model, req, doc, options.accessControl, options.access);
|
|
296
|
+
return next();
|
|
418
297
|
}
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
var loadDocAndPostPermissions = scope === "instance"
|
|
422
|
-
? function (req, _res, next) { return __awaiter(void 0, void 0, void 0, function () {
|
|
423
|
-
var doc, error_2;
|
|
424
|
-
return __generator(this, function (_a) {
|
|
425
|
-
switch (_a.label) {
|
|
426
|
-
case 0:
|
|
427
|
-
_a.trys.push([0, 3, , 4]);
|
|
428
|
-
return [4 /*yield*/, (0, docLoader_1.loadDocOr404)(model, req.params.id, options.populatePaths)];
|
|
429
|
-
case 1:
|
|
430
|
-
doc = _a.sent();
|
|
431
|
-
req.obj = doc;
|
|
432
|
-
return [4 /*yield*/, (0, exports.runActionPermissions)(action, scope, model, req, doc, options.accessControl, options.access)];
|
|
433
|
-
case 2:
|
|
434
|
-
_a.sent();
|
|
435
|
-
return [2 /*return*/, next()];
|
|
436
|
-
case 3:
|
|
437
|
-
error_2 = _a.sent();
|
|
438
|
-
return [2 /*return*/, next(error_2)];
|
|
439
|
-
case 4: return [2 /*return*/];
|
|
440
|
-
}
|
|
441
|
-
});
|
|
442
|
-
}); }
|
|
443
|
-
: null;
|
|
444
|
-
var validateAndRun = (0, api_1.asyncHandler)(function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
|
|
445
|
-
var _a, body, query, doc, ctx, result, _b;
|
|
446
|
-
return __generator(this, function (_c) {
|
|
447
|
-
switch (_c.label) {
|
|
448
|
-
case 0:
|
|
449
|
-
_a = (0, exports.validateActionRequest)({ action: action, req: req }), body = _a.body, query = _a.query;
|
|
450
|
-
doc = scope === "instance" ? req.obj : undefined;
|
|
451
|
-
ctx = {
|
|
452
|
-
body: body,
|
|
453
|
-
doc: doc,
|
|
454
|
-
query: query,
|
|
455
|
-
req: req,
|
|
456
|
-
res: res,
|
|
457
|
-
user: req.user,
|
|
458
|
-
};
|
|
459
|
-
if (!(scope === "instance")) return [3 /*break*/, 2];
|
|
460
|
-
return [4 /*yield*/, action.handler(ctx)];
|
|
461
|
-
case 1:
|
|
462
|
-
_b = _c.sent();
|
|
463
|
-
return [3 /*break*/, 4];
|
|
464
|
-
case 2: return [4 /*yield*/, action.handler(ctx)];
|
|
465
|
-
case 3:
|
|
466
|
-
_b = _c.sent();
|
|
467
|
-
_c.label = 4;
|
|
468
|
-
case 4:
|
|
469
|
-
result = _b;
|
|
470
|
-
(0, exports.wrapActionResponse)(result, action, res);
|
|
471
|
-
return [2 /*return*/];
|
|
298
|
+
catch (error) {
|
|
299
|
+
return next(error);
|
|
472
300
|
}
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
|
|
301
|
+
}
|
|
302
|
+
: null;
|
|
303
|
+
const validateAndRun = (0, api_1.asyncHandler)(async (req, res) => {
|
|
304
|
+
const { body, query } = (0, exports.validateActionRequest)({ action, req });
|
|
305
|
+
const doc = scope === "instance" ? req.obj : undefined;
|
|
306
|
+
const ctx = {
|
|
307
|
+
body,
|
|
308
|
+
doc,
|
|
309
|
+
query,
|
|
310
|
+
req,
|
|
311
|
+
res,
|
|
312
|
+
user: req.user,
|
|
313
|
+
};
|
|
314
|
+
const result = scope === "instance"
|
|
315
|
+
? await action.handler(ctx)
|
|
316
|
+
: await action.handler(ctx);
|
|
317
|
+
(0, exports.wrapActionResponse)(result, action, res);
|
|
318
|
+
});
|
|
319
|
+
const chain = [
|
|
476
320
|
(0, auth_1.authenticateMiddleware)(options.allowAnonymous),
|
|
477
|
-
(0, exports.createActionOpenApiMiddleware)({ action
|
|
321
|
+
(0, exports.createActionOpenApiMiddleware)({ action, actionName, model, options, scope }),
|
|
478
322
|
preDocPermissions,
|
|
479
323
|
];
|
|
480
324
|
if (loadDocAndPostPermissions) {
|
|
@@ -483,46 +327,31 @@ var buildActionMiddleware = function (model, options, registered) {
|
|
|
483
327
|
chain.push(validateAndRun);
|
|
484
328
|
return chain;
|
|
485
329
|
};
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
return ({
|
|
494
|
-
config: config,
|
|
495
|
-
name: name,
|
|
330
|
+
const registerActionRoutes = (router, model, options) => {
|
|
331
|
+
const instanceActions = options.instanceActions ?? {};
|
|
332
|
+
const collectionActions = options.collectionActions ?? {};
|
|
333
|
+
const registeredActions = [
|
|
334
|
+
...Object.entries(instanceActions).map(([name, config]) => ({
|
|
335
|
+
config,
|
|
336
|
+
name,
|
|
496
337
|
scope: "instance",
|
|
497
|
-
})
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
config: config,
|
|
502
|
-
name: name,
|
|
338
|
+
})),
|
|
339
|
+
...Object.entries(collectionActions).map(([name, config]) => ({
|
|
340
|
+
config,
|
|
341
|
+
name,
|
|
503
342
|
scope: "collection",
|
|
504
|
-
})
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
if (config.method === "GET") {
|
|
513
|
-
router.get(routePath, middleware);
|
|
514
|
-
}
|
|
515
|
-
else {
|
|
516
|
-
router.post(routePath, middleware);
|
|
517
|
-
}
|
|
343
|
+
})),
|
|
344
|
+
];
|
|
345
|
+
for (const registered of registeredActions) {
|
|
346
|
+
const { config, name, scope } = registered;
|
|
347
|
+
const middleware = buildActionMiddleware(model, options, registered);
|
|
348
|
+
const routePath = scope === "instance" ? `/:id/${name}` : `/${name}`;
|
|
349
|
+
if (config.method === "GET") {
|
|
350
|
+
router.get(routePath, middleware);
|
|
518
351
|
}
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
finally {
|
|
522
|
-
try {
|
|
523
|
-
if (registeredActions_1_1 && !registeredActions_1_1.done && (_a = registeredActions_1.return)) _a.call(registeredActions_1);
|
|
352
|
+
else {
|
|
353
|
+
router.post(routePath, middleware);
|
|
524
354
|
}
|
|
525
|
-
finally { if (e_3) throw e_3.error; }
|
|
526
355
|
}
|
|
527
356
|
};
|
|
528
357
|
exports.registerActionRoutes = registerActionRoutes;
|