@terreno/api 57.4.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.
- 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 +598 -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/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/mcp/handlers.js
CHANGED
|
@@ -1,115 +1,30 @@
|
|
|
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 __rest = (this && this.__rest) || function (s, e) {
|
|
50
|
-
var t = {};
|
|
51
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
52
|
-
t[p] = s[p];
|
|
53
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
54
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
55
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
56
|
-
t[p[i]] = s[p[i]];
|
|
57
|
-
}
|
|
58
|
-
return t;
|
|
59
|
-
};
|
|
60
|
-
var __values = (this && this.__values) || function(o) {
|
|
61
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
62
|
-
if (m) return m.call(o);
|
|
63
|
-
if (o && typeof o.length === "number") return {
|
|
64
|
-
next: function () {
|
|
65
|
-
if (o && i >= o.length) o = void 0;
|
|
66
|
-
return { value: o && o[i++], done: !o };
|
|
67
|
-
}
|
|
68
|
-
};
|
|
69
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
70
|
-
};
|
|
71
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
72
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
73
|
-
if (!m) return o;
|
|
74
|
-
var i = m.call(o), r, ar = [], e;
|
|
75
|
-
try {
|
|
76
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
77
|
-
}
|
|
78
|
-
catch (error) { e = { error: error }; }
|
|
79
|
-
finally {
|
|
80
|
-
try {
|
|
81
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
82
|
-
}
|
|
83
|
-
finally { if (e) throw e.error; }
|
|
84
|
-
}
|
|
85
|
-
return ar;
|
|
86
|
-
};
|
|
87
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
88
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
89
4
|
};
|
|
90
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
91
6
|
exports.handleDelete = exports.handleUpdate = exports.handleCreate = exports.handleRead = exports.handleList = exports.getMCPErrorCause = void 0;
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
7
|
+
const mongoose_1 = __importDefault(require("mongoose"));
|
|
8
|
+
const api_1 = require("../api");
|
|
9
|
+
const errors_1 = require("../errors");
|
|
10
|
+
const permissions_1 = require("../permissions");
|
|
11
|
+
const executors_1 = require("../sync/executors");
|
|
12
|
+
const transformers_1 = require("../transformers");
|
|
13
|
+
const createMCPRequest_1 = require("./createMCPRequest");
|
|
14
|
+
const query_1 = require("./query");
|
|
15
|
+
const schemaGenerator_1 = require("./schemaGenerator");
|
|
101
16
|
// Causes stay server-side: a WeakMap lets the observability wrapper capture the
|
|
102
17
|
// original exception without adding stack traces or internal details to the MCP result.
|
|
103
|
-
|
|
104
|
-
|
|
18
|
+
const errorCauses = new WeakMap();
|
|
19
|
+
const getMCPErrorCause = (result) => {
|
|
105
20
|
return errorCauses.get(result);
|
|
106
21
|
};
|
|
107
22
|
exports.getMCPErrorCause = getMCPErrorCause;
|
|
108
|
-
|
|
23
|
+
const isPlainObject = (value) => {
|
|
109
24
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
110
25
|
};
|
|
111
26
|
/** Mongoose documents only expose their fields through toObject(). */
|
|
112
|
-
|
|
27
|
+
const toPlain = (value) => {
|
|
113
28
|
if (isPlainObject(value) && typeof value.toObject === "function") {
|
|
114
29
|
return value.toObject();
|
|
115
30
|
}
|
|
@@ -120,35 +35,24 @@ var toPlain = function (value) {
|
|
|
120
35
|
* through untouched. Rebuilding them entry by entry would replace an ObjectId with its
|
|
121
36
|
* raw `{buffer: ...}` internals and a Date with `{}`.
|
|
122
37
|
*/
|
|
123
|
-
|
|
38
|
+
const isSelfSerializing = (value) => {
|
|
124
39
|
return typeof value.toJSON === "function";
|
|
125
40
|
};
|
|
126
41
|
/**
|
|
127
42
|
* Delete a dot-notation path, descending into arrays element-wise so paths through
|
|
128
43
|
* arrays of subdocuments (e.g. "items.secret") are removed from every element.
|
|
129
44
|
*/
|
|
130
|
-
|
|
131
|
-
var e_1, _a;
|
|
45
|
+
const deleteAtPath = (obj, segments) => {
|
|
132
46
|
if (Array.isArray(obj)) {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
var item = obj_1_1.value;
|
|
136
|
-
deleteAtPath(item, segments);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
140
|
-
finally {
|
|
141
|
-
try {
|
|
142
|
-
if (obj_1_1 && !obj_1_1.done && (_a = obj_1.return)) _a.call(obj_1);
|
|
143
|
-
}
|
|
144
|
-
finally { if (e_1) throw e_1.error; }
|
|
47
|
+
for (const item of obj) {
|
|
48
|
+
deleteAtPath(item, segments);
|
|
145
49
|
}
|
|
146
50
|
return;
|
|
147
51
|
}
|
|
148
52
|
if (!isPlainObject(obj) || segments.length === 0) {
|
|
149
53
|
return;
|
|
150
54
|
}
|
|
151
|
-
|
|
55
|
+
const [head, ...rest] = segments;
|
|
152
56
|
if (rest.length === 0) {
|
|
153
57
|
delete obj[head];
|
|
154
58
|
return;
|
|
@@ -163,58 +67,36 @@ var deleteAtPath = function (obj, segments) {
|
|
|
163
67
|
* path when only one specific location should be removed. Literal object keys that
|
|
164
68
|
* equal a denylist entry (including dotted names) are also dropped.
|
|
165
69
|
*/
|
|
166
|
-
|
|
167
|
-
var e_2, _a;
|
|
70
|
+
const stripExcludedFields = (data, excludeFields) => {
|
|
168
71
|
if (!excludeFields.length || !data) {
|
|
169
72
|
return data;
|
|
170
73
|
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
.filter(
|
|
174
|
-
.map(
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
var plain = toPlain(value);
|
|
74
|
+
const bareKeys = new Set(excludeFields.filter((field) => !field.includes(".")));
|
|
75
|
+
const dotPaths = excludeFields
|
|
76
|
+
.filter((field) => field.includes("."))
|
|
77
|
+
.map((field) => field.split("."));
|
|
78
|
+
const strip = (value) => {
|
|
79
|
+
const plain = toPlain(value);
|
|
178
80
|
if (Array.isArray(plain)) {
|
|
179
81
|
return plain.map(strip);
|
|
180
82
|
}
|
|
181
83
|
if (!isPlainObject(plain) || isSelfSerializing(plain)) {
|
|
182
84
|
return plain;
|
|
183
85
|
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
if (bareKeys.has(key) || excludeFields.includes(key)) {
|
|
191
|
-
continue;
|
|
192
|
-
}
|
|
193
|
-
result[key] = strip(nested);
|
|
86
|
+
const result = {};
|
|
87
|
+
for (const [key, nested] of Object.entries(plain)) {
|
|
88
|
+
// Exact key match covers both bare names and literal dotted keys
|
|
89
|
+
// ("metadata.nested.token") that Mongoose would otherwise treat as a path.
|
|
90
|
+
if (bareKeys.has(key) || excludeFields.includes(key)) {
|
|
91
|
+
continue;
|
|
194
92
|
}
|
|
195
|
-
|
|
196
|
-
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
197
|
-
finally {
|
|
198
|
-
try {
|
|
199
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
200
|
-
}
|
|
201
|
-
finally { if (e_3) throw e_3.error; }
|
|
93
|
+
result[key] = strip(nested);
|
|
202
94
|
}
|
|
203
95
|
return result;
|
|
204
96
|
};
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
var segments = dotPaths_1_1.value;
|
|
209
|
-
deleteAtPath(stripped, segments);
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
213
|
-
finally {
|
|
214
|
-
try {
|
|
215
|
-
if (dotPaths_1_1 && !dotPaths_1_1.done && (_a = dotPaths_1.return)) _a.call(dotPaths_1);
|
|
216
|
-
}
|
|
217
|
-
finally { if (e_2) throw e_2.error; }
|
|
97
|
+
const stripped = strip(data);
|
|
98
|
+
for (const segments of dotPaths) {
|
|
99
|
+
deleteAtPath(stripped, segments);
|
|
218
100
|
}
|
|
219
101
|
return stripped;
|
|
220
102
|
};
|
|
@@ -227,52 +109,36 @@ var stripExcludedFields = function (data, excludeFields) {
|
|
|
227
109
|
* whole referenced documents, so a requested path must match a declared one. Requesting
|
|
228
110
|
* paths only narrows the set; anything undeclared is an error rather than a silent widen.
|
|
229
111
|
*/
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
var declared = declaredPaths !== null && declaredPaths !== void 0 ? declaredPaths : [];
|
|
112
|
+
const resolvePopulatePaths = (populateStr, declaredPaths) => {
|
|
113
|
+
const declared = declaredPaths ?? [];
|
|
233
114
|
if (!populateStr) {
|
|
234
115
|
return { paths: declared };
|
|
235
116
|
}
|
|
236
|
-
|
|
117
|
+
const requested = populateStr
|
|
237
118
|
.split(",")
|
|
238
|
-
.map(
|
|
119
|
+
.map((path) => path.trim())
|
|
239
120
|
.filter(Boolean);
|
|
240
121
|
if (!requested.length) {
|
|
241
122
|
return { paths: declared };
|
|
242
123
|
}
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
124
|
+
const paths = [];
|
|
125
|
+
for (const path of requested) {
|
|
126
|
+
const match = declared.find((declaredPath) => declaredPath.path === path);
|
|
246
127
|
if (!match) {
|
|
247
|
-
|
|
248
|
-
return {
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
128
|
+
const allowed = declared.map((declaredPath) => declaredPath.path).join(", ");
|
|
129
|
+
return {
|
|
130
|
+
error: allowed
|
|
131
|
+
? `${path} is not a populate-able path. Allowed: ${allowed}`
|
|
132
|
+
: `${path} is not a populate-able path. This model has no populate-able paths`,
|
|
133
|
+
};
|
|
253
134
|
}
|
|
254
135
|
if (!paths.includes(match)) {
|
|
255
136
|
paths.push(match);
|
|
256
137
|
}
|
|
257
|
-
};
|
|
258
|
-
try {
|
|
259
|
-
for (var requested_1 = __values(requested), requested_1_1 = requested_1.next(); !requested_1_1.done; requested_1_1 = requested_1.next()) {
|
|
260
|
-
var path = requested_1_1.value;
|
|
261
|
-
var state_1 = _loop_1(path);
|
|
262
|
-
if (typeof state_1 === "object")
|
|
263
|
-
return state_1.value;
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
267
|
-
finally {
|
|
268
|
-
try {
|
|
269
|
-
if (requested_1_1 && !requested_1_1.done && (_a = requested_1.return)) _a.call(requested_1);
|
|
270
|
-
}
|
|
271
|
-
finally { if (e_4) throw e_4.error; }
|
|
272
138
|
}
|
|
273
|
-
return { paths
|
|
139
|
+
return { paths };
|
|
274
140
|
};
|
|
275
|
-
|
|
141
|
+
const asOptionalString = (value) => {
|
|
276
142
|
return typeof value === "string" ? value : undefined;
|
|
277
143
|
};
|
|
278
144
|
/**
|
|
@@ -282,42 +148,34 @@ var asOptionalString = function (value) {
|
|
|
282
148
|
* Mixed/embedded objects and arrays), and a dotted path is deleted at that
|
|
283
149
|
* location only. The incoming args object is not mutated.
|
|
284
150
|
*/
|
|
285
|
-
|
|
151
|
+
const omitDeniedWriteFields = (body, denied) => {
|
|
286
152
|
if (denied.length === 0) {
|
|
287
153
|
return body;
|
|
288
154
|
}
|
|
289
155
|
return stripExcludedFields(body, denied);
|
|
290
156
|
};
|
|
291
157
|
/** Mongoose's populated query result types don't narrow to a single document. */
|
|
292
|
-
|
|
293
|
-
return (result
|
|
158
|
+
const asDocument = (result) => {
|
|
159
|
+
return (result ?? null);
|
|
294
160
|
};
|
|
295
161
|
/**
|
|
296
162
|
* Run a findById query and map Mongoose CastError (malformed ids) to null so
|
|
297
163
|
* callers can return a structured not-found. 24-hex ids with mixed case are
|
|
298
164
|
* valid for findById even though `isValidObjectId` requires canonical lowercase.
|
|
299
165
|
*/
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
return [4 /*yield*/, query.exec()];
|
|
308
|
-
case 1: return [2 /*return*/, _a.apply(void 0, [_b.sent()])];
|
|
309
|
-
case 2:
|
|
310
|
-
error_1 = _b.sent();
|
|
311
|
-
if (error_1 instanceof mongoose_1.default.Error.CastError) {
|
|
312
|
-
return [2 /*return*/, null];
|
|
313
|
-
}
|
|
314
|
-
throw error_1;
|
|
315
|
-
case 3: return [2 /*return*/];
|
|
166
|
+
const execFindByIdQuery = async (query) => {
|
|
167
|
+
try {
|
|
168
|
+
return asDocument(await query.exec());
|
|
169
|
+
}
|
|
170
|
+
catch (error) {
|
|
171
|
+
if (error instanceof mongoose_1.default.Error.CastError) {
|
|
172
|
+
return null;
|
|
316
173
|
}
|
|
317
|
-
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
|
|
174
|
+
throw error;
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
const missingDocumentResult = (id) => {
|
|
178
|
+
return errorResult(`Document ${id} not found`);
|
|
321
179
|
};
|
|
322
180
|
/**
|
|
323
181
|
* Mirror REST's `authenticateMiddleware(options.allowAnonymous)`, which answers 401 before
|
|
@@ -325,298 +183,228 @@ var missingDocumentResult = function (id) {
|
|
|
325
183
|
* `IsAuthenticatedOrReadOnly` would let an anonymous MCP client list or read data that the
|
|
326
184
|
* same router refuses over HTTP.
|
|
327
185
|
*/
|
|
328
|
-
|
|
186
|
+
const requiresAuthentication = (entry, user) => {
|
|
329
187
|
return !user && entry.options.allowAnonymous !== true;
|
|
330
188
|
};
|
|
331
189
|
/** Map executor/APIError failures onto the MCP error envelope. */
|
|
332
|
-
|
|
190
|
+
const fromExecutorError = (error) => {
|
|
333
191
|
if ((0, errors_1.isAPIError)(error)) {
|
|
334
192
|
return errorResult(error.title, error);
|
|
335
193
|
}
|
|
336
194
|
return errorResult(errorMessage(error), error);
|
|
337
195
|
};
|
|
338
|
-
|
|
196
|
+
const deniedWriteFields = (entry, phase) => {
|
|
339
197
|
return (0, schemaGenerator_1.writeExcludeFields)(entry.config, (0, schemaGenerator_1.restWriteExcludeFields)(phase, entry.options.validation));
|
|
340
198
|
};
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
_c.trys.push([0, 4, , 5]);
|
|
348
|
-
excludeFields = (_a = entry.config.excludeFields) !== null && _a !== void 0 ? _a : [];
|
|
349
|
-
if (!entry.config.mcpResponseHandler) return [3 /*break*/, 2];
|
|
350
|
-
return [4 /*yield*/, entry.config.mcpResponseHandler(data, method, user)];
|
|
351
|
-
case 1:
|
|
352
|
-
result_1 = _c.sent();
|
|
353
|
-
return [2 /*return*/, { value: stripExcludedFields(result_1, excludeFields) }];
|
|
354
|
-
case 2:
|
|
355
|
-
responseHandler = (_b = entry.options.responseHandler) !== null && _b !== void 0 ? _b : transformers_1.defaultResponseHandler;
|
|
356
|
-
return [4 /*yield*/, responseHandler(data, method, (0, createMCPRequest_1.createMCPRequest)({ user: user }), entry.options)];
|
|
357
|
-
case 3:
|
|
358
|
-
result = _c.sent();
|
|
359
|
-
return [2 /*return*/, { value: stripExcludedFields(result, excludeFields) }];
|
|
360
|
-
case 4:
|
|
361
|
-
error_2 = _c.sent();
|
|
362
|
-
if ((0, errors_1.isAPIError)(error_2)) {
|
|
363
|
-
return [2 /*return*/, { error: errorResult(error_2.title, error_2) }];
|
|
364
|
-
}
|
|
365
|
-
return [2 /*return*/, { error: errorResult("Response handler failed: ".concat(errorMessage(error_2)), error_2) }];
|
|
366
|
-
case 5: return [2 /*return*/];
|
|
199
|
+
const serializeResponse = async (data, method, entry, user) => {
|
|
200
|
+
try {
|
|
201
|
+
const excludeFields = entry.config.excludeFields ?? [];
|
|
202
|
+
if (entry.config.mcpResponseHandler) {
|
|
203
|
+
const result = await entry.config.mcpResponseHandler(data, method, user);
|
|
204
|
+
return { value: stripExcludedFields(result, excludeFields) };
|
|
367
205
|
}
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
model = entry.model, config = entry.config, options = entry.options;
|
|
377
|
-
maxLimit = (_c = config.maxLimit) !== null && _c !== void 0 ? _c : 50;
|
|
378
|
-
if (requiresAuthentication(entry, user)) {
|
|
379
|
-
return [2 /*return*/, errorResult("Permission denied: authentication required")];
|
|
380
|
-
}
|
|
381
|
-
return [4 /*yield*/, (0, permissions_1.checkPermissions)("list", options.permissions.list, user)];
|
|
382
|
-
case 1:
|
|
383
|
-
// Check permissions
|
|
384
|
-
if (!(_e.sent())) {
|
|
385
|
-
return [2 /*return*/, errorResult("Permission denied: cannot list")];
|
|
386
|
-
}
|
|
387
|
-
_a = (0, query_1.buildListQuery)({ args: args, config: config, options: options }), queryError = _a.error, builtFilter = _a.query;
|
|
388
|
-
if (queryError || !builtFilter) {
|
|
389
|
-
return [2 /*return*/, errorResult(queryError !== null && queryError !== void 0 ? queryError : "Could not build query")];
|
|
390
|
-
}
|
|
391
|
-
query = builtFilter;
|
|
392
|
-
if (!options.queryFilter) return [3 /*break*/, 5];
|
|
393
|
-
_e.label = 2;
|
|
394
|
-
case 2:
|
|
395
|
-
_e.trys.push([2, 4, , 5]);
|
|
396
|
-
return [4 /*yield*/, options.queryFilter(user, query)];
|
|
397
|
-
case 3:
|
|
398
|
-
filtered = _e.sent();
|
|
399
|
-
if (filtered === null) {
|
|
400
|
-
return [2 /*return*/, textResult(JSON.stringify({ data: [], more: false, page: 1, total: 0 }))];
|
|
401
|
-
}
|
|
402
|
-
query = __assign(__assign({}, query), filtered);
|
|
403
|
-
return [3 /*break*/, 5];
|
|
404
|
-
case 4:
|
|
405
|
-
error_3 = _e.sent();
|
|
406
|
-
if ((0, errors_1.isAPIError)(error_3)) {
|
|
407
|
-
return [2 /*return*/, errorResult(error_3.title, error_3)];
|
|
408
|
-
}
|
|
409
|
-
return [2 /*return*/, errorResult("queryFilter failed: ".concat(errorMessage(error_3)), error_3)];
|
|
410
|
-
case 5:
|
|
411
|
-
limit = Math.max(1, Math.min(Number(args.limit) || maxLimit, maxLimit));
|
|
412
|
-
page = Math.max(1, Number(args.page) || 1);
|
|
413
|
-
builtQuery = model.find(query).limit(limit + 1);
|
|
414
|
-
return [4 /*yield*/, model.countDocuments(query)];
|
|
415
|
-
case 6:
|
|
416
|
-
total = _e.sent();
|
|
417
|
-
if (page > 1) {
|
|
418
|
-
builtQuery = builtQuery.skip((page - 1) * limit);
|
|
419
|
-
}
|
|
420
|
-
sort = (_d = asOptionalString(args.sort)) !== null && _d !== void 0 ? _d : options.sort;
|
|
421
|
-
if (sort) {
|
|
422
|
-
builtQuery = builtQuery.sort(sort);
|
|
423
|
-
}
|
|
424
|
-
_b = resolvePopulatePaths(asOptionalString(args.populate), options.populatePaths), populateError = _b.error, populatePaths = _b.paths;
|
|
425
|
-
if (populateError || !populatePaths) {
|
|
426
|
-
return [2 /*return*/, errorResult(populateError !== null && populateError !== void 0 ? populateError : "Could not resolve populate paths")];
|
|
427
|
-
}
|
|
428
|
-
populatedQuery = (0, api_1.addPopulateToQuery)(builtQuery, populatePaths);
|
|
429
|
-
return [4 /*yield*/, populatedQuery.exec()];
|
|
430
|
-
case 7:
|
|
431
|
-
data = _e.sent();
|
|
432
|
-
more = data.length > limit;
|
|
433
|
-
sliced = more ? data.slice(0, limit) : data;
|
|
434
|
-
return [4 /*yield*/, serializeResponse(sliced, "list", entry, user)];
|
|
435
|
-
case 8:
|
|
436
|
-
serialized = _e.sent();
|
|
437
|
-
if ("error" in serialized) {
|
|
438
|
-
return [2 /*return*/, serialized.error];
|
|
439
|
-
}
|
|
440
|
-
return [2 /*return*/, textResult(JSON.stringify({ data: serialized.value, more: more, page: page, total: total }))];
|
|
206
|
+
// Use the model router's responseHandler if available, otherwise default
|
|
207
|
+
const responseHandler = entry.options.responseHandler ?? transformers_1.defaultResponseHandler;
|
|
208
|
+
const result = await responseHandler(data, method, (0, createMCPRequest_1.createMCPRequest)({ user }), entry.options);
|
|
209
|
+
return { value: stripExcludedFields(result, excludeFields) };
|
|
210
|
+
}
|
|
211
|
+
catch (error) {
|
|
212
|
+
if ((0, errors_1.isAPIError)(error)) {
|
|
213
|
+
return { error: errorResult(error.title, error) };
|
|
441
214
|
}
|
|
442
|
-
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
case 2:
|
|
470
|
-
data = _b.sent();
|
|
471
|
-
if (!data) {
|
|
472
|
-
return [2 /*return*/, missingDocumentResult(id)];
|
|
473
|
-
}
|
|
474
|
-
return [4 /*yield*/, (0, permissions_1.checkPermissions)("read", options.permissions.read, user, data)];
|
|
475
|
-
case 3:
|
|
476
|
-
// Check object-level permission
|
|
477
|
-
if (!(_b.sent())) {
|
|
478
|
-
return [2 /*return*/, errorResult("Permission denied: cannot read this document")];
|
|
479
|
-
}
|
|
480
|
-
return [4 /*yield*/, serializeResponse(data, "read", entry, user)];
|
|
481
|
-
case 4:
|
|
482
|
-
serialized = _b.sent();
|
|
483
|
-
if ("error" in serialized) {
|
|
484
|
-
return [2 /*return*/, serialized.error];
|
|
485
|
-
}
|
|
486
|
-
return [2 /*return*/, textResult(JSON.stringify({ data: serialized.value }))];
|
|
215
|
+
return { error: errorResult(`Response handler failed: ${errorMessage(error)}`, error) };
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
const handleList = async (entry, args, user) => {
|
|
219
|
+
const { model, config, options } = entry;
|
|
220
|
+
const maxLimit = config.maxLimit ?? 50;
|
|
221
|
+
if (requiresAuthentication(entry, user)) {
|
|
222
|
+
return errorResult("Permission denied: authentication required");
|
|
223
|
+
}
|
|
224
|
+
// Check permissions
|
|
225
|
+
if (!(await (0, permissions_1.checkPermissions)("list", options.permissions.list, user))) {
|
|
226
|
+
return errorResult("Permission denied: cannot list");
|
|
227
|
+
}
|
|
228
|
+
// Build query from args — only fields in queryFields, with an allowlist of operators
|
|
229
|
+
const { error: queryError, query: builtFilter } = (0, query_1.buildListQuery)({ args, config, options });
|
|
230
|
+
if (queryError || !builtFilter) {
|
|
231
|
+
return errorResult(queryError ?? "Could not build query");
|
|
232
|
+
}
|
|
233
|
+
let query = builtFilter;
|
|
234
|
+
// Apply query filter
|
|
235
|
+
if (options.queryFilter) {
|
|
236
|
+
try {
|
|
237
|
+
const filtered = await options.queryFilter(user, query);
|
|
238
|
+
if (filtered === null) {
|
|
239
|
+
return textResult(JSON.stringify({ data: [], more: false, page: 1, total: 0 }));
|
|
240
|
+
}
|
|
241
|
+
query = { ...query, ...filtered };
|
|
487
242
|
}
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
return __generator(this, function (_a) {
|
|
494
|
-
switch (_a.label) {
|
|
495
|
-
case 0:
|
|
496
|
-
model = entry.model, options = entry.options;
|
|
497
|
-
if (requiresAuthentication(entry, user)) {
|
|
498
|
-
return [2 /*return*/, errorResult("Permission denied: authentication required")];
|
|
499
|
-
}
|
|
500
|
-
body = omitDeniedWriteFields(args, deniedWriteFields(entry, "create"));
|
|
501
|
-
req = (0, createMCPRequest_1.createMCPRequest)({ body: body, user: user });
|
|
502
|
-
_a.label = 1;
|
|
503
|
-
case 1:
|
|
504
|
-
_a.trys.push([1, 3, , 4]);
|
|
505
|
-
return [4 /*yield*/, (0, executors_1.executeCreate)({
|
|
506
|
-
body: body,
|
|
507
|
-
model: model,
|
|
508
|
-
options: options,
|
|
509
|
-
req: req,
|
|
510
|
-
user: user,
|
|
511
|
-
writeModelName: entry.modelName,
|
|
512
|
-
})];
|
|
513
|
-
case 2:
|
|
514
|
-
result = _a.sent();
|
|
515
|
-
data = result.doc;
|
|
516
|
-
return [3 /*break*/, 4];
|
|
517
|
-
case 3:
|
|
518
|
-
error_4 = _a.sent();
|
|
519
|
-
return [2 /*return*/, fromExecutorError(error_4)];
|
|
520
|
-
case 4: return [4 /*yield*/, serializeResponse(data, "create", entry, user)];
|
|
521
|
-
case 5:
|
|
522
|
-
serialized = _a.sent();
|
|
523
|
-
if ("error" in serialized) {
|
|
524
|
-
return [2 /*return*/, serialized.error];
|
|
525
|
-
}
|
|
526
|
-
return [2 /*return*/, textResult(JSON.stringify({ data: serialized.value }))];
|
|
243
|
+
catch (error) {
|
|
244
|
+
if ((0, errors_1.isAPIError)(error)) {
|
|
245
|
+
return errorResult(error.title, error);
|
|
246
|
+
}
|
|
247
|
+
return errorResult(`queryFilter failed: ${errorMessage(error)}`, error);
|
|
527
248
|
}
|
|
528
|
-
}
|
|
529
|
-
|
|
249
|
+
}
|
|
250
|
+
// Pagination
|
|
251
|
+
const limit = Math.max(1, Math.min(Number(args.limit) || maxLimit, maxLimit));
|
|
252
|
+
const page = Math.max(1, Number(args.page) || 1);
|
|
253
|
+
let builtQuery = model.find(query).limit(limit + 1);
|
|
254
|
+
const total = await model.countDocuments(query);
|
|
255
|
+
if (page > 1) {
|
|
256
|
+
builtQuery = builtQuery.skip((page - 1) * limit);
|
|
257
|
+
}
|
|
258
|
+
// Sort
|
|
259
|
+
const sort = asOptionalString(args.sort) ?? options.sort;
|
|
260
|
+
if (sort) {
|
|
261
|
+
builtQuery = builtQuery.sort(sort);
|
|
262
|
+
}
|
|
263
|
+
// Populate
|
|
264
|
+
const { error: populateError, paths: populatePaths } = resolvePopulatePaths(asOptionalString(args.populate), options.populatePaths);
|
|
265
|
+
if (populateError || !populatePaths) {
|
|
266
|
+
return errorResult(populateError ?? "Could not resolve populate paths");
|
|
267
|
+
}
|
|
268
|
+
const populatedQuery = (0, api_1.addPopulateToQuery)(builtQuery, populatePaths);
|
|
269
|
+
const data = await populatedQuery.exec();
|
|
270
|
+
const more = data.length > limit;
|
|
271
|
+
const sliced = more ? data.slice(0, limit) : data;
|
|
272
|
+
const serialized = await serializeResponse(sliced, "list", entry, user);
|
|
273
|
+
if ("error" in serialized) {
|
|
274
|
+
return serialized.error;
|
|
275
|
+
}
|
|
276
|
+
return textResult(JSON.stringify({ data: serialized.value, more, page, total }));
|
|
277
|
+
};
|
|
278
|
+
exports.handleList = handleList;
|
|
279
|
+
const handleRead = async (entry, args, user) => {
|
|
280
|
+
const { model, options } = entry;
|
|
281
|
+
if (requiresAuthentication(entry, user)) {
|
|
282
|
+
return errorResult("Permission denied: authentication required");
|
|
283
|
+
}
|
|
284
|
+
// Check method-level permission
|
|
285
|
+
if (!(await (0, permissions_1.checkPermissions)("read", options.permissions.read, user))) {
|
|
286
|
+
return errorResult("Permission denied: cannot read");
|
|
287
|
+
}
|
|
288
|
+
const { error: populateError, paths: populatePaths } = resolvePopulatePaths(asOptionalString(args.populate), options.populatePaths);
|
|
289
|
+
if (populateError || !populatePaths) {
|
|
290
|
+
return errorResult(populateError ?? "Could not resolve populate paths");
|
|
291
|
+
}
|
|
292
|
+
const id = asOptionalString(args.id);
|
|
293
|
+
if (!id) {
|
|
294
|
+
return missingDocumentResult(args.id);
|
|
295
|
+
}
|
|
296
|
+
const data = await execFindByIdQuery((0, api_1.addPopulateToQuery)(model.findById(id), populatePaths));
|
|
297
|
+
if (!data) {
|
|
298
|
+
return missingDocumentResult(id);
|
|
299
|
+
}
|
|
300
|
+
// Check object-level permission
|
|
301
|
+
if (!(await (0, permissions_1.checkPermissions)("read", options.permissions.read, user, data))) {
|
|
302
|
+
return errorResult("Permission denied: cannot read this document");
|
|
303
|
+
}
|
|
304
|
+
const serialized = await serializeResponse(data, "read", entry, user);
|
|
305
|
+
if ("error" in serialized) {
|
|
306
|
+
return serialized.error;
|
|
307
|
+
}
|
|
308
|
+
return textResult(JSON.stringify({ data: serialized.value }));
|
|
309
|
+
};
|
|
310
|
+
exports.handleRead = handleRead;
|
|
311
|
+
const handleCreate = async (entry, args, user) => {
|
|
312
|
+
const { model, options } = entry;
|
|
313
|
+
if (requiresAuthentication(entry, user)) {
|
|
314
|
+
return errorResult("Permission denied: authentication required");
|
|
315
|
+
}
|
|
316
|
+
const body = omitDeniedWriteFields(args, deniedWriteFields(entry, "create"));
|
|
317
|
+
const req = (0, createMCPRequest_1.createMCPRequest)({ body, user });
|
|
318
|
+
let data;
|
|
319
|
+
try {
|
|
320
|
+
const result = await (0, executors_1.executeCreate)({
|
|
321
|
+
body,
|
|
322
|
+
model,
|
|
323
|
+
options,
|
|
324
|
+
req,
|
|
325
|
+
user,
|
|
326
|
+
writeModelName: entry.modelName,
|
|
327
|
+
});
|
|
328
|
+
data = result.doc;
|
|
329
|
+
}
|
|
330
|
+
catch (error) {
|
|
331
|
+
return fromExecutorError(error);
|
|
332
|
+
}
|
|
333
|
+
const serialized = await serializeResponse(data, "create", entry, user);
|
|
334
|
+
if ("error" in serialized) {
|
|
335
|
+
return serialized.error;
|
|
336
|
+
}
|
|
337
|
+
return textResult(JSON.stringify({ data: serialized.value }));
|
|
338
|
+
};
|
|
530
339
|
exports.handleCreate = handleCreate;
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
return [2 /*return*/, fromExecutorError(error_5)];
|
|
566
|
-
case 4: return [4 /*yield*/, serializeResponse(doc, "update", entry, user)];
|
|
567
|
-
case 5:
|
|
568
|
-
serialized = _a.sent();
|
|
569
|
-
if ("error" in serialized) {
|
|
570
|
-
return [2 /*return*/, serialized.error];
|
|
571
|
-
}
|
|
572
|
-
return [2 /*return*/, textResult(JSON.stringify({ data: serialized.value }))];
|
|
573
|
-
}
|
|
574
|
-
});
|
|
575
|
-
}); };
|
|
340
|
+
const handleUpdate = async (entry, args, user) => {
|
|
341
|
+
const { model, options } = entry;
|
|
342
|
+
const { id, ...updateFields } = args;
|
|
343
|
+
if (requiresAuthentication(entry, user)) {
|
|
344
|
+
return errorResult("Permission denied: authentication required");
|
|
345
|
+
}
|
|
346
|
+
const documentId = asOptionalString(id);
|
|
347
|
+
if (!documentId) {
|
|
348
|
+
return missingDocumentResult(id);
|
|
349
|
+
}
|
|
350
|
+
const body = omitDeniedWriteFields(updateFields, deniedWriteFields(entry, "update"));
|
|
351
|
+
const req = (0, createMCPRequest_1.createMCPRequest)({ body, user });
|
|
352
|
+
let doc;
|
|
353
|
+
try {
|
|
354
|
+
const result = await (0, executors_1.executeUpdate)({
|
|
355
|
+
body,
|
|
356
|
+
id: documentId,
|
|
357
|
+
model,
|
|
358
|
+
options,
|
|
359
|
+
req,
|
|
360
|
+
user,
|
|
361
|
+
writeModelName: entry.modelName,
|
|
362
|
+
});
|
|
363
|
+
doc = result.doc;
|
|
364
|
+
}
|
|
365
|
+
catch (error) {
|
|
366
|
+
return fromExecutorError(error);
|
|
367
|
+
}
|
|
368
|
+
const serialized = await serializeResponse(doc, "update", entry, user);
|
|
369
|
+
if ("error" in serialized) {
|
|
370
|
+
return serialized.error;
|
|
371
|
+
}
|
|
372
|
+
return textResult(JSON.stringify({ data: serialized.value }));
|
|
373
|
+
};
|
|
576
374
|
exports.handleUpdate = handleUpdate;
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
case 2:
|
|
602
|
-
_a.sent();
|
|
603
|
-
return [3 /*break*/, 4];
|
|
604
|
-
case 3:
|
|
605
|
-
error_6 = _a.sent();
|
|
606
|
-
return [2 /*return*/, fromExecutorError(error_6)];
|
|
607
|
-
case 4: return [2 /*return*/, textResult(JSON.stringify({ success: true }))];
|
|
608
|
-
}
|
|
609
|
-
});
|
|
610
|
-
}); };
|
|
375
|
+
const handleDelete = async (entry, args, user) => {
|
|
376
|
+
const { model, options } = entry;
|
|
377
|
+
const { id } = args;
|
|
378
|
+
if (requiresAuthentication(entry, user)) {
|
|
379
|
+
return errorResult("Permission denied: authentication required");
|
|
380
|
+
}
|
|
381
|
+
const documentId = asOptionalString(id);
|
|
382
|
+
if (!documentId) {
|
|
383
|
+
return missingDocumentResult(id);
|
|
384
|
+
}
|
|
385
|
+
try {
|
|
386
|
+
await (0, executors_1.executeDelete)({
|
|
387
|
+
id: documentId,
|
|
388
|
+
model,
|
|
389
|
+
options,
|
|
390
|
+
req: (0, createMCPRequest_1.createMCPRequest)({ body: args, user }),
|
|
391
|
+
user,
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
catch (error) {
|
|
395
|
+
return fromExecutorError(error);
|
|
396
|
+
}
|
|
397
|
+
return textResult(JSON.stringify({ success: true }));
|
|
398
|
+
};
|
|
611
399
|
exports.handleDelete = handleDelete;
|
|
612
|
-
|
|
400
|
+
const errorMessage = (error) => {
|
|
613
401
|
return error instanceof Error ? error.message : String(error);
|
|
614
402
|
};
|
|
615
|
-
|
|
616
|
-
content: [{ text
|
|
617
|
-
});
|
|
618
|
-
|
|
619
|
-
|
|
403
|
+
const textResult = (text) => ({
|
|
404
|
+
content: [{ text, type: "text" }],
|
|
405
|
+
});
|
|
406
|
+
const errorResult = (message, cause) => {
|
|
407
|
+
const result = {
|
|
620
408
|
content: [{ text: JSON.stringify({ error: message }), type: "text" }],
|
|
621
409
|
isError: true,
|
|
622
410
|
};
|