@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
|
@@ -0,0 +1,405 @@
|
|
|
1
|
+
import {describe, expect, it} from "bun:test";
|
|
2
|
+
import mongoose, {Schema} from "mongoose";
|
|
3
|
+
import {z} from "zod";
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
describeModel,
|
|
7
|
+
describeModelForRouter,
|
|
8
|
+
type FieldDescription,
|
|
9
|
+
fieldDescriptionToAdminMeta,
|
|
10
|
+
fieldDescriptionToOpenApiProperty,
|
|
11
|
+
fieldDescriptionToZodType,
|
|
12
|
+
modelDescriptionToAdminFields,
|
|
13
|
+
modelDescriptionToOpenApiSpec,
|
|
14
|
+
nestDottedFieldDescriptions,
|
|
15
|
+
} from "./schemaMetadata";
|
|
16
|
+
|
|
17
|
+
const fixtureSchema = new Schema({
|
|
18
|
+
completed: {default: false, description: "Whether the item is done", type: Boolean},
|
|
19
|
+
createdAt: {description: "When the record was created", type: Date},
|
|
20
|
+
label: {description: "Display label", required: true, type: String},
|
|
21
|
+
metadata: {description: "Arbitrary metadata", type: Schema.Types.Mixed},
|
|
22
|
+
ownerId: {description: "Owning user", ref: "User", required: true, type: Schema.Types.ObjectId},
|
|
23
|
+
status: {
|
|
24
|
+
description: "Workflow status",
|
|
25
|
+
enum: ["draft", "published"],
|
|
26
|
+
type: String,
|
|
27
|
+
},
|
|
28
|
+
tags: {description: "Search tags", type: [String]},
|
|
29
|
+
tasks: {
|
|
30
|
+
description: "Nested tasks",
|
|
31
|
+
type: [
|
|
32
|
+
new Schema({
|
|
33
|
+
done: {description: "Whether the task is done", type: Boolean},
|
|
34
|
+
title: {description: "Task title", required: true, type: String},
|
|
35
|
+
}),
|
|
36
|
+
],
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
fixtureSchema.add({
|
|
41
|
+
created: {description: "Created timestamp", type: Date},
|
|
42
|
+
deleted: {default: false, description: "Soft delete flag", type: Boolean},
|
|
43
|
+
updated: {description: "Updated timestamp", type: Date},
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const FixtureModel =
|
|
47
|
+
mongoose.models.SchemaMetadataFixture ?? mongoose.model("SchemaMetadataFixture", fixtureSchema);
|
|
48
|
+
|
|
49
|
+
const mapSchema = new Schema({
|
|
50
|
+
attributes: {description: "Untyped map values", type: Map},
|
|
51
|
+
checkboxValues: {description: "Boolean map", of: Boolean, type: Map},
|
|
52
|
+
lastEatenWith: {description: "Date map", of: Date, type: Map},
|
|
53
|
+
typedMap: {description: "String map", of: String, type: Map},
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
const MapModel =
|
|
57
|
+
mongoose.models.SchemaMetadataMap ?? mongoose.model("SchemaMetadataMap", mapSchema);
|
|
58
|
+
|
|
59
|
+
const embeddedSchema = new Schema({
|
|
60
|
+
profile: {
|
|
61
|
+
description: "Nested profile",
|
|
62
|
+
type: new Schema({
|
|
63
|
+
bio: {description: "Short bio", type: String},
|
|
64
|
+
}),
|
|
65
|
+
},
|
|
66
|
+
"settings.theme": {description: "Theme name", type: String},
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
const EmbeddedModel =
|
|
70
|
+
mongoose.models.SchemaMetadataEmbedded ??
|
|
71
|
+
mongoose.model("SchemaMetadataEmbedded", embeddedSchema);
|
|
72
|
+
|
|
73
|
+
describe("describeModel", () => {
|
|
74
|
+
it("returns a field graph snapshot for the fixture schema", () => {
|
|
75
|
+
const description = describeModel(FixtureModel);
|
|
76
|
+
|
|
77
|
+
expect(description.modelName).toBe("SchemaMetadataFixture");
|
|
78
|
+
expect(description.fields).toMatchSnapshot();
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("marks system fields", () => {
|
|
82
|
+
const description = describeModel(FixtureModel);
|
|
83
|
+
|
|
84
|
+
expect(description.fields._id?.system).toBe(true);
|
|
85
|
+
expect(description.fields.__v?.system).toBe(true);
|
|
86
|
+
expect(description.fields.created?.system).toBe(true);
|
|
87
|
+
expect(description.fields.updated?.system).toBe(true);
|
|
88
|
+
expect(description.fields.deleted?.system).toBe(true);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("captures enum, ref, array, subdocument, and mixed fields", () => {
|
|
92
|
+
const description = describeModel(FixtureModel);
|
|
93
|
+
|
|
94
|
+
expect(description.fields.status).toEqual(
|
|
95
|
+
expect.objectContaining({
|
|
96
|
+
enum: ["draft", "published"],
|
|
97
|
+
kind: "string",
|
|
98
|
+
})
|
|
99
|
+
);
|
|
100
|
+
expect(description.fields.ownerId).toEqual(
|
|
101
|
+
expect.objectContaining({
|
|
102
|
+
kind: "objectId",
|
|
103
|
+
ref: "User",
|
|
104
|
+
required: true,
|
|
105
|
+
})
|
|
106
|
+
);
|
|
107
|
+
expect(description.fields.tags).toEqual(
|
|
108
|
+
expect.objectContaining({
|
|
109
|
+
isArray: true,
|
|
110
|
+
item: expect.objectContaining({kind: "string"}),
|
|
111
|
+
})
|
|
112
|
+
);
|
|
113
|
+
expect(description.fields.tasks).toEqual(
|
|
114
|
+
expect.objectContaining({
|
|
115
|
+
isArray: true,
|
|
116
|
+
item: expect.objectContaining({
|
|
117
|
+
fields: expect.objectContaining({
|
|
118
|
+
done: expect.objectContaining({kind: "boolean"}),
|
|
119
|
+
title: expect.objectContaining({kind: "string", required: true}),
|
|
120
|
+
}),
|
|
121
|
+
}),
|
|
122
|
+
})
|
|
123
|
+
);
|
|
124
|
+
expect(description.fields.metadata).toEqual(
|
|
125
|
+
expect.objectContaining({
|
|
126
|
+
kind: "mixed",
|
|
127
|
+
})
|
|
128
|
+
);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it("describes map fields from Map of/caster kinds, not a date fallback", () => {
|
|
132
|
+
const description = describeModel(MapModel);
|
|
133
|
+
|
|
134
|
+
expect(description.fields.attributes).toEqual(
|
|
135
|
+
expect.objectContaining({
|
|
136
|
+
item: expect.objectContaining({kind: "mixed"}),
|
|
137
|
+
kind: "map",
|
|
138
|
+
})
|
|
139
|
+
);
|
|
140
|
+
expect(description.fields.typedMap).toEqual(
|
|
141
|
+
expect.objectContaining({
|
|
142
|
+
item: expect.objectContaining({kind: "string"}),
|
|
143
|
+
kind: "map",
|
|
144
|
+
})
|
|
145
|
+
);
|
|
146
|
+
expect(description.fields.checkboxValues).toEqual(
|
|
147
|
+
expect.objectContaining({
|
|
148
|
+
item: expect.objectContaining({kind: "boolean"}),
|
|
149
|
+
kind: "map",
|
|
150
|
+
})
|
|
151
|
+
);
|
|
152
|
+
expect(description.fields.lastEatenWith).toEqual(
|
|
153
|
+
expect.objectContaining({
|
|
154
|
+
item: expect.objectContaining({kind: "date"}),
|
|
155
|
+
kind: "map",
|
|
156
|
+
})
|
|
157
|
+
);
|
|
158
|
+
expect(Object.keys(description.fields).some((path) => path.includes("$*"))).toBe(false);
|
|
159
|
+
|
|
160
|
+
expect(fieldDescriptionToOpenApiProperty(description.fields.lastEatenWith)).toEqual({
|
|
161
|
+
additionalProperties: {format: "date-time", type: "string"},
|
|
162
|
+
description: "Date map",
|
|
163
|
+
type: "object",
|
|
164
|
+
});
|
|
165
|
+
expect(fieldDescriptionToOpenApiProperty(description.fields.checkboxValues)).toEqual({
|
|
166
|
+
additionalProperties: {type: "boolean"},
|
|
167
|
+
description: "Boolean map",
|
|
168
|
+
type: "object",
|
|
169
|
+
});
|
|
170
|
+
expect(fieldDescriptionToOpenApiProperty(description.fields.typedMap)).toEqual({
|
|
171
|
+
additionalProperties: {type: "string"},
|
|
172
|
+
description: "String map",
|
|
173
|
+
type: "object",
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it("merges extraProperties into the field graph", () => {
|
|
178
|
+
const extraField: FieldDescription = {
|
|
179
|
+
description: "Synthetic field",
|
|
180
|
+
kind: "string",
|
|
181
|
+
required: false,
|
|
182
|
+
};
|
|
183
|
+
const description = describeModel(FixtureModel, {
|
|
184
|
+
extraProperties: {synthetic: extraField},
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
expect(description.fields.synthetic).toEqual(extraField);
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
describe("describeModelForRouter", () => {
|
|
192
|
+
it("adds write masks from system fields and validation exclusions", () => {
|
|
193
|
+
const description = describeModelForRouter(FixtureModel, {
|
|
194
|
+
validation: {
|
|
195
|
+
excludeFromCreate: ["ownerId"],
|
|
196
|
+
excludeFromUpdate: ["status"],
|
|
197
|
+
},
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
expect(description.fields.created?.writableOnCreate).toBe(false);
|
|
201
|
+
expect(description.fields.ownerId?.writableOnCreate).toBe(false);
|
|
202
|
+
expect(description.fields.label?.writableOnCreate).toBe(true);
|
|
203
|
+
expect(description.fields.status?.writableOnUpdate).toBe(false);
|
|
204
|
+
expect(description.fields.label?.writableOnUpdate).toBe(true);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it("respects fieldView write masks for nested paths", () => {
|
|
208
|
+
const description = describeModelForRouter(EmbeddedModel, {
|
|
209
|
+
fieldView: {read: "*", write: ["profile"]},
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
expect(description.fields.profile?.writableOnCreate).toBe(true);
|
|
213
|
+
expect(description.fields["settings.theme"]?.writableOnCreate).toBe(false);
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
describe("nestDottedFieldDescriptions", () => {
|
|
218
|
+
it("nests dotted paths into embedded field graphs", () => {
|
|
219
|
+
const nested = nestDottedFieldDescriptions({
|
|
220
|
+
"settings.theme": {kind: "string", required: false},
|
|
221
|
+
title: {kind: "string", required: true},
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
expect(nested.settings).toEqual(
|
|
225
|
+
expect.objectContaining({
|
|
226
|
+
fields: {
|
|
227
|
+
theme: {kind: "string", required: false},
|
|
228
|
+
},
|
|
229
|
+
kind: "embedded",
|
|
230
|
+
})
|
|
231
|
+
);
|
|
232
|
+
expect(nested.title).toEqual({kind: "string", required: true});
|
|
233
|
+
});
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
describe("modelDescriptionToOpenApiSpec", () => {
|
|
237
|
+
it("converts the fixture description into OpenAPI properties", () => {
|
|
238
|
+
const description = describeModel(FixtureModel);
|
|
239
|
+
const spec = modelDescriptionToOpenApiSpec(description);
|
|
240
|
+
|
|
241
|
+
expect(spec.properties.ownerId).toEqual({
|
|
242
|
+
description: "Owning user",
|
|
243
|
+
format: "objectid",
|
|
244
|
+
type: "string",
|
|
245
|
+
});
|
|
246
|
+
expect(spec.properties.metadata).toEqual({
|
|
247
|
+
description: "Arbitrary metadata",
|
|
248
|
+
});
|
|
249
|
+
expect(spec.required).toContain("label");
|
|
250
|
+
expect(spec.required).toContain("ownerId");
|
|
251
|
+
expect(spec.properties.__v).toBeUndefined();
|
|
252
|
+
});
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
describe("fieldDescriptionToOpenApiProperty", () => {
|
|
256
|
+
it("maps arrays, embedded objects, maps, and date-only fields", () => {
|
|
257
|
+
const arrayField: FieldDescription = {
|
|
258
|
+
description: "Tags",
|
|
259
|
+
isArray: true,
|
|
260
|
+
item: {kind: "string", required: false},
|
|
261
|
+
kind: "string",
|
|
262
|
+
required: false,
|
|
263
|
+
};
|
|
264
|
+
const embeddedField: FieldDescription = {
|
|
265
|
+
description: "Profile",
|
|
266
|
+
fields: {
|
|
267
|
+
bio: {description: "Bio", kind: "string", required: false},
|
|
268
|
+
},
|
|
269
|
+
kind: "embedded",
|
|
270
|
+
required: false,
|
|
271
|
+
};
|
|
272
|
+
const mapField: FieldDescription = {
|
|
273
|
+
description: "Attributes",
|
|
274
|
+
item: {kind: "string", required: false},
|
|
275
|
+
kind: "map",
|
|
276
|
+
required: false,
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
expect(fieldDescriptionToOpenApiProperty(arrayField)).toEqual({
|
|
280
|
+
description: "Tags",
|
|
281
|
+
items: {type: "string"},
|
|
282
|
+
type: "array",
|
|
283
|
+
});
|
|
284
|
+
expect(fieldDescriptionToOpenApiProperty(embeddedField)).toEqual(
|
|
285
|
+
expect.objectContaining({
|
|
286
|
+
description: "Profile",
|
|
287
|
+
properties: {bio: {description: "Bio", type: "string"}},
|
|
288
|
+
type: "object",
|
|
289
|
+
})
|
|
290
|
+
);
|
|
291
|
+
expect(fieldDescriptionToOpenApiProperty(mapField)).toEqual({
|
|
292
|
+
additionalProperties: {type: "string"},
|
|
293
|
+
description: "Attributes",
|
|
294
|
+
type: "object",
|
|
295
|
+
});
|
|
296
|
+
expect(
|
|
297
|
+
fieldDescriptionToOpenApiProperty({
|
|
298
|
+
description: "Birthday",
|
|
299
|
+
kind: "dateOnly",
|
|
300
|
+
required: false,
|
|
301
|
+
})
|
|
302
|
+
).toEqual({
|
|
303
|
+
description: "Birthday",
|
|
304
|
+
type: "dateonly",
|
|
305
|
+
});
|
|
306
|
+
});
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
describe("modelDescriptionToAdminFields", () => {
|
|
310
|
+
it("converts the fixture description into admin field metadata", () => {
|
|
311
|
+
const description = describeModel(FixtureModel);
|
|
312
|
+
const fields = modelDescriptionToAdminFields(description);
|
|
313
|
+
|
|
314
|
+
expect(fields.ownerId).toEqual(
|
|
315
|
+
expect.objectContaining({
|
|
316
|
+
ref: "User",
|
|
317
|
+
required: true,
|
|
318
|
+
searchable: false,
|
|
319
|
+
type: "objectid",
|
|
320
|
+
})
|
|
321
|
+
);
|
|
322
|
+
expect(fields.tags).toEqual(
|
|
323
|
+
expect.objectContaining({
|
|
324
|
+
itemType: "string",
|
|
325
|
+
type: "array",
|
|
326
|
+
})
|
|
327
|
+
);
|
|
328
|
+
expect(fields.tasks).toEqual(
|
|
329
|
+
expect.objectContaining({
|
|
330
|
+
items: expect.objectContaining({
|
|
331
|
+
title: expect.objectContaining({required: true, type: "string"}),
|
|
332
|
+
}),
|
|
333
|
+
type: "array",
|
|
334
|
+
})
|
|
335
|
+
);
|
|
336
|
+
});
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
describe("fieldDescriptionToAdminMeta", () => {
|
|
340
|
+
it("maps embedded object fields for admin forms", () => {
|
|
341
|
+
const meta = fieldDescriptionToAdminMeta({
|
|
342
|
+
description: "Profile",
|
|
343
|
+
fields: {
|
|
344
|
+
bio: {description: "Bio", kind: "string", required: false},
|
|
345
|
+
},
|
|
346
|
+
kind: "embedded",
|
|
347
|
+
required: false,
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
expect(meta).toEqual(
|
|
351
|
+
expect.objectContaining({
|
|
352
|
+
items: {
|
|
353
|
+
bio: expect.objectContaining({searchable: true, type: "string"}),
|
|
354
|
+
},
|
|
355
|
+
type: "object",
|
|
356
|
+
})
|
|
357
|
+
);
|
|
358
|
+
});
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
describe("fieldDescriptionToZodType", () => {
|
|
362
|
+
it("builds zod types for primitive, enum, array, and map fields", () => {
|
|
363
|
+
expect(fieldDescriptionToZodType({kind: "string", required: true}, z).parse("ok")).toBe("ok");
|
|
364
|
+
expect(
|
|
365
|
+
fieldDescriptionToZodType(
|
|
366
|
+
{enum: ["draft", "published"], kind: "string", required: true},
|
|
367
|
+
z
|
|
368
|
+
).parse("draft")
|
|
369
|
+
).toBe("draft");
|
|
370
|
+
expect(
|
|
371
|
+
fieldDescriptionToZodType(
|
|
372
|
+
{
|
|
373
|
+
description: "Tags",
|
|
374
|
+
isArray: true,
|
|
375
|
+
item: {kind: "string", required: false},
|
|
376
|
+
kind: "string",
|
|
377
|
+
required: false,
|
|
378
|
+
},
|
|
379
|
+
z
|
|
380
|
+
).parse(["a"])
|
|
381
|
+
).toEqual(["a"]);
|
|
382
|
+
expect(
|
|
383
|
+
fieldDescriptionToZodType(
|
|
384
|
+
{
|
|
385
|
+
description: "Attributes",
|
|
386
|
+
item: {kind: "string", required: false},
|
|
387
|
+
kind: "map",
|
|
388
|
+
required: false,
|
|
389
|
+
},
|
|
390
|
+
z
|
|
391
|
+
).parse({key: "value"})
|
|
392
|
+
).toEqual({key: "value"});
|
|
393
|
+
expect(
|
|
394
|
+
fieldDescriptionToZodType(
|
|
395
|
+
{
|
|
396
|
+
description: "Metadata",
|
|
397
|
+
fields: {note: {kind: "string", required: false}},
|
|
398
|
+
kind: "embedded",
|
|
399
|
+
required: false,
|
|
400
|
+
},
|
|
401
|
+
z
|
|
402
|
+
).parse({note: "hello"})
|
|
403
|
+
).toEqual({note: "hello"});
|
|
404
|
+
});
|
|
405
|
+
});
|