@terreno/api 57.2.0 → 57.3.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 +17 -7
- package/dist/actions.js +19 -7
- package/dist/api.d.ts +4 -0
- package/dist/api.js +12 -24
- package/dist/auth.d.ts +23 -10
- package/dist/auth.js +97 -32
- package/dist/authRecovery.d.ts +5 -0
- package/dist/authRecovery.js +350 -0
- package/dist/authTokens.d.ts +8 -0
- package/dist/authTokens.js +162 -0
- package/dist/betterAuth.d.ts +33 -0
- package/dist/betterAuthApp.d.ts +4 -0
- package/dist/betterAuthApp.js +26 -8
- package/dist/betterAuthSetup.d.ts +44 -2
- package/dist/betterAuthSetup.js +288 -18
- package/dist/collectionRegistry.d.ts +38 -0
- package/dist/collectionRegistry.js +72 -0
- package/dist/configurationApp.d.ts +11 -2
- package/dist/configurationApp.js +2 -4
- package/dist/docLoader.js +35 -6
- package/dist/expressServer.d.ts +26 -3
- package/dist/expressServer.js +79 -11
- package/dist/index.d.ts +16 -0
- package/dist/index.js +38 -2
- package/dist/mcp/auth.d.ts +1 -0
- package/dist/mcp/auth.js +47 -25
- package/dist/mcp/handlers.d.ts +1 -1
- package/dist/mcp/handlers.js +67 -233
- package/dist/mcp/query.d.ts +2 -2
- package/dist/mcp/registry.d.ts +1 -5
- package/dist/mcp/registry.js +31 -20
- package/dist/mcp/schemaGenerator.d.ts +2 -7
- package/dist/mcp/schemaGenerator.js +41 -116
- package/dist/mcp/server.d.ts +1 -0
- package/dist/mcp/server.js +1 -0
- package/dist/mcp/serviceTokens.d.ts +16 -0
- package/dist/mcp/serviceTokens.js +345 -0
- package/dist/mcp/types.d.ts +2 -2
- package/dist/models/mcpServiceToken.d.ts +3 -0
- package/dist/models/mcpServiceToken.js +208 -0
- package/dist/openApiBuilder.d.ts +5 -4
- package/dist/openApiBuilder.js +61 -8
- package/dist/openApiCompat.js +1 -1
- package/dist/openApiValidator.js +0 -2
- package/dist/plugins.d.ts +14 -1
- package/dist/plugins.js +17 -6
- package/dist/populate.d.ts +2 -2
- package/dist/populate.js +47 -42
- package/dist/rateLimit/applyTrustProxy.d.ts +3 -0
- package/dist/rateLimit/applyTrustProxy.js +17 -0
- package/dist/rateLimit/createStore.d.ts +2 -0
- package/dist/rateLimit/createStore.js +28 -0
- package/dist/rateLimit/memoryStore.d.ts +2 -0
- package/dist/rateLimit/memoryStore.js +108 -0
- package/dist/rateLimit/middleware.d.ts +11 -0
- package/dist/rateLimit/middleware.js +110 -0
- package/dist/rateLimit/mongoStore.d.ts +3 -0
- package/dist/rateLimit/mongoStore.js +122 -0
- package/dist/rateLimit/policies.d.ts +14 -0
- package/dist/rateLimit/policies.js +151 -0
- package/dist/rateLimit/redisStore.d.ts +3 -0
- package/dist/rateLimit/redisStore.js +155 -0
- package/dist/rateLimit/types.d.ts +54 -0
- package/dist/rateLimit/types.js +9 -0
- package/dist/rbac/access.js +5 -1
- package/dist/rbac/assertAllowed.d.ts +9 -0
- package/dist/rbac/assertAllowed.js +182 -0
- package/dist/rbac/modelRouterAccess.d.ts +1 -0
- package/dist/rbac/modelRouterAccess.js +8 -4
- package/dist/rbac/permissionUtils.js +3 -2
- package/dist/rbac/resolve.js +5 -4
- package/dist/rbac/routes.js +15 -6
- package/dist/realtime/deprecation.d.ts +12 -0
- package/dist/realtime/deprecation.js +32 -0
- package/dist/realtime/realtimeApp.d.ts +1 -1
- package/dist/realtime/realtimeApp.js +10 -11
- package/dist/realtime/registry.d.ts +0 -31
- package/dist/realtime/registry.js +44 -31
- package/dist/realtime/socketAuth.d.ts +5 -5
- package/dist/realtime/socketAuth.js +11 -1
- package/dist/realtime/socketUser.d.ts +5 -1
- package/dist/realtime/types.d.ts +14 -4
- package/dist/schemaMetadata.d.ts +56 -0
- package/dist/schemaMetadata.js +513 -0
- package/dist/seedRunner.d.ts +88 -0
- package/dist/seedRunner.js +627 -0
- package/dist/sync/executors.d.ts +12 -2
- package/dist/sync/executors.js +8 -10
- package/dist/sync/loadHarness.js +28 -19
- package/dist/sync/models.js +26 -24
- package/dist/sync/mutationHandler.d.ts +9 -0
- package/dist/sync/mutationHandler.js +11 -6
- package/dist/sync/registrationSideEffects.d.ts +35 -0
- package/dist/sync/registrationSideEffects.js +151 -0
- package/dist/sync/registry.d.ts +2 -52
- package/dist/sync/registry.js +38 -187
- package/dist/sync/routes.js +3 -2
- package/dist/sync/scripts/compactTombstones.js +0 -2
- package/dist/sync/syncApp.d.ts +3 -3
- package/dist/sync/syncApp.js +4 -4
- package/dist/sync/syncBatch.js +3 -2
- package/dist/sync/syncSeqPlugin.d.ts +1 -1
- package/dist/sync/syncSeqPlugin.js +20 -16
- package/dist/terrenoApp.d.ts +19 -5
- package/dist/terrenoApp.js +66 -18
- package/dist/tests/models.d.ts +2 -0
- package/dist/tests/models.js +6 -0
- package/dist/types/authToken.d.ts +33 -0
- package/dist/types/authToken.js +2 -0
- package/dist/types/mcpServiceToken.d.ts +41 -0
- package/dist/types/mcpServiceToken.js +2 -0
- package/dist/webhooks/headerValue.d.ts +2 -0
- package/dist/webhooks/headerValue.js +14 -0
- package/dist/webhooks/idempotency/memoryStore.d.ts +10 -0
- package/dist/webhooks/idempotency/memoryStore.js +65 -0
- package/dist/webhooks/idempotency/mongoStore.d.ts +4 -0
- package/dist/webhooks/idempotency/mongoStore.js +150 -0
- package/dist/webhooks/idempotency/webhookReceipt.d.ts +62 -0
- package/dist/webhooks/idempotency/webhookReceipt.js +39 -0
- package/dist/webhooks/rawBody.d.ts +14 -0
- package/dist/webhooks/rawBody.js +22 -0
- package/dist/webhooks/timingSafeEqual.d.ts +4 -0
- package/dist/webhooks/timingSafeEqual.js +17 -0
- package/dist/webhooks/verifiers/hmac.d.ts +15 -0
- package/dist/webhooks/verifiers/hmac.js +49 -0
- package/dist/webhooks/verifiers/sendgrid.d.ts +10 -0
- package/dist/webhooks/verifiers/sendgrid.js +56 -0
- package/dist/webhooks/verifiers/stripe.d.ts +9 -0
- package/dist/webhooks/verifiers/stripe.js +99 -0
- package/dist/webhooks/verifiers/twilio.d.ts +9 -0
- package/dist/webhooks/verifiers/twilio.js +112 -0
- package/dist/webhooks/webhooksApp.d.ts +33 -0
- package/dist/webhooks/webhooksApp.js +195 -0
- package/package.json +4 -5
- package/src/__snapshots__/openApi.test.ts.snap +270 -0
- package/src/__snapshots__/openApiBuilder.test.ts.snap +30 -12
- package/src/__snapshots__/schemaMetadata.test.ts.snap +197 -0
- package/src/actions.ts +20 -13
- package/src/adminModelRouter.test.ts +133 -0
- package/src/api.arrayOperations.test.ts +43 -31
- package/src/api.hooks.test.ts +38 -29
- package/src/api.query.test.ts +31 -31
- package/src/api.test.ts +95 -96
- package/src/api.ts +16 -27
- package/src/auth.test.ts +107 -51
- package/src/auth.ts +98 -29
- package/src/authRecovery.test.ts +281 -0
- package/src/authRecovery.ts +279 -0
- package/src/authTokens.test.ts +94 -0
- package/src/authTokens.ts +118 -0
- package/src/authVerification.test.ts +293 -0
- package/src/betterAuth.ts +24 -0
- package/src/betterAuthApp.test.ts +1 -0
- package/src/betterAuthApp.ts +27 -9
- package/src/betterAuthSetup.test.ts +568 -38
- package/src/betterAuthSetup.ts +260 -5
- package/src/collectionRegistry.test.ts +165 -0
- package/src/collectionRegistry.ts +115 -0
- package/src/configurationApp.ts +13 -7
- package/src/docLoader.test.ts +45 -1
- package/src/docLoader.ts +37 -6
- package/src/express.d.ts +1 -0
- package/src/expressServer.test.ts +130 -66
- package/src/expressServer.ts +75 -19
- package/src/githubAuth.test.ts +60 -55
- package/src/index.ts +58 -0
- package/src/mcp/auth.test.ts +47 -0
- package/src/mcp/auth.ts +32 -11
- package/src/mcp/handlers.ts +45 -172
- package/src/mcp/integration.test.ts +59 -9
- package/src/mcp/query.ts +2 -4
- package/src/mcp/registry.ts +29 -20
- package/src/mcp/schemaGenerator.test.ts +8 -0
- package/src/mcp/schemaGenerator.ts +49 -131
- package/src/mcp/server.ts +2 -0
- package/src/mcp/serviceTokens.test.ts +330 -0
- package/src/mcp/serviceTokens.ts +307 -0
- package/src/mcp/toolGenerator.test.ts +1 -1
- package/src/mcp/types.ts +2 -6
- package/src/models/mcpServiceToken.test.ts +129 -0
- package/src/models/mcpServiceToken.ts +158 -0
- package/src/openApi.test.ts +80 -64
- package/src/openApiBuilder.test.ts +95 -7
- package/src/openApiBuilder.ts +46 -9
- package/src/openApiCompat.ts +1 -1
- package/src/openApiValidator.test.ts +49 -54
- package/src/openApiValidator.ts +1 -3
- package/src/plugins.test.ts +14 -0
- package/src/plugins.ts +34 -9
- package/src/populate.test.ts +34 -0
- package/src/populate.ts +40 -29
- package/src/rateLimit/applyTrustProxy.ts +17 -0
- package/src/rateLimit/createStore.ts +25 -0
- package/src/rateLimit/memoryStore.ts +40 -0
- package/src/rateLimit/middleware.ts +79 -0
- package/src/rateLimit/mongoStore.test.ts +80 -0
- package/src/rateLimit/mongoStore.ts +76 -0
- package/src/rateLimit/policies.ts +130 -0
- package/src/rateLimit/rateLimit.test.ts +328 -0
- package/src/rateLimit/redisStore.ioredis.test.ts +29 -0
- package/src/rateLimit/redisStore.test.ts +114 -0
- package/src/rateLimit/redisStore.ts +85 -0
- package/src/rateLimit/types.ts +64 -0
- package/src/rbac/access.ts +5 -1
- package/src/rbac/assertAllowed.test.ts +325 -0
- package/src/rbac/assertAllowed.ts +157 -0
- package/src/rbac/backfillAdmins.test.ts +124 -0
- package/src/rbac/fieldViews.test.ts +89 -0
- package/src/rbac/modelRouterAccess.test.ts +126 -0
- package/src/rbac/modelRouterAccess.ts +5 -2
- package/src/rbac/permissionUtils.ts +3 -2
- package/src/rbac/realtimeAccess.test.ts +109 -1
- package/src/rbac/resolve.ts +5 -4
- package/src/rbac/roleManager.test.ts +169 -0
- package/src/rbac/routes.test.ts +233 -0
- package/src/rbac/routes.ts +16 -6
- package/src/realtime/deprecation.test.ts +60 -0
- package/src/realtime/deprecation.ts +32 -0
- package/src/realtime/realtime.test.ts +8 -4
- package/src/realtime/realtimeApp.ts +10 -11
- package/src/realtime/realtimeAppSetup.test.ts +32 -5
- package/src/realtime/registry.ts +48 -42
- package/src/realtime/sessionRevalidation.test.ts +57 -44
- package/src/realtime/socketAuth.test.ts +75 -23
- package/src/realtime/socketAuth.ts +17 -6
- package/src/realtime/socketUser.ts +5 -3
- package/src/realtime/types.ts +14 -4
- package/src/schemaMetadata.test.ts +405 -0
- package/src/schemaMetadata.ts +593 -0
- package/src/scrubAdminFields.test.ts +105 -0
- package/src/seedRunner.test.ts +591 -0
- package/src/seedRunner.ts +464 -0
- package/src/sync/docMatchesMutationData.test.ts +16 -0
- package/src/sync/ensureSyncIndexes.test.ts +54 -15
- package/src/sync/executors.test.ts +40 -0
- package/src/sync/executors.ts +24 -12
- package/src/sync/integration.test.ts +15 -7
- package/src/sync/loadHarness.ts +28 -19
- package/src/sync/models.ts +32 -18
- package/src/sync/mutationHandler.test.ts +16 -16
- package/src/sync/mutationHandler.ts +9 -7
- package/src/sync/registrationSideEffects.test.ts +126 -0
- package/src/sync/registrationSideEffects.ts +131 -0
- package/src/sync/registry.ts +45 -171
- package/src/sync/routes.ts +3 -2
- package/src/sync/scripts/compactTombstones.ts +0 -2
- package/src/sync/sessionRevalidationIntegration.test.ts +12 -11
- package/src/sync/sync.test.ts +25 -23
- package/src/sync/syncApp.ts +4 -4
- package/src/sync/syncBatch.ts +3 -2
- package/src/sync/syncFrontier.test.ts +17 -15
- package/src/sync/syncPhaseC.test.ts +54 -43
- package/src/sync/syncRoutes.test.ts +51 -36
- package/src/sync/syncSeqPlugin.ts +66 -44
- package/src/sync/syncSocket.test.ts +112 -81
- package/src/terrenoApp.mcpServiceTokens.test.ts +173 -0
- package/src/terrenoApp.ts +87 -15
- package/src/tests/models.ts +14 -1
- package/src/types/authToken.ts +43 -0
- package/src/types/mcpServiceToken.ts +54 -0
- package/src/webhooks/headerValue.test.ts +22 -0
- package/src/webhooks/headerValue.ts +12 -0
- package/src/webhooks/idempotency/memoryStore.ts +34 -0
- package/src/webhooks/idempotency/mongoStore.test.ts +274 -0
- package/src/webhooks/idempotency/mongoStore.ts +83 -0
- package/src/webhooks/idempotency/webhookReceipt.ts +70 -0
- package/src/webhooks/rawBody.test.ts +74 -0
- package/src/webhooks/rawBody.ts +23 -0
- package/src/webhooks/timingSafeEqual.ts +16 -0
- package/src/webhooks/verifiers/hmac.test.ts +110 -0
- package/src/webhooks/verifiers/hmac.ts +54 -0
- package/src/webhooks/verifiers/providerVerifiers.test.ts +388 -0
- package/src/webhooks/verifiers/sendgrid.ts +59 -0
- package/src/webhooks/verifiers/stripe.ts +67 -0
- package/src/webhooks/verifiers/twilio.ts +69 -0
- package/src/webhooks/webhooksApp.test.ts +221 -0
- package/src/webhooks/webhooksApp.ts +144 -0
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
// noExplicitAny: test mock typing
|
|
2
|
-
// biome-ignore-all lint/suspicious/noExplicitAny: test mock typing
|
|
3
1
|
import {afterEach, beforeEach, describe, expect, it} from "bun:test";
|
|
4
2
|
import type {ErrorObject} from "ajv";
|
|
5
3
|
import type {NextFunction, Request, Response} from "express";
|
|
6
4
|
|
|
7
5
|
import {modelRouter} from "./api";
|
|
8
|
-
import {addAuthRoutes, setupAuth} from "./auth";
|
|
6
|
+
import {type UserModel as AuthUserModel, addAuthRoutes, setupAuth} from "./auth";
|
|
9
7
|
import type {OpenApiSchemaProperty} from "./openApiBuilder";
|
|
10
8
|
import {
|
|
11
9
|
buildQuerySchemaFromFields,
|
|
@@ -40,8 +38,8 @@ const requiredRouterOptions = {
|
|
|
40
38
|
|
|
41
39
|
const setupFreshApp = async () => {
|
|
42
40
|
const freshApp = getBaseServer();
|
|
43
|
-
setupAuth(freshApp, UserModel as
|
|
44
|
-
addAuthRoutes(freshApp, UserModel as
|
|
41
|
+
setupAuth(freshApp, UserModel as unknown as AuthUserModel);
|
|
42
|
+
addAuthRoutes(freshApp, UserModel as unknown as AuthUserModel);
|
|
45
43
|
return freshApp;
|
|
46
44
|
};
|
|
47
45
|
|
|
@@ -283,8 +281,8 @@ describe("openApiValidator", () => {
|
|
|
283
281
|
});
|
|
284
282
|
|
|
285
283
|
let nextCalled = false;
|
|
286
|
-
const req = {body: {}} as
|
|
287
|
-
const res = {} as
|
|
284
|
+
const req = {body: {}} as unknown as Request;
|
|
285
|
+
const res = {} as Response;
|
|
288
286
|
const next = () => {
|
|
289
287
|
nextCalled = true;
|
|
290
288
|
};
|
|
@@ -300,8 +298,8 @@ describe("openApiValidator", () => {
|
|
|
300
298
|
name: {required: true, type: "string"},
|
|
301
299
|
});
|
|
302
300
|
|
|
303
|
-
const req = {body: {}, method: "POST", path: "/test"} as
|
|
304
|
-
const res = {} as
|
|
301
|
+
const req = {body: {}, method: "POST", path: "/test"} as unknown as Request;
|
|
302
|
+
const res = {} as Response;
|
|
305
303
|
|
|
306
304
|
expect(() => {
|
|
307
305
|
middleware(req, res, () => {});
|
|
@@ -322,8 +320,8 @@ describe("openApiValidator", () => {
|
|
|
322
320
|
);
|
|
323
321
|
|
|
324
322
|
let nextCalled = false;
|
|
325
|
-
const req = {body: {}, method: "POST", path: "/test"} as
|
|
326
|
-
const res = {} as
|
|
323
|
+
const req = {body: {}, method: "POST", path: "/test"} as unknown as Request;
|
|
324
|
+
const res = {} as Response;
|
|
327
325
|
middleware(req, res, () => {
|
|
328
326
|
nextCalled = true;
|
|
329
327
|
});
|
|
@@ -343,8 +341,8 @@ describe("openApiValidator", () => {
|
|
|
343
341
|
const middleware = validateRequestBody({name: {required: true, type: "string"}});
|
|
344
342
|
|
|
345
343
|
let nextCalled = false;
|
|
346
|
-
const req = {body: {}, method: "POST", path: "/test"} as
|
|
347
|
-
const res = {} as
|
|
344
|
+
const req = {body: {}, method: "POST", path: "/test"} as unknown as Request;
|
|
345
|
+
const res = {} as Response;
|
|
348
346
|
middleware(req, res, () => {
|
|
349
347
|
nextCalled = true;
|
|
350
348
|
});
|
|
@@ -362,8 +360,8 @@ describe("openApiValidator", () => {
|
|
|
362
360
|
);
|
|
363
361
|
|
|
364
362
|
let nextCalled = false;
|
|
365
|
-
const req = {body: {}, method: "POST", path: "/test"} as
|
|
366
|
-
const res = {} as
|
|
363
|
+
const req = {body: {}, method: "POST", path: "/test"} as unknown as Request;
|
|
364
|
+
const res = {} as Response;
|
|
367
365
|
middleware(req, res, () => {
|
|
368
366
|
nextCalled = true;
|
|
369
367
|
});
|
|
@@ -377,8 +375,8 @@ describe("openApiValidator", () => {
|
|
|
377
375
|
const middleware = validateRequestBody({name: {required: true, type: "string"}});
|
|
378
376
|
|
|
379
377
|
let nextCalled = false;
|
|
380
|
-
const req = {body: {}, method: "POST", path: "/test"} as
|
|
381
|
-
const res = {} as
|
|
378
|
+
const req = {body: {}, method: "POST", path: "/test"} as unknown as Request;
|
|
379
|
+
const res = {} as Response;
|
|
382
380
|
middleware(req, res, () => {
|
|
383
381
|
nextCalled = true;
|
|
384
382
|
});
|
|
@@ -396,8 +394,8 @@ describe("openApiValidator", () => {
|
|
|
396
394
|
});
|
|
397
395
|
|
|
398
396
|
let nextCalled = false;
|
|
399
|
-
const req = {method: "GET", path: "/test", query: {}} as
|
|
400
|
-
const res = {} as
|
|
397
|
+
const req = {method: "GET", path: "/test", query: {}} as unknown as Request;
|
|
398
|
+
const res = {} as Response;
|
|
401
399
|
middleware(req, res, () => {
|
|
402
400
|
nextCalled = true;
|
|
403
401
|
});
|
|
@@ -412,8 +410,8 @@ describe("openApiValidator", () => {
|
|
|
412
410
|
page: {type: "number"},
|
|
413
411
|
});
|
|
414
412
|
|
|
415
|
-
const req = {method: "GET", path: "/test", query: {page: "3"}} as
|
|
416
|
-
const res = {} as
|
|
413
|
+
const req = {method: "GET", path: "/test", query: {page: "3"}} as unknown as Request;
|
|
414
|
+
const res = {} as Response;
|
|
417
415
|
middleware(req, res, () => {});
|
|
418
416
|
|
|
419
417
|
expect(req.query.page).toBe(3);
|
|
@@ -426,8 +424,8 @@ describe("openApiValidator", () => {
|
|
|
426
424
|
page: {required: true, type: "number"},
|
|
427
425
|
});
|
|
428
426
|
|
|
429
|
-
const req = {method: "GET", path: "/test", query: {}} as
|
|
430
|
-
const res = {} as
|
|
427
|
+
const req = {method: "GET", path: "/test", query: {}} as unknown as Request;
|
|
428
|
+
const res = {} as Response;
|
|
431
429
|
// Required top-level property missing triggers an error
|
|
432
430
|
// We need required: [] at schema level via required: true on property. Confirm via calling.
|
|
433
431
|
expect(() => {
|
|
@@ -436,7 +434,7 @@ describe("openApiValidator", () => {
|
|
|
436
434
|
});
|
|
437
435
|
|
|
438
436
|
it("uses onError callback for query validation", () => {
|
|
439
|
-
let captured:
|
|
437
|
+
let captured: ErrorObject[] = [];
|
|
440
438
|
configureOpenApiValidator({coerceTypes: false});
|
|
441
439
|
|
|
442
440
|
const middleware = validateQueryParams(
|
|
@@ -449,8 +447,8 @@ describe("openApiValidator", () => {
|
|
|
449
447
|
);
|
|
450
448
|
|
|
451
449
|
let nextCalled = false;
|
|
452
|
-
const req = {method: "GET", path: "/test", query: {}} as
|
|
453
|
-
const res = {} as
|
|
450
|
+
const req = {method: "GET", path: "/test", query: {}} as unknown as Request;
|
|
451
|
+
const res = {} as Response;
|
|
454
452
|
middleware(req, res, () => {
|
|
455
453
|
nextCalled = true;
|
|
456
454
|
});
|
|
@@ -468,8 +466,8 @@ describe("openApiValidator", () => {
|
|
|
468
466
|
);
|
|
469
467
|
|
|
470
468
|
let nextCalled = false;
|
|
471
|
-
const req = {method: "GET", path: "/test", query: {}} as
|
|
472
|
-
const res = {} as
|
|
469
|
+
const req = {method: "GET", path: "/test", query: {}} as unknown as Request;
|
|
470
|
+
const res = {} as Response;
|
|
473
471
|
middleware(req, res, () => {
|
|
474
472
|
nextCalled = true;
|
|
475
473
|
});
|
|
@@ -496,10 +494,7 @@ describe("openApiValidator", () => {
|
|
|
496
494
|
|
|
497
495
|
it("returns errors for invalid response shape", () => {
|
|
498
496
|
configureOpenApiValidator({coerceTypes: false, validateResponses: true});
|
|
499
|
-
const result = validateResponseData(
|
|
500
|
-
{name: 42 as any},
|
|
501
|
-
{name: {required: true, type: "string"}}
|
|
502
|
-
);
|
|
497
|
+
const result = validateResponseData({name: 42}, {name: {required: true, type: "string"}});
|
|
503
498
|
expect(result.valid).toBe(false);
|
|
504
499
|
expect(result.errors).toBeDefined();
|
|
505
500
|
});
|
|
@@ -520,8 +515,8 @@ describe("openApiValidator", () => {
|
|
|
520
515
|
method: "POST",
|
|
521
516
|
path: "/test",
|
|
522
517
|
query: {page: "2"},
|
|
523
|
-
} as
|
|
524
|
-
const res = {} as
|
|
518
|
+
} as unknown as Request;
|
|
519
|
+
const res = {} as Response;
|
|
525
520
|
middleware(req, res, () => {
|
|
526
521
|
nextCalled = true;
|
|
527
522
|
});
|
|
@@ -536,8 +531,8 @@ describe("openApiValidator", () => {
|
|
|
536
531
|
const middleware = createValidator({});
|
|
537
532
|
|
|
538
533
|
let nextCalled = false;
|
|
539
|
-
const req = {body: {}, method: "POST", path: "/test", query: {}} as
|
|
540
|
-
const res = {} as
|
|
534
|
+
const req = {body: {}, method: "POST", path: "/test", query: {}} as unknown as Request;
|
|
535
|
+
const res = {} as Response;
|
|
541
536
|
middleware(req, res, () => {
|
|
542
537
|
nextCalled = true;
|
|
543
538
|
});
|
|
@@ -553,8 +548,8 @@ describe("openApiValidator", () => {
|
|
|
553
548
|
});
|
|
554
549
|
|
|
555
550
|
let nextCalled = false;
|
|
556
|
-
const req = {method: "GET", path: "/test", query: {page: "5"}} as
|
|
557
|
-
const res = {} as
|
|
551
|
+
const req = {method: "GET", path: "/test", query: {page: "5"}} as unknown as Request;
|
|
552
|
+
const res = {} as Response;
|
|
558
553
|
middleware(req, res, () => {
|
|
559
554
|
nextCalled = true;
|
|
560
555
|
});
|
|
@@ -617,8 +612,8 @@ describe("openApiValidator", () => {
|
|
|
617
612
|
query: {page: {type: "number"}},
|
|
618
613
|
});
|
|
619
614
|
|
|
620
|
-
const req = {body: {}, method: "POST", path: "/test", query: {}} as
|
|
621
|
-
const res = {} as
|
|
615
|
+
const req = {body: {}, method: "POST", path: "/test", query: {}} as unknown as Request;
|
|
616
|
+
const res = {} as Response;
|
|
622
617
|
|
|
623
618
|
let nextCalled = false;
|
|
624
619
|
middleware(req, res, () => {
|
|
@@ -645,8 +640,8 @@ describe("openApiValidator", () => {
|
|
|
645
640
|
configureOpenApiValidator({coerceTypes: false});
|
|
646
641
|
const {create} = createModelValidators(RequiredModel);
|
|
647
642
|
|
|
648
|
-
const req = {body: {name: 123}, method: "POST", path: "/required"} as
|
|
649
|
-
const res = {} as
|
|
643
|
+
const req = {body: {name: 123}, method: "POST", path: "/required"} as unknown as Request;
|
|
644
|
+
const res = {} as Response;
|
|
650
645
|
expect(() => {
|
|
651
646
|
create(req, res, () => {});
|
|
652
647
|
}).toThrow();
|
|
@@ -656,8 +651,8 @@ describe("openApiValidator", () => {
|
|
|
656
651
|
const {update} = createModelValidators(RequiredModel);
|
|
657
652
|
|
|
658
653
|
let nextCalled = false;
|
|
659
|
-
const req = {body: {about: "info"}, method: "PATCH", path: "/required"} as
|
|
660
|
-
const res = {} as
|
|
654
|
+
const req = {body: {about: "info"}, method: "PATCH", path: "/required"} as unknown as Request;
|
|
655
|
+
const res = {} as Response;
|
|
661
656
|
update(req, res, () => {
|
|
662
657
|
nextCalled = true;
|
|
663
658
|
});
|
|
@@ -679,14 +674,14 @@ describe("openApiValidator", () => {
|
|
|
679
674
|
body: {extra: "strip me", name: "Apple"},
|
|
680
675
|
method: "POST",
|
|
681
676
|
path: "/required",
|
|
682
|
-
} as
|
|
683
|
-
create(req, {} as
|
|
677
|
+
} as unknown as Request;
|
|
678
|
+
create(req, {} as Response, () => {});
|
|
684
679
|
expect(removedProps).toContain("extra");
|
|
685
680
|
});
|
|
686
681
|
|
|
687
682
|
it("supports onError option", () => {
|
|
688
683
|
configureOpenApiValidator({coerceTypes: false});
|
|
689
|
-
let errorHandled:
|
|
684
|
+
let errorHandled: ErrorObject[] = [];
|
|
690
685
|
const {create} = createModelValidators(RequiredModel, {
|
|
691
686
|
onError: (errors) => {
|
|
692
687
|
errorHandled = errors;
|
|
@@ -694,8 +689,8 @@ describe("openApiValidator", () => {
|
|
|
694
689
|
});
|
|
695
690
|
|
|
696
691
|
// Wrong type triggers error handler
|
|
697
|
-
const req = {body: {name: 42}, method: "POST", path: "/required"} as
|
|
698
|
-
create(req, {} as
|
|
692
|
+
const req = {body: {name: 42}, method: "POST", path: "/required"} as unknown as Request;
|
|
693
|
+
create(req, {} as Response, () => {});
|
|
699
694
|
expect(errorHandled.length).toBeGreaterThan(0);
|
|
700
695
|
});
|
|
701
696
|
});
|
|
@@ -708,8 +703,8 @@ describe("openApiValidator", () => {
|
|
|
708
703
|
it("creates a validator from a Mongoose model", () => {
|
|
709
704
|
const middleware = validateModelRequestBody(RequiredModel);
|
|
710
705
|
|
|
711
|
-
const req = {body: {}, method: "POST", path: "/required"} as
|
|
712
|
-
const res = {} as
|
|
706
|
+
const req = {body: {}, method: "POST", path: "/required"} as unknown as Request;
|
|
707
|
+
const res = {} as Response;
|
|
713
708
|
expect(() => {
|
|
714
709
|
middleware(req, res, () => {});
|
|
715
710
|
}).toThrow();
|
|
@@ -722,8 +717,8 @@ describe("openApiValidator", () => {
|
|
|
722
717
|
|
|
723
718
|
// Without 'name' required, empty body passes
|
|
724
719
|
let nextCalled = false;
|
|
725
|
-
const req = {body: {}, method: "POST", path: "/required"} as
|
|
726
|
-
const res = {} as
|
|
720
|
+
const req = {body: {}, method: "POST", path: "/required"} as unknown as Request;
|
|
721
|
+
const res = {} as Response;
|
|
727
722
|
middleware(req, res, () => {
|
|
728
723
|
nextCalled = true;
|
|
729
724
|
});
|
|
@@ -747,7 +742,7 @@ describe("openApiValidator", () => {
|
|
|
747
742
|
expect(config.removeAdditional).toBe(false);
|
|
748
743
|
|
|
749
744
|
// Mutating the returned copy should not affect internal state
|
|
750
|
-
|
|
745
|
+
config.removeAdditional = true;
|
|
751
746
|
expect(getOpenApiValidatorConfig().removeAdditional).toBe(false);
|
|
752
747
|
});
|
|
753
748
|
});
|
package/src/openApiValidator.ts
CHANGED
|
@@ -184,9 +184,7 @@ const getAjvInstance = (): Ajv => {
|
|
|
184
184
|
useDefaults: true,
|
|
185
185
|
validateSchema: false,
|
|
186
186
|
});
|
|
187
|
-
|
|
188
|
-
// biome-ignore lint/suspicious/noExplicitAny: ajv-formats has a known type compat issue with AJV instances
|
|
189
|
-
addFormats(instance as any);
|
|
187
|
+
addFormats(instance as unknown as Parameters<typeof addFormats>[0]);
|
|
190
188
|
ajvCache.set(key, instance);
|
|
191
189
|
}
|
|
192
190
|
|
package/src/plugins.test.ts
CHANGED
|
@@ -19,6 +19,7 @@ import {
|
|
|
19
19
|
baseUserPlugin,
|
|
20
20
|
createdUpdatedPlugin,
|
|
21
21
|
DateOnly,
|
|
22
|
+
emailVerificationPlugin,
|
|
22
23
|
excludeArchivedPlugin,
|
|
23
24
|
findExactlyOne,
|
|
24
25
|
findOneOrNone,
|
|
@@ -94,6 +95,19 @@ describe("baseUserPlugin", () => {
|
|
|
94
95
|
});
|
|
95
96
|
});
|
|
96
97
|
|
|
98
|
+
describe("emailVerificationPlugin", () => {
|
|
99
|
+
it("adds emailVerified defaulting to false", () => {
|
|
100
|
+
const testSchema = new Schema({});
|
|
101
|
+
emailVerificationPlugin(testSchema);
|
|
102
|
+
|
|
103
|
+
const emailVerifiedPath = testSchema.path("emailVerified");
|
|
104
|
+
expect(emailVerifiedPath).toBeDefined();
|
|
105
|
+
expect((emailVerifiedPath as unknown as {options: {default: boolean}}).options.default).toBe(
|
|
106
|
+
false
|
|
107
|
+
);
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
|
|
97
111
|
describe("firebaseJWTPlugin", () => {
|
|
98
112
|
it("adds firebaseId field to the schema", () => {
|
|
99
113
|
const testSchema = new Schema({});
|
package/src/plugins.ts
CHANGED
|
@@ -42,6 +42,24 @@ export const baseUserPlugin = (schema: Schema<any, any, any, any>): void => {
|
|
|
42
42
|
schema.add({email: {description: "The user's email address", index: true, type: String}});
|
|
43
43
|
};
|
|
44
44
|
|
|
45
|
+
export interface EmailVerified {
|
|
46
|
+
emailVerified: boolean;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Opt-in `emailVerified` field for user schemas. Defaults to false so existing
|
|
51
|
+
* documents stay unverified until they complete the email-verification flow.
|
|
52
|
+
*/
|
|
53
|
+
export const emailVerificationPlugin = (schema: Parameters<typeof baseUserPlugin>[0]): void => {
|
|
54
|
+
schema.add({
|
|
55
|
+
emailVerified: {
|
|
56
|
+
default: false,
|
|
57
|
+
description: "Whether the user has verified their email address",
|
|
58
|
+
type: Boolean,
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
|
|
45
63
|
/** For models with the isDeletedPlugin, extend this interface to add the appropriate fields. */
|
|
46
64
|
export interface IsDeleted {
|
|
47
65
|
// Whether the model should be treated as deleted or not.
|
|
@@ -355,6 +373,18 @@ export interface FindExactlyOnePlugin<T> {
|
|
|
355
373
|
): Promise<Document & T>;
|
|
356
374
|
}
|
|
357
375
|
|
|
376
|
+
type DateOnlyConditionalHandler = (this: DateOnly, val: unknown) => Date | undefined;
|
|
377
|
+
|
|
378
|
+
interface SchemaTypeWithConditionalHandlers {
|
|
379
|
+
$conditionalHandlers?: Record<string, DateOnlyConditionalHandler>;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
interface SchemaTypeWithApplySetters {
|
|
383
|
+
applySetters: (val: unknown, context: unknown) => Date | undefined;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
type SchemaTypesWithDateOnly = typeof mongoose.Schema.Types & {DateOnly: typeof DateOnly};
|
|
387
|
+
|
|
358
388
|
export class DateOnly extends SchemaType {
|
|
359
389
|
constructor(key: string, options: SchemaTypeOptions<Date>) {
|
|
360
390
|
super(key, options, "DateOnly");
|
|
@@ -365,9 +395,8 @@ export class DateOnly extends SchemaType {
|
|
|
365
395
|
}
|
|
366
396
|
|
|
367
397
|
$conditionalHandlers = {
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
...(SchemaType as any).prototype.$conditionalHandlers,
|
|
398
|
+
...(SchemaType as unknown as {prototype: SchemaTypeWithConditionalHandlers}).prototype
|
|
399
|
+
.$conditionalHandlers,
|
|
371
400
|
$gt: this.handleSingle,
|
|
372
401
|
$gte: this.handleSingle,
|
|
373
402
|
$lt: this.handleSingle,
|
|
@@ -378,9 +407,7 @@ export class DateOnly extends SchemaType {
|
|
|
378
407
|
// When using $gt, $gte, $lt, $lte, etc, we need to cast the value to a Date
|
|
379
408
|
castForQuery($conditional: string | undefined, val: unknown, context: unknown): Date | undefined {
|
|
380
409
|
if ($conditional == null) {
|
|
381
|
-
|
|
382
|
-
// biome-ignore lint/suspicious/noExplicitAny: applySetters is an internal Mongoose SchemaType method not in public type definitions
|
|
383
|
-
return (this as any).applySetters(val, context);
|
|
410
|
+
return (this as unknown as SchemaTypeWithApplySetters).applySetters(val, context);
|
|
384
411
|
}
|
|
385
412
|
|
|
386
413
|
const handler = this.$conditionalHandlers[$conditional];
|
|
@@ -447,6 +474,4 @@ export class DateOnly extends SchemaType {
|
|
|
447
474
|
}
|
|
448
475
|
|
|
449
476
|
// Register DateOnly with Mongoose's Schema.Types
|
|
450
|
-
|
|
451
|
-
// biome-ignore lint/suspicious/noExplicitAny: DateOnly is a custom SchemaType not declared in Mongoose's Schema.Types interface
|
|
452
|
-
(mongoose.Schema.Types as any).DateOnly = DateOnly;
|
|
477
|
+
(mongoose.Schema.Types as SchemaTypesWithDateOnly).DateOnly = DateOnly;
|
package/src/populate.test.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {beforeEach, describe, expect, it} from "bun:test";
|
|
2
|
+
import {assert} from "chai";
|
|
2
3
|
import mongoose, {type Document, type HydratedDocument, Schema} from "mongoose";
|
|
3
4
|
|
|
4
5
|
import {fixMixedFields, getOpenApiSpecForModel, unpopulate} from "./populate";
|
|
@@ -116,6 +117,31 @@ describe("unpopulate edge cases", () => {
|
|
|
116
117
|
expect(result).toEqual(doc);
|
|
117
118
|
});
|
|
118
119
|
|
|
120
|
+
it("leaves unpopulated values untouched", () => {
|
|
121
|
+
const doc = {
|
|
122
|
+
name: "test",
|
|
123
|
+
// Already an id rather than a populated document.
|
|
124
|
+
ownerId: "owner-123",
|
|
125
|
+
tags: ["tag-1", "tag-2"],
|
|
126
|
+
};
|
|
127
|
+
const result = unpopulate(doc as unknown as Document<unknown>, "ownerId") as unknown as {
|
|
128
|
+
ownerId: string;
|
|
129
|
+
tags: string[];
|
|
130
|
+
};
|
|
131
|
+
assert.equal(result.ownerId, "owner-123");
|
|
132
|
+
unpopulate(doc as unknown as Document<unknown>, "tags");
|
|
133
|
+
assert.deepEqual(result.tags, ["tag-1", "tag-2"]);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("returns the doc when a nested path is missing on the parent", () => {
|
|
137
|
+
const doc = {
|
|
138
|
+
name: "test",
|
|
139
|
+
nested: {other: "value"},
|
|
140
|
+
};
|
|
141
|
+
const result = unpopulate(doc as unknown as Document<unknown>, "nested.items");
|
|
142
|
+
assert.deepEqual(result, doc as unknown as Document<unknown>);
|
|
143
|
+
});
|
|
144
|
+
|
|
119
145
|
it("handles nested array paths", () => {
|
|
120
146
|
const doc = {
|
|
121
147
|
containers: [
|
|
@@ -170,6 +196,14 @@ describe("fixMixedFields", () => {
|
|
|
170
196
|
expect(properties.items.items.properties.meta).toEqual({description: undefined});
|
|
171
197
|
});
|
|
172
198
|
|
|
199
|
+
it("skips arrays of sub-documents whose OpenAPI items have no properties", () => {
|
|
200
|
+
const subSchema = new Schema({meta: {type: Schema.Types.Mixed}});
|
|
201
|
+
const schema = new Schema({items: [subSchema]});
|
|
202
|
+
const properties = {items: {type: "array" as const}};
|
|
203
|
+
fixMixedFields(schema, properties);
|
|
204
|
+
assert.deepEqual(properties, {items: {type: "array"}});
|
|
205
|
+
});
|
|
206
|
+
|
|
173
207
|
it("skips unknown paths", () => {
|
|
174
208
|
const schema = new Schema({foo: String});
|
|
175
209
|
const properties = {unknownKey: {type: "string"}};
|
package/src/populate.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import isArray from "lodash/isArray";
|
|
2
|
-
import type {Document, Schema} from "mongoose";
|
|
2
|
+
import type {Document, Model, Schema} from "mongoose";
|
|
3
3
|
import m2s from "mongoose-to-swagger";
|
|
4
4
|
|
|
5
5
|
import {APIError} from "./errors";
|
|
6
|
+
import {describeModel, modelDescriptionToOpenApiSpec} from "./schemaMetadata";
|
|
6
7
|
|
|
7
8
|
const m2sOptions = {
|
|
8
9
|
props: ["readOnly", "required", "enum", "default"],
|
|
@@ -139,20 +140,22 @@ export const fixMixedFields = (schema: Schema | null, properties: Record<string,
|
|
|
139
140
|
}
|
|
140
141
|
};
|
|
141
142
|
|
|
142
|
-
export const getOpenApiSpecForModel = (
|
|
143
|
-
|
|
144
|
-
// biome-ignore lint/suspicious/noExplicitAny: Mongoose Model param uses deep internal APIs (schema.path().options.ref, schema.virtuals, schema.childSchemas, db.model) that are not exposed in public type definitions
|
|
145
|
-
model: any,
|
|
143
|
+
export const getOpenApiSpecForModel = <T>(
|
|
144
|
+
model: Model<T>,
|
|
146
145
|
{
|
|
147
146
|
populatePaths,
|
|
148
147
|
extraModelProperties,
|
|
149
148
|
}: {populatePaths?: PopulatePath[]; extraModelProperties?: Record<string, unknown>} = {}
|
|
150
149
|
): {properties: Record<string, unknown>; required: string[]} => {
|
|
151
|
-
const
|
|
152
|
-
|
|
153
|
-
|
|
150
|
+
const description = describeModel(model);
|
|
151
|
+
const {properties, required} = modelDescriptionToOpenApiSpec(description);
|
|
152
|
+
const modelProperties = properties as Record<string, OpenApiSchemaNode>;
|
|
153
|
+
const modelSwagger = {
|
|
154
|
+
properties: modelProperties,
|
|
155
|
+
required,
|
|
156
|
+
};
|
|
154
157
|
|
|
155
|
-
fixMixedFields(model.schema,
|
|
158
|
+
fixMixedFields(model.schema, modelProperties);
|
|
156
159
|
|
|
157
160
|
if (populatePaths && isArray(populatePaths)) {
|
|
158
161
|
for (const populatePath of populatePaths) {
|
|
@@ -190,7 +193,7 @@ export const getOpenApiSpecForModel = (
|
|
|
190
193
|
|
|
191
194
|
// Navigate through the nested structure and set the schema
|
|
192
195
|
const pathParts = openApiPath.split(".");
|
|
193
|
-
let currentSchema = modelSwagger.properties;
|
|
196
|
+
let currentSchema: Record<string, OpenApiSchemaNode> = modelSwagger.properties;
|
|
194
197
|
for (let i = 0; i < pathParts.length; i++) {
|
|
195
198
|
const part = pathParts[i];
|
|
196
199
|
if (i === pathParts.length - 1) {
|
|
@@ -201,7 +204,7 @@ export const getOpenApiSpecForModel = (
|
|
|
201
204
|
...(schemaToSet.properties || {[part]: schemaToSet}),
|
|
202
205
|
};
|
|
203
206
|
} else {
|
|
204
|
-
currentSchema[part] = schemaToSet;
|
|
207
|
+
currentSchema[part] = schemaToSet as OpenApiSchemaNode;
|
|
205
208
|
}
|
|
206
209
|
} else {
|
|
207
210
|
// We're still navigating, ensure the path exists
|
|
@@ -214,7 +217,8 @@ export const getOpenApiSpecForModel = (
|
|
|
214
217
|
currentSchema[part] = {properties: {}, type: "object"};
|
|
215
218
|
}
|
|
216
219
|
}
|
|
217
|
-
|
|
220
|
+
const nextSchema = currentSchema[part].properties ?? currentSchema[part];
|
|
221
|
+
currentSchema = nextSchema as Record<string, OpenApiSchemaNode>;
|
|
218
222
|
}
|
|
219
223
|
}
|
|
220
224
|
}
|
|
@@ -238,7 +242,11 @@ export const getOpenApiSpecForModel = (
|
|
|
238
242
|
if (virtual === "id" || virtual === "__v") {
|
|
239
243
|
continue;
|
|
240
244
|
}
|
|
241
|
-
|
|
245
|
+
const childPath = childSchema.model.path;
|
|
246
|
+
if (!childPath || !modelSwagger.properties[childPath]?.properties) {
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
modelSwagger.properties[childPath].properties[virtual] = {
|
|
242
250
|
type: "any",
|
|
243
251
|
};
|
|
244
252
|
}
|
|
@@ -261,41 +269,44 @@ export const unpopulate = <T>(doc: Document<T>, path: string): Document<T> => {
|
|
|
261
269
|
}
|
|
262
270
|
const pathParts = path.split(".");
|
|
263
271
|
|
|
272
|
+
// Traversal treats documents as plain nested records: the populated shapes are only known
|
|
273
|
+
// at runtime, so each level is narrowed as it is visited.
|
|
274
|
+
type NestedRecord = Record<string, unknown>;
|
|
275
|
+
const asRecord = (value: unknown): NestedRecord => value as NestedRecord;
|
|
276
|
+
const idOf = (value: unknown): unknown => (value as {_id?: unknown} | null)?._id;
|
|
277
|
+
|
|
264
278
|
// Recursive because we need to support nested paths.
|
|
265
|
-
|
|
266
|
-
// biome-ignore lint/suspicious/noExplicitAny: recursive document traversal uses bracket-notation indexing on arbitrary nested document shapes that Mongoose Document types do not expose
|
|
267
|
-
const recursiveUnpopulate = (current: any, parts: string[]): any => {
|
|
279
|
+
const recursiveUnpopulate = (current: NestedRecord, parts: string[]): NestedRecord => {
|
|
268
280
|
const part = parts[0];
|
|
281
|
+
const value = current[part];
|
|
269
282
|
|
|
270
283
|
// If the path doesn't exist, return the original doc
|
|
271
|
-
if (!
|
|
272
|
-
return doc;
|
|
284
|
+
if (!value) {
|
|
285
|
+
return asRecord(doc);
|
|
273
286
|
}
|
|
274
287
|
|
|
275
288
|
if (parts.length === 1) {
|
|
276
289
|
// Base case: we've reached the last part of the path
|
|
277
|
-
if (Array.isArray(
|
|
290
|
+
if (Array.isArray(value)) {
|
|
278
291
|
// If the field is an array, recursively unpopulate each element
|
|
279
|
-
current[part] =
|
|
280
|
-
|
|
281
|
-
});
|
|
282
|
-
} else if (current[part]?._id) {
|
|
292
|
+
current[part] = value.map((item) => idOf(item) ?? item);
|
|
293
|
+
} else if (idOf(value)) {
|
|
283
294
|
// If the field is a populated document, revert to _id
|
|
284
|
-
current[part] =
|
|
295
|
+
current[part] = idOf(value);
|
|
285
296
|
}
|
|
286
297
|
} else {
|
|
287
298
|
// Recursive case: continue down the path
|
|
288
|
-
if (Array.isArray(
|
|
289
|
-
for (const item of
|
|
290
|
-
recursiveUnpopulate(item, parts.slice(1)); // Recursively handle each item in the array
|
|
299
|
+
if (Array.isArray(value)) {
|
|
300
|
+
for (const item of value) {
|
|
301
|
+
recursiveUnpopulate(asRecord(item), parts.slice(1)); // Recursively handle each item in the array
|
|
291
302
|
}
|
|
292
303
|
} else {
|
|
293
|
-
recursiveUnpopulate(
|
|
304
|
+
recursiveUnpopulate(asRecord(value), parts.slice(1)); // Recursively handle the next part
|
|
294
305
|
}
|
|
295
306
|
}
|
|
296
307
|
|
|
297
308
|
return current;
|
|
298
309
|
};
|
|
299
310
|
|
|
300
|
-
return recursiveUnpopulate(doc, pathParts)
|
|
311
|
+
return recursiveUnpopulate(asRecord(doc), pathParts) as unknown as Document<T>;
|
|
301
312
|
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type {Application} from "express";
|
|
2
|
+
|
|
3
|
+
import type {RateLimitOptions} from "./types";
|
|
4
|
+
|
|
5
|
+
export const applyRateLimitTrustProxy = (app: Application, options: RateLimitOptions): void => {
|
|
6
|
+
if (options.trustProxy === false) {
|
|
7
|
+
app.set("trust proxy", false);
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
if (options.trustProxy !== undefined) {
|
|
11
|
+
app.set("trust proxy", options.trustProxy);
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
// Default off so client X-Forwarded-For cannot rotate unauthenticated buckets.
|
|
15
|
+
// Cloud Run / GFE: pass trustProxy: 1.
|
|
16
|
+
app.set("trust proxy", false);
|
|
17
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import {APIError} from "../errors";
|
|
2
|
+
import {createMemoryRateLimitStore} from "./memoryStore";
|
|
3
|
+
import {createMongoRateLimitStore} from "./mongoStore";
|
|
4
|
+
import {createRedisRateLimitStore} from "./redisStore";
|
|
5
|
+
import type {RateLimitOptions, RateLimitStore} from "./types";
|
|
6
|
+
|
|
7
|
+
export const createRateLimitStore = (options: RateLimitOptions): RateLimitStore => {
|
|
8
|
+
if (options.storeImpl) {
|
|
9
|
+
return options.storeImpl;
|
|
10
|
+
}
|
|
11
|
+
const kind = options.store ?? "memory";
|
|
12
|
+
if (kind === "memory") {
|
|
13
|
+
return createMemoryRateLimitStore();
|
|
14
|
+
}
|
|
15
|
+
if (kind === "redis") {
|
|
16
|
+
return createRedisRateLimitStore(options);
|
|
17
|
+
}
|
|
18
|
+
if (kind === "mongo") {
|
|
19
|
+
return createMongoRateLimitStore();
|
|
20
|
+
}
|
|
21
|
+
throw new APIError({
|
|
22
|
+
status: 500,
|
|
23
|
+
title: `Unknown rate limit store: ${String(kind)}`,
|
|
24
|
+
});
|
|
25
|
+
};
|