@zenstackhq/server 3.5.5 → 3.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. package/dist/api.cjs +2729 -4451
  2. package/dist/api.cjs.map +1 -1
  3. package/dist/api.d.cts +219 -207
  4. package/dist/api.d.mts +249 -0
  5. package/dist/api.mjs +2745 -0
  6. package/dist/api.mjs.map +1 -0
  7. package/dist/common-BjzwQE8S.d.mts +16 -0
  8. package/dist/common-Cdj4rLFr.mjs +9 -0
  9. package/dist/common-Cdj4rLFr.mjs.map +1 -0
  10. package/dist/common-D2RyWTAZ.cjs +14 -0
  11. package/dist/common-D2RyWTAZ.cjs.map +1 -0
  12. package/dist/common-D3fx-IHt.d.cts +16 -0
  13. package/dist/elysia.cjs +46 -114
  14. package/dist/elysia.cjs.map +1 -1
  15. package/dist/elysia.d.cts +37 -36
  16. package/dist/elysia.d.mts +54 -0
  17. package/dist/elysia.mjs +48 -0
  18. package/dist/elysia.mjs.map +1 -0
  19. package/dist/express.cjs +40 -110
  20. package/dist/express.cjs.map +1 -1
  21. package/dist/express.d.cts +21 -20
  22. package/dist/express.d.mts +32 -0
  23. package/dist/express.mjs +42 -0
  24. package/dist/express.mjs.map +1 -0
  25. package/dist/fastify.cjs +36 -100
  26. package/dist/fastify.cjs.map +1 -1
  27. package/dist/fastify.d.cts +15 -14
  28. package/dist/fastify.d.mts +23 -0
  29. package/dist/fastify.mjs +36 -0
  30. package/dist/fastify.mjs.map +1 -0
  31. package/dist/hono.cjs +31 -108
  32. package/dist/hono.cjs.map +1 -1
  33. package/dist/hono.d.cts +11 -10
  34. package/dist/hono.d.mts +19 -0
  35. package/dist/hono.mjs +33 -0
  36. package/dist/hono.mjs.map +1 -0
  37. package/dist/next.cjs +81 -169
  38. package/dist/next.cjs.map +1 -1
  39. package/dist/next.d.cts +32 -29
  40. package/dist/{next.d.ts → next.d.mts} +32 -29
  41. package/dist/next.mjs +86 -0
  42. package/dist/next.mjs.map +1 -0
  43. package/dist/nuxt.cjs +414 -104
  44. package/dist/nuxt.cjs.map +1 -1
  45. package/dist/nuxt.d.cts +538 -12
  46. package/dist/nuxt.d.mts +545 -0
  47. package/dist/nuxt.mjs +416 -0
  48. package/dist/nuxt.mjs.map +1 -0
  49. package/dist/openapi-BPFL4Hn6.mjs +585 -0
  50. package/dist/openapi-BPFL4Hn6.mjs.map +1 -0
  51. package/dist/openapi-BUzfCKi-.cjs +586 -0
  52. package/dist/openapi-BUzfCKi-.cjs.map +1 -0
  53. package/dist/spec-utils-C7vlChMD.cjs +278 -0
  54. package/dist/spec-utils-C7vlChMD.cjs.map +1 -0
  55. package/dist/spec-utils-Syk4SLCX.mjs +200 -0
  56. package/dist/spec-utils-Syk4SLCX.mjs.map +1 -0
  57. package/dist/sveltekit.cjs +87 -188
  58. package/dist/sveltekit.cjs.map +1 -1
  59. package/dist/sveltekit.d.cts +24 -22
  60. package/dist/sveltekit.d.mts +43 -0
  61. package/dist/sveltekit.mjs +88 -0
  62. package/dist/sveltekit.mjs.map +1 -0
  63. package/dist/tanstack-start.cjs +59 -133
  64. package/dist/tanstack-start.cjs.map +1 -1
  65. package/dist/tanstack-start.d.cts +19 -14
  66. package/dist/tanstack-start.d.mts +37 -0
  67. package/dist/tanstack-start.mjs +60 -0
  68. package/dist/tanstack-start.mjs.map +1 -0
  69. package/dist/types-6NFtxov5.d.mts +74 -0
  70. package/dist/types-DYdFPTkH.d.cts +74 -0
  71. package/dist/utils-B9NX7OcP.cjs +85 -0
  72. package/dist/utils-B9NX7OcP.cjs.map +1 -0
  73. package/dist/utils-Doc_CQNd.mjs +39 -0
  74. package/dist/utils-Doc_CQNd.mjs.map +1 -0
  75. package/package.json +28 -27
  76. package/dist/api.d.ts +0 -237
  77. package/dist/api.js +0 -4437
  78. package/dist/api.js.map +0 -1
  79. package/dist/common-Cuu8bjzK.d.cts +0 -14
  80. package/dist/common-WHbiInuf.d.ts +0 -14
  81. package/dist/elysia.d.ts +0 -53
  82. package/dist/elysia.js +0 -82
  83. package/dist/elysia.js.map +0 -1
  84. package/dist/express.d.ts +0 -31
  85. package/dist/express.js +0 -78
  86. package/dist/express.js.map +0 -1
  87. package/dist/fastify.d.ts +0 -22
  88. package/dist/fastify.js +0 -68
  89. package/dist/fastify.js.map +0 -1
  90. package/dist/hono.d.ts +0 -18
  91. package/dist/hono.js +0 -76
  92. package/dist/hono.js.map +0 -1
  93. package/dist/next.js +0 -140
  94. package/dist/next.js.map +0 -1
  95. package/dist/nuxt.d.ts +0 -19
  96. package/dist/nuxt.js +0 -73
  97. package/dist/nuxt.js.map +0 -1
  98. package/dist/sveltekit.d.ts +0 -41
  99. package/dist/sveltekit.js +0 -156
  100. package/dist/sveltekit.js.map +0 -1
  101. package/dist/tanstack-start.d.ts +0 -32
  102. package/dist/tanstack-start.js +0 -104
  103. package/dist/tanstack-start.js.map +0 -1
  104. package/dist/types-CU_PWKr9.d.cts +0 -72
  105. package/dist/types-CU_PWKr9.d.ts +0 -72
