@terreno/api 56.0.0-beta.0 → 56.0.0-beta.2
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/CHANGELOG.md +2 -122
- package/LICENSE +21 -202
- package/dist/api.d.ts +3 -2
- package/dist/auth.js +21 -25
- package/dist/betterAuthSetup.d.ts +15 -1
- package/dist/betterAuthSetup.js +4 -6
- package/dist/configurationPlugin.js +2 -2
- package/dist/expressServer.js +11 -5
- package/dist/githubAuth.js +1 -4
- package/dist/plugins.d.ts +7 -6
- package/dist/plugins.js +24 -8
- package/dist/realtime/realtimeApp.d.ts +1 -1
- package/dist/realtime/realtimeApp.js +16 -8
- package/dist/realtime/types.d.ts +14 -0
- package/dist/sync/executors.js +14 -14
- package/dist/sync/models.js +1 -1
- package/dist/sync/registry.js +1 -1
- package/dist/sync/syncSeqPlugin.js +25 -29
- package/dist/tests/models.d.ts +27 -11
- package/dist/tests/models.js +5 -3
- package/dist/tests/testHelper.d.ts +9 -3
- package/package.json +5 -6
- package/src/actions.test.ts +19 -12
- package/src/actions.ts +2 -2
- package/src/api.coverage.test.ts +7 -9
- package/src/api.errorPaths.test.ts +47 -1
- package/src/api.errors.test.ts +32 -23
- package/src/api.ts +22 -12
- package/src/auth.test.ts +251 -1
- package/src/auth.ts +25 -23
- package/src/betterAuthSetup.ts +7 -7
- package/src/configuration.test.ts +59 -38
- package/src/configurationPlugin.test.ts +72 -42
- package/src/configurationPlugin.ts +2 -2
- package/src/consentApp.test.ts +12 -12
- package/src/expressServer.ts +11 -5
- package/src/githubAuth.ts +1 -3
- package/src/middleware.test.ts +16 -0
- package/src/models/versionConfig.ts +1 -1
- package/src/openApiBuilder.test.ts +24 -9
- package/src/openApiValidator.test.ts +41 -0
- package/src/permissions.test.ts +35 -25
- package/src/plugins.test.ts +24 -0
- package/src/plugins.ts +29 -11
- package/src/populate.test.ts +25 -16
- package/src/realtime/realtime.test.ts +239 -0
- package/src/realtime/realtimeApp.ts +15 -8
- package/src/realtime/realtimeAppSetup.test.ts +159 -0
- package/src/realtime/types.ts +14 -0
- package/src/secretProvidersGcpClient.test.ts +66 -0
- package/src/sync/executors.ts +16 -14
- package/src/sync/models.ts +2 -1
- package/src/sync/registry.ts +1 -1
- package/src/sync/syncFrontier.test.ts +2 -2
- package/src/sync/syncSeqPlugin.ts +43 -51
- package/src/tests/models.ts +8 -6
package/dist/plugins.js
CHANGED
|
@@ -355,10 +355,10 @@ var upsertPlugin = function (schema) {
|
|
|
355
355
|
};
|
|
356
356
|
exports.upsertPlugin = upsertPlugin;
|
|
357
357
|
/**
|
|
358
|
-
* Adds an `archived` boolean field and excludes archived documents from `find()`
|
|
359
|
-
* default. Pass `{archived: true}` explicitly to include them. This is a soft-archive
|
|
360
|
-
* {@link isDeletedPlugin}: use it when documents should be hidden from normal listings
|
|
361
|
-
* (and still directly queryable) rather than treated as deleted.
|
|
358
|
+
* Adds an `archived` boolean field and excludes archived documents from `find()` / `findOne()`
|
|
359
|
+
* queries by default. Pass `{archived: true}` explicitly to include them. This is a soft-archive
|
|
360
|
+
* analog to {@link isDeletedPlugin}: use it when documents should be hidden from normal listings
|
|
361
|
+
* but kept (and still directly queryable) rather than treated as deleted.
|
|
362
362
|
* @param schema Mongoose Schema
|
|
363
363
|
* @param defaultValue Default value for the `archived` field (defaults to `false`)
|
|
364
364
|
*/
|
|
@@ -376,15 +376,27 @@ schema, defaultValue) {
|
|
|
376
376
|
type: Boolean,
|
|
377
377
|
},
|
|
378
378
|
});
|
|
379
|
+
// Mirror isDeletedPlugin: filter both find and findOne so findExactlyOne / findOneOrNone
|
|
380
|
+
// also exclude archived documents unless the query sets `archived` explicitly.
|
|
379
381
|
// noExplicitAny: Query<any, any> must be loose to accept arbitrary consumer queries
|
|
380
382
|
// biome-ignore lint/suspicious/noExplicitAny: Query<any, any> must be loose to accept arbitrary consumer queries
|
|
381
|
-
|
|
382
|
-
var conditions =
|
|
383
|
+
var applyArchiveFilter = function (query) {
|
|
384
|
+
var conditions = query.getFilter();
|
|
383
385
|
// Only apply the default filter when the query does not mention `archived` at all, so an
|
|
384
386
|
// explicit `{archived: true}` (or `false`) is always respected.
|
|
385
387
|
if (conditions.archived === undefined) {
|
|
386
|
-
|
|
388
|
+
query.setQuery(__assign(__assign({}, conditions), { archived: { $ne: true } }));
|
|
387
389
|
}
|
|
390
|
+
};
|
|
391
|
+
// noExplicitAny: Query<any, any> must be loose to accept arbitrary consumer queries
|
|
392
|
+
// biome-ignore lint/suspicious/noExplicitAny: Query<any, any> must be loose to accept arbitrary consumer queries
|
|
393
|
+
schema.pre("find", function () {
|
|
394
|
+
applyArchiveFilter(this);
|
|
395
|
+
});
|
|
396
|
+
// noExplicitAny: Query<any, any> must be loose to accept arbitrary consumer queries
|
|
397
|
+
// biome-ignore lint/suspicious/noExplicitAny: Query<any, any> must be loose to accept arbitrary consumer queries
|
|
398
|
+
schema.pre("findOne", function () {
|
|
399
|
+
applyArchiveFilter(this);
|
|
388
400
|
});
|
|
389
401
|
};
|
|
390
402
|
exports.excludeArchivedPlugin = excludeArchivedPlugin;
|
|
@@ -408,7 +420,11 @@ var DateOnly = /** @class */ (function (_super) {
|
|
|
408
420
|
}
|
|
409
421
|
var handler = this.$conditionalHandlers[$conditional];
|
|
410
422
|
if (!handler) {
|
|
411
|
-
throw new
|
|
423
|
+
throw new errors_1.APIError({
|
|
424
|
+
detail: "Can't use ".concat($conditional, " with DateOnly."),
|
|
425
|
+
status: 400,
|
|
426
|
+
title: "Unsupported query conditional for DateOnly",
|
|
427
|
+
});
|
|
412
428
|
}
|
|
413
429
|
return handler.call(this, val);
|
|
414
430
|
};
|
|
@@ -28,7 +28,7 @@ export interface RealtimeSocketLike extends SocketWithDecodedToken {
|
|
|
28
28
|
join: (room: string) => Promise<void> | void;
|
|
29
29
|
leave: (room: string) => Promise<void> | void;
|
|
30
30
|
emit: (event: string, payload: unknown) => void;
|
|
31
|
-
on: (event: string, handler: (...args:
|
|
31
|
+
on: (event: string, handler: (...args: never[]) => void | Promise<void>) => void;
|
|
32
32
|
}
|
|
33
33
|
/**
|
|
34
34
|
* Install the realtime subscription handlers on a single socket. Extracted from the
|
|
@@ -83,6 +83,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
83
83
|
exports.RealtimeApp = exports.installRealtimeSocketHandlers = exports.redactCredentials = exports.MAX_QUERY_SUBSCRIPTIONS = exports.MAX_DOCUMENT_SUBSCRIPTIONS = exports.MAX_MODEL_SUBSCRIPTIONS = void 0;
|
|
84
84
|
var Sentry = __importStar(require("@sentry/bun"));
|
|
85
85
|
var socket_io_1 = require("socket.io");
|
|
86
|
+
var errors_1 = require("../errors");
|
|
86
87
|
var logger_1 = require("../logger");
|
|
87
88
|
var permissions_1 = require("../permissions");
|
|
88
89
|
var registry_1 = require("../sync/registry");
|
|
@@ -454,7 +455,7 @@ var RealtimeApp = /** @class */ (function () {
|
|
|
454
455
|
*/
|
|
455
456
|
RealtimeApp.prototype.onServerCreated = function (server) {
|
|
456
457
|
var _this = this;
|
|
457
|
-
var _a, _b, _c;
|
|
458
|
+
var _a, _b, _c, _d, _e;
|
|
458
459
|
var debug = (_a = this.config.debug) !== null && _a !== void 0 ? _a : false;
|
|
459
460
|
var logInfo = function (message) {
|
|
460
461
|
if (debug) {
|
|
@@ -463,17 +464,23 @@ var RealtimeApp = /** @class */ (function () {
|
|
|
463
464
|
};
|
|
464
465
|
try {
|
|
465
466
|
logInfo("[realtime] Setting up Socket.io server...");
|
|
466
|
-
|
|
467
|
-
|
|
467
|
+
// Prefer an injected Server constructor in tests so callers never need
|
|
468
|
+
// process-wide `mock.module("socket.io")` (which poisons later files).
|
|
469
|
+
var SocketServerCtor = (_b = this.config.SocketServer) !== null && _b !== void 0 ? _b : socket_io_1.Server;
|
|
470
|
+
this.io = new SocketServerCtor(server, {
|
|
471
|
+
cors: (_c = this.config.cors) !== null && _c !== void 0 ? _c : {
|
|
468
472
|
methods: ["GET", "POST"],
|
|
469
473
|
origin: "*",
|
|
470
474
|
},
|
|
471
475
|
});
|
|
472
476
|
// Authentication middleware: legacy JWT first, optionally Better Auth sessions.
|
|
473
|
-
var tokenSecret = (
|
|
477
|
+
var tokenSecret = (_d = this.config.tokenSecret) !== null && _d !== void 0 ? _d : process.env.TOKEN_SECRET;
|
|
474
478
|
if (!tokenSecret) {
|
|
475
|
-
throw new
|
|
476
|
-
|
|
479
|
+
throw new errors_1.APIError({
|
|
480
|
+
status: 500,
|
|
481
|
+
title: "[realtime] TOKEN_SECRET is required for socket authentication. " +
|
|
482
|
+
"Set process.env.TOKEN_SECRET or pass tokenSecret in RealtimeAppOptions.",
|
|
483
|
+
});
|
|
477
484
|
}
|
|
478
485
|
// D1: pass the issuer through for parity with the HTTP JWT path
|
|
479
486
|
// (jwt.verify(token, secret, {issuer})) — without it a validly-signed token
|
|
@@ -521,8 +528,9 @@ var RealtimeApp = /** @class */ (function () {
|
|
|
521
528
|
logger_1.logger.error("[realtime] Connection error: ".concat(error.message));
|
|
522
529
|
Sentry.captureException(error);
|
|
523
530
|
});
|
|
524
|
-
// Start the change stream watcher
|
|
525
|
-
|
|
531
|
+
// Start the change stream watcher (injectable for unit tests — see SocketServer).
|
|
532
|
+
var startWatcher = (_e = this.config.startChangeStreamWatcher) !== null && _e !== void 0 ? _e : changeStreamWatcher_1.startChangeStreamWatcher;
|
|
533
|
+
startWatcher(this.io, this.config.changeStream, debug);
|
|
526
534
|
// D1: periodic session re-validation sweep — disconnects sockets whose JWT has
|
|
527
535
|
// expired, whose Better Auth session is no longer valid, or whose user has
|
|
528
536
|
// since been disabled; also refreshes socket.data.fullUser (D2) and
|
package/dist/realtime/types.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type express from "express";
|
|
2
|
+
import type { Server as SocketIoServer } from "socket.io";
|
|
2
3
|
import type { UserModel } from "../auth";
|
|
3
4
|
import type { SyncAppOptions } from "../sync/routes";
|
|
5
|
+
import type { startChangeStreamWatcher } from "./changeStreamWatcher";
|
|
4
6
|
import type { BetterAuthSocketOptions } from "./socketAuth";
|
|
5
7
|
/**
|
|
6
8
|
* Configuration for real-time sync on a modelRouter.
|
|
@@ -117,6 +119,18 @@ export interface RealtimeAppOptions {
|
|
|
117
119
|
sessionRevalidationIntervalMs?: number;
|
|
118
120
|
/** Enable debug logging */
|
|
119
121
|
debug?: boolean;
|
|
122
|
+
/**
|
|
123
|
+
* Test seam: Socket.IO server constructor. Defaults to `socket.io`'s `Server`.
|
|
124
|
+
* Prefer this over `mock.module("socket.io")` — bun's module mocks are process-wide
|
|
125
|
+
* and break later files that need a real Socket.IO server (sync integration, etc.).
|
|
126
|
+
*/
|
|
127
|
+
SocketServer?: typeof SocketIoServer;
|
|
128
|
+
/**
|
|
129
|
+
* Test seam: change-stream watcher starter. Defaults to the real
|
|
130
|
+
* `startChangeStreamWatcher`. Same rationale as {@link SocketServer} — avoid
|
|
131
|
+
* process-wide `mock.module` of `./changeStreamWatcher`.
|
|
132
|
+
*/
|
|
133
|
+
startChangeStreamWatcher?: typeof startChangeStreamWatcher;
|
|
120
134
|
}
|
|
121
135
|
/**
|
|
122
136
|
* Payload sent by the client to subscribe to a single document's changes.
|
package/dist/sync/executors.js
CHANGED
|
@@ -197,25 +197,25 @@ var executeCreate = function (_a) { return __awaiter(void 0, [_a], void 0, funct
|
|
|
197
197
|
});
|
|
198
198
|
case 5:
|
|
199
199
|
if (cleanedBody === undefined) {
|
|
200
|
-
throw new errors_1.
|
|
200
|
+
throw new errors_1.ForbiddenError({
|
|
201
|
+
code: "create-not-allowed",
|
|
201
202
|
detail: "A body must be returned from preCreate",
|
|
202
|
-
status: 403,
|
|
203
203
|
title: "Create not allowed",
|
|
204
204
|
});
|
|
205
205
|
}
|
|
206
206
|
if (cleanedBody === null) {
|
|
207
|
-
throw new errors_1.
|
|
207
|
+
throw new errors_1.ForbiddenError({
|
|
208
|
+
code: "create-not-allowed",
|
|
208
209
|
detail: "preCreate hook returned null",
|
|
209
|
-
status: 403,
|
|
210
210
|
title: "Create not allowed",
|
|
211
211
|
});
|
|
212
212
|
}
|
|
213
213
|
_c.label = 6;
|
|
214
214
|
case 6:
|
|
215
215
|
if (cleanedBody === undefined) {
|
|
216
|
-
throw new errors_1.
|
|
216
|
+
throw new errors_1.BadRequestError({
|
|
217
|
+
code: "invalid-request-body",
|
|
217
218
|
detail: "Body is undefined",
|
|
218
|
-
status: 400,
|
|
219
219
|
title: "Invalid request body",
|
|
220
220
|
});
|
|
221
221
|
}
|
|
@@ -371,16 +371,16 @@ var executeUpdate = function (_a) { return __awaiter(void 0, [_a], void 0, funct
|
|
|
371
371
|
});
|
|
372
372
|
case 9:
|
|
373
373
|
if (cleanedBody === undefined) {
|
|
374
|
-
throw new errors_1.
|
|
374
|
+
throw new errors_1.ForbiddenError({
|
|
375
|
+
code: "update-not-allowed",
|
|
375
376
|
detail: "A body must be returned from preUpdate",
|
|
376
|
-
status: 403,
|
|
377
377
|
title: "Update not allowed",
|
|
378
378
|
});
|
|
379
379
|
}
|
|
380
380
|
if (cleanedBody === null) {
|
|
381
|
-
throw new errors_1.
|
|
381
|
+
throw new errors_1.ForbiddenError({
|
|
382
|
+
code: "update-not-allowed",
|
|
382
383
|
detail: "preUpdate hook on ".concat(id, " returned null"),
|
|
383
|
-
status: 403,
|
|
384
384
|
title: "Update not allowed",
|
|
385
385
|
});
|
|
386
386
|
}
|
|
@@ -596,16 +596,16 @@ var executeDelete = function (_a) { return __awaiter(void 0, [_a], void 0, funct
|
|
|
596
596
|
});
|
|
597
597
|
case 13:
|
|
598
598
|
if (body === undefined) {
|
|
599
|
-
throw new errors_1.
|
|
599
|
+
throw new errors_1.ForbiddenError({
|
|
600
|
+
code: "delete-not-allowed",
|
|
600
601
|
detail: "A body must be returned from preDelete",
|
|
601
|
-
status: 403,
|
|
602
602
|
title: "Delete not allowed",
|
|
603
603
|
});
|
|
604
604
|
}
|
|
605
605
|
if (body === null) {
|
|
606
|
-
throw new errors_1.
|
|
606
|
+
throw new errors_1.ForbiddenError({
|
|
607
|
+
code: "delete-not-allowed",
|
|
607
608
|
detail: "preDelete hook for ".concat(id, " returned null"),
|
|
608
|
-
status: 403,
|
|
609
609
|
title: "Delete not allowed",
|
|
610
610
|
});
|
|
611
611
|
}
|
package/dist/sync/models.js
CHANGED
|
@@ -196,7 +196,7 @@ var claimSyncSeqs = function (_a) { return __awaiter(void 0, [_a], void 0, funct
|
|
|
196
196
|
},
|
|
197
197
|
},
|
|
198
198
|
{ $unset: "_syncOldSeq" },
|
|
199
|
-
], {
|
|
199
|
+
], { returnDocument: "after", updatePipeline: true, upsert: true })];
|
|
200
200
|
case 1:
|
|
201
201
|
doc = _a.sent();
|
|
202
202
|
return [2 /*return*/, { lastSeq: doc.seq, registered: true, seqs: materialize(doc.seq) }];
|
package/dist/sync/registry.js
CHANGED
|
@@ -75,7 +75,7 @@ var registerSync = function (_a) {
|
|
|
75
75
|
var model = _a.model, routePath = _a.routePath, config = _a.config, options = _a.options;
|
|
76
76
|
var name = model.modelName;
|
|
77
77
|
var deletedPath = model.schema.path("deleted");
|
|
78
|
-
if (
|
|
78
|
+
if ((deletedPath === null || deletedPath === void 0 ? void 0 : deletedPath.instance) !== "Boolean") {
|
|
79
79
|
throw new Error("Model ".concat(name, " has a sync config but no soft delete support. ") +
|
|
80
80
|
"Apply isDeletedPlugin to the schema — sync catch-up requires delete tombstones.");
|
|
81
81
|
}
|
|
@@ -76,7 +76,6 @@ var __read = (this && this.__read) || function (o, n) {
|
|
|
76
76
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
77
77
|
exports.syncPlugin = exports.SCOPE_MOVE_MARKER_ATTEMPTS = void 0;
|
|
78
78
|
var logger_1 = require("../logger");
|
|
79
|
-
var executors_1 = require("./executors");
|
|
80
79
|
var models_1 = require("./models");
|
|
81
80
|
var registry_1 = require("./registry");
|
|
82
81
|
var streams_1 = require("./streams");
|
|
@@ -496,12 +495,16 @@ var syncPlugin = function (schema) {
|
|
|
496
495
|
});
|
|
497
496
|
});
|
|
498
497
|
});
|
|
499
|
-
//
|
|
500
|
-
//
|
|
501
|
-
//
|
|
502
|
-
//
|
|
503
|
-
//
|
|
504
|
-
|
|
498
|
+
// Any save that claimed a seq in pre('save') but then failed (VersionError, E11000,
|
|
499
|
+
// simulated/hook failure, etc.) never stamped that seq. Release it so the stable
|
|
500
|
+
// frontier is not held for the full pending-claim lease.
|
|
501
|
+
//
|
|
502
|
+
// Mongoose 9 / Kareem 3: async error middleware must `throw error` (or return a
|
|
503
|
+
// rejected promise). Calling `next(error)` after an async hook returns a Promise
|
|
504
|
+
// leaves an orphaned rejection that Bun reports as an unhandled error between tests,
|
|
505
|
+
// even when the caller correctly awaited `save().catch(...)`.
|
|
506
|
+
// Keep arity ≥ 3 so Kareem still classifies this as error middleware.
|
|
507
|
+
schema.post("save", function (error, doc, _next) {
|
|
505
508
|
return __awaiter(this, void 0, void 0, function () {
|
|
506
509
|
var locals, pending;
|
|
507
510
|
var _a;
|
|
@@ -513,9 +516,9 @@ var syncPlugin = function (schema) {
|
|
|
513
516
|
if (locals) {
|
|
514
517
|
locals[PENDING_COMMIT_KEY] = undefined;
|
|
515
518
|
}
|
|
516
|
-
if (!(
|
|
519
|
+
if (!(pending === null || pending === void 0 ? void 0 : pending.claim.registered)) return [3 /*break*/, 2];
|
|
517
520
|
return [4 /*yield*/, (0, models_1.releaseSyncSeqs)({ seqs: pending.claim.seqs, stream: pending.currentStream }).catch(function (releaseError) {
|
|
518
|
-
logger_1.logger.error("[sync] Failed to release
|
|
521
|
+
logger_1.logger.error("[sync] Failed to release seq claim after failed save", {
|
|
519
522
|
error: String(releaseError),
|
|
520
523
|
stream: pending.currentStream,
|
|
521
524
|
});
|
|
@@ -523,32 +526,33 @@ var syncPlugin = function (schema) {
|
|
|
523
526
|
case 1:
|
|
524
527
|
_b.sent();
|
|
525
528
|
_b.label = 2;
|
|
526
|
-
case 2:
|
|
527
|
-
next(error);
|
|
528
|
-
return [2 /*return*/];
|
|
529
|
+
case 2: throw error;
|
|
529
530
|
}
|
|
530
531
|
});
|
|
531
532
|
});
|
|
532
533
|
});
|
|
533
|
-
|
|
534
|
+
// Mongoose 9 insertMany pre hooks are async (no `next` callback) and receive docs + options.
|
|
535
|
+
schema.pre("insertMany", function (docs, options) {
|
|
534
536
|
return __awaiter(this, void 0, void 0, function () {
|
|
535
|
-
var model, entry, session, byStream,
|
|
537
|
+
var docsArray, model, entry, session, byStream, docsArray_1, docsArray_1_1, doc, stream, group, _loop_1, byStream_1, byStream_1_1, _a, stream, group, e_1_1;
|
|
536
538
|
var e_2, _b, e_1, _c;
|
|
537
539
|
var _d, _e;
|
|
538
540
|
return __generator(this, function (_f) {
|
|
539
541
|
switch (_f.label) {
|
|
540
542
|
case 0:
|
|
541
|
-
|
|
543
|
+
docsArray = Array.isArray(docs)
|
|
544
|
+
? docs
|
|
545
|
+
: [docs];
|
|
542
546
|
model = this;
|
|
543
547
|
entry = (0, registry_1.findSyncEntryByModelName)(model.modelName);
|
|
544
|
-
if (!entry ||
|
|
545
|
-
return [2 /*return
|
|
548
|
+
if (!entry || docsArray.length === 0) {
|
|
549
|
+
return [2 /*return*/];
|
|
546
550
|
}
|
|
547
551
|
session = (_d = options === null || options === void 0 ? void 0 : options.session) !== null && _d !== void 0 ? _d : null;
|
|
548
552
|
byStream = new Map();
|
|
549
553
|
try {
|
|
550
|
-
for (
|
|
551
|
-
doc =
|
|
554
|
+
for (docsArray_1 = __values(docsArray), docsArray_1_1 = docsArray_1.next(); !docsArray_1_1.done; docsArray_1_1 = docsArray_1.next()) {
|
|
555
|
+
doc = docsArray_1_1.value;
|
|
552
556
|
stream = writableStreamForObject(entry, doc);
|
|
553
557
|
group = (_e = byStream.get(stream)) !== null && _e !== void 0 ? _e : [];
|
|
554
558
|
group.push(doc);
|
|
@@ -558,7 +562,7 @@ var syncPlugin = function (schema) {
|
|
|
558
562
|
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
559
563
|
finally {
|
|
560
564
|
try {
|
|
561
|
-
if (
|
|
565
|
+
if (docsArray_1_1 && !docsArray_1_1.done && (_b = docsArray_1.return)) _b.call(docsArray_1);
|
|
562
566
|
}
|
|
563
567
|
finally { if (e_2) throw e_2.error; }
|
|
564
568
|
}
|
|
@@ -603,15 +607,7 @@ var syncPlugin = function (schema) {
|
|
|
603
607
|
}
|
|
604
608
|
finally { if (e_1) throw e_1.error; }
|
|
605
609
|
return [7 /*endfinally*/];
|
|
606
|
-
case 8:
|
|
607
|
-
// C1: the confirm belongs in post('insertMany') — confirming here would let the
|
|
608
|
-
// stable frontier (and therefore a snapshot cursor) advance past seqs whose
|
|
609
|
-
// documents have not committed yet, stranding them below every catch-up cursor.
|
|
610
|
-
return [2 /*return*/, next()];
|
|
611
|
-
case 9:
|
|
612
|
-
error_4 = _f.sent();
|
|
613
|
-
return [2 /*return*/, next(error_4)];
|
|
614
|
-
case 10: return [2 /*return*/];
|
|
610
|
+
case 8: return [2 /*return*/];
|
|
615
611
|
}
|
|
616
612
|
});
|
|
617
613
|
});
|
package/dist/tests/models.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import mongoose, { type Model
|
|
1
|
+
import mongoose, { type Model } from "mongoose";
|
|
2
2
|
export interface User {
|
|
3
3
|
admin: boolean;
|
|
4
4
|
name?: string;
|
|
@@ -33,14 +33,14 @@ export interface Food {
|
|
|
33
33
|
dateAdded?: string;
|
|
34
34
|
};
|
|
35
35
|
tags: string[];
|
|
36
|
-
eatenBy:
|
|
36
|
+
eatenBy: Array<mongoose.Types.ObjectId | User>;
|
|
37
37
|
lastEatenWith: {
|
|
38
38
|
[name: string]: Date;
|
|
39
39
|
};
|
|
40
40
|
categories: FoodCategory[];
|
|
41
41
|
expiration: string;
|
|
42
42
|
likesIds: {
|
|
43
|
-
userId: string;
|
|
43
|
+
userId: mongoose.Types.ObjectId | string;
|
|
44
44
|
likes: boolean;
|
|
45
45
|
}[];
|
|
46
46
|
}
|
|
@@ -48,24 +48,40 @@ export interface RequiredField {
|
|
|
48
48
|
name: string;
|
|
49
49
|
about?: string;
|
|
50
50
|
}
|
|
51
|
-
export declare const UserModel: mongoose.Model<User, {}, {}, {}, mongoose.Document<unknown, {}, User, {},
|
|
51
|
+
export declare const UserModel: mongoose.Model<User, {}, {}, {}, mongoose.Document<unknown, {}, User, {}, mongoose.DefaultSchemaOptions> & User & {
|
|
52
52
|
_id: mongoose.Types.ObjectId;
|
|
53
53
|
} & {
|
|
54
54
|
__v: number;
|
|
55
|
-
}
|
|
56
|
-
|
|
55
|
+
} & {
|
|
56
|
+
id: string;
|
|
57
|
+
}, any, User>;
|
|
58
|
+
export declare const SuperUserModel: mongoose.Model<User & SuperUser, {}, {}, {
|
|
59
|
+
id: string;
|
|
60
|
+
}, mongoose.Document<unknown, {}, User & SuperUser, {
|
|
61
|
+
id: string;
|
|
62
|
+
}, mongoose.DefaultSchemaOptions> & Omit<User & SuperUser & {
|
|
57
63
|
_id: mongoose.Types.ObjectId;
|
|
58
64
|
} & {
|
|
59
65
|
__v: number;
|
|
60
|
-
},
|
|
61
|
-
|
|
66
|
+
}, "id"> & mongoose.HydratedDocumentOverrides<{
|
|
67
|
+
id: string;
|
|
68
|
+
}>, any, User & SuperUser>;
|
|
69
|
+
export declare const StaffUserModel: mongoose.Model<User & StaffUser, {}, {}, {
|
|
70
|
+
id: string;
|
|
71
|
+
}, mongoose.Document<unknown, {}, User & StaffUser, {
|
|
72
|
+
id: string;
|
|
73
|
+
}, mongoose.DefaultSchemaOptions> & Omit<User & StaffUser & {
|
|
62
74
|
_id: mongoose.Types.ObjectId;
|
|
63
75
|
} & {
|
|
64
76
|
__v: number;
|
|
65
|
-
},
|
|
77
|
+
}, "id"> & mongoose.HydratedDocumentOverrides<{
|
|
78
|
+
id: string;
|
|
79
|
+
}>, any, User & StaffUser>;
|
|
66
80
|
export declare const FoodModel: Model<Food>;
|
|
67
|
-
export declare const RequiredModel: mongoose.Model<RequiredField, {}, {}, {}, mongoose.Document<unknown, {}, RequiredField, {},
|
|
81
|
+
export declare const RequiredModel: mongoose.Model<RequiredField, {}, {}, {}, mongoose.Document<unknown, {}, RequiredField, {}, mongoose.DefaultSchemaOptions> & RequiredField & {
|
|
68
82
|
_id: mongoose.Types.ObjectId;
|
|
69
83
|
} & {
|
|
70
84
|
__v: number;
|
|
71
|
-
}
|
|
85
|
+
} & {
|
|
86
|
+
id: string;
|
|
87
|
+
}, any, RequiredField>;
|
package/dist/tests/models.js
CHANGED
|
@@ -101,9 +101,11 @@ var foodSchema = new mongoose_1.Schema({
|
|
|
101
101
|
type: mongoose_1.Schema.Types.ObjectId,
|
|
102
102
|
},
|
|
103
103
|
],
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
104
|
+
expiration: {
|
|
105
|
+
description: "Expiration date of the food",
|
|
106
|
+
// DateOnly is a custom SchemaType registered on Schema.Types; it stores an ISO date string.
|
|
107
|
+
type: plugins_1.DateOnly,
|
|
108
|
+
},
|
|
107
109
|
hidden: {
|
|
108
110
|
default: false,
|
|
109
111
|
description: "Whether this food is hidden from listings",
|
|
@@ -1,18 +1,24 @@
|
|
|
1
1
|
import type { TestData } from "./types";
|
|
2
2
|
export declare const applyTestAuthEnv: () => void;
|
|
3
3
|
/** Seeds only the standard users (legacy helper). */
|
|
4
|
-
export declare const setupDb: () => Promise<readonly [import("mongoose").Document<unknown, {}, import("./models").User, {},
|
|
4
|
+
export declare const setupDb: () => Promise<readonly [import("mongoose").Document<unknown, {}, import("./models").User, {}, import("mongoose").DefaultSchemaOptions> & import("./models").User & {
|
|
5
5
|
_id: import("mongoose").Types.ObjectId;
|
|
6
6
|
} & {
|
|
7
7
|
__v: number;
|
|
8
|
-
}
|
|
8
|
+
} & {
|
|
9
|
+
id: string;
|
|
10
|
+
}, import("mongoose").Document<unknown, {}, import("./models").User, {}, import("mongoose").DefaultSchemaOptions> & import("./models").User & {
|
|
9
11
|
_id: import("mongoose").Types.ObjectId;
|
|
10
12
|
} & {
|
|
11
13
|
__v: number;
|
|
12
|
-
}
|
|
14
|
+
} & {
|
|
15
|
+
id: string;
|
|
16
|
+
}, import("mongoose").Document<unknown, {}, import("./models").User, {}, import("mongoose").DefaultSchemaOptions> & import("./models").User & {
|
|
13
17
|
_id: import("mongoose").Types.ObjectId;
|
|
14
18
|
} & {
|
|
15
19
|
__v: number;
|
|
20
|
+
} & {
|
|
21
|
+
id: string;
|
|
16
22
|
}]>;
|
|
17
23
|
/** Seeds users, foods, and required docs — the recommended API test baseline. */
|
|
18
24
|
export declare const setupTestData: () => Promise<TestData>;
|
package/package.json
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
"@asteasolutions/zod-to-openapi": "^8.5.0",
|
|
8
8
|
"@sentry/bun": "^10.39.0",
|
|
9
9
|
"@sentry/profiling-node": "^10.25.0",
|
|
10
|
-
"@terreno/test": "56.0.0-beta.
|
|
10
|
+
"@terreno/test": "56.0.0-beta.2",
|
|
11
11
|
"@types/qs": "^6.14.0",
|
|
12
12
|
"ajv": "8.20.0",
|
|
13
13
|
"ajv-formats": "^3.0.1",
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"description": "Styled after the Django & Django REST Framework, a batteries-include framework for building REST APIs with Node/Express/Mongoose.",
|
|
48
48
|
"devDependencies": {
|
|
49
49
|
"@biomejs/biome": "^2.3.6",
|
|
50
|
-
"@terreno/syncdb": "56.0.0-beta.
|
|
50
|
+
"@terreno/syncdb": "56.0.0-beta.2",
|
|
51
51
|
"@types/bcrypt": "^6.0.0",
|
|
52
52
|
"@types/bun": "^1.3.8",
|
|
53
53
|
"@types/cors": "^2.8.17",
|
|
@@ -55,7 +55,6 @@
|
|
|
55
55
|
"@types/express": "^5.0.6",
|
|
56
56
|
"@types/jsonwebtoken": "^9.0.9",
|
|
57
57
|
"@types/lodash": "^4.17.17",
|
|
58
|
-
"@types/mongodb": "^4.0.7",
|
|
59
58
|
"@types/node": "^25.3.0",
|
|
60
59
|
"@types/on-finished": "^2.3.4",
|
|
61
60
|
"@types/passport": "^1.0.17",
|
|
@@ -66,7 +65,7 @@
|
|
|
66
65
|
"@types/sinon": "^21.0.0",
|
|
67
66
|
"@types/supertest": "^7.2.0",
|
|
68
67
|
"mongodb-memory-server": "^11.0.1",
|
|
69
|
-
"mongoose": "
|
|
68
|
+
"mongoose": "9.7.4",
|
|
70
69
|
"sinon": "^22.0.0",
|
|
71
70
|
"supertest": "^7.2.2",
|
|
72
71
|
"typescript": "~6.0.3",
|
|
@@ -79,7 +78,7 @@
|
|
|
79
78
|
"mongoose",
|
|
80
79
|
"express"
|
|
81
80
|
],
|
|
82
|
-
"license": "
|
|
81
|
+
"license": "MIT",
|
|
83
82
|
"main": "dist/index.js",
|
|
84
83
|
"exports": {
|
|
85
84
|
".": {
|
|
@@ -124,5 +123,5 @@
|
|
|
124
123
|
"updateSnapshot": "bun test --update-snapshots"
|
|
125
124
|
},
|
|
126
125
|
"types": "dist/index.d.ts",
|
|
127
|
-
"version": "56.0.0-beta.
|
|
126
|
+
"version": "56.0.0-beta.2"
|
|
128
127
|
}
|
package/src/actions.test.ts
CHANGED
|
@@ -1,22 +1,29 @@
|
|
|
1
|
-
// noExplicitAny: test mock typing
|
|
2
|
-
// biome-ignore-all lint/suspicious/noExplicitAny: test mock typing
|
|
3
1
|
import {beforeEach, describe, expect, it} from "bun:test";
|
|
4
2
|
import type express from "express";
|
|
5
|
-
import {type Model, model, Schema} from "mongoose";
|
|
3
|
+
import {type HydratedDocument, type Model, model, Schema} from "mongoose";
|
|
6
4
|
import supertest from "supertest";
|
|
7
5
|
import type TestAgent from "supertest/lib/agent";
|
|
8
6
|
import {
|
|
9
7
|
ACTION_NAME_PATTERN,
|
|
8
|
+
type CollectionActionConfig,
|
|
10
9
|
createActionOpenApiMiddleware,
|
|
11
10
|
defineCollectionAction,
|
|
12
11
|
defineInstanceAction,
|
|
13
12
|
} from "./actions";
|
|
14
13
|
import {modelRouter, type OpenApiMiddleware} from "./api";
|
|
15
|
-
import {addAuthRoutes, setupAuth} from "./auth";
|
|
14
|
+
import {addAuthRoutes, setupAuth, type UserModel as UserMongooseModel} from "./auth";
|
|
16
15
|
import {apiUnauthorizedMiddleware} from "./errors";
|
|
17
16
|
import {Permissions} from "./permissions";
|
|
18
17
|
import {type IsDeleted, isDeletedPlugin} from "./plugins";
|
|
19
|
-
import {
|
|
18
|
+
import {
|
|
19
|
+
authAsUser,
|
|
20
|
+
type Food,
|
|
21
|
+
FoodModel,
|
|
22
|
+
getBaseServer,
|
|
23
|
+
setupDb,
|
|
24
|
+
type User,
|
|
25
|
+
UserModel,
|
|
26
|
+
} from "./tests";
|
|
20
27
|
import {z} from "./zodOpenApi";
|
|
21
28
|
|
|
22
29
|
interface Stuff extends IsDeleted {
|
|
@@ -49,7 +56,7 @@ describe("modelRouter actions", () => {
|
|
|
49
56
|
broken: {
|
|
50
57
|
handler: async () => ({ok: true}),
|
|
51
58
|
method: "POST",
|
|
52
|
-
} as
|
|
59
|
+
} as unknown as CollectionActionConfig<unknown, unknown, unknown>,
|
|
53
60
|
},
|
|
54
61
|
permissions: allPermissions,
|
|
55
62
|
})
|
|
@@ -142,8 +149,8 @@ describe("modelRouter actions", () => {
|
|
|
142
149
|
describe("integration", () => {
|
|
143
150
|
let app: express.Application;
|
|
144
151
|
let server: TestAgent;
|
|
145
|
-
let admin:
|
|
146
|
-
let notAdmin:
|
|
152
|
+
let admin: HydratedDocument<User>;
|
|
153
|
+
let notAdmin: HydratedDocument<User>;
|
|
147
154
|
let spinach: Food;
|
|
148
155
|
|
|
149
156
|
const mountFoodRouter = (options: Parameters<typeof modelRouter<Food>>[1]): void => {
|
|
@@ -174,8 +181,8 @@ describe("modelRouter actions", () => {
|
|
|
174
181
|
}),
|
|
175
182
|
]);
|
|
176
183
|
app = getBaseServer();
|
|
177
|
-
setupAuth(app, UserModel as
|
|
178
|
-
addAuthRoutes(app, UserModel as
|
|
184
|
+
setupAuth(app, UserModel as unknown as UserMongooseModel);
|
|
185
|
+
addAuthRoutes(app, UserModel as unknown as UserMongooseModel);
|
|
179
186
|
server = supertest(app);
|
|
180
187
|
});
|
|
181
188
|
|
|
@@ -284,8 +291,8 @@ describe("modelRouter actions", () => {
|
|
|
284
291
|
await StuffModel.deleteMany({});
|
|
285
292
|
const doc = await StuffModel.create({deleted: true, name: "hidden", ownerId: "1"});
|
|
286
293
|
app = getBaseServer();
|
|
287
|
-
setupAuth(app, UserModel as
|
|
288
|
-
addAuthRoutes(app, UserModel as
|
|
294
|
+
setupAuth(app, UserModel as unknown as UserMongooseModel);
|
|
295
|
+
addAuthRoutes(app, UserModel as unknown as UserMongooseModel);
|
|
289
296
|
app.use(
|
|
290
297
|
"/stuff",
|
|
291
298
|
modelRouter(StuffModel as Model<Stuff>, {
|
package/src/actions.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {OpenAPIRegistry, OpenApiGeneratorV3} from "@asteasolutions/zod-to-openapi";
|
|
2
2
|
import type express from "express";
|
|
3
3
|
import type {NextFunction, Request, Response} from "express";
|
|
4
|
-
import type {Model} from "mongoose";
|
|
4
|
+
import type {Model, SchemaType} from "mongoose";
|
|
5
5
|
import type {ZodSchema, ZodType} from "zod";
|
|
6
6
|
import {asyncHandler, type ModelRouterOptions, type RESTMethod} from "./api";
|
|
7
7
|
import {authenticateMiddleware, type User} from "./auth";
|
|
@@ -282,7 +282,7 @@ export const createActionOpenApiMiddleware = <T>({
|
|
|
282
282
|
};
|
|
283
283
|
|
|
284
284
|
const getArrayFieldNames = <T>(model: Model<T>): string[] => {
|
|
285
|
-
return Object.values(model.schema.paths)
|
|
285
|
+
return (Object.values(model.schema.paths) as SchemaType[])
|
|
286
286
|
.filter((config) => config.instance === "Array")
|
|
287
287
|
.map((config) => config.path);
|
|
288
288
|
};
|