@@ -0,0 +1,586 @@
1
+ require("./utils-B9NX7OcP.cjs");
2
+ const require_spec_utils = require("./spec-utils-C7vlChMD.cjs");
3
+ let _zenstackhq_common_helpers = require("@zenstackhq/common-helpers");
4
+ let _zenstackhq_orm = require("@zenstackhq/orm");
5
+ //#region src/api/rpc/openapi.ts
6
+ const GET_OPERATIONS = new Set([
7
+ "findFirst",
8
+ "findUnique",
9
+ "findMany",
10
+ "aggregate",
11
+ "groupBy",
12
+ "count",
13
+ "exists"
14
+ ]);
15
+ const POST_OPERATIONS = new Set([
16
+ "create",
17
+ "createMany",
18
+ "createManyAndReturn",
19
+ "upsert"
20
+ ]);
21
+ const PUT_OPERATIONS = new Set([
22
+ "update",
23
+ "updateMany",
24
+ "updateManyAndReturn"
25
+ ]);
26
+ const DELETE_OPERATIONS = new Set(["delete", "deleteMany"]);
27
+ const JSON_CT = "application/json";
28
+ /**
29
+ * Per-operation metadata: human-readable summary and response data schema shape.
30
+ * Operations absent from this map (aggregate, groupBy, count) return a generic schema.
31
+ */
32
+ const OPERATION_INFO = {
33
+ findUnique: {
34
+ summary: (m) => `Find a unique ${m}`,
35
+ dataSchema: (ref) => ({ anyOf: [ref, { type: "null" }] })
36
+ },
37
+ findFirst: {
38
+ summary: (m) => `Find the first ${m}`,
39
+ dataSchema: (ref) => ({ anyOf: [ref, { type: "null" }] })
40
+ },
41
+ findMany: {
42
+ summary: (m) => `List ${m} entities`,
43
+ dataSchema: (ref) => ({
44
+ type: "array",
45
+ items: ref
46
+ })
47
+ },
48
+ create: {
49
+ summary: (m) => `Create a ${m}`,
50
+ dataSchema: (ref) => ref
51
+ },
52
+ createMany: {
53
+ summary: (m) => `Create multiple ${m} entities`,
54
+ dataSchema: () => ({
55
+ type: "object",
56
+ properties: { count: { type: "integer" } },
57
+ required: ["count"]
58
+ })
59
+ },
60
+ createManyAndReturn: {
61
+ summary: (m) => `Create multiple ${m}s and return them`,
62
+ dataSchema: (ref) => ({
63
+ type: "array",
64
+ items: ref
65
+ })
66
+ },
67
+ update: {
68
+ summary: (m) => `Update a ${m}`,
69
+ dataSchema: (ref) => ref
70
+ },
71
+ updateMany: {
72
+ summary: (m) => `Update multiple ${m} entities`,
73
+ dataSchema: () => ({
74
+ type: "object",
75
+ properties: { count: { type: "integer" } },
76
+ required: ["count"]
77
+ })
78
+ },
79
+ updateManyAndReturn: {
80
+ summary: (m) => `Update multiple ${m} entities and return them`,
81
+ dataSchema: (ref) => ({
82
+ type: "array",
83
+ items: ref
84
+ })
85
+ },
86
+ upsert: {
87
+ summary: (m) => `Upsert a ${m}`,
88
+ dataSchema: (ref) => ref
89
+ },
90
+ delete: {
91
+ summary: (m) => `Delete a ${m}`,
92
+ dataSchema: (ref) => ref
93
+ },
94
+ deleteMany: {
95
+ summary: (m) => `Delete multiple ${m} entities`,
96
+ dataSchema: () => ({
97
+ type: "object",
98
+ properties: { count: { type: "integer" } },
99
+ required: ["count"]
100
+ })
101
+ },
102
+ exists: {
103
+ summary: (m) => `Check ${m} existence`,
104
+ dataSchema: () => ({ type: "boolean" })
105
+ }
106
+ };
107
+ function errorResponse(description) {
108
+ return {
109
+ description,
110
+ content: { [JSON_CT]: { schema: { $ref: "#/components/schemas/_rpcErrorResponse" } } }
111
+ };
112
+ }
113
+ const ERROR_400 = errorResponse("Error occurred while processing the request");
114
+ const ERROR_403 = errorResponse("Forbidden: insufficient permissions to perform this operation");
115
+ const ERROR_404 = errorResponse("Resource not found");
116
+ const ERROR_422 = errorResponse("Operation is unprocessable due to validation errors");
117
+ const ERROR_500 = errorResponse("Internal server error");
118
+ const NOT_FOUND_OPERATIONS = new Set(["update", "delete"]);
119
+ /**
120
+ * Generates an OpenAPI v3.1 specification for the RPC-style API handler.
121
+ */
122
+ var RPCApiSpecGenerator = class {
123
+ specOptions;
124
+ factory;
125
+ /**
126
+ * Schemas extracted from the Zod registry, keyed by their registered ID, with
127
+ * all `$ref` values already rewritten to `#/components/schemas/<id>`.
128
+ */
129
+ registrySchemas = {};
130
+ constructor(handlerOptions) {
131
+ this.handlerOptions = handlerOptions;
132
+ this.factory = (0, _zenstackhq_orm.createQuerySchemaFactory)(handlerOptions.schema, handlerOptions.queryOptions);
133
+ }
134
+ get schema() {
135
+ return this.handlerOptions.schema;
136
+ }
137
+ get queryOptions() {
138
+ return this.handlerOptions?.queryOptions;
139
+ }
140
+ generateSpec(options) {
141
+ this.specOptions = options;
142
+ const rawRegistry = this.factory.toJSONSchema();
143
+ this.registrySchemas = this.transformRegistrySchemas(rawRegistry.schemas);
144
+ return {
145
+ openapi: "3.1.0",
146
+ info: {
147
+ title: options?.title ?? "ZenStack Generated API",
148
+ version: options?.version ?? "1.0.0",
149
+ ...options?.description && { description: options.description },
150
+ ...options?.summary && { summary: options.summary }
151
+ },
152
+ tags: this.generateTags(),
153
+ paths: this.generatePaths(),
154
+ components: { schemas: {
155
+ ...this.registrySchemas,
156
+ ...this.generateSharedSchemas()
157
+ } }
158
+ };
159
+ }
160
+ /**
161
+ * Takes the raw `schemas` map from `z.toJSONSchema(registry)` and:
162
+ * 1. Rewrites bare-ID `$ref` values (e.g. `"UserWhereInput"`) to full
163
+ * component paths (`"#/components/schemas/UserWhereInput"`).
164
+ * 2. Strips the `$schema` keyword from each top-level schema object, as it
165
+ * is redundant inside an OpenAPI document.
166
+ * 3. Resolves Zod's auto-generated `__shared/$defs/schemaN` aliases.
167
+ * When a `__shared/$defs/schemaN` entry is itself a plain `$ref` to a
168
+ * named schema, every reference to it is rewritten to point directly at
169
+ * the target schema, removing the double indirection. Any remaining
170
+ * `__shared` entries (complex shared sub-schemas) are promoted to
171
+ * top-level component schemas with stable names.
172
+ */
173
+ transformRegistrySchemas(schemas) {
174
+ let result;
175
+ const serialized = JSON.stringify(schemas).replace(/"(\$ref)":"([^"#][^"]*)"/g, (_, key, id) => `"${key}":"#/components/schemas/${id}"`).replaceAll("{\"type\":\"integer\",\"minimum\":0,\"maximum\":9007199254740991}", "{\"$ref\":\"#/components/schemas/_nonNegativeInteger\"}").replaceAll("{\"type\":\"integer\",\"minimum\":-9007199254740991,\"maximum\":9007199254740991}", "{\"$ref\":\"#/components/schemas/_integer\"}");
176
+ result = JSON.parse(serialized);
177
+ const shared = result["__shared"];
178
+ if (shared?.$defs) {
179
+ const refMap = {};
180
+ const promoted = {};
181
+ for (const [key, entry] of Object.entries(shared.$defs)) {
182
+ const sharedRef = `#/components/schemas/__shared#/$defs/${key}`;
183
+ if (entry && typeof entry === "object" && "$ref" in entry && Object.keys(entry).length === 1) refMap[sharedRef] = entry.$ref;
184
+ else {
185
+ const promotedName = `_shared_${key}`;
186
+ promoted[promotedName] = entry;
187
+ refMap[sharedRef] = `#/components/schemas/${promotedName}`;
188
+ }
189
+ }
190
+ if (Object.keys(refMap).length > 0) {
191
+ const escapedKeys = Object.keys(refMap).map((k) => k.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
192
+ const pattern = new RegExp(`"\\$ref":"(${escapedKeys.join("|")})"`, "g");
193
+ const resolved = JSON.stringify(result).replace(pattern, (_, ref) => `"$ref":"${refMap[ref]}"`);
194
+ result = JSON.parse(resolved);
195
+ delete result["__shared"];
196
+ Object.assign(result, promoted);
197
+ }
198
+ }
199
+ for (const s of Object.values(result)) if (s && typeof s === "object") delete s["$schema"];
200
+ return result;
201
+ }
202
+ generateTags() {
203
+ return require_spec_utils.getIncludedModels(this.schema, this.queryOptions).map((modelName) => ({
204
+ name: (0, _zenstackhq_common_helpers.lowerCaseFirst)(modelName),
205
+ description: `${modelName} operations`
206
+ }));
207
+ }
208
+ generatePaths() {
209
+ const paths = {};
210
+ for (const modelName of require_spec_utils.getIncludedModels(this.schema, this.queryOptions)) {
211
+ const modelDef = this.schema.models[modelName];
212
+ const tag = (0, _zenstackhq_common_helpers.lowerCaseFirst)(modelName);
213
+ for (const op of _zenstackhq_orm.CoreCrudOperations) {
214
+ if (!require_spec_utils.isOperationIncluded(modelName, op, this.queryOptions)) continue;
215
+ const method = this.getHttpMethod(op);
216
+ const operation = this.buildModelOperation(modelName, modelDef, op, tag);
217
+ paths[`/${(0, _zenstackhq_common_helpers.lowerCaseFirst)(modelName)}/${op}`] = { [method]: operation };
218
+ }
219
+ }
220
+ if (this.schema.procedures) for (const [procName, procDef] of Object.entries(this.schema.procedures)) {
221
+ if (!require_spec_utils.isProcedureIncluded(procName, this.queryOptions)) continue;
222
+ const method = procDef.mutation ? "post" : "get";
223
+ paths[`/${require_spec_utils.PROCEDURE_ROUTE_PREFIXES}/${procName}`] = { [method]: this.buildProcedureOperation(procName, procDef, method) };
224
+ }
225
+ paths["/$transaction/sequential"] = { post: this.buildTransactionOperation() };
226
+ return paths;
227
+ }
228
+ getHttpMethod(op) {
229
+ if (GET_OPERATIONS.has(op)) return "get";
230
+ if (POST_OPERATIONS.has(op)) return "post";
231
+ if (PUT_OPERATIONS.has(op)) return "put";
232
+ if (DELETE_OPERATIONS.has(op)) return "delete";
233
+ return "post";
234
+ }
235
+ /**
236
+ * Returns a JSON Schema `$ref` pointing to the pre-built component schema for
237
+ * the given model+operation, or `undefined` if the schema was not registered
238
+ * (e.g. the operation is not supported for that model).
239
+ */
240
+ getOperationInputSchemaRef(modelName, op) {
241
+ const id = `${modelName}${(0, _zenstackhq_common_helpers.upperCaseFirst)(op)}Args`;
242
+ return id in this.registrySchemas ? { $ref: `#/components/schemas/${id}` } : void 0;
243
+ }
244
+ /**
245
+ * Maps an RPC operation name to its corresponding access policy operation type.
246
+ * Policy attributes use 'read', 'create', 'update', 'delete', 'all'.
247
+ */
248
+ policyOp(op) {
249
+ if (GET_OPERATIONS.has(op)) return "read";
250
+ if (DELETE_OPERATIONS.has(op)) return "delete";
251
+ if (PUT_OPERATIONS.has(op)) return "update";
252
+ return "create";
253
+ }
254
+ buildModelOperation(modelName, modelDef, op, tag) {
255
+ const isQueryOp = GET_OPERATIONS.has(op) || DELETE_OPERATIONS.has(op);
256
+ const successCode = POST_OPERATIONS.has(op) ? "201" : "200";
257
+ const inputSchemaRef = this.getOperationInputSchemaRef(modelName, op);
258
+ const operation = {
259
+ tags: [tag],
260
+ summary: OPERATION_INFO[op]?.summary(modelName) ?? `${modelName}.${op}`,
261
+ operationId: `${(0, _zenstackhq_common_helpers.lowerCaseFirst)(modelName)}_${op}`,
262
+ responses: {
263
+ [successCode]: {
264
+ description: "Operation succeeded",
265
+ content: { [JSON_CT]: { schema: this.buildOperationSuccessSchema(modelName, op) } }
266
+ },
267
+ "400": ERROR_400,
268
+ ...NOT_FOUND_OPERATIONS.has(op) && { "404": ERROR_404 },
269
+ "422": ERROR_422,
270
+ "500": ERROR_500,
271
+ ...require_spec_utils.mayDenyAccess(modelDef, this.policyOp(op), this.specOptions?.respectAccessPolicies) && { "403": ERROR_403 }
272
+ }
273
+ };
274
+ if (inputSchemaRef) if (isQueryOp) {
275
+ const inputSchemaId = `${modelName}${(0, _zenstackhq_common_helpers.upperCaseFirst)(op)}Args`;
276
+ const inputSchema = this.registrySchemas[inputSchemaId];
277
+ operation["parameters"] = [{
278
+ name: "q",
279
+ in: "query",
280
+ ...Array.isArray(inputSchema?.required) && inputSchema.required.length > 0 && { required: true },
281
+ description: `JSON-encoded arguments for ${modelName}.${op}`,
282
+ content: { [JSON_CT]: { schema: inputSchemaRef } }
283
+ }, {
284
+ name: "meta",
285
+ in: "query",
286
+ description: "JSON-encoded SuperJSON serialization metadata for the \"q\" parameter",
287
+ schema: { type: "string" }
288
+ }];
289
+ } else operation["requestBody"] = {
290
+ required: true,
291
+ content: { [JSON_CT]: { schema: inputSchemaRef } }
292
+ };
293
+ return operation;
294
+ }
295
+ buildProcedureOperation(procName, procDef, method) {
296
+ const argsSchemaId = `${procName}ProcArgs`;
297
+ const argsSchemaRef = argsSchemaId in this.registrySchemas ? { $ref: `#/components/schemas/${argsSchemaId}` } : { type: "object" };
298
+ const hasRequiredParams = Object.values(procDef.params ?? {}).some((p) => !p.optional);
299
+ const envelopeSchema = {
300
+ type: "object",
301
+ properties: { args: argsSchemaRef },
302
+ ...hasRequiredParams && { required: ["args"] }
303
+ };
304
+ const op = {
305
+ tags: ["$procs"],
306
+ summary: `Execute procedure \`${procName}\``,
307
+ operationId: `proc_${procName}`,
308
+ responses: {
309
+ "200": {
310
+ description: `Result of ${procName}`,
311
+ content: { [JSON_CT]: { schema: {
312
+ type: "object",
313
+ properties: {
314
+ data: this.getProcedureDataSchema(procDef),
315
+ meta: {
316
+ type: "object",
317
+ properties: { serialization: {} }
318
+ }
319
+ }
320
+ } } }
321
+ },
322
+ "400": ERROR_400,
323
+ "403": ERROR_403,
324
+ "404": ERROR_404,
325
+ "422": ERROR_422,
326
+ "500": ERROR_500
327
+ }
328
+ };
329
+ const hasParams = Object.keys(procDef.params ?? {}).length > 0;
330
+ if (method === "get") {
331
+ if (hasParams) op["parameters"] = [{
332
+ name: "q",
333
+ in: "query",
334
+ ...hasRequiredParams && { required: true },
335
+ description: `JSON-encoded arguments for procedure ${procName}`,
336
+ content: { [JSON_CT]: { schema: envelopeSchema } }
337
+ }, {
338
+ name: "meta",
339
+ in: "query",
340
+ description: "JSON-encoded SuperJSON serialization metadata for the \"q\" parameter",
341
+ schema: { type: "string" }
342
+ }];
343
+ } else if (hasParams) op["requestBody"] = {
344
+ ...hasRequiredParams && { required: true },
345
+ content: { [JSON_CT]: { schema: envelopeSchema } }
346
+ };
347
+ return op;
348
+ }
349
+ getProcedureDataSchema(procDef) {
350
+ const { returnType, returnArray } = procDef;
351
+ let base;
352
+ if (this.isBuiltinType(returnType)) base = this.builtinTypeToJsonSchema(returnType);
353
+ else if (this.schema.enums?.[returnType] || this.schema.models?.[returnType] || this.schema.typeDefs?.[returnType]) base = { $ref: `#/components/schemas/${returnType}` };
354
+ else base = {};
355
+ return returnArray ? {
356
+ type: "array",
357
+ items: base
358
+ } : base;
359
+ }
360
+ buildTransactionOperation() {
361
+ return {
362
+ tags: ["$transaction"],
363
+ summary: "Execute a sequential transaction",
364
+ operationId: "transaction_sequential",
365
+ requestBody: {
366
+ required: true,
367
+ content: { [JSON_CT]: { schema: { $ref: "#/components/schemas/_rpcTransactionRequest" } } }
368
+ },
369
+ responses: {
370
+ "200": {
371
+ description: "Transaction succeeded",
372
+ content: { [JSON_CT]: { schema: { $ref: "#/components/schemas/_rpcSuccessResponse" } } }
373
+ },
374
+ "400": ERROR_400,
375
+ "403": ERROR_403,
376
+ "404": ERROR_404,
377
+ "422": ERROR_422,
378
+ "500": ERROR_500
379
+ }
380
+ };
381
+ }
382
+ generateSharedSchemas() {
383
+ const enumSchemas = {};
384
+ for (const [enumName, enumDef] of Object.entries(this.schema.enums ?? {})) enumSchemas[enumName] = this.buildEnumSchema(enumDef);
385
+ const typeDefSchemas = {};
386
+ for (const [typeName, typeDef] of Object.entries(this.schema.typeDefs ?? {})) typeDefSchemas[typeName] = this.buildTypeDefSchema(typeDef);
387
+ const modelEntitySchemas = {};
388
+ for (const modelName of require_spec_utils.getIncludedModels(this.schema, this.queryOptions)) modelEntitySchemas[modelName] = this.buildModelEntitySchema(this.schema.models[modelName]);
389
+ return {
390
+ ...enumSchemas,
391
+ ...typeDefSchemas,
392
+ ...modelEntitySchemas,
393
+ _integer: {
394
+ type: "integer",
395
+ minimum: -9007199254740991,
396
+ maximum: 9007199254740991
397
+ },
398
+ _nonNegativeInteger: {
399
+ type: "integer",
400
+ minimum: 0,
401
+ maximum: 9007199254740991
402
+ },
403
+ _rpcSuccessResponse: {
404
+ type: "object",
405
+ properties: {
406
+ data: {},
407
+ meta: {
408
+ type: "object",
409
+ properties: { serialization: {} }
410
+ }
411
+ }
412
+ },
413
+ _rpcErrorResponse: {
414
+ type: "object",
415
+ properties: { error: {
416
+ type: "object",
417
+ properties: {
418
+ message: { type: "string" },
419
+ reason: { type: "string" },
420
+ model: { type: "string" },
421
+ rejectedByPolicy: { type: "boolean" },
422
+ rejectedByValidation: { type: "boolean" },
423
+ rejectReason: { type: "string" },
424
+ dbErrorCode: { type: "string" }
425
+ },
426
+ required: ["message"]
427
+ } },
428
+ required: ["error"]
429
+ },
430
+ _rpcTransactionRequest: {
431
+ type: "array",
432
+ items: {
433
+ type: "object",
434
+ properties: {
435
+ model: { type: "string" },
436
+ op: {
437
+ type: "string",
438
+ enum: [..._zenstackhq_orm.CoreCrudOperations]
439
+ },
440
+ args: { type: "object" }
441
+ },
442
+ required: ["model", "op"]
443
+ }
444
+ }
445
+ };
446
+ }
447
+ builtinTypeToJsonSchema(type) {
448
+ switch (type) {
449
+ case "String": return { type: "string" };
450
+ case "Boolean": return { type: "boolean" };
451
+ case "Int": return { type: "integer" };
452
+ case "Float": return { type: "number" };
453
+ case "BigInt": return { type: "integer" };
454
+ case "Decimal": return { type: "string" };
455
+ case "DateTime": return {
456
+ type: "string",
457
+ format: "date-time"
458
+ };
459
+ case "Bytes": return {
460
+ type: "string",
461
+ format: "byte"
462
+ };
463
+ default: return {};
464
+ }
465
+ }
466
+ isBuiltinType(type) {
467
+ return [
468
+ "String",
469
+ "Boolean",
470
+ "Int",
471
+ "Float",
472
+ "BigInt",
473
+ "Decimal",
474
+ "DateTime",
475
+ "Bytes",
476
+ "Json",
477
+ "Unsupported"
478
+ ].includes(type);
479
+ }
480
+ /**
481
+ * Builds a JSON Schema object describing a custom typedef
482
+ */
483
+ buildEnumSchema(enumDef) {
484
+ return {
485
+ type: "string",
486
+ enum: Object.values(enumDef.values)
487
+ };
488
+ }
489
+ buildTypeDefSchema(typeDef) {
490
+ const properties = {};
491
+ const required = [];
492
+ for (const [fieldName, fieldDef] of Object.entries(typeDef.fields)) {
493
+ const isRef = !this.isBuiltinType(fieldDef.type) && (this.schema.enums?.[fieldDef.type] || this.schema.typeDefs?.[fieldDef.type]);
494
+ const base = this.isBuiltinType(fieldDef.type) ? this.builtinTypeToJsonSchema(fieldDef.type) : isRef ? { $ref: `#/components/schemas/${fieldDef.type}` } : { type: "object" };
495
+ const fieldDesc = require_spec_utils.getMetaDescription(fieldDef.attributes);
496
+ if (fieldDesc && !isRef) base.description = fieldDesc;
497
+ const typed = fieldDef.array ? {
498
+ type: "array",
499
+ items: base
500
+ } : base;
501
+ properties[fieldName] = fieldDef.optional ? { anyOf: [typed, { type: "null" }] } : typed;
502
+ if (!fieldDef.optional) required.push(fieldName);
503
+ }
504
+ const schema = {
505
+ type: "object",
506
+ properties
507
+ };
508
+ if (required.length > 0) schema["required"] = required;
509
+ return schema;
510
+ }
511
+ /**
512
+ * Builds a JSON Schema object describing a model entity
513
+ */
514
+ buildModelEntitySchema(modelDef) {
515
+ const properties = {};
516
+ const required = [];
517
+ for (const [fieldName, fieldDef] of Object.entries(modelDef.fields)) {
518
+ if (fieldDef.omit) continue;
519
+ if (fieldDef.relation) {
520
+ const refSchema = { $ref: `#/components/schemas/${fieldDef.type}` };
521
+ const base = fieldDef.array ? {
522
+ type: "array",
523
+ items: refSchema
524
+ } : refSchema;
525
+ properties[fieldName] = !fieldDef.array && fieldDef.optional ? { anyOf: [base, { type: "null" }] } : base;
526
+ } else if (this.schema.enums?.[fieldDef.type]) {
527
+ const refSchema = { $ref: `#/components/schemas/${fieldDef.type}` };
528
+ const base = fieldDef.array ? {
529
+ type: "array",
530
+ items: refSchema
531
+ } : refSchema;
532
+ properties[fieldName] = fieldDef.optional ? { anyOf: [base, { type: "null" }] } : base;
533
+ required.push(fieldName);
534
+ } else if (this.isBuiltinType(fieldDef.type)) {
535
+ const base = this.builtinTypeToJsonSchema(fieldDef.type);
536
+ const fieldDesc = require_spec_utils.getMetaDescription(fieldDef.attributes);
537
+ if (fieldDesc) base.description = fieldDesc;
538
+ const typed = fieldDef.array ? {
539
+ type: "array",
540
+ items: base
541
+ } : base;
542
+ properties[fieldName] = fieldDef.optional ? { anyOf: [typed, { type: "null" }] } : typed;
543
+ required.push(fieldName);
544
+ } else if (this.schema.typeDefs?.[fieldDef.type]) {
545
+ const refSchema = { $ref: `#/components/schemas/${fieldDef.type}` };
546
+ const base = fieldDef.array ? {
547
+ type: "array",
548
+ items: refSchema
549
+ } : refSchema;
550
+ properties[fieldName] = fieldDef.optional ? { anyOf: [base, { type: "null" }] } : base;
551
+ required.push(fieldName);
552
+ } else {
553
+ properties[fieldName] = fieldDef.array ? {
554
+ type: "array",
555
+ items: { type: "object" }
556
+ } : { type: "object" };
557
+ required.push(fieldName);
558
+ }
559
+ }
560
+ const schema = {
561
+ type: "object",
562
+ properties
563
+ };
564
+ if (required.length > 0) schema["required"] = required;
565
+ const modelDesc = require_spec_utils.getMetaDescription(modelDef.attributes);
566
+ if (modelDesc) schema.description = modelDesc;
567
+ return schema;
568
+ }
569
+ buildOperationSuccessSchema(modelName, op) {
570
+ const entityRef = { $ref: `#/components/schemas/${modelName}` };
571
+ return {
572
+ type: "object",
573
+ properties: {
574
+ data: OPERATION_INFO[op]?.dataSchema(entityRef) ?? {},
575
+ meta: {
576
+ type: "object",
577
+ properties: { serialization: {} }
578
+ }
579
+ }
580
+ };
581
+ }
582
+ };
583
+ //#endregion
584
+ exports.RPCApiSpecGenerator = RPCApiSpecGenerator;
585
+
586
+ //# sourceMappingURL=openapi-BUzfCKi-.cjs.map