@globalart/zod-to-proto 1.0.14 → 1.0.16
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/dist/index.cjs +176 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +40 -0
- package/dist/index.d.mts +40 -0
- package/dist/index.mjs +166 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -6,12 +6,16 @@ var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
|
6
6
|
var __getProtoOf = Object.getPrototypeOf;
|
|
7
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
8
|
var __copyProps = (to, from, except, desc) => {
|
|
9
|
-
if (from && typeof from === "object" || typeof from === "function")
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
13
|
+
__defProp(to, key, {
|
|
14
|
+
get: ((k) => from[k]).bind(null, key),
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
15
19
|
}
|
|
16
20
|
return to;
|
|
17
21
|
};
|
|
@@ -26,18 +30,51 @@ inflection = __toESM(inflection);
|
|
|
26
30
|
let zod = require("zod");
|
|
27
31
|
|
|
28
32
|
//#region src/utils.ts
|
|
33
|
+
/**
|
|
34
|
+
* Determines the protobuf number type name based on Zod number schema.
|
|
35
|
+
* Returns "int32" for integers, "double" for floating point numbers.
|
|
36
|
+
*
|
|
37
|
+
* @param value - Zod number schema
|
|
38
|
+
* @returns Protobuf number type name ("int32" or "double")
|
|
39
|
+
*/
|
|
29
40
|
const getNumberTypeName = ({ value }) => {
|
|
30
41
|
return value.isInt ? "int32" : "double";
|
|
31
42
|
};
|
|
43
|
+
/**
|
|
44
|
+
* Converts a string to PascalCase format.
|
|
45
|
+
* Handles dot-separated strings by capitalizing each part.
|
|
46
|
+
*
|
|
47
|
+
* @param value - String to convert
|
|
48
|
+
* @returns PascalCase string
|
|
49
|
+
*/
|
|
32
50
|
const toPascalCase = ({ value }) => {
|
|
33
51
|
return value.split(".").map((part) => part.charAt(0).toUpperCase() + part.slice(1)).join("");
|
|
34
52
|
};
|
|
53
|
+
/**
|
|
54
|
+
* Converts a protobuf field definition to its type string representation.
|
|
55
|
+
* Filters out null/empty values and joins the remaining types.
|
|
56
|
+
*
|
|
57
|
+
* @param field - Protobuf field definition
|
|
58
|
+
* @returns Type string for the protobuf field
|
|
59
|
+
*/
|
|
35
60
|
const protobufFieldToType = ({ field }) => {
|
|
36
61
|
return field.types.filter(Boolean).join(" ");
|
|
37
62
|
};
|
|
38
63
|
|
|
39
64
|
//#endregion
|
|
40
65
|
//#region src/traversers.ts
|
|
66
|
+
/**
|
|
67
|
+
* Traverses a Zod array or set schema and converts it to protobuf repeated fields.
|
|
68
|
+
* Handles nested types and generates appropriate field definitions.
|
|
69
|
+
*
|
|
70
|
+
* @param key - Field name
|
|
71
|
+
* @param value - Zod array or set schema
|
|
72
|
+
* @param messages - Map of message names to their protobuf fields
|
|
73
|
+
* @param enums - Map of enum names to their protobuf values
|
|
74
|
+
* @param typePrefix - Optional prefix for type names
|
|
75
|
+
* @param parentKey - Optional parent message name for nested types
|
|
76
|
+
* @returns Array of protobuf field definitions
|
|
77
|
+
*/
|
|
41
78
|
const traverseArray = ({ key, value, messages, enums, typePrefix, parentKey }) => {
|
|
42
79
|
const nestedValue = value instanceof zod.ZodArray ? value.element : value instanceof zod.ZodSet ? value.def.valueType : value.def.element;
|
|
43
80
|
const singularKey = inflection.singularize(key);
|
|
@@ -56,6 +93,18 @@ const traverseArray = ({ key, value, messages, enums, typePrefix, parentKey }) =
|
|
|
56
93
|
name: field.name.replace(singularKey, key)
|
|
57
94
|
}));
|
|
58
95
|
};
|
|
96
|
+
/**
|
|
97
|
+
* Traverses a Zod map schema and converts it to a protobuf map type.
|
|
98
|
+
* Validates that both key and value types are simple types suitable for map keys/values.
|
|
99
|
+
*
|
|
100
|
+
* @param key - Field name
|
|
101
|
+
* @param value - Zod map schema
|
|
102
|
+
* @param messages - Map of message names to their protobuf fields
|
|
103
|
+
* @param enums - Map of enum names to their protobuf values
|
|
104
|
+
* @param typePrefix - Optional prefix for type names
|
|
105
|
+
* @param parentKey - Optional parent message name for nested types
|
|
106
|
+
* @returns Array containing a single protobuf map field definition, or empty array if invalid
|
|
107
|
+
*/
|
|
59
108
|
const traverseMap = ({ key, value, messages, enums, typePrefix, parentKey }) => {
|
|
60
109
|
const mapDef = value.def;
|
|
61
110
|
const keyType = traverseKey({
|
|
@@ -85,6 +134,18 @@ const traverseMap = ({ key, value, messages, enums, typePrefix, parentKey }) =>
|
|
|
85
134
|
name: key
|
|
86
135
|
}];
|
|
87
136
|
};
|
|
137
|
+
/**
|
|
138
|
+
* Traverses a Zod record schema and converts it to a protobuf map type.
|
|
139
|
+
* Similar to traverseMap but handles ZodRecord type.
|
|
140
|
+
*
|
|
141
|
+
* @param key - Field name
|
|
142
|
+
* @param value - Zod record schema
|
|
143
|
+
* @param messages - Map of message names to their protobuf fields
|
|
144
|
+
* @param enums - Map of enum names to their protobuf values
|
|
145
|
+
* @param typePrefix - Optional prefix for type names
|
|
146
|
+
* @param parentKey - Optional parent message name for nested types
|
|
147
|
+
* @returns Array containing a single protobuf map field definition, or empty array if invalid
|
|
148
|
+
*/
|
|
88
149
|
const traverseRecord = ({ key, value, messages, enums, typePrefix, parentKey }) => {
|
|
89
150
|
const recordDef = value.def;
|
|
90
151
|
const keyType = traverseKey({
|
|
@@ -114,6 +175,20 @@ const traverseRecord = ({ key, value, messages, enums, typePrefix, parentKey })
|
|
|
114
175
|
name: key
|
|
115
176
|
}];
|
|
116
177
|
};
|
|
178
|
+
/**
|
|
179
|
+
* Traverses a single key-value pair from a Zod schema and converts it to protobuf field definitions.
|
|
180
|
+
* Handles various Zod types including primitives, objects, arrays, maps, enums, tuples, and optional/nullable fields.
|
|
181
|
+
*
|
|
182
|
+
* @param key - Field name
|
|
183
|
+
* @param value - Zod schema value
|
|
184
|
+
* @param messages - Map of message names to their protobuf fields
|
|
185
|
+
* @param enums - Map of enum names to their protobuf values
|
|
186
|
+
* @param isOptional - Whether the field is optional
|
|
187
|
+
* @param isInArray - Whether the field is inside an array
|
|
188
|
+
* @param typePrefix - Optional prefix for type names
|
|
189
|
+
* @param parentKey - Optional parent message name for nested types
|
|
190
|
+
* @returns Array of protobuf field definitions
|
|
191
|
+
*/
|
|
117
192
|
const traverseKey = ({ key, value, messages, enums, isOptional, isInArray, typePrefix, parentKey }) => {
|
|
118
193
|
if (!value) return [];
|
|
119
194
|
if (value instanceof zod.ZodOptional || value instanceof zod.ZodNullable) return traverseKey({
|
|
@@ -201,6 +276,15 @@ const traverseKey = ({ key, value, messages, enums, isOptional, isInArray, typeP
|
|
|
201
276
|
types: [optional, "int64"],
|
|
202
277
|
name: key
|
|
203
278
|
}];
|
|
279
|
+
if (value instanceof zod.ZodType) {
|
|
280
|
+
const def = value.def;
|
|
281
|
+
if (def.type === "custom" && def.check === "custom" && def.fn) try {
|
|
282
|
+
if (typeof Buffer !== "undefined" && def.fn(Buffer.alloc(0)) || typeof Uint8Array !== "undefined" && def.fn(new Uint8Array(0))) return [{
|
|
283
|
+
types: [optional, "bytes"],
|
|
284
|
+
name: key
|
|
285
|
+
}];
|
|
286
|
+
} catch {}
|
|
287
|
+
}
|
|
204
288
|
if (value instanceof zod.ZodTuple) {
|
|
205
289
|
const tupleFields = value.def.items.flatMap((item, index) => {
|
|
206
290
|
return traverseKey({
|
|
@@ -227,6 +311,17 @@ const traverseKey = ({ key, value, messages, enums, isOptional, isInArray, typeP
|
|
|
227
311
|
if (value instanceof zod.ZodType) return [];
|
|
228
312
|
return [];
|
|
229
313
|
};
|
|
314
|
+
/**
|
|
315
|
+
* Traverses a Zod object schema and converts it to protobuf message field definitions.
|
|
316
|
+
* Processes all fields in the schema and generates appropriate protobuf types.
|
|
317
|
+
*
|
|
318
|
+
* @param schema - Zod schema to traverse
|
|
319
|
+
* @param messages - Map of message names to their protobuf fields
|
|
320
|
+
* @param enums - Map of enum names to their protobuf values
|
|
321
|
+
* @param typePrefix - Optional prefix for type names
|
|
322
|
+
* @param parentKey - Optional parent message name for nested types
|
|
323
|
+
* @returns Array of protobuf field definition strings
|
|
324
|
+
*/
|
|
230
325
|
const traverseSchema = ({ schema, messages, enums, typePrefix, parentKey }) => {
|
|
231
326
|
if (!schema || typeof schema !== "object" || !("def" in schema) || schema.constructor.name !== "ZodObject" && schema.def.type !== "object") return [];
|
|
232
327
|
const zodObject = schema;
|
|
@@ -246,6 +341,14 @@ const traverseSchema = ({ schema, messages, enums, typePrefix, parentKey }) => {
|
|
|
246
341
|
|
|
247
342
|
//#endregion
|
|
248
343
|
//#region src/service-generator.ts
|
|
344
|
+
/**
|
|
345
|
+
* Parses a Zod service schema and extracts method definitions.
|
|
346
|
+
* Each method must be a function with one argument (request) and a return value (response).
|
|
347
|
+
*
|
|
348
|
+
* @param name - Service name
|
|
349
|
+
* @param schema - Zod object containing service methods as functions
|
|
350
|
+
* @returns Service definition with extracted methods
|
|
351
|
+
*/
|
|
249
352
|
const parseZodServiceSchema = (name, schema) => {
|
|
250
353
|
const shape = schema.shape;
|
|
251
354
|
const methods = [];
|
|
@@ -257,10 +360,15 @@ const parseZodServiceSchema = (name, schema) => {
|
|
|
257
360
|
if (args.length > 0 && args[0] && output) {
|
|
258
361
|
const request = args[0];
|
|
259
362
|
const response = output;
|
|
363
|
+
let streaming;
|
|
364
|
+
const metadata = methodSchema.meta();
|
|
365
|
+
console.log(methodSchema.meta());
|
|
366
|
+
streaming = metadata?.streaming;
|
|
260
367
|
methods.push({
|
|
261
368
|
name: methodName,
|
|
262
369
|
request,
|
|
263
|
-
response
|
|
370
|
+
response,
|
|
371
|
+
streaming
|
|
264
372
|
});
|
|
265
373
|
}
|
|
266
374
|
}
|
|
@@ -270,22 +378,60 @@ const parseZodServiceSchema = (name, schema) => {
|
|
|
270
378
|
methods
|
|
271
379
|
};
|
|
272
380
|
};
|
|
381
|
+
/**
|
|
382
|
+
* Normalizes service input data into an array of service definitions.
|
|
383
|
+
* If an array is passed, returns it as is.
|
|
384
|
+
* If an object is passed, converts each key-value pair into a service definition.
|
|
385
|
+
*
|
|
386
|
+
* @param services - Array of service definitions or object with Zod schemas
|
|
387
|
+
* @returns Array of normalized service definitions
|
|
388
|
+
*/
|
|
273
389
|
const normalizeServices = (services) => {
|
|
274
390
|
if (Array.isArray(services)) return services;
|
|
275
391
|
return Object.entries(services).map(([name, schema]) => parseZodServiceSchema(name, schema));
|
|
276
392
|
};
|
|
393
|
+
/**
|
|
394
|
+
* Ensures that the schema is a ZodObject.
|
|
395
|
+
* If the schema is already an object, returns it as is.
|
|
396
|
+
* Otherwise, wraps the schema in an object with a "data" field.
|
|
397
|
+
*
|
|
398
|
+
* @param schema - Zod schema of any type
|
|
399
|
+
* @returns ZodObject containing the original schema
|
|
400
|
+
*/
|
|
277
401
|
const ensureZodObject = (schema) => {
|
|
278
402
|
if ((schema.def.type || schema.constructor.name) === "object" || schema.constructor.name === "ZodObject") return schema;
|
|
279
403
|
return zod.z.object({ data: schema });
|
|
280
404
|
};
|
|
405
|
+
/**
|
|
406
|
+
* Generates the name for a request message based on the method name.
|
|
407
|
+
*
|
|
408
|
+
* @param methodName - Name of the service method
|
|
409
|
+
* @param typePrefix - Optional prefix for type names
|
|
410
|
+
* @returns Generated request message name in PascalCase
|
|
411
|
+
*/
|
|
281
412
|
const generateRequestMessageName = (methodName, typePrefix) => {
|
|
282
413
|
const messageName = toPascalCase({ value: `${methodName}Request` });
|
|
283
414
|
return typePrefix ? `${typePrefix}${messageName}` : messageName;
|
|
284
415
|
};
|
|
416
|
+
/**
|
|
417
|
+
* Generates the name for a response message based on the method name.
|
|
418
|
+
*
|
|
419
|
+
* @param methodName - Name of the service method
|
|
420
|
+
* @param typePrefix - Optional prefix for type names
|
|
421
|
+
* @returns Generated response message name in PascalCase
|
|
422
|
+
*/
|
|
285
423
|
const generateResponseMessageName = (methodName, typePrefix) => {
|
|
286
424
|
const messageName = toPascalCase({ value: `${methodName}Response` });
|
|
287
425
|
return typePrefix ? `${typePrefix}${messageName}` : messageName;
|
|
288
426
|
};
|
|
427
|
+
/**
|
|
428
|
+
* Processes a service method by generating request and response message names
|
|
429
|
+
* and traversing their schemas to populate the context with message definitions.
|
|
430
|
+
*
|
|
431
|
+
* @param method - Service method definition
|
|
432
|
+
* @param context - Generation context containing messages, enums, and type prefix
|
|
433
|
+
* @returns Object containing request and response message names
|
|
434
|
+
*/
|
|
289
435
|
const processServiceMethod = (method, context) => {
|
|
290
436
|
const { messages, enums, typePrefix } = context;
|
|
291
437
|
const requestName = generateRequestMessageName(method.name, typePrefix);
|
|
@@ -315,6 +461,14 @@ const processServiceMethod = (method, context) => {
|
|
|
315
461
|
responseName
|
|
316
462
|
};
|
|
317
463
|
};
|
|
464
|
+
/**
|
|
465
|
+
* Generates protobuf service definitions from Zod service schemas.
|
|
466
|
+
* Supports streaming methods (client, server, bidirectional).
|
|
467
|
+
*
|
|
468
|
+
* @param services - Service definitions as array or object with Zod schemas
|
|
469
|
+
* @param context - Generation context containing messages, enums, and type prefix
|
|
470
|
+
* @returns Array of protobuf service definition strings
|
|
471
|
+
*/
|
|
318
472
|
const generateServices = (services, context) => {
|
|
319
473
|
return normalizeServices(services).map((service) => {
|
|
320
474
|
const methods = service.methods.map((method) => {
|
|
@@ -331,9 +485,24 @@ const generateServices = (services, context) => {
|
|
|
331
485
|
|
|
332
486
|
//#endregion
|
|
333
487
|
//#region src/zod-to-protobuf.ts
|
|
488
|
+
/**
|
|
489
|
+
* Generates a protobuf definition string for services only (without root message).
|
|
490
|
+
* Convenience function that calls zodToProtobuf with an empty schema.
|
|
491
|
+
*
|
|
492
|
+
* @param options - Options for protobuf generation
|
|
493
|
+
* @returns Protobuf definition string
|
|
494
|
+
*/
|
|
334
495
|
const zodToProtobufService = (options = {}) => {
|
|
335
496
|
return zodToProtobuf(zod.z.object(), options);
|
|
336
497
|
};
|
|
498
|
+
/**
|
|
499
|
+
* Converts a Zod schema to a protobuf definition string.
|
|
500
|
+
* Generates messages, enums, and services based on the provided schema and options.
|
|
501
|
+
*
|
|
502
|
+
* @param schema - Optional Zod schema to convert (if not provided, only services will be generated)
|
|
503
|
+
* @param options - Options for protobuf generation including package name, root message name, type prefix, and services
|
|
504
|
+
* @returns Complete protobuf definition string
|
|
505
|
+
*/
|
|
337
506
|
const zodToProtobuf = (schema, options = {}) => {
|
|
338
507
|
const { packageName = "default", rootMessageName = "Message", typePrefix = "", services, skipRootMessage = false } = options;
|
|
339
508
|
const messages = /* @__PURE__ */ new Map();
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["ZodArray","ZodSet","ZodObject","ZodOptional","ZodNullable","ZodMap","ZodRecord","ZodString","ZodNumber","ZodBoolean","ZodEnum","ZodDate","ZodBigInt","ZodTuple","tupleFields: ProtobufField[]","ZodType","methods: ServiceMethod[]","z","z"],"sources":["../src/utils.ts","../src/traversers.ts","../src/service-generator.ts","../src/zod-to-protobuf.ts"],"sourcesContent":["import { ZodNumber } from \"zod\";\nimport type { ProtobufField } from \"./types\";\n\nexport const getNumberTypeName = ({ value }: { value: ZodNumber }): string => {\n return value.isInt ? \"int32\" : \"double\";\n};\n\nexport const toPascalCase = ({ value }: { value: string }): string => {\n return value\n .split(\".\")\n .map((part) => part.charAt(0).toUpperCase() + part.slice(1))\n .join(\"\");\n};\n\nexport const protobufFieldToType = ({\n field,\n}: {\n field: ProtobufField;\n}): string => {\n return field.types.filter(Boolean).join(\" \");\n};\n","import * as inflection from \"inflection\";\nimport {\n ZodArray,\n ZodBigInt,\n ZodBoolean,\n ZodDate,\n ZodEnum,\n ZodMap,\n ZodNullable,\n ZodNumber,\n ZodObject,\n ZodOptional,\n ZodRecord,\n ZodSet,\n ZodString,\n ZodTuple,\n ZodType,\n type ZodTypeAny,\n} from \"zod\";\nimport {\n ZodArrayDefinition,\n ZodMapDefinition,\n ZodRecordDefinition,\n type ProtobufField,\n} from \"./types\";\nimport { getNumberTypeName, toPascalCase, protobufFieldToType } from \"./utils\";\n\nexport const traverseArray = ({\n key,\n value,\n messages,\n enums,\n typePrefix,\n parentKey,\n}: {\n key: string;\n value: ZodArray<ZodTypeAny> | ZodSet<ZodTypeAny>;\n messages: Map<string, string[]>;\n enums: Map<string, string[]>;\n typePrefix: string | null;\n parentKey?: string;\n}): ProtobufField[] => {\n const nestedValue =\n value instanceof ZodArray\n ? value.element\n : value instanceof ZodSet\n ? (value.def as unknown as ZodArrayDefinition).valueType\n : // @ts-expect-error\n (value.def as unknown as ZodArrayDefinition).element;\n\n const singularKey = inflection.singularize(key);\n const elementFields = traverseKey({\n key: singularKey,\n value: nestedValue,\n messages,\n enums,\n isOptional: false,\n isInArray: true,\n typePrefix,\n parentKey: nestedValue instanceof ZodObject ? parentKey : undefined,\n });\n return elementFields.map((field) => ({\n ...field,\n types: [\"repeated\", ...field.types],\n name: field.name.replace(singularKey, key),\n }));\n};\n\nexport const traverseMap = ({\n key,\n value,\n messages,\n enums,\n typePrefix,\n parentKey,\n}: {\n key: string;\n value: ZodMap<ZodTypeAny, ZodTypeAny>;\n messages: Map<string, string[]>;\n enums: Map<string, string[]>;\n typePrefix: string | null;\n parentKey?: string;\n}): ProtobufField[] => {\n const mapDef = value.def as ZodMapDefinition;\n\n const keyType = traverseKey({\n key: inflection.singularize(key),\n value: mapDef.keyType,\n messages,\n enums,\n isOptional: false,\n isInArray: true,\n typePrefix,\n parentKey,\n });\n const valueType = traverseKey({\n key: inflection.singularize(key),\n value: mapDef.valueType,\n messages,\n enums,\n isOptional: false,\n isInArray: true,\n typePrefix,\n parentKey,\n });\n\n if (!keyType[0] || keyType.length !== 1) {\n return [];\n }\n\n if (!valueType[0] || valueType.length !== 1) {\n return [];\n }\n\n const mapType = `map<${protobufFieldToType({ field: keyType[0] })}, ${protobufFieldToType({ field: valueType[0] })}>`;\n return [\n {\n types: [mapType],\n name: key,\n },\n ];\n};\n\nexport const traverseRecord = ({\n key,\n value,\n messages,\n enums,\n typePrefix,\n parentKey,\n}: {\n key: string;\n value: ZodRecord;\n messages: Map<string, string[]>;\n enums: Map<string, string[]>;\n typePrefix: string | null;\n parentKey?: string;\n}): ProtobufField[] => {\n const recordDef = value.def as unknown as ZodRecordDefinition;\n\n const keyType = traverseKey({\n key: inflection.singularize(key),\n value: recordDef.keyType,\n messages,\n enums,\n isOptional: false,\n isInArray: true,\n typePrefix,\n parentKey,\n });\n const valueType = traverseKey({\n key: inflection.singularize(key),\n value: recordDef.valueType,\n messages,\n enums,\n isOptional: false,\n isInArray: true,\n typePrefix,\n parentKey,\n });\n\n if (!keyType[0] || keyType.length !== 1) {\n return [];\n }\n\n if (!valueType[0] || valueType.length !== 1) {\n return [];\n }\n\n const mapType = `map<${protobufFieldToType({ field: keyType[0] })}, ${protobufFieldToType({ field: valueType[0] })}>`;\n return [\n {\n types: [mapType],\n name: key,\n },\n ];\n};\n\nexport const traverseKey = ({\n key,\n value,\n messages,\n enums,\n isOptional,\n isInArray,\n typePrefix,\n parentKey,\n}: {\n key: string;\n value: unknown;\n messages: Map<string, string[]>;\n enums: Map<string, string[]>;\n isOptional: boolean;\n isInArray: boolean;\n typePrefix: string | null;\n parentKey?: string;\n}): ProtobufField[] => {\n if (!value) {\n return [];\n }\n\n if (value instanceof ZodOptional || value instanceof ZodNullable) {\n return traverseKey({\n key,\n value: value.unwrap(),\n messages,\n enums,\n isOptional: true,\n isInArray,\n typePrefix,\n parentKey,\n });\n }\n\n if (value instanceof ZodArray || value instanceof ZodSet) {\n return traverseArray({\n key,\n value: value as ZodArray<ZodTypeAny> | ZodSet<ZodTypeAny>,\n messages,\n enums,\n typePrefix,\n parentKey,\n });\n }\n\n if (value instanceof ZodMap) {\n return traverseMap({\n key,\n value: value as ZodMap<ZodTypeAny, ZodTypeAny>,\n messages,\n enums,\n typePrefix,\n parentKey,\n });\n }\n\n if (value instanceof ZodRecord) {\n return traverseRecord({\n key,\n value: value as ZodRecord,\n messages,\n enums,\n typePrefix,\n parentKey,\n });\n }\n\n const optional = isOptional && !isInArray ? \"optional\" : null;\n\n if (value instanceof ZodObject) {\n let messageName = toPascalCase({ value: key });\n if (parentKey) {\n const isParentAlreadyPascalCase = /^[A-Z][a-zA-Z0-9]*$/.test(parentKey);\n if (isParentAlreadyPascalCase) {\n messageName = `${parentKey}${messageName}`;\n } else {\n const parentMessageName = toPascalCase({ value: parentKey });\n messageName = `${parentMessageName}${messageName}`;\n }\n }\n if (typePrefix) {\n messageName = `${typePrefix}${messageName}`;\n }\n const nestedMessageFields = traverseSchema({\n schema: value,\n messages,\n enums,\n typePrefix,\n parentKey: messageName,\n });\n messages.set(messageName, nestedMessageFields);\n return [\n {\n types: [optional, messageName],\n name: key,\n },\n ];\n }\n\n if (value instanceof ZodString) {\n return [\n {\n types: [optional, \"string\"],\n name: key,\n },\n ];\n }\n\n if (value instanceof ZodNumber) {\n const typeName = getNumberTypeName({ value });\n return [\n {\n types: [optional, typeName],\n name: key,\n },\n ];\n }\n\n if (value instanceof ZodBoolean) {\n return [\n {\n types: [optional, \"bool\"],\n name: key,\n },\n ];\n }\n\n if (value instanceof ZodEnum) {\n const enumFields = value.options\n .map(\n (option: string | number, index: number) =>\n ` ${String(option)} = ${index};`\n )\n .join(\"\\n\");\n let enumName = toPascalCase({ value: key });\n if (parentKey) {\n const isParentAlreadyPascalCase = /^[A-Z][a-zA-Z0-9]*$/.test(parentKey);\n if (isParentAlreadyPascalCase) {\n enumName = `${parentKey}${enumName}`;\n } else {\n const parentMessageName = toPascalCase({ value: parentKey });\n enumName = `${parentMessageName}${enumName}`;\n }\n }\n if (typePrefix) {\n enumName = `${typePrefix}${enumName}`;\n }\n enums.set(enumName, [`enum ${enumName} {\\n${enumFields}\\n}`]);\n return [\n {\n types: [optional, enumName],\n name: key,\n },\n ];\n }\n\n if (value instanceof ZodDate) {\n return [\n {\n types: [optional, \"string\"],\n name: key,\n },\n ];\n }\n\n if (value instanceof ZodBigInt) {\n return [\n {\n types: [optional, \"int64\"],\n name: key,\n },\n ];\n }\n\n if (value instanceof ZodTuple) {\n const tupleFields: ProtobufField[] = (\n value.def.items as ZodTypeAny[]\n ).flatMap((item: ZodTypeAny, index: number) => {\n return traverseKey({\n key: `${key}_${index}`,\n value: item,\n messages,\n enums,\n isOptional: false,\n isInArray,\n typePrefix,\n parentKey,\n });\n });\n\n let tupleMessageName = toPascalCase({ value: key });\n if (parentKey) {\n const isParentAlreadyPascalCase = /^[A-Z][a-zA-Z0-9]*$/.test(parentKey);\n if (isParentAlreadyPascalCase) {\n tupleMessageName = `${parentKey}${tupleMessageName}`;\n } else {\n const parentMessageName = toPascalCase({ value: parentKey });\n tupleMessageName = `${parentMessageName}${tupleMessageName}`;\n }\n }\n if (typePrefix) {\n tupleMessageName = `${typePrefix}${tupleMessageName}`;\n }\n messages.set(\n tupleMessageName,\n tupleFields.map(\n (field, index) =>\n ` ${field.types.join(\" \")} ${field.name} = ${index + 1};`\n )\n );\n return [\n {\n types: [optional, tupleMessageName],\n name: key,\n },\n ];\n }\n\n if (value instanceof ZodType) {\n return [];\n }\n\n return [];\n};\n\nexport const traverseSchema = ({\n schema,\n messages,\n enums,\n typePrefix,\n parentKey,\n}: {\n schema: ZodTypeAny;\n messages: Map<string, string[]>;\n enums: Map<string, string[]>;\n typePrefix: string | null;\n parentKey?: string;\n}): string[] => {\n if (\n !schema ||\n typeof schema !== \"object\" ||\n !(\"def\" in schema) ||\n (schema.constructor.name !== \"ZodObject\" &&\n (schema.def as { type?: string }).type !== \"object\")\n ) {\n return [];\n }\n\n const zodObject = schema as ZodObject<any>;\n const fields = Object.entries(zodObject.shape).flatMap(([key, value]) => {\n return traverseKey({\n key,\n value,\n messages,\n enums,\n isOptional: false,\n isInArray: false,\n typePrefix,\n parentKey,\n });\n });\n\n return fields.map(\n (field, index) =>\n `${protobufFieldToType({ field })} ${field.name} = ${index + 1};`\n );\n};\n","import { z, ZodObject, type ZodTypeAny } from \"zod\";\nimport type {\n ServiceDefinition,\n ServiceMethod,\n ServicesInput,\n ZodFunctionDefinition,\n ZodTupleDefinition,\n} from \"./types\";\nimport { toPascalCase } from \"./utils\";\nimport { traverseSchema } from \"./traversers\";\n\ninterface ServiceGenerationContext {\n messages: Map<string, string[]>;\n enums: Map<string, string[]>;\n typePrefix: string | null;\n}\n\nconst parseZodServiceSchema = (\n name: string,\n schema: ZodObject<Record<string, ZodTypeAny>>\n): ServiceDefinition => {\n const shape = schema.shape as Record<string, ZodTypeAny>;\n const methods: ServiceMethod[] = [];\n\n for (const [methodName, methodSchema] of Object.entries(shape)) {\n const methodDef = (methodSchema as ZodTypeAny).def as ZodFunctionDefinition;\n\n if (methodDef.type === \"function\") {\n const inputDef = methodDef.input;\n\n const args = (inputDef?.def as ZodTupleDefinition)?.items ?? [];\n const output = methodDef.output as ZodTypeAny;\n\n if (args.length > 0 && args[0] && output) {\n const request = args[0];\n const response = output;\n\n methods.push({\n name: methodName,\n request,\n response,\n });\n }\n }\n }\n\n return {\n name,\n methods,\n };\n};\n\nconst normalizeServices = (services: ServicesInput): ServiceDefinition[] => {\n if (Array.isArray(services)) {\n return services;\n }\n\n return Object.entries(services).map(([name, schema]) =>\n parseZodServiceSchema(name, schema)\n );\n};\n\nconst ensureZodObject = (\n schema: ZodTypeAny\n): ZodObject<Record<string, ZodTypeAny>> => {\n const schemaType =\n (schema.def as { type?: string }).type || schema.constructor.name;\n\n if (schemaType === \"object\" || schema.constructor.name === \"ZodObject\") {\n return schema as ZodObject<Record<string, ZodTypeAny>>;\n }\n\n return z.object({\n data: schema,\n });\n};\n\nconst generateRequestMessageName = (\n methodName: string,\n typePrefix: string | null\n): string => {\n const messageName = toPascalCase({ value: `${methodName}Request` });\n return typePrefix ? `${typePrefix}${messageName}` : messageName;\n};\n\nconst generateResponseMessageName = (\n methodName: string,\n typePrefix: string | null\n): string => {\n const messageName = toPascalCase({ value: `${methodName}Response` });\n return typePrefix ? `${typePrefix}${messageName}` : messageName;\n};\n\nconst processServiceMethod = (\n method: ServiceMethod,\n context: ServiceGenerationContext\n): { requestName: string; responseName: string } => {\n const { messages, enums, typePrefix } = context;\n\n const requestName = generateRequestMessageName(method.name, typePrefix);\n const responseName = generateResponseMessageName(method.name, typePrefix);\n\n if (!messages.has(requestName)) {\n const requestSchema = ensureZodObject(method.request);\n const requestFields = traverseSchema({\n schema: requestSchema,\n messages,\n enums,\n typePrefix,\n parentKey: requestName,\n });\n messages.set(requestName, requestFields);\n }\n\n if (!messages.has(responseName)) {\n const responseSchema = ensureZodObject(method.response);\n const responseFields = traverseSchema({\n schema: responseSchema,\n messages,\n enums,\n typePrefix,\n parentKey: responseName,\n });\n messages.set(responseName, responseFields);\n }\n\n return { requestName, responseName };\n};\n\nexport const generateServices = (\n services: ServicesInput,\n context: ServiceGenerationContext\n): string[] => {\n const normalizedServices = normalizeServices(services);\n\n return normalizedServices.map((service) => {\n const methods = service.methods.map((method) => {\n const { requestName, responseName } = processServiceMethod(\n method,\n context\n );\n\n const requestStreaming =\n method.streaming === \"client\" || method.streaming === \"bidirectional\";\n const responseStreaming =\n method.streaming === \"server\" || method.streaming === \"bidirectional\";\n\n const requestType = requestStreaming\n ? `stream ${requestName}`\n : requestName;\n const responseType = responseStreaming\n ? `stream ${responseName}`\n : responseName;\n\n return ` rpc ${toPascalCase({ value: method.name })}(${requestType}) returns (${responseType});`;\n });\n\n return `service ${toPascalCase({ value: service.name })} {\\n${methods.join(\"\\n\")}\\n}`;\n });\n};\n","import type { ZodTypeAny } from \"zod\";\nimport type { ZodToProtobufOptions } from \"./types\";\nimport { traverseSchema } from \"./traversers\";\nimport { generateServices } from \"./service-generator\";\nimport { z } from \"zod\";\n\nexport const zodToProtobufService = (\n options: ZodToProtobufOptions = {}\n): string => {\n return zodToProtobuf(z.object(), options);\n};\n\nexport const zodToProtobuf = (\n schema?: ZodTypeAny,\n options: ZodToProtobufOptions = {}\n): string => {\n const {\n packageName = \"default\",\n rootMessageName = \"Message\",\n typePrefix = \"\",\n services,\n skipRootMessage = false,\n } = options;\n\n const messages = new Map<string, string[]>();\n const enums = new Map<string, string[]>();\n\n if (schema && !skipRootMessage) {\n const fields = traverseSchema({ schema, messages, enums, typePrefix });\n if (fields.length > 0) {\n const rootMessageKey = `${typePrefix}${rootMessageName}`;\n messages.set(rootMessageKey, fields);\n }\n }\n\n const context = {\n messages,\n enums,\n typePrefix: typePrefix || null,\n };\n\n const hasServices =\n services &&\n (Array.isArray(services)\n ? services.length > 0\n : Object.keys(services).length > 0);\n\n const servicesString = hasServices ? generateServices(services, context) : [];\n\n const enumsString = Array.from(enums.values()).map((enumDef) =>\n enumDef.join(\"\\n\")\n );\n\n const messagesString = Array.from(messages.entries()).map(\n ([name, fields]) =>\n `message ${name} {\\n${fields.map((field) => ` ${field}`).join(\"\\n\")}\\n}`\n );\n\n const content = [servicesString, enumsString, messagesString]\n .filter((strings) => !!strings.length)\n .map((strings) => strings.join(\"\\n\\n\"))\n .join(\"\\n\\n\");\n\n const protoDefinition = `\nsyntax = \"proto3\";\npackage ${packageName};\n\n${content}\n`;\n\n return protoDefinition.trim();\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGA,MAAa,qBAAqB,EAAE,YAA0C;AAC5E,QAAO,MAAM,QAAQ,UAAU;;AAGjC,MAAa,gBAAgB,EAAE,YAAuC;AACpE,QAAO,MACJ,MAAM,IAAI,CACV,KAAK,SAAS,KAAK,OAAO,EAAE,CAAC,aAAa,GAAG,KAAK,MAAM,EAAE,CAAC,CAC3D,KAAK,GAAG;;AAGb,MAAa,uBAAuB,EAClC,YAGY;AACZ,QAAO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UAAU;EACd;EACA;EACA,YAAY,cAAc;EAC3B;AA+BD,QAPwB;;UAEhB,YAAY;;EAPJ;EAhBd,aACC,MAAM,QAAQ,SAAS,GACpB,SAAS,SAAS,IAClB,OAAO,KAAK,SAAS,CAAC,SAAS,KAEA,iBAAiB,UAAU,QAAQ,GAAG,EAAE;EAEzD,MAAM,KAAK,MAAM,QAAQ,CAAC,CAAC,KAAK,YAClD,QAAQ,KAAK,KAAK,CACnB;EAEsB,MAAM,KAAK,SAAS,SAAS,CAAC,CAAC,KACnD,CAAC,MAAM,YACN,WAAW,KAAK,MAAM,OAAO,KAAK,UAAU,OAAO,QAAQ,CAAC,KAAK,KAAK,CAAC,KAC1E;EAE4D,CAC1D,QAAQ,YAAY,CAAC,CAAC,QAAQ,OAAO,CACrC,KAAK,YAAY,QAAQ,KAAK,OAAO,CAAC,CACtC,KAAK,OAAO,CAMP;EAGe,MAAM"}
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["ZodArray","ZodSet","ZodObject","ZodOptional","ZodNullable","ZodMap","ZodRecord","ZodString","ZodNumber","ZodBoolean","ZodEnum","ZodDate","ZodBigInt","ZodType","ZodTuple","tupleFields: ProtobufField[]","methods: ServiceMethod[]","streaming: \"client\" | \"server\" | \"bidirectional\" | undefined","z","z"],"sources":["../src/utils.ts","../src/traversers.ts","../src/service-generator.ts","../src/zod-to-protobuf.ts"],"sourcesContent":["import { ZodNumber } from \"zod\";\nimport type { ProtobufField } from \"./types\";\n\n/**\n * Determines the protobuf number type name based on Zod number schema.\n * Returns \"int32\" for integers, \"double\" for floating point numbers.\n *\n * @param value - Zod number schema\n * @returns Protobuf number type name (\"int32\" or \"double\")\n */\nexport const getNumberTypeName = ({ value }: { value: ZodNumber }): string => {\n return value.isInt ? \"int32\" : \"double\";\n};\n\n/**\n * Converts a string to PascalCase format.\n * Handles dot-separated strings by capitalizing each part.\n *\n * @param value - String to convert\n * @returns PascalCase string\n */\nexport const toPascalCase = ({ value }: { value: string }): string => {\n return value\n .split(\".\")\n .map((part) => part.charAt(0).toUpperCase() + part.slice(1))\n .join(\"\");\n};\n\n/**\n * Converts a protobuf field definition to its type string representation.\n * Filters out null/empty values and joins the remaining types.\n *\n * @param field - Protobuf field definition\n * @returns Type string for the protobuf field\n */\nexport const protobufFieldToType = ({\n field,\n}: {\n field: ProtobufField;\n}): string => {\n return field.types.filter(Boolean).join(\" \");\n};\n","import * as inflection from \"inflection\";\nimport {\n ZodArray,\n ZodBigInt,\n ZodBoolean,\n ZodDate,\n ZodEnum,\n ZodMap,\n ZodNullable,\n ZodNumber,\n ZodObject,\n ZodOptional,\n ZodRecord,\n ZodSet,\n ZodString,\n ZodTuple,\n ZodType,\n type ZodTypeAny,\n} from \"zod\";\nimport {\n ZodArrayDefinition,\n ZodMapDefinition,\n ZodRecordDefinition,\n type ProtobufField,\n} from \"./types\";\nimport { getNumberTypeName, toPascalCase, protobufFieldToType } from \"./utils\";\n\n/**\n * Traverses a Zod array or set schema and converts it to protobuf repeated fields.\n * Handles nested types and generates appropriate field definitions.\n *\n * @param key - Field name\n * @param value - Zod array or set schema\n * @param messages - Map of message names to their protobuf fields\n * @param enums - Map of enum names to their protobuf values\n * @param typePrefix - Optional prefix for type names\n * @param parentKey - Optional parent message name for nested types\n * @returns Array of protobuf field definitions\n */\nexport const traverseArray = ({\n key,\n value,\n messages,\n enums,\n typePrefix,\n parentKey,\n}: {\n key: string;\n value: ZodArray<ZodTypeAny> | ZodSet<ZodTypeAny>;\n messages: Map<string, string[]>;\n enums: Map<string, string[]>;\n typePrefix: string | null;\n parentKey?: string;\n}): ProtobufField[] => {\n const nestedValue =\n value instanceof ZodArray\n ? value.element\n : value instanceof ZodSet\n ? (value.def as unknown as ZodArrayDefinition).valueType\n : // @ts-expect-error\n (value.def as unknown as ZodArrayDefinition).element;\n\n const singularKey = inflection.singularize(key);\n const elementFields = traverseKey({\n key: singularKey,\n value: nestedValue,\n messages,\n enums,\n isOptional: false,\n isInArray: true,\n typePrefix,\n parentKey: nestedValue instanceof ZodObject ? parentKey : undefined,\n });\n return elementFields.map((field) => ({\n ...field,\n types: [\"repeated\", ...field.types],\n name: field.name.replace(singularKey, key),\n }));\n};\n\n/**\n * Traverses a Zod map schema and converts it to a protobuf map type.\n * Validates that both key and value types are simple types suitable for map keys/values.\n *\n * @param key - Field name\n * @param value - Zod map schema\n * @param messages - Map of message names to their protobuf fields\n * @param enums - Map of enum names to their protobuf values\n * @param typePrefix - Optional prefix for type names\n * @param parentKey - Optional parent message name for nested types\n * @returns Array containing a single protobuf map field definition, or empty array if invalid\n */\nexport const traverseMap = ({\n key,\n value,\n messages,\n enums,\n typePrefix,\n parentKey,\n}: {\n key: string;\n value: ZodMap<ZodTypeAny, ZodTypeAny>;\n messages: Map<string, string[]>;\n enums: Map<string, string[]>;\n typePrefix: string | null;\n parentKey?: string;\n}): ProtobufField[] => {\n const mapDef = value.def as ZodMapDefinition;\n\n const keyType = traverseKey({\n key: inflection.singularize(key),\n value: mapDef.keyType,\n messages,\n enums,\n isOptional: false,\n isInArray: true,\n typePrefix,\n parentKey,\n });\n const valueType = traverseKey({\n key: inflection.singularize(key),\n value: mapDef.valueType,\n messages,\n enums,\n isOptional: false,\n isInArray: true,\n typePrefix,\n parentKey,\n });\n\n if (!keyType[0] || keyType.length !== 1) {\n return [];\n }\n\n if (!valueType[0] || valueType.length !== 1) {\n return [];\n }\n\n const mapType = `map<${protobufFieldToType({ field: keyType[0] })}, ${protobufFieldToType({ field: valueType[0] })}>`;\n return [\n {\n types: [mapType],\n name: key,\n },\n ];\n};\n\n/**\n * Traverses a Zod record schema and converts it to a protobuf map type.\n * Similar to traverseMap but handles ZodRecord type.\n *\n * @param key - Field name\n * @param value - Zod record schema\n * @param messages - Map of message names to their protobuf fields\n * @param enums - Map of enum names to their protobuf values\n * @param typePrefix - Optional prefix for type names\n * @param parentKey - Optional parent message name for nested types\n * @returns Array containing a single protobuf map field definition, or empty array if invalid\n */\nexport const traverseRecord = ({\n key,\n value,\n messages,\n enums,\n typePrefix,\n parentKey,\n}: {\n key: string;\n value: ZodRecord;\n messages: Map<string, string[]>;\n enums: Map<string, string[]>;\n typePrefix: string | null;\n parentKey?: string;\n}): ProtobufField[] => {\n const recordDef = value.def as unknown as ZodRecordDefinition;\n\n const keyType = traverseKey({\n key: inflection.singularize(key),\n value: recordDef.keyType,\n messages,\n enums,\n isOptional: false,\n isInArray: true,\n typePrefix,\n parentKey,\n });\n const valueType = traverseKey({\n key: inflection.singularize(key),\n value: recordDef.valueType,\n messages,\n enums,\n isOptional: false,\n isInArray: true,\n typePrefix,\n parentKey,\n });\n\n if (!keyType[0] || keyType.length !== 1) {\n return [];\n }\n\n if (!valueType[0] || valueType.length !== 1) {\n return [];\n }\n\n const mapType = `map<${protobufFieldToType({ field: keyType[0] })}, ${protobufFieldToType({ field: valueType[0] })}>`;\n return [\n {\n types: [mapType],\n name: key,\n },\n ];\n};\n\n/**\n * Traverses a single key-value pair from a Zod schema and converts it to protobuf field definitions.\n * Handles various Zod types including primitives, objects, arrays, maps, enums, tuples, and optional/nullable fields.\n *\n * @param key - Field name\n * @param value - Zod schema value\n * @param messages - Map of message names to their protobuf fields\n * @param enums - Map of enum names to their protobuf values\n * @param isOptional - Whether the field is optional\n * @param isInArray - Whether the field is inside an array\n * @param typePrefix - Optional prefix for type names\n * @param parentKey - Optional parent message name for nested types\n * @returns Array of protobuf field definitions\n */\nexport const traverseKey = ({\n key,\n value,\n messages,\n enums,\n isOptional,\n isInArray,\n typePrefix,\n parentKey,\n}: {\n key: string;\n value: unknown;\n messages: Map<string, string[]>;\n enums: Map<string, string[]>;\n isOptional: boolean;\n isInArray: boolean;\n typePrefix: string | null;\n parentKey?: string;\n}): ProtobufField[] => {\n if (!value) {\n return [];\n }\n\n if (value instanceof ZodOptional || value instanceof ZodNullable) {\n return traverseKey({\n key,\n value: value.unwrap(),\n messages,\n enums,\n isOptional: true,\n isInArray,\n typePrefix,\n parentKey,\n });\n }\n\n if (value instanceof ZodArray || value instanceof ZodSet) {\n return traverseArray({\n key,\n value: value as ZodArray<ZodTypeAny> | ZodSet<ZodTypeAny>,\n messages,\n enums,\n typePrefix,\n parentKey,\n });\n }\n\n if (value instanceof ZodMap) {\n return traverseMap({\n key,\n value: value as ZodMap<ZodTypeAny, ZodTypeAny>,\n messages,\n enums,\n typePrefix,\n parentKey,\n });\n }\n\n if (value instanceof ZodRecord) {\n return traverseRecord({\n key,\n value: value as ZodRecord,\n messages,\n enums,\n typePrefix,\n parentKey,\n });\n }\n\n const optional = isOptional && !isInArray ? \"optional\" : null;\n\n if (value instanceof ZodObject) {\n let messageName = toPascalCase({ value: key });\n if (parentKey) {\n const isParentAlreadyPascalCase = /^[A-Z][a-zA-Z0-9]*$/.test(parentKey);\n if (isParentAlreadyPascalCase) {\n messageName = `${parentKey}${messageName}`;\n } else {\n const parentMessageName = toPascalCase({ value: parentKey });\n messageName = `${parentMessageName}${messageName}`;\n }\n }\n if (typePrefix) {\n messageName = `${typePrefix}${messageName}`;\n }\n const nestedMessageFields = traverseSchema({\n schema: value,\n messages,\n enums,\n typePrefix,\n parentKey: messageName,\n });\n messages.set(messageName, nestedMessageFields);\n return [\n {\n types: [optional, messageName],\n name: key,\n },\n ];\n }\n\n if (value instanceof ZodString) {\n return [\n {\n types: [optional, \"string\"],\n name: key,\n },\n ];\n }\n\n if (value instanceof ZodNumber) {\n const typeName = getNumberTypeName({ value });\n return [\n {\n types: [optional, typeName],\n name: key,\n },\n ];\n }\n\n if (value instanceof ZodBoolean) {\n return [\n {\n types: [optional, \"bool\"],\n name: key,\n },\n ];\n }\n\n if (value instanceof ZodEnum) {\n const enumFields = value.options\n .map(\n (option: string | number, index: number) =>\n ` ${String(option)} = ${index};`\n )\n .join(\"\\n\");\n let enumName = toPascalCase({ value: key });\n if (parentKey) {\n const isParentAlreadyPascalCase = /^[A-Z][a-zA-Z0-9]*$/.test(parentKey);\n if (isParentAlreadyPascalCase) {\n enumName = `${parentKey}${enumName}`;\n } else {\n const parentMessageName = toPascalCase({ value: parentKey });\n enumName = `${parentMessageName}${enumName}`;\n }\n }\n if (typePrefix) {\n enumName = `${typePrefix}${enumName}`;\n }\n enums.set(enumName, [`enum ${enumName} {\\n${enumFields}\\n}`]);\n return [\n {\n types: [optional, enumName],\n name: key,\n },\n ];\n }\n\n if (value instanceof ZodDate) {\n return [\n {\n types: [optional, \"string\"],\n name: key,\n },\n ];\n }\n\n if (value instanceof ZodBigInt) {\n return [\n {\n types: [optional, \"int64\"],\n name: key,\n },\n ];\n }\n\n if (value instanceof ZodType) {\n const def = value.def as {\n type?: string;\n check?: unknown;\n fn?: (data: unknown) => boolean;\n };\n if (def.type === \"custom\" && def.check === \"custom\" && def.fn) {\n try {\n if (\n (typeof Buffer !== \"undefined\" && def.fn(Buffer.alloc(0))) ||\n (typeof Uint8Array !== \"undefined\" && def.fn(new Uint8Array(0)))\n ) {\n return [\n {\n types: [optional, \"bytes\"],\n name: key,\n },\n ];\n }\n } catch {}\n }\n }\n\n if (value instanceof ZodTuple) {\n const tupleFields: ProtobufField[] = (\n value.def.items as ZodTypeAny[]\n ).flatMap((item: ZodTypeAny, index: number) => {\n return traverseKey({\n key: `${key}_${index}`,\n value: item,\n messages,\n enums,\n isOptional: false,\n isInArray,\n typePrefix,\n parentKey,\n });\n });\n\n let tupleMessageName = toPascalCase({ value: key });\n if (parentKey) {\n const isParentAlreadyPascalCase = /^[A-Z][a-zA-Z0-9]*$/.test(parentKey);\n if (isParentAlreadyPascalCase) {\n tupleMessageName = `${parentKey}${tupleMessageName}`;\n } else {\n const parentMessageName = toPascalCase({ value: parentKey });\n tupleMessageName = `${parentMessageName}${tupleMessageName}`;\n }\n }\n if (typePrefix) {\n tupleMessageName = `${typePrefix}${tupleMessageName}`;\n }\n messages.set(\n tupleMessageName,\n tupleFields.map(\n (field, index) =>\n ` ${field.types.join(\" \")} ${field.name} = ${index + 1};`\n )\n );\n return [\n {\n types: [optional, tupleMessageName],\n name: key,\n },\n ];\n }\n\n if (value instanceof ZodType) {\n return [];\n }\n\n return [];\n};\n\n/**\n * Traverses a Zod object schema and converts it to protobuf message field definitions.\n * Processes all fields in the schema and generates appropriate protobuf types.\n *\n * @param schema - Zod schema to traverse\n * @param messages - Map of message names to their protobuf fields\n * @param enums - Map of enum names to their protobuf values\n * @param typePrefix - Optional prefix for type names\n * @param parentKey - Optional parent message name for nested types\n * @returns Array of protobuf field definition strings\n */\nexport const traverseSchema = ({\n schema,\n messages,\n enums,\n typePrefix,\n parentKey,\n}: {\n schema: ZodTypeAny;\n messages: Map<string, string[]>;\n enums: Map<string, string[]>;\n typePrefix: string | null;\n parentKey?: string;\n}): string[] => {\n if (\n !schema ||\n typeof schema !== \"object\" ||\n !(\"def\" in schema) ||\n (schema.constructor.name !== \"ZodObject\" &&\n (schema.def as { type?: string }).type !== \"object\")\n ) {\n return [];\n }\n\n const zodObject = schema as ZodObject<any>;\n const fields = Object.entries(zodObject.shape).flatMap(([key, value]) => {\n return traverseKey({\n key,\n value,\n messages,\n enums,\n isOptional: false,\n isInArray: false,\n typePrefix,\n parentKey,\n });\n });\n\n return fields.map(\n (field, index) =>\n `${protobufFieldToType({ field })} ${field.name} = ${index + 1};`\n );\n};\n","import { z, ZodObject, type ZodTypeAny } from \"zod\";\nimport type {\n ServiceDefinition,\n ServiceMethod,\n ServicesInput,\n ZodFunctionDefinition,\n ZodTupleDefinition,\n} from \"./types\";\nimport { toPascalCase } from \"./utils\";\nimport { traverseSchema } from \"./traversers\";\n\n/**\n * Context for generating protobuf services.\n * Contains accumulated messages, enums, and type prefix.\n */\ninterface ServiceGenerationContext {\n /** Map of message names to their protobuf fields */\n messages: Map<string, string[]>;\n /** Map of enum names to their protobuf values */\n enums: Map<string, string[]>;\n /** Prefix for type names or null if prefix is not used */\n typePrefix: string | null;\n}\n\n/**\n * Parses a Zod service schema and extracts method definitions.\n * Each method must be a function with one argument (request) and a return value (response).\n *\n * @param name - Service name\n * @param schema - Zod object containing service methods as functions\n * @returns Service definition with extracted methods\n */\nconst parseZodServiceSchema = (\n name: string,\n schema: ZodObject<Record<string, ZodTypeAny>>\n): ServiceDefinition => {\n const shape = schema.shape as Record<string, ZodTypeAny>;\n const methods: ServiceMethod[] = [];\n\n for (const [methodName, methodSchema] of Object.entries(shape)) {\n const methodDef = (methodSchema as ZodTypeAny).def as ZodFunctionDefinition;\n\n if (methodDef.type === \"function\") {\n const inputDef = methodDef.input;\n\n const args = (inputDef?.def as ZodTupleDefinition)?.items ?? [];\n const output = methodDef.output as ZodTypeAny;\n\n if (args.length > 0 && args[0] && output) {\n const request = args[0];\n const response = output;\n\n let streaming: \"client\" | \"server\" | \"bidirectional\" | undefined;\n const metadata = methodSchema.meta();\n console.log(methodSchema.meta());\n streaming = metadata?.streaming as\n | \"client\"\n | \"server\"\n | \"bidirectional\"\n | undefined;\n\n methods.push({\n name: methodName,\n request,\n response,\n streaming,\n });\n }\n }\n }\n\n return {\n name,\n methods,\n };\n};\n\n/**\n * Normalizes service input data into an array of service definitions.\n * If an array is passed, returns it as is.\n * If an object is passed, converts each key-value pair into a service definition.\n *\n * @param services - Array of service definitions or object with Zod schemas\n * @returns Array of normalized service definitions\n */\nconst normalizeServices = (services: ServicesInput): ServiceDefinition[] => {\n if (Array.isArray(services)) {\n return services;\n }\n\n return Object.entries(services).map(([name, schema]) =>\n parseZodServiceSchema(name, schema)\n );\n};\n\n/**\n * Ensures that the schema is a ZodObject.\n * If the schema is already an object, returns it as is.\n * Otherwise, wraps the schema in an object with a \"data\" field.\n *\n * @param schema - Zod schema of any type\n * @returns ZodObject containing the original schema\n */\nconst ensureZodObject = (\n schema: ZodTypeAny\n): ZodObject<Record<string, ZodTypeAny>> => {\n const schemaType =\n (schema.def as { type?: string }).type || schema.constructor.name;\n\n if (schemaType === \"object\" || schema.constructor.name === \"ZodObject\") {\n return schema as ZodObject<Record<string, ZodTypeAny>>;\n }\n\n return z.object({\n data: schema,\n });\n};\n\n/**\n * Generates the name for a request message based on the method name.\n *\n * @param methodName - Name of the service method\n * @param typePrefix - Optional prefix for type names\n * @returns Generated request message name in PascalCase\n */\nconst generateRequestMessageName = (\n methodName: string,\n typePrefix: string | null\n): string => {\n const messageName = toPascalCase({ value: `${methodName}Request` });\n return typePrefix ? `${typePrefix}${messageName}` : messageName;\n};\n\n/**\n * Generates the name for a response message based on the method name.\n *\n * @param methodName - Name of the service method\n * @param typePrefix - Optional prefix for type names\n * @returns Generated response message name in PascalCase\n */\nconst generateResponseMessageName = (\n methodName: string,\n typePrefix: string | null\n): string => {\n const messageName = toPascalCase({ value: `${methodName}Response` });\n return typePrefix ? `${typePrefix}${messageName}` : messageName;\n};\n\n/**\n * Processes a service method by generating request and response message names\n * and traversing their schemas to populate the context with message definitions.\n *\n * @param method - Service method definition\n * @param context - Generation context containing messages, enums, and type prefix\n * @returns Object containing request and response message names\n */\nconst processServiceMethod = (\n method: ServiceMethod,\n context: ServiceGenerationContext\n): { requestName: string; responseName: string } => {\n const { messages, enums, typePrefix } = context;\n\n const requestName = generateRequestMessageName(method.name, typePrefix);\n const responseName = generateResponseMessageName(method.name, typePrefix);\n\n if (!messages.has(requestName)) {\n const requestSchema = ensureZodObject(method.request);\n const requestFields = traverseSchema({\n schema: requestSchema,\n messages,\n enums,\n typePrefix,\n parentKey: requestName,\n });\n messages.set(requestName, requestFields);\n }\n\n if (!messages.has(responseName)) {\n const responseSchema = ensureZodObject(method.response);\n const responseFields = traverseSchema({\n schema: responseSchema,\n messages,\n enums,\n typePrefix,\n parentKey: responseName,\n });\n messages.set(responseName, responseFields);\n }\n\n return { requestName, responseName };\n};\n\n/**\n * Generates protobuf service definitions from Zod service schemas.\n * Supports streaming methods (client, server, bidirectional).\n *\n * @param services - Service definitions as array or object with Zod schemas\n * @param context - Generation context containing messages, enums, and type prefix\n * @returns Array of protobuf service definition strings\n */\nexport const generateServices = (\n services: ServicesInput,\n context: ServiceGenerationContext\n): string[] => {\n const normalizedServices = normalizeServices(services);\n\n return normalizedServices.map((service) => {\n const methods = service.methods.map((method) => {\n const { requestName, responseName } = processServiceMethod(\n method,\n context\n );\n\n const requestStreaming =\n method.streaming === \"client\" || method.streaming === \"bidirectional\";\n const responseStreaming =\n method.streaming === \"server\" || method.streaming === \"bidirectional\";\n\n const requestType = requestStreaming\n ? `stream ${requestName}`\n : requestName;\n const responseType = responseStreaming\n ? `stream ${responseName}`\n : responseName;\n\n return ` rpc ${toPascalCase({ value: method.name })}(${requestType}) returns (${responseType});`;\n });\n\n return `service ${toPascalCase({ value: service.name })} {\\n${methods.join(\"\\n\")}\\n}`;\n });\n};\n","import type { ZodTypeAny } from \"zod\";\nimport type { ZodToProtobufOptions } from \"./types\";\nimport { traverseSchema } from \"./traversers\";\nimport { generateServices } from \"./service-generator\";\nimport { z } from \"zod\";\n\n/**\n * Generates a protobuf definition string for services only (without root message).\n * Convenience function that calls zodToProtobuf with an empty schema.\n *\n * @param options - Options for protobuf generation\n * @returns Protobuf definition string\n */\nexport const zodToProtobufService = (\n options: ZodToProtobufOptions = {}\n): string => {\n return zodToProtobuf(z.object(), options);\n};\n\n/**\n * Converts a Zod schema to a protobuf definition string.\n * Generates messages, enums, and services based on the provided schema and options.\n *\n * @param schema - Optional Zod schema to convert (if not provided, only services will be generated)\n * @param options - Options for protobuf generation including package name, root message name, type prefix, and services\n * @returns Complete protobuf definition string\n */\nexport const zodToProtobuf = (\n schema?: ZodTypeAny,\n options: ZodToProtobufOptions = {}\n): string => {\n const {\n packageName = \"default\",\n rootMessageName = \"Message\",\n typePrefix = \"\",\n services,\n skipRootMessage = false,\n } = options;\n\n const messages = new Map<string, string[]>();\n const enums = new Map<string, string[]>();\n\n if (schema && !skipRootMessage) {\n const fields = traverseSchema({ schema, messages, enums, typePrefix });\n if (fields.length > 0) {\n const rootMessageKey = `${typePrefix}${rootMessageName}`;\n messages.set(rootMessageKey, fields);\n }\n }\n\n const context = {\n messages,\n enums,\n typePrefix: typePrefix || null,\n };\n\n const hasServices =\n services &&\n (Array.isArray(services)\n ? services.length > 0\n : Object.keys(services).length > 0);\n\n const servicesString = hasServices ? generateServices(services, context) : [];\n\n const enumsString = Array.from(enums.values()).map((enumDef) =>\n enumDef.join(\"\\n\")\n );\n\n const messagesString = Array.from(messages.entries()).map(\n ([name, fields]) =>\n `message ${name} {\\n${fields.map((field) => ` ${field}`).join(\"\\n\")}\\n}`\n );\n\n const content = [servicesString, enumsString, messagesString]\n .filter((strings) => !!strings.length)\n .map((strings) => strings.join(\"\\n\\n\"))\n .join(\"\\n\\n\");\n\n const protoDefinition = `\nsyntax = \"proto3\";\npackage ${packageName};\n\n${content}\n`;\n\n return protoDefinition.trim();\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAUA,MAAa,qBAAqB,EAAE,YAA0C;AAC5E,QAAO,MAAM,QAAQ,UAAU;;;;;;;;;AAUjC,MAAa,gBAAgB,EAAE,YAAuC;AACpE,QAAO,MACJ,MAAM,IAAI,CACV,KAAK,SAAS,KAAK,OAAO,EAAE,CAAC,aAAa,GAAG,KAAK,MAAM,EAAE,CAAC,CAC3D,KAAK,GAAG;;;;;;;;;AAUb,MAAa,uBAAuB,EAClC,YAGY;AACZ,QAAO,MAAM,MAAM,OAAO,QAAQ,CAAC,KAAK,IAAI;;;;;;;;;;;;;;;;;ACD9C,MAAa,iBAAiB,EAC5B,KACA,OACA,UACA,OACA,YACA,gBAQqB;CACrB,MAAM,cACJ,iBAAiBA,eACb,MAAM,UACN,iBAAiBC,aACd,MAAM,IAAsC,YAE5C,MAAM,IAAsC;CAErD,MAAM,cAAc,WAAW,YAAY,IAAI;AAW/C,QAVsB,YAAY;EAChC,KAAK;EACL,OAAO;EACP;EACA;EACA,YAAY;EACZ,WAAW;EACX;EACA,WAAW,uBAAuBC,gBAAY,YAAY;EAC3D,CAAC,CACmB,KAAK,WAAW;EACnC,GAAG;EACH,OAAO,CAAC,YAAY,GAAG,MAAM,MAAM;EACnC,MAAM,MAAM,KAAK,QAAQ,aAAa,IAAI;EAC3C,EAAE;;;;;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AAM,iBAAiB,GAAG,aAAa;AACvC,YAAS,IAAI,gBAAgB,OAAO;;;CAIxC,MAAM,UAAU;EACd;EACA;EACA,YAAY,cAAc;EAC3B;AA+BD,QAPwB;;UAEhB,YAAY;;EAPJ;EAhBd,aACC,MAAM,QAAQ,SAAS,GACpB,SAAS,SAAS,IAClB,OAAO,KAAK,SAAS,CAAC,SAAS,KAEA,iBAAiB,UAAU,QAAQ,GAAG,EAAE;EAEzD,MAAM,KAAK,MAAM,QAAQ,CAAC,CAAC,KAAK,YAClD,QAAQ,KAAK,KAAK,CACnB;EAEsB,MAAM,KAAK,SAAS,SAAS,CAAC,CAAC,KACnD,CAAC,MAAM,YACN,WAAW,KAAK,MAAM,OAAO,KAAK,UAAU,OAAO,QAAQ,CAAC,KAAK,KAAK,CAAC,KAC1E;EAE4D,CAC1D,QAAQ,YAAY,CAAC,CAAC,QAAQ,OAAO,CACrC,KAAK,YAAY,QAAQ,KAAK,OAAO,CAAC,CACtC,KAAK,OAAO,CAMP;EAGe,MAAM"}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,27 +1,67 @@
|
|
|
1
1
|
import { ZodObject, ZodTypeAny } from "zod";
|
|
2
2
|
|
|
3
3
|
//#region src/types.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Input type for service definitions.
|
|
7
|
+
* Can be either an array of service definitions or an object mapping service names to Zod schemas.
|
|
8
|
+
*/
|
|
4
9
|
type ServicesInput = ServiceDefinition[] | Record<string, ZodObject<any>>;
|
|
10
|
+
/**
|
|
11
|
+
* Options for converting Zod schemas to protobuf definitions.
|
|
12
|
+
*/
|
|
5
13
|
interface ZodToProtobufOptions {
|
|
14
|
+
/** Package name for the generated protobuf file */
|
|
6
15
|
packageName?: string;
|
|
16
|
+
/** Name of the root message (default: "Message") */
|
|
7
17
|
rootMessageName?: string;
|
|
18
|
+
/** Prefix to add to all generated type names */
|
|
8
19
|
typePrefix?: string;
|
|
20
|
+
/** Service definitions to include in the protobuf output */
|
|
9
21
|
services?: ServicesInput;
|
|
22
|
+
/** Whether to skip generating the root message */
|
|
10
23
|
skipRootMessage?: boolean;
|
|
11
24
|
}
|
|
25
|
+
/**
|
|
26
|
+
* Definition of a single service method.
|
|
27
|
+
*/
|
|
12
28
|
interface ServiceMethod {
|
|
29
|
+
/** Method name */
|
|
13
30
|
name: string;
|
|
31
|
+
/** Zod schema for the request message */
|
|
14
32
|
request: ZodTypeAny;
|
|
33
|
+
/** Zod schema for the response message */
|
|
15
34
|
response: ZodTypeAny;
|
|
35
|
+
/** Streaming type: "client" for client streaming, "server" for server streaming, "bidirectional" for both */
|
|
16
36
|
streaming?: "client" | "server" | "bidirectional";
|
|
17
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* Definition of a protobuf service containing multiple methods.
|
|
40
|
+
*/
|
|
18
41
|
interface ServiceDefinition {
|
|
42
|
+
/** Service name */
|
|
19
43
|
name: string;
|
|
44
|
+
/** Array of service methods */
|
|
20
45
|
methods: ServiceMethod[];
|
|
21
46
|
}
|
|
22
47
|
//#endregion
|
|
23
48
|
//#region src/zod-to-protobuf.d.ts
|
|
49
|
+
/**
|
|
50
|
+
* Generates a protobuf definition string for services only (without root message).
|
|
51
|
+
* Convenience function that calls zodToProtobuf with an empty schema.
|
|
52
|
+
*
|
|
53
|
+
* @param options - Options for protobuf generation
|
|
54
|
+
* @returns Protobuf definition string
|
|
55
|
+
*/
|
|
24
56
|
declare const zodToProtobufService: (options?: ZodToProtobufOptions) => string;
|
|
57
|
+
/**
|
|
58
|
+
* Converts a Zod schema to a protobuf definition string.
|
|
59
|
+
* Generates messages, enums, and services based on the provided schema and options.
|
|
60
|
+
*
|
|
61
|
+
* @param schema - Optional Zod schema to convert (if not provided, only services will be generated)
|
|
62
|
+
* @param options - Options for protobuf generation including package name, root message name, type prefix, and services
|
|
63
|
+
* @returns Complete protobuf definition string
|
|
64
|
+
*/
|
|
25
65
|
declare const zodToProtobuf: (schema?: ZodTypeAny, options?: ZodToProtobufOptions) => string;
|
|
26
66
|
//#endregion
|
|
27
67
|
export { zodToProtobuf, zodToProtobufService };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,27 +1,67 @@
|
|
|
1
1
|
import { ZodObject, ZodTypeAny } from "zod";
|
|
2
2
|
|
|
3
3
|
//#region src/types.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Input type for service definitions.
|
|
7
|
+
* Can be either an array of service definitions or an object mapping service names to Zod schemas.
|
|
8
|
+
*/
|
|
4
9
|
type ServicesInput = ServiceDefinition[] | Record<string, ZodObject<any>>;
|
|
10
|
+
/**
|
|
11
|
+
* Options for converting Zod schemas to protobuf definitions.
|
|
12
|
+
*/
|
|
5
13
|
interface ZodToProtobufOptions {
|
|
14
|
+
/** Package name for the generated protobuf file */
|
|
6
15
|
packageName?: string;
|
|
16
|
+
/** Name of the root message (default: "Message") */
|
|
7
17
|
rootMessageName?: string;
|
|
18
|
+
/** Prefix to add to all generated type names */
|
|
8
19
|
typePrefix?: string;
|
|
20
|
+
/** Service definitions to include in the protobuf output */
|
|
9
21
|
services?: ServicesInput;
|
|
22
|
+
/** Whether to skip generating the root message */
|
|
10
23
|
skipRootMessage?: boolean;
|
|
11
24
|
}
|
|
25
|
+
/**
|
|
26
|
+
* Definition of a single service method.
|
|
27
|
+
*/
|
|
12
28
|
interface ServiceMethod {
|
|
29
|
+
/** Method name */
|
|
13
30
|
name: string;
|
|
31
|
+
/** Zod schema for the request message */
|
|
14
32
|
request: ZodTypeAny;
|
|
33
|
+
/** Zod schema for the response message */
|
|
15
34
|
response: ZodTypeAny;
|
|
35
|
+
/** Streaming type: "client" for client streaming, "server" for server streaming, "bidirectional" for both */
|
|
16
36
|
streaming?: "client" | "server" | "bidirectional";
|
|
17
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* Definition of a protobuf service containing multiple methods.
|
|
40
|
+
*/
|
|
18
41
|
interface ServiceDefinition {
|
|
42
|
+
/** Service name */
|
|
19
43
|
name: string;
|
|
44
|
+
/** Array of service methods */
|
|
20
45
|
methods: ServiceMethod[];
|
|
21
46
|
}
|
|
22
47
|
//#endregion
|
|
23
48
|
//#region src/zod-to-protobuf.d.ts
|
|
49
|
+
/**
|
|
50
|
+
* Generates a protobuf definition string for services only (without root message).
|
|
51
|
+
* Convenience function that calls zodToProtobuf with an empty schema.
|
|
52
|
+
*
|
|
53
|
+
* @param options - Options for protobuf generation
|
|
54
|
+
* @returns Protobuf definition string
|
|
55
|
+
*/
|
|
24
56
|
declare const zodToProtobufService: (options?: ZodToProtobufOptions) => string;
|
|
57
|
+
/**
|
|
58
|
+
* Converts a Zod schema to a protobuf definition string.
|
|
59
|
+
* Generates messages, enums, and services based on the provided schema and options.
|
|
60
|
+
*
|
|
61
|
+
* @param schema - Optional Zod schema to convert (if not provided, only services will be generated)
|
|
62
|
+
* @param options - Options for protobuf generation including package name, root message name, type prefix, and services
|
|
63
|
+
* @returns Complete protobuf definition string
|
|
64
|
+
*/
|
|
25
65
|
declare const zodToProtobuf: (schema?: ZodTypeAny, options?: ZodToProtobufOptions) => string;
|
|
26
66
|
//#endregion
|
|
27
67
|
export { zodToProtobuf, zodToProtobufService };
|
package/dist/index.mjs
CHANGED
|
@@ -2,18 +2,51 @@ import * as inflection from "inflection";
|
|
|
2
2
|
import { ZodArray, ZodBigInt, ZodBoolean, ZodDate, ZodEnum, ZodMap, ZodNullable, ZodNumber, ZodObject, ZodOptional, ZodRecord, ZodSet, ZodString, ZodTuple, ZodType, z } from "zod";
|
|
3
3
|
|
|
4
4
|
//#region src/utils.ts
|
|
5
|
+
/**
|
|
6
|
+
* Determines the protobuf number type name based on Zod number schema.
|
|
7
|
+
* Returns "int32" for integers, "double" for floating point numbers.
|
|
8
|
+
*
|
|
9
|
+
* @param value - Zod number schema
|
|
10
|
+
* @returns Protobuf number type name ("int32" or "double")
|
|
11
|
+
*/
|
|
5
12
|
const getNumberTypeName = ({ value }) => {
|
|
6
13
|
return value.isInt ? "int32" : "double";
|
|
7
14
|
};
|
|
15
|
+
/**
|
|
16
|
+
* Converts a string to PascalCase format.
|
|
17
|
+
* Handles dot-separated strings by capitalizing each part.
|
|
18
|
+
*
|
|
19
|
+
* @param value - String to convert
|
|
20
|
+
* @returns PascalCase string
|
|
21
|
+
*/
|
|
8
22
|
const toPascalCase = ({ value }) => {
|
|
9
23
|
return value.split(".").map((part) => part.charAt(0).toUpperCase() + part.slice(1)).join("");
|
|
10
24
|
};
|
|
25
|
+
/**
|
|
26
|
+
* Converts a protobuf field definition to its type string representation.
|
|
27
|
+
* Filters out null/empty values and joins the remaining types.
|
|
28
|
+
*
|
|
29
|
+
* @param field - Protobuf field definition
|
|
30
|
+
* @returns Type string for the protobuf field
|
|
31
|
+
*/
|
|
11
32
|
const protobufFieldToType = ({ field }) => {
|
|
12
33
|
return field.types.filter(Boolean).join(" ");
|
|
13
34
|
};
|
|
14
35
|
|
|
15
36
|
//#endregion
|
|
16
37
|
//#region src/traversers.ts
|
|
38
|
+
/**
|
|
39
|
+
* Traverses a Zod array or set schema and converts it to protobuf repeated fields.
|
|
40
|
+
* Handles nested types and generates appropriate field definitions.
|
|
41
|
+
*
|
|
42
|
+
* @param key - Field name
|
|
43
|
+
* @param value - Zod array or set schema
|
|
44
|
+
* @param messages - Map of message names to their protobuf fields
|
|
45
|
+
* @param enums - Map of enum names to their protobuf values
|
|
46
|
+
* @param typePrefix - Optional prefix for type names
|
|
47
|
+
* @param parentKey - Optional parent message name for nested types
|
|
48
|
+
* @returns Array of protobuf field definitions
|
|
49
|
+
*/
|
|
17
50
|
const traverseArray = ({ key, value, messages, enums, typePrefix, parentKey }) => {
|
|
18
51
|
const nestedValue = value instanceof ZodArray ? value.element : value instanceof ZodSet ? value.def.valueType : value.def.element;
|
|
19
52
|
const singularKey = inflection.singularize(key);
|
|
@@ -32,6 +65,18 @@ const traverseArray = ({ key, value, messages, enums, typePrefix, parentKey }) =
|
|
|
32
65
|
name: field.name.replace(singularKey, key)
|
|
33
66
|
}));
|
|
34
67
|
};
|
|
68
|
+
/**
|
|
69
|
+
* Traverses a Zod map schema and converts it to a protobuf map type.
|
|
70
|
+
* Validates that both key and value types are simple types suitable for map keys/values.
|
|
71
|
+
*
|
|
72
|
+
* @param key - Field name
|
|
73
|
+
* @param value - Zod map schema
|
|
74
|
+
* @param messages - Map of message names to their protobuf fields
|
|
75
|
+
* @param enums - Map of enum names to their protobuf values
|
|
76
|
+
* @param typePrefix - Optional prefix for type names
|
|
77
|
+
* @param parentKey - Optional parent message name for nested types
|
|
78
|
+
* @returns Array containing a single protobuf map field definition, or empty array if invalid
|
|
79
|
+
*/
|
|
35
80
|
const traverseMap = ({ key, value, messages, enums, typePrefix, parentKey }) => {
|
|
36
81
|
const mapDef = value.def;
|
|
37
82
|
const keyType = traverseKey({
|
|
@@ -61,6 +106,18 @@ const traverseMap = ({ key, value, messages, enums, typePrefix, parentKey }) =>
|
|
|
61
106
|
name: key
|
|
62
107
|
}];
|
|
63
108
|
};
|
|
109
|
+
/**
|
|
110
|
+
* Traverses a Zod record schema and converts it to a protobuf map type.
|
|
111
|
+
* Similar to traverseMap but handles ZodRecord type.
|
|
112
|
+
*
|
|
113
|
+
* @param key - Field name
|
|
114
|
+
* @param value - Zod record schema
|
|
115
|
+
* @param messages - Map of message names to their protobuf fields
|
|
116
|
+
* @param enums - Map of enum names to their protobuf values
|
|
117
|
+
* @param typePrefix - Optional prefix for type names
|
|
118
|
+
* @param parentKey - Optional parent message name for nested types
|
|
119
|
+
* @returns Array containing a single protobuf map field definition, or empty array if invalid
|
|
120
|
+
*/
|
|
64
121
|
const traverseRecord = ({ key, value, messages, enums, typePrefix, parentKey }) => {
|
|
65
122
|
const recordDef = value.def;
|
|
66
123
|
const keyType = traverseKey({
|
|
@@ -90,6 +147,20 @@ const traverseRecord = ({ key, value, messages, enums, typePrefix, parentKey })
|
|
|
90
147
|
name: key
|
|
91
148
|
}];
|
|
92
149
|
};
|
|
150
|
+
/**
|
|
151
|
+
* Traverses a single key-value pair from a Zod schema and converts it to protobuf field definitions.
|
|
152
|
+
* Handles various Zod types including primitives, objects, arrays, maps, enums, tuples, and optional/nullable fields.
|
|
153
|
+
*
|
|
154
|
+
* @param key - Field name
|
|
155
|
+
* @param value - Zod schema value
|
|
156
|
+
* @param messages - Map of message names to their protobuf fields
|
|
157
|
+
* @param enums - Map of enum names to their protobuf values
|
|
158
|
+
* @param isOptional - Whether the field is optional
|
|
159
|
+
* @param isInArray - Whether the field is inside an array
|
|
160
|
+
* @param typePrefix - Optional prefix for type names
|
|
161
|
+
* @param parentKey - Optional parent message name for nested types
|
|
162
|
+
* @returns Array of protobuf field definitions
|
|
163
|
+
*/
|
|
93
164
|
const traverseKey = ({ key, value, messages, enums, isOptional, isInArray, typePrefix, parentKey }) => {
|
|
94
165
|
if (!value) return [];
|
|
95
166
|
if (value instanceof ZodOptional || value instanceof ZodNullable) return traverseKey({
|
|
@@ -177,6 +248,15 @@ const traverseKey = ({ key, value, messages, enums, isOptional, isInArray, typeP
|
|
|
177
248
|
types: [optional, "int64"],
|
|
178
249
|
name: key
|
|
179
250
|
}];
|
|
251
|
+
if (value instanceof ZodType) {
|
|
252
|
+
const def = value.def;
|
|
253
|
+
if (def.type === "custom" && def.check === "custom" && def.fn) try {
|
|
254
|
+
if (typeof Buffer !== "undefined" && def.fn(Buffer.alloc(0)) || typeof Uint8Array !== "undefined" && def.fn(new Uint8Array(0))) return [{
|
|
255
|
+
types: [optional, "bytes"],
|
|
256
|
+
name: key
|
|
257
|
+
}];
|
|
258
|
+
} catch {}
|
|
259
|
+
}
|
|
180
260
|
if (value instanceof ZodTuple) {
|
|
181
261
|
const tupleFields = value.def.items.flatMap((item, index) => {
|
|
182
262
|
return traverseKey({
|
|
@@ -203,6 +283,17 @@ const traverseKey = ({ key, value, messages, enums, isOptional, isInArray, typeP
|
|
|
203
283
|
if (value instanceof ZodType) return [];
|
|
204
284
|
return [];
|
|
205
285
|
};
|
|
286
|
+
/**
|
|
287
|
+
* Traverses a Zod object schema and converts it to protobuf message field definitions.
|
|
288
|
+
* Processes all fields in the schema and generates appropriate protobuf types.
|
|
289
|
+
*
|
|
290
|
+
* @param schema - Zod schema to traverse
|
|
291
|
+
* @param messages - Map of message names to their protobuf fields
|
|
292
|
+
* @param enums - Map of enum names to their protobuf values
|
|
293
|
+
* @param typePrefix - Optional prefix for type names
|
|
294
|
+
* @param parentKey - Optional parent message name for nested types
|
|
295
|
+
* @returns Array of protobuf field definition strings
|
|
296
|
+
*/
|
|
206
297
|
const traverseSchema = ({ schema, messages, enums, typePrefix, parentKey }) => {
|
|
207
298
|
if (!schema || typeof schema !== "object" || !("def" in schema) || schema.constructor.name !== "ZodObject" && schema.def.type !== "object") return [];
|
|
208
299
|
const zodObject = schema;
|
|
@@ -222,6 +313,14 @@ const traverseSchema = ({ schema, messages, enums, typePrefix, parentKey }) => {
|
|
|
222
313
|
|
|
223
314
|
//#endregion
|
|
224
315
|
//#region src/service-generator.ts
|
|
316
|
+
/**
|
|
317
|
+
* Parses a Zod service schema and extracts method definitions.
|
|
318
|
+
* Each method must be a function with one argument (request) and a return value (response).
|
|
319
|
+
*
|
|
320
|
+
* @param name - Service name
|
|
321
|
+
* @param schema - Zod object containing service methods as functions
|
|
322
|
+
* @returns Service definition with extracted methods
|
|
323
|
+
*/
|
|
225
324
|
const parseZodServiceSchema = (name, schema) => {
|
|
226
325
|
const shape = schema.shape;
|
|
227
326
|
const methods = [];
|
|
@@ -233,10 +332,15 @@ const parseZodServiceSchema = (name, schema) => {
|
|
|
233
332
|
if (args.length > 0 && args[0] && output) {
|
|
234
333
|
const request = args[0];
|
|
235
334
|
const response = output;
|
|
335
|
+
let streaming;
|
|
336
|
+
const metadata = methodSchema.meta();
|
|
337
|
+
console.log(methodSchema.meta());
|
|
338
|
+
streaming = metadata?.streaming;
|
|
236
339
|
methods.push({
|
|
237
340
|
name: methodName,
|
|
238
341
|
request,
|
|
239
|
-
response
|
|
342
|
+
response,
|
|
343
|
+
streaming
|
|
240
344
|
});
|
|
241
345
|
}
|
|
242
346
|
}
|
|
@@ -246,22 +350,60 @@ const parseZodServiceSchema = (name, schema) => {
|
|
|
246
350
|
methods
|
|
247
351
|
};
|
|
248
352
|
};
|
|
353
|
+
/**
|
|
354
|
+
* Normalizes service input data into an array of service definitions.
|
|
355
|
+
* If an array is passed, returns it as is.
|
|
356
|
+
* If an object is passed, converts each key-value pair into a service definition.
|
|
357
|
+
*
|
|
358
|
+
* @param services - Array of service definitions or object with Zod schemas
|
|
359
|
+
* @returns Array of normalized service definitions
|
|
360
|
+
*/
|
|
249
361
|
const normalizeServices = (services) => {
|
|
250
362
|
if (Array.isArray(services)) return services;
|
|
251
363
|
return Object.entries(services).map(([name, schema]) => parseZodServiceSchema(name, schema));
|
|
252
364
|
};
|
|
365
|
+
/**
|
|
366
|
+
* Ensures that the schema is a ZodObject.
|
|
367
|
+
* If the schema is already an object, returns it as is.
|
|
368
|
+
* Otherwise, wraps the schema in an object with a "data" field.
|
|
369
|
+
*
|
|
370
|
+
* @param schema - Zod schema of any type
|
|
371
|
+
* @returns ZodObject containing the original schema
|
|
372
|
+
*/
|
|
253
373
|
const ensureZodObject = (schema) => {
|
|
254
374
|
if ((schema.def.type || schema.constructor.name) === "object" || schema.constructor.name === "ZodObject") return schema;
|
|
255
375
|
return z.object({ data: schema });
|
|
256
376
|
};
|
|
377
|
+
/**
|
|
378
|
+
* Generates the name for a request message based on the method name.
|
|
379
|
+
*
|
|
380
|
+
* @param methodName - Name of the service method
|
|
381
|
+
* @param typePrefix - Optional prefix for type names
|
|
382
|
+
* @returns Generated request message name in PascalCase
|
|
383
|
+
*/
|
|
257
384
|
const generateRequestMessageName = (methodName, typePrefix) => {
|
|
258
385
|
const messageName = toPascalCase({ value: `${methodName}Request` });
|
|
259
386
|
return typePrefix ? `${typePrefix}${messageName}` : messageName;
|
|
260
387
|
};
|
|
388
|
+
/**
|
|
389
|
+
* Generates the name for a response message based on the method name.
|
|
390
|
+
*
|
|
391
|
+
* @param methodName - Name of the service method
|
|
392
|
+
* @param typePrefix - Optional prefix for type names
|
|
393
|
+
* @returns Generated response message name in PascalCase
|
|
394
|
+
*/
|
|
261
395
|
const generateResponseMessageName = (methodName, typePrefix) => {
|
|
262
396
|
const messageName = toPascalCase({ value: `${methodName}Response` });
|
|
263
397
|
return typePrefix ? `${typePrefix}${messageName}` : messageName;
|
|
264
398
|
};
|
|
399
|
+
/**
|
|
400
|
+
* Processes a service method by generating request and response message names
|
|
401
|
+
* and traversing their schemas to populate the context with message definitions.
|
|
402
|
+
*
|
|
403
|
+
* @param method - Service method definition
|
|
404
|
+
* @param context - Generation context containing messages, enums, and type prefix
|
|
405
|
+
* @returns Object containing request and response message names
|
|
406
|
+
*/
|
|
265
407
|
const processServiceMethod = (method, context) => {
|
|
266
408
|
const { messages, enums, typePrefix } = context;
|
|
267
409
|
const requestName = generateRequestMessageName(method.name, typePrefix);
|
|
@@ -291,6 +433,14 @@ const processServiceMethod = (method, context) => {
|
|
|
291
433
|
responseName
|
|
292
434
|
};
|
|
293
435
|
};
|
|
436
|
+
/**
|
|
437
|
+
* Generates protobuf service definitions from Zod service schemas.
|
|
438
|
+
* Supports streaming methods (client, server, bidirectional).
|
|
439
|
+
*
|
|
440
|
+
* @param services - Service definitions as array or object with Zod schemas
|
|
441
|
+
* @param context - Generation context containing messages, enums, and type prefix
|
|
442
|
+
* @returns Array of protobuf service definition strings
|
|
443
|
+
*/
|
|
294
444
|
const generateServices = (services, context) => {
|
|
295
445
|
return normalizeServices(services).map((service) => {
|
|
296
446
|
const methods = service.methods.map((method) => {
|
|
@@ -307,9 +457,24 @@ const generateServices = (services, context) => {
|
|
|
307
457
|
|
|
308
458
|
//#endregion
|
|
309
459
|
//#region src/zod-to-protobuf.ts
|
|
460
|
+
/**
|
|
461
|
+
* Generates a protobuf definition string for services only (without root message).
|
|
462
|
+
* Convenience function that calls zodToProtobuf with an empty schema.
|
|
463
|
+
*
|
|
464
|
+
* @param options - Options for protobuf generation
|
|
465
|
+
* @returns Protobuf definition string
|
|
466
|
+
*/
|
|
310
467
|
const zodToProtobufService = (options = {}) => {
|
|
311
468
|
return zodToProtobuf(z.object(), options);
|
|
312
469
|
};
|
|
470
|
+
/**
|
|
471
|
+
* Converts a Zod schema to a protobuf definition string.
|
|
472
|
+
* Generates messages, enums, and services based on the provided schema and options.
|
|
473
|
+
*
|
|
474
|
+
* @param schema - Optional Zod schema to convert (if not provided, only services will be generated)
|
|
475
|
+
* @param options - Options for protobuf generation including package name, root message name, type prefix, and services
|
|
476
|
+
* @returns Complete protobuf definition string
|
|
477
|
+
*/
|
|
313
478
|
const zodToProtobuf = (schema, options = {}) => {
|
|
314
479
|
const { packageName = "default", rootMessageName = "Message", typePrefix = "", services, skipRootMessage = false } = options;
|
|
315
480
|
const messages = /* @__PURE__ */ new Map();
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":["tupleFields: ProtobufField[]","methods: ServiceMethod[]"],"sources":["../src/utils.ts","../src/traversers.ts","../src/service-generator.ts","../src/zod-to-protobuf.ts"],"sourcesContent":["import { ZodNumber } from \"zod\";\nimport type { ProtobufField } from \"./types\";\n\nexport const getNumberTypeName = ({ value }: { value: ZodNumber }): string => {\n return value.isInt ? \"int32\" : \"double\";\n};\n\nexport const toPascalCase = ({ value }: { value: string }): string => {\n return value\n .split(\".\")\n .map((part) => part.charAt(0).toUpperCase() + part.slice(1))\n .join(\"\");\n};\n\nexport const protobufFieldToType = ({\n field,\n}: {\n field: ProtobufField;\n}): string => {\n return field.types.filter(Boolean).join(\" \");\n};\n","import * as inflection from \"inflection\";\nimport {\n ZodArray,\n ZodBigInt,\n ZodBoolean,\n ZodDate,\n ZodEnum,\n ZodMap,\n ZodNullable,\n ZodNumber,\n ZodObject,\n ZodOptional,\n ZodRecord,\n ZodSet,\n ZodString,\n ZodTuple,\n ZodType,\n type ZodTypeAny,\n} from \"zod\";\nimport {\n ZodArrayDefinition,\n ZodMapDefinition,\n ZodRecordDefinition,\n type ProtobufField,\n} from \"./types\";\nimport { getNumberTypeName, toPascalCase, protobufFieldToType } from \"./utils\";\n\nexport const traverseArray = ({\n key,\n value,\n messages,\n enums,\n typePrefix,\n parentKey,\n}: {\n key: string;\n value: ZodArray<ZodTypeAny> | ZodSet<ZodTypeAny>;\n messages: Map<string, string[]>;\n enums: Map<string, string[]>;\n typePrefix: string | null;\n parentKey?: string;\n}): ProtobufField[] => {\n const nestedValue =\n value instanceof ZodArray\n ? value.element\n : value instanceof ZodSet\n ? (value.def as unknown as ZodArrayDefinition).valueType\n : // @ts-expect-error\n (value.def as unknown as ZodArrayDefinition).element;\n\n const singularKey = inflection.singularize(key);\n const elementFields = traverseKey({\n key: singularKey,\n value: nestedValue,\n messages,\n enums,\n isOptional: false,\n isInArray: true,\n typePrefix,\n parentKey: nestedValue instanceof ZodObject ? parentKey : undefined,\n });\n return elementFields.map((field) => ({\n ...field,\n types: [\"repeated\", ...field.types],\n name: field.name.replace(singularKey, key),\n }));\n};\n\nexport const traverseMap = ({\n key,\n value,\n messages,\n enums,\n typePrefix,\n parentKey,\n}: {\n key: string;\n value: ZodMap<ZodTypeAny, ZodTypeAny>;\n messages: Map<string, string[]>;\n enums: Map<string, string[]>;\n typePrefix: string | null;\n parentKey?: string;\n}): ProtobufField[] => {\n const mapDef = value.def as ZodMapDefinition;\n\n const keyType = traverseKey({\n key: inflection.singularize(key),\n value: mapDef.keyType,\n messages,\n enums,\n isOptional: false,\n isInArray: true,\n typePrefix,\n parentKey,\n });\n const valueType = traverseKey({\n key: inflection.singularize(key),\n value: mapDef.valueType,\n messages,\n enums,\n isOptional: false,\n isInArray: true,\n typePrefix,\n parentKey,\n });\n\n if (!keyType[0] || keyType.length !== 1) {\n return [];\n }\n\n if (!valueType[0] || valueType.length !== 1) {\n return [];\n }\n\n const mapType = `map<${protobufFieldToType({ field: keyType[0] })}, ${protobufFieldToType({ field: valueType[0] })}>`;\n return [\n {\n types: [mapType],\n name: key,\n },\n ];\n};\n\nexport const traverseRecord = ({\n key,\n value,\n messages,\n enums,\n typePrefix,\n parentKey,\n}: {\n key: string;\n value: ZodRecord;\n messages: Map<string, string[]>;\n enums: Map<string, string[]>;\n typePrefix: string | null;\n parentKey?: string;\n}): ProtobufField[] => {\n const recordDef = value.def as unknown as ZodRecordDefinition;\n\n const keyType = traverseKey({\n key: inflection.singularize(key),\n value: recordDef.keyType,\n messages,\n enums,\n isOptional: false,\n isInArray: true,\n typePrefix,\n parentKey,\n });\n const valueType = traverseKey({\n key: inflection.singularize(key),\n value: recordDef.valueType,\n messages,\n enums,\n isOptional: false,\n isInArray: true,\n typePrefix,\n parentKey,\n });\n\n if (!keyType[0] || keyType.length !== 1) {\n return [];\n }\n\n if (!valueType[0] || valueType.length !== 1) {\n return [];\n }\n\n const mapType = `map<${protobufFieldToType({ field: keyType[0] })}, ${protobufFieldToType({ field: valueType[0] })}>`;\n return [\n {\n types: [mapType],\n name: key,\n },\n ];\n};\n\nexport const traverseKey = ({\n key,\n value,\n messages,\n enums,\n isOptional,\n isInArray,\n typePrefix,\n parentKey,\n}: {\n key: string;\n value: unknown;\n messages: Map<string, string[]>;\n enums: Map<string, string[]>;\n isOptional: boolean;\n isInArray: boolean;\n typePrefix: string | null;\n parentKey?: string;\n}): ProtobufField[] => {\n if (!value) {\n return [];\n }\n\n if (value instanceof ZodOptional || value instanceof ZodNullable) {\n return traverseKey({\n key,\n value: value.unwrap(),\n messages,\n enums,\n isOptional: true,\n isInArray,\n typePrefix,\n parentKey,\n });\n }\n\n if (value instanceof ZodArray || value instanceof ZodSet) {\n return traverseArray({\n key,\n value: value as ZodArray<ZodTypeAny> | ZodSet<ZodTypeAny>,\n messages,\n enums,\n typePrefix,\n parentKey,\n });\n }\n\n if (value instanceof ZodMap) {\n return traverseMap({\n key,\n value: value as ZodMap<ZodTypeAny, ZodTypeAny>,\n messages,\n enums,\n typePrefix,\n parentKey,\n });\n }\n\n if (value instanceof ZodRecord) {\n return traverseRecord({\n key,\n value: value as ZodRecord,\n messages,\n enums,\n typePrefix,\n parentKey,\n });\n }\n\n const optional = isOptional && !isInArray ? \"optional\" : null;\n\n if (value instanceof ZodObject) {\n let messageName = toPascalCase({ value: key });\n if (parentKey) {\n const isParentAlreadyPascalCase = /^[A-Z][a-zA-Z0-9]*$/.test(parentKey);\n if (isParentAlreadyPascalCase) {\n messageName = `${parentKey}${messageName}`;\n } else {\n const parentMessageName = toPascalCase({ value: parentKey });\n messageName = `${parentMessageName}${messageName}`;\n }\n }\n if (typePrefix) {\n messageName = `${typePrefix}${messageName}`;\n }\n const nestedMessageFields = traverseSchema({\n schema: value,\n messages,\n enums,\n typePrefix,\n parentKey: messageName,\n });\n messages.set(messageName, nestedMessageFields);\n return [\n {\n types: [optional, messageName],\n name: key,\n },\n ];\n }\n\n if (value instanceof ZodString) {\n return [\n {\n types: [optional, \"string\"],\n name: key,\n },\n ];\n }\n\n if (value instanceof ZodNumber) {\n const typeName = getNumberTypeName({ value });\n return [\n {\n types: [optional, typeName],\n name: key,\n },\n ];\n }\n\n if (value instanceof ZodBoolean) {\n return [\n {\n types: [optional, \"bool\"],\n name: key,\n },\n ];\n }\n\n if (value instanceof ZodEnum) {\n const enumFields = value.options\n .map(\n (option: string | number, index: number) =>\n ` ${String(option)} = ${index};`\n )\n .join(\"\\n\");\n let enumName = toPascalCase({ value: key });\n if (parentKey) {\n const isParentAlreadyPascalCase = /^[A-Z][a-zA-Z0-9]*$/.test(parentKey);\n if (isParentAlreadyPascalCase) {\n enumName = `${parentKey}${enumName}`;\n } else {\n const parentMessageName = toPascalCase({ value: parentKey });\n enumName = `${parentMessageName}${enumName}`;\n }\n }\n if (typePrefix) {\n enumName = `${typePrefix}${enumName}`;\n }\n enums.set(enumName, [`enum ${enumName} {\\n${enumFields}\\n}`]);\n return [\n {\n types: [optional, enumName],\n name: key,\n },\n ];\n }\n\n if (value instanceof ZodDate) {\n return [\n {\n types: [optional, \"string\"],\n name: key,\n },\n ];\n }\n\n if (value instanceof ZodBigInt) {\n return [\n {\n types: [optional, \"int64\"],\n name: key,\n },\n ];\n }\n\n if (value instanceof ZodTuple) {\n const tupleFields: ProtobufField[] = (\n value.def.items as ZodTypeAny[]\n ).flatMap((item: ZodTypeAny, index: number) => {\n return traverseKey({\n key: `${key}_${index}`,\n value: item,\n messages,\n enums,\n isOptional: false,\n isInArray,\n typePrefix,\n parentKey,\n });\n });\n\n let tupleMessageName = toPascalCase({ value: key });\n if (parentKey) {\n const isParentAlreadyPascalCase = /^[A-Z][a-zA-Z0-9]*$/.test(parentKey);\n if (isParentAlreadyPascalCase) {\n tupleMessageName = `${parentKey}${tupleMessageName}`;\n } else {\n const parentMessageName = toPascalCase({ value: parentKey });\n tupleMessageName = `${parentMessageName}${tupleMessageName}`;\n }\n }\n if (typePrefix) {\n tupleMessageName = `${typePrefix}${tupleMessageName}`;\n }\n messages.set(\n tupleMessageName,\n tupleFields.map(\n (field, index) =>\n ` ${field.types.join(\" \")} ${field.name} = ${index + 1};`\n )\n );\n return [\n {\n types: [optional, tupleMessageName],\n name: key,\n },\n ];\n }\n\n if (value instanceof ZodType) {\n return [];\n }\n\n return [];\n};\n\nexport const traverseSchema = ({\n schema,\n messages,\n enums,\n typePrefix,\n parentKey,\n}: {\n schema: ZodTypeAny;\n messages: Map<string, string[]>;\n enums: Map<string, string[]>;\n typePrefix: string | null;\n parentKey?: string;\n}): string[] => {\n if (\n !schema ||\n typeof schema !== \"object\" ||\n !(\"def\" in schema) ||\n (schema.constructor.name !== \"ZodObject\" &&\n (schema.def as { type?: string }).type !== \"object\")\n ) {\n return [];\n }\n\n const zodObject = schema as ZodObject<any>;\n const fields = Object.entries(zodObject.shape).flatMap(([key, value]) => {\n return traverseKey({\n key,\n value,\n messages,\n enums,\n isOptional: false,\n isInArray: false,\n typePrefix,\n parentKey,\n });\n });\n\n return fields.map(\n (field, index) =>\n `${protobufFieldToType({ field })} ${field.name} = ${index + 1};`\n );\n};\n","import { z, ZodObject, type ZodTypeAny } from \"zod\";\nimport type {\n ServiceDefinition,\n ServiceMethod,\n ServicesInput,\n ZodFunctionDefinition,\n ZodTupleDefinition,\n} from \"./types\";\nimport { toPascalCase } from \"./utils\";\nimport { traverseSchema } from \"./traversers\";\n\ninterface ServiceGenerationContext {\n messages: Map<string, string[]>;\n enums: Map<string, string[]>;\n typePrefix: string | null;\n}\n\nconst parseZodServiceSchema = (\n name: string,\n schema: ZodObject<Record<string, ZodTypeAny>>\n): ServiceDefinition => {\n const shape = schema.shape as Record<string, ZodTypeAny>;\n const methods: ServiceMethod[] = [];\n\n for (const [methodName, methodSchema] of Object.entries(shape)) {\n const methodDef = (methodSchema as ZodTypeAny).def as ZodFunctionDefinition;\n\n if (methodDef.type === \"function\") {\n const inputDef = methodDef.input;\n\n const args = (inputDef?.def as ZodTupleDefinition)?.items ?? [];\n const output = methodDef.output as ZodTypeAny;\n\n if (args.length > 0 && args[0] && output) {\n const request = args[0];\n const response = output;\n\n methods.push({\n name: methodName,\n request,\n response,\n });\n }\n }\n }\n\n return {\n name,\n methods,\n };\n};\n\nconst normalizeServices = (services: ServicesInput): ServiceDefinition[] => {\n if (Array.isArray(services)) {\n return services;\n }\n\n return Object.entries(services).map(([name, schema]) =>\n parseZodServiceSchema(name, schema)\n );\n};\n\nconst ensureZodObject = (\n schema: ZodTypeAny\n): ZodObject<Record<string, ZodTypeAny>> => {\n const schemaType =\n (schema.def as { type?: string }).type || schema.constructor.name;\n\n if (schemaType === \"object\" || schema.constructor.name === \"ZodObject\") {\n return schema as ZodObject<Record<string, ZodTypeAny>>;\n }\n\n return z.object({\n data: schema,\n });\n};\n\nconst generateRequestMessageName = (\n methodName: string,\n typePrefix: string | null\n): string => {\n const messageName = toPascalCase({ value: `${methodName}Request` });\n return typePrefix ? `${typePrefix}${messageName}` : messageName;\n};\n\nconst generateResponseMessageName = (\n methodName: string,\n typePrefix: string | null\n): string => {\n const messageName = toPascalCase({ value: `${methodName}Response` });\n return typePrefix ? `${typePrefix}${messageName}` : messageName;\n};\n\nconst processServiceMethod = (\n method: ServiceMethod,\n context: ServiceGenerationContext\n): { requestName: string; responseName: string } => {\n const { messages, enums, typePrefix } = context;\n\n const requestName = generateRequestMessageName(method.name, typePrefix);\n const responseName = generateResponseMessageName(method.name, typePrefix);\n\n if (!messages.has(requestName)) {\n const requestSchema = ensureZodObject(method.request);\n const requestFields = traverseSchema({\n schema: requestSchema,\n messages,\n enums,\n typePrefix,\n parentKey: requestName,\n });\n messages.set(requestName, requestFields);\n }\n\n if (!messages.has(responseName)) {\n const responseSchema = ensureZodObject(method.response);\n const responseFields = traverseSchema({\n schema: responseSchema,\n messages,\n enums,\n typePrefix,\n parentKey: responseName,\n });\n messages.set(responseName, responseFields);\n }\n\n return { requestName, responseName };\n};\n\nexport const generateServices = (\n services: ServicesInput,\n context: ServiceGenerationContext\n): string[] => {\n const normalizedServices = normalizeServices(services);\n\n return normalizedServices.map((service) => {\n const methods = service.methods.map((method) => {\n const { requestName, responseName } = processServiceMethod(\n method,\n context\n );\n\n const requestStreaming =\n method.streaming === \"client\" || method.streaming === \"bidirectional\";\n const responseStreaming =\n method.streaming === \"server\" || method.streaming === \"bidirectional\";\n\n const requestType = requestStreaming\n ? `stream ${requestName}`\n : requestName;\n const responseType = responseStreaming\n ? `stream ${responseName}`\n : responseName;\n\n return ` rpc ${toPascalCase({ value: method.name })}(${requestType}) returns (${responseType});`;\n });\n\n return `service ${toPascalCase({ value: service.name })} {\\n${methods.join(\"\\n\")}\\n}`;\n });\n};\n","import type { ZodTypeAny } from \"zod\";\nimport type { ZodToProtobufOptions } from \"./types\";\nimport { traverseSchema } from \"./traversers\";\nimport { generateServices } from \"./service-generator\";\nimport { z } from \"zod\";\n\nexport const zodToProtobufService = (\n options: ZodToProtobufOptions = {}\n): string => {\n return zodToProtobuf(z.object(), options);\n};\n\nexport const zodToProtobuf = (\n schema?: ZodTypeAny,\n options: ZodToProtobufOptions = {}\n): string => {\n const {\n packageName = \"default\",\n rootMessageName = \"Message\",\n typePrefix = \"\",\n services,\n skipRootMessage = false,\n } = options;\n\n const messages = new Map<string, string[]>();\n const enums = new Map<string, string[]>();\n\n if (schema && !skipRootMessage) {\n const fields = traverseSchema({ schema, messages, enums, typePrefix });\n if (fields.length > 0) {\n const rootMessageKey = `${typePrefix}${rootMessageName}`;\n messages.set(rootMessageKey, fields);\n }\n }\n\n const context = {\n messages,\n enums,\n typePrefix: typePrefix || null,\n };\n\n const hasServices =\n services &&\n (Array.isArray(services)\n ? services.length > 0\n : Object.keys(services).length > 0);\n\n const servicesString = hasServices ? generateServices(services, context) : [];\n\n const enumsString = Array.from(enums.values()).map((enumDef) =>\n enumDef.join(\"\\n\")\n );\n\n const messagesString = Array.from(messages.entries()).map(\n ([name, fields]) =>\n `message ${name} {\\n${fields.map((field) => ` ${field}`).join(\"\\n\")}\\n}`\n );\n\n const content = [servicesString, enumsString, messagesString]\n .filter((strings) => !!strings.length)\n .map((strings) => strings.join(\"\\n\\n\"))\n .join(\"\\n\\n\");\n\n const protoDefinition = `\nsyntax = \"proto3\";\npackage ${packageName};\n\n${content}\n`;\n\n return protoDefinition.trim();\n};\n"],"mappings":";;;;AAGA,MAAa,qBAAqB,EAAE,YAA0C;AAC5E,QAAO,MAAM,QAAQ,UAAU;;AAGjC,MAAa,gBAAgB,EAAE,YAAuC;AACpE,QAAO,MACJ,MAAM,IAAI,CACV,KAAK,SAAS,KAAK,OAAO,EAAE,CAAC,aAAa,GAAG,KAAK,MAAM,EAAE,CAAC,CAC3D,KAAK,GAAG;;AAGb,MAAa,uBAAuB,EAClC,YAGY;AACZ,QAAO,MAAM,MAAM,OAAO,QAAQ,CAAC,KAAK,IAAI;;;;;ACQ9C,MAAa,iBAAiB,EAC5B,KACA,OACA,UACA,OACA,YACA,gBAQqB;CACrB,MAAM,cACJ,iBAAiB,WACb,MAAM,UACN,iBAAiB,SACd,MAAM,IAAsC,YAE5C,MAAM,IAAsC;CAErD,MAAM,cAAc,WAAW,YAAY,IAAI;AAW/C,Q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,CACnB;EAEsB,MAAM,KAAK,SAAS,SAAS,CAAC,CAAC,KACnD,CAAC,MAAM,YACN,WAAW,KAAK,MAAM,OAAO,KAAK,UAAU,OAAO,QAAQ,CAAC,KAAK,KAAK,CAAC,KAC1E;EAE4D,CAC1D,QAAQ,YAAY,CAAC,CAAC,QAAQ,OAAO,CACrC,KAAK,YAAY,QAAQ,KAAK,OAAO,CAAC,CACtC,KAAK,OAAO,CAMP;EAGe,MAAM"}
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["tupleFields: ProtobufField[]","methods: ServiceMethod[]","streaming: \"client\" | \"server\" | \"bidirectional\" | undefined"],"sources":["../src/utils.ts","../src/traversers.ts","../src/service-generator.ts","../src/zod-to-protobuf.ts"],"sourcesContent":["import { ZodNumber } from \"zod\";\nimport type { ProtobufField } from \"./types\";\n\n/**\n * Determines the protobuf number type name based on Zod number schema.\n * Returns \"int32\" for integers, \"double\" for floating point numbers.\n *\n * @param value - Zod number schema\n * @returns Protobuf number type name (\"int32\" or \"double\")\n */\nexport const getNumberTypeName = ({ value }: { value: ZodNumber }): string => {\n return value.isInt ? \"int32\" : \"double\";\n};\n\n/**\n * Converts a string to PascalCase format.\n * Handles dot-separated strings by capitalizing each part.\n *\n * @param value - String to convert\n * @returns PascalCase string\n */\nexport const toPascalCase = ({ value }: { value: string }): string => {\n return value\n .split(\".\")\n .map((part) => part.charAt(0).toUpperCase() + part.slice(1))\n .join(\"\");\n};\n\n/**\n * Converts a protobuf field definition to its type string representation.\n * Filters out null/empty values and joins the remaining types.\n *\n * @param field - Protobuf field definition\n * @returns Type string for the protobuf field\n */\nexport const protobufFieldToType = ({\n field,\n}: {\n field: ProtobufField;\n}): string => {\n return field.types.filter(Boolean).join(\" \");\n};\n","import * as inflection from \"inflection\";\nimport {\n ZodArray,\n ZodBigInt,\n ZodBoolean,\n ZodDate,\n ZodEnum,\n ZodMap,\n ZodNullable,\n ZodNumber,\n ZodObject,\n ZodOptional,\n ZodRecord,\n ZodSet,\n ZodString,\n ZodTuple,\n ZodType,\n type ZodTypeAny,\n} from \"zod\";\nimport {\n ZodArrayDefinition,\n ZodMapDefinition,\n ZodRecordDefinition,\n type ProtobufField,\n} from \"./types\";\nimport { getNumberTypeName, toPascalCase, protobufFieldToType } from \"./utils\";\n\n/**\n * Traverses a Zod array or set schema and converts it to protobuf repeated fields.\n * Handles nested types and generates appropriate field definitions.\n *\n * @param key - Field name\n * @param value - Zod array or set schema\n * @param messages - Map of message names to their protobuf fields\n * @param enums - Map of enum names to their protobuf values\n * @param typePrefix - Optional prefix for type names\n * @param parentKey - Optional parent message name for nested types\n * @returns Array of protobuf field definitions\n */\nexport const traverseArray = ({\n key,\n value,\n messages,\n enums,\n typePrefix,\n parentKey,\n}: {\n key: string;\n value: ZodArray<ZodTypeAny> | ZodSet<ZodTypeAny>;\n messages: Map<string, string[]>;\n enums: Map<string, string[]>;\n typePrefix: string | null;\n parentKey?: string;\n}): ProtobufField[] => {\n const nestedValue =\n value instanceof ZodArray\n ? value.element\n : value instanceof ZodSet\n ? (value.def as unknown as ZodArrayDefinition).valueType\n : // @ts-expect-error\n (value.def as unknown as ZodArrayDefinition).element;\n\n const singularKey = inflection.singularize(key);\n const elementFields = traverseKey({\n key: singularKey,\n value: nestedValue,\n messages,\n enums,\n isOptional: false,\n isInArray: true,\n typePrefix,\n parentKey: nestedValue instanceof ZodObject ? parentKey : undefined,\n });\n return elementFields.map((field) => ({\n ...field,\n types: [\"repeated\", ...field.types],\n name: field.name.replace(singularKey, key),\n }));\n};\n\n/**\n * Traverses a Zod map schema and converts it to a protobuf map type.\n * Validates that both key and value types are simple types suitable for map keys/values.\n *\n * @param key - Field name\n * @param value - Zod map schema\n * @param messages - Map of message names to their protobuf fields\n * @param enums - Map of enum names to their protobuf values\n * @param typePrefix - Optional prefix for type names\n * @param parentKey - Optional parent message name for nested types\n * @returns Array containing a single protobuf map field definition, or empty array if invalid\n */\nexport const traverseMap = ({\n key,\n value,\n messages,\n enums,\n typePrefix,\n parentKey,\n}: {\n key: string;\n value: ZodMap<ZodTypeAny, ZodTypeAny>;\n messages: Map<string, string[]>;\n enums: Map<string, string[]>;\n typePrefix: string | null;\n parentKey?: string;\n}): ProtobufField[] => {\n const mapDef = value.def as ZodMapDefinition;\n\n const keyType = traverseKey({\n key: inflection.singularize(key),\n value: mapDef.keyType,\n messages,\n enums,\n isOptional: false,\n isInArray: true,\n typePrefix,\n parentKey,\n });\n const valueType = traverseKey({\n key: inflection.singularize(key),\n value: mapDef.valueType,\n messages,\n enums,\n isOptional: false,\n isInArray: true,\n typePrefix,\n parentKey,\n });\n\n if (!keyType[0] || keyType.length !== 1) {\n return [];\n }\n\n if (!valueType[0] || valueType.length !== 1) {\n return [];\n }\n\n const mapType = `map<${protobufFieldToType({ field: keyType[0] })}, ${protobufFieldToType({ field: valueType[0] })}>`;\n return [\n {\n types: [mapType],\n name: key,\n },\n ];\n};\n\n/**\n * Traverses a Zod record schema and converts it to a protobuf map type.\n * Similar to traverseMap but handles ZodRecord type.\n *\n * @param key - Field name\n * @param value - Zod record schema\n * @param messages - Map of message names to their protobuf fields\n * @param enums - Map of enum names to their protobuf values\n * @param typePrefix - Optional prefix for type names\n * @param parentKey - Optional parent message name for nested types\n * @returns Array containing a single protobuf map field definition, or empty array if invalid\n */\nexport const traverseRecord = ({\n key,\n value,\n messages,\n enums,\n typePrefix,\n parentKey,\n}: {\n key: string;\n value: ZodRecord;\n messages: Map<string, string[]>;\n enums: Map<string, string[]>;\n typePrefix: string | null;\n parentKey?: string;\n}): ProtobufField[] => {\n const recordDef = value.def as unknown as ZodRecordDefinition;\n\n const keyType = traverseKey({\n key: inflection.singularize(key),\n value: recordDef.keyType,\n messages,\n enums,\n isOptional: false,\n isInArray: true,\n typePrefix,\n parentKey,\n });\n const valueType = traverseKey({\n key: inflection.singularize(key),\n value: recordDef.valueType,\n messages,\n enums,\n isOptional: false,\n isInArray: true,\n typePrefix,\n parentKey,\n });\n\n if (!keyType[0] || keyType.length !== 1) {\n return [];\n }\n\n if (!valueType[0] || valueType.length !== 1) {\n return [];\n }\n\n const mapType = `map<${protobufFieldToType({ field: keyType[0] })}, ${protobufFieldToType({ field: valueType[0] })}>`;\n return [\n {\n types: [mapType],\n name: key,\n },\n ];\n};\n\n/**\n * Traverses a single key-value pair from a Zod schema and converts it to protobuf field definitions.\n * Handles various Zod types including primitives, objects, arrays, maps, enums, tuples, and optional/nullable fields.\n *\n * @param key - Field name\n * @param value - Zod schema value\n * @param messages - Map of message names to their protobuf fields\n * @param enums - Map of enum names to their protobuf values\n * @param isOptional - Whether the field is optional\n * @param isInArray - Whether the field is inside an array\n * @param typePrefix - Optional prefix for type names\n * @param parentKey - Optional parent message name for nested types\n * @returns Array of protobuf field definitions\n */\nexport const traverseKey = ({\n key,\n value,\n messages,\n enums,\n isOptional,\n isInArray,\n typePrefix,\n parentKey,\n}: {\n key: string;\n value: unknown;\n messages: Map<string, string[]>;\n enums: Map<string, string[]>;\n isOptional: boolean;\n isInArray: boolean;\n typePrefix: string | null;\n parentKey?: string;\n}): ProtobufField[] => {\n if (!value) {\n return [];\n }\n\n if (value instanceof ZodOptional || value instanceof ZodNullable) {\n return traverseKey({\n key,\n value: value.unwrap(),\n messages,\n enums,\n isOptional: true,\n isInArray,\n typePrefix,\n parentKey,\n });\n }\n\n if (value instanceof ZodArray || value instanceof ZodSet) {\n return traverseArray({\n key,\n value: value as ZodArray<ZodTypeAny> | ZodSet<ZodTypeAny>,\n messages,\n enums,\n typePrefix,\n parentKey,\n });\n }\n\n if (value instanceof ZodMap) {\n return traverseMap({\n key,\n value: value as ZodMap<ZodTypeAny, ZodTypeAny>,\n messages,\n enums,\n typePrefix,\n parentKey,\n });\n }\n\n if (value instanceof ZodRecord) {\n return traverseRecord({\n key,\n value: value as ZodRecord,\n messages,\n enums,\n typePrefix,\n parentKey,\n });\n }\n\n const optional = isOptional && !isInArray ? \"optional\" : null;\n\n if (value instanceof ZodObject) {\n let messageName = toPascalCase({ value: key });\n if (parentKey) {\n const isParentAlreadyPascalCase = /^[A-Z][a-zA-Z0-9]*$/.test(parentKey);\n if (isParentAlreadyPascalCase) {\n messageName = `${parentKey}${messageName}`;\n } else {\n const parentMessageName = toPascalCase({ value: parentKey });\n messageName = `${parentMessageName}${messageName}`;\n }\n }\n if (typePrefix) {\n messageName = `${typePrefix}${messageName}`;\n }\n const nestedMessageFields = traverseSchema({\n schema: value,\n messages,\n enums,\n typePrefix,\n parentKey: messageName,\n });\n messages.set(messageName, nestedMessageFields);\n return [\n {\n types: [optional, messageName],\n name: key,\n },\n ];\n }\n\n if (value instanceof ZodString) {\n return [\n {\n types: [optional, \"string\"],\n name: key,\n },\n ];\n }\n\n if (value instanceof ZodNumber) {\n const typeName = getNumberTypeName({ value });\n return [\n {\n types: [optional, typeName],\n name: key,\n },\n ];\n }\n\n if (value instanceof ZodBoolean) {\n return [\n {\n types: [optional, \"bool\"],\n name: key,\n },\n ];\n }\n\n if (value instanceof ZodEnum) {\n const enumFields = value.options\n .map(\n (option: string | number, index: number) =>\n ` ${String(option)} = ${index};`\n )\n .join(\"\\n\");\n let enumName = toPascalCase({ value: key });\n if (parentKey) {\n const isParentAlreadyPascalCase = /^[A-Z][a-zA-Z0-9]*$/.test(parentKey);\n if (isParentAlreadyPascalCase) {\n enumName = `${parentKey}${enumName}`;\n } else {\n const parentMessageName = toPascalCase({ value: parentKey });\n enumName = `${parentMessageName}${enumName}`;\n }\n }\n if (typePrefix) {\n enumName = `${typePrefix}${enumName}`;\n }\n enums.set(enumName, [`enum ${enumName} {\\n${enumFields}\\n}`]);\n return [\n {\n types: [optional, enumName],\n name: key,\n },\n ];\n }\n\n if (value instanceof ZodDate) {\n return [\n {\n types: [optional, \"string\"],\n name: key,\n },\n ];\n }\n\n if (value instanceof ZodBigInt) {\n return [\n {\n types: [optional, \"int64\"],\n name: key,\n },\n ];\n }\n\n if (value instanceof ZodType) {\n const def = value.def as {\n type?: string;\n check?: unknown;\n fn?: (data: unknown) => boolean;\n };\n if (def.type === \"custom\" && def.check === \"custom\" && def.fn) {\n try {\n if (\n (typeof Buffer !== \"undefined\" && def.fn(Buffer.alloc(0))) ||\n (typeof Uint8Array !== \"undefined\" && def.fn(new Uint8Array(0)))\n ) {\n return [\n {\n types: [optional, \"bytes\"],\n name: key,\n },\n ];\n }\n } catch {}\n }\n }\n\n if (value instanceof ZodTuple) {\n const tupleFields: ProtobufField[] = (\n value.def.items as ZodTypeAny[]\n ).flatMap((item: ZodTypeAny, index: number) => {\n return traverseKey({\n key: `${key}_${index}`,\n value: item,\n messages,\n enums,\n isOptional: false,\n isInArray,\n typePrefix,\n parentKey,\n });\n });\n\n let tupleMessageName = toPascalCase({ value: key });\n if (parentKey) {\n const isParentAlreadyPascalCase = /^[A-Z][a-zA-Z0-9]*$/.test(parentKey);\n if (isParentAlreadyPascalCase) {\n tupleMessageName = `${parentKey}${tupleMessageName}`;\n } else {\n const parentMessageName = toPascalCase({ value: parentKey });\n tupleMessageName = `${parentMessageName}${tupleMessageName}`;\n }\n }\n if (typePrefix) {\n tupleMessageName = `${typePrefix}${tupleMessageName}`;\n }\n messages.set(\n tupleMessageName,\n tupleFields.map(\n (field, index) =>\n ` ${field.types.join(\" \")} ${field.name} = ${index + 1};`\n )\n );\n return [\n {\n types: [optional, tupleMessageName],\n name: key,\n },\n ];\n }\n\n if (value instanceof ZodType) {\n return [];\n }\n\n return [];\n};\n\n/**\n * Traverses a Zod object schema and converts it to protobuf message field definitions.\n * Processes all fields in the schema and generates appropriate protobuf types.\n *\n * @param schema - Zod schema to traverse\n * @param messages - Map of message names to their protobuf fields\n * @param enums - Map of enum names to their protobuf values\n * @param typePrefix - Optional prefix for type names\n * @param parentKey - Optional parent message name for nested types\n * @returns Array of protobuf field definition strings\n */\nexport const traverseSchema = ({\n schema,\n messages,\n enums,\n typePrefix,\n parentKey,\n}: {\n schema: ZodTypeAny;\n messages: Map<string, string[]>;\n enums: Map<string, string[]>;\n typePrefix: string | null;\n parentKey?: string;\n}): string[] => {\n if (\n !schema ||\n typeof schema !== \"object\" ||\n !(\"def\" in schema) ||\n (schema.constructor.name !== \"ZodObject\" &&\n (schema.def as { type?: string }).type !== \"object\")\n ) {\n return [];\n }\n\n const zodObject = schema as ZodObject<any>;\n const fields = Object.entries(zodObject.shape).flatMap(([key, value]) => {\n return traverseKey({\n key,\n value,\n messages,\n enums,\n isOptional: false,\n isInArray: false,\n typePrefix,\n parentKey,\n });\n });\n\n return fields.map(\n (field, index) =>\n `${protobufFieldToType({ field })} ${field.name} = ${index + 1};`\n );\n};\n","import { z, ZodObject, type ZodTypeAny } from \"zod\";\nimport type {\n ServiceDefinition,\n ServiceMethod,\n ServicesInput,\n ZodFunctionDefinition,\n ZodTupleDefinition,\n} from \"./types\";\nimport { toPascalCase } from \"./utils\";\nimport { traverseSchema } from \"./traversers\";\n\n/**\n * Context for generating protobuf services.\n * Contains accumulated messages, enums, and type prefix.\n */\ninterface ServiceGenerationContext {\n /** Map of message names to their protobuf fields */\n messages: Map<string, string[]>;\n /** Map of enum names to their protobuf values */\n enums: Map<string, string[]>;\n /** Prefix for type names or null if prefix is not used */\n typePrefix: string | null;\n}\n\n/**\n * Parses a Zod service schema and extracts method definitions.\n * Each method must be a function with one argument (request) and a return value (response).\n *\n * @param name - Service name\n * @param schema - Zod object containing service methods as functions\n * @returns Service definition with extracted methods\n */\nconst parseZodServiceSchema = (\n name: string,\n schema: ZodObject<Record<string, ZodTypeAny>>\n): ServiceDefinition => {\n const shape = schema.shape as Record<string, ZodTypeAny>;\n const methods: ServiceMethod[] = [];\n\n for (const [methodName, methodSchema] of Object.entries(shape)) {\n const methodDef = (methodSchema as ZodTypeAny).def as ZodFunctionDefinition;\n\n if (methodDef.type === \"function\") {\n const inputDef = methodDef.input;\n\n const args = (inputDef?.def as ZodTupleDefinition)?.items ?? [];\n const output = methodDef.output as ZodTypeAny;\n\n if (args.length > 0 && args[0] && output) {\n const request = args[0];\n const response = output;\n\n let streaming: \"client\" | \"server\" | \"bidirectional\" | undefined;\n const metadata = methodSchema.meta();\n console.log(methodSchema.meta());\n streaming = metadata?.streaming as\n | \"client\"\n | \"server\"\n | \"bidirectional\"\n | undefined;\n\n methods.push({\n name: methodName,\n request,\n response,\n streaming,\n });\n }\n }\n }\n\n return {\n name,\n methods,\n };\n};\n\n/**\n * Normalizes service input data into an array of service definitions.\n * If an array is passed, returns it as is.\n * If an object is passed, converts each key-value pair into a service definition.\n *\n * @param services - Array of service definitions or object with Zod schemas\n * @returns Array of normalized service definitions\n */\nconst normalizeServices = (services: ServicesInput): ServiceDefinition[] => {\n if (Array.isArray(services)) {\n return services;\n }\n\n return Object.entries(services).map(([name, schema]) =>\n parseZodServiceSchema(name, schema)\n );\n};\n\n/**\n * Ensures that the schema is a ZodObject.\n * If the schema is already an object, returns it as is.\n * Otherwise, wraps the schema in an object with a \"data\" field.\n *\n * @param schema - Zod schema of any type\n * @returns ZodObject containing the original schema\n */\nconst ensureZodObject = (\n schema: ZodTypeAny\n): ZodObject<Record<string, ZodTypeAny>> => {\n const schemaType =\n (schema.def as { type?: string }).type || schema.constructor.name;\n\n if (schemaType === \"object\" || schema.constructor.name === \"ZodObject\") {\n return schema as ZodObject<Record<string, ZodTypeAny>>;\n }\n\n return z.object({\n data: schema,\n });\n};\n\n/**\n * Generates the name for a request message based on the method name.\n *\n * @param methodName - Name of the service method\n * @param typePrefix - Optional prefix for type names\n * @returns Generated request message name in PascalCase\n */\nconst generateRequestMessageName = (\n methodName: string,\n typePrefix: string | null\n): string => {\n const messageName = toPascalCase({ value: `${methodName}Request` });\n return typePrefix ? `${typePrefix}${messageName}` : messageName;\n};\n\n/**\n * Generates the name for a response message based on the method name.\n *\n * @param methodName - Name of the service method\n * @param typePrefix - Optional prefix for type names\n * @returns Generated response message name in PascalCase\n */\nconst generateResponseMessageName = (\n methodName: string,\n typePrefix: string | null\n): string => {\n const messageName = toPascalCase({ value: `${methodName}Response` });\n return typePrefix ? `${typePrefix}${messageName}` : messageName;\n};\n\n/**\n * Processes a service method by generating request and response message names\n * and traversing their schemas to populate the context with message definitions.\n *\n * @param method - Service method definition\n * @param context - Generation context containing messages, enums, and type prefix\n * @returns Object containing request and response message names\n */\nconst processServiceMethod = (\n method: ServiceMethod,\n context: ServiceGenerationContext\n): { requestName: string; responseName: string } => {\n const { messages, enums, typePrefix } = context;\n\n const requestName = generateRequestMessageName(method.name, typePrefix);\n const responseName = generateResponseMessageName(method.name, typePrefix);\n\n if (!messages.has(requestName)) {\n const requestSchema = ensureZodObject(method.request);\n const requestFields = traverseSchema({\n schema: requestSchema,\n messages,\n enums,\n typePrefix,\n parentKey: requestName,\n });\n messages.set(requestName, requestFields);\n }\n\n if (!messages.has(responseName)) {\n const responseSchema = ensureZodObject(method.response);\n const responseFields = traverseSchema({\n schema: responseSchema,\n messages,\n enums,\n typePrefix,\n parentKey: responseName,\n });\n messages.set(responseName, responseFields);\n }\n\n return { requestName, responseName };\n};\n\n/**\n * Generates protobuf service definitions from Zod service schemas.\n * Supports streaming methods (client, server, bidirectional).\n *\n * @param services - Service definitions as array or object with Zod schemas\n * @param context - Generation context containing messages, enums, and type prefix\n * @returns Array of protobuf service definition strings\n */\nexport const generateServices = (\n services: ServicesInput,\n context: ServiceGenerationContext\n): string[] => {\n const normalizedServices = normalizeServices(services);\n\n return normalizedServices.map((service) => {\n const methods = service.methods.map((method) => {\n const { requestName, responseName } = processServiceMethod(\n method,\n context\n );\n\n const requestStreaming =\n method.streaming === \"client\" || method.streaming === \"bidirectional\";\n const responseStreaming =\n method.streaming === \"server\" || method.streaming === \"bidirectional\";\n\n const requestType = requestStreaming\n ? `stream ${requestName}`\n : requestName;\n const responseType = responseStreaming\n ? `stream ${responseName}`\n : responseName;\n\n return ` rpc ${toPascalCase({ value: method.name })}(${requestType}) returns (${responseType});`;\n });\n\n return `service ${toPascalCase({ value: service.name })} {\\n${methods.join(\"\\n\")}\\n}`;\n });\n};\n","import type { ZodTypeAny } from \"zod\";\nimport type { ZodToProtobufOptions } from \"./types\";\nimport { traverseSchema } from \"./traversers\";\nimport { generateServices } from \"./service-generator\";\nimport { z } from \"zod\";\n\n/**\n * Generates a protobuf definition string for services only (without root message).\n * Convenience function that calls zodToProtobuf with an empty schema.\n *\n * @param options - Options for protobuf generation\n * @returns Protobuf definition string\n */\nexport const zodToProtobufService = (\n options: ZodToProtobufOptions = {}\n): string => {\n return zodToProtobuf(z.object(), options);\n};\n\n/**\n * Converts a Zod schema to a protobuf definition string.\n * Generates messages, enums, and services based on the provided schema and options.\n *\n * @param schema - Optional Zod schema to convert (if not provided, only services will be generated)\n * @param options - Options for protobuf generation including package name, root message name, type prefix, and services\n * @returns Complete protobuf definition string\n */\nexport const zodToProtobuf = (\n schema?: ZodTypeAny,\n options: ZodToProtobufOptions = {}\n): string => {\n const {\n packageName = \"default\",\n rootMessageName = \"Message\",\n typePrefix = \"\",\n services,\n skipRootMessage = false,\n } = options;\n\n const messages = new Map<string, string[]>();\n const enums = new Map<string, string[]>();\n\n if (schema && !skipRootMessage) {\n const fields = traverseSchema({ schema, messages, enums, typePrefix });\n if (fields.length > 0) {\n const rootMessageKey = `${typePrefix}${rootMessageName}`;\n messages.set(rootMessageKey, fields);\n }\n }\n\n const context = {\n messages,\n enums,\n typePrefix: typePrefix || null,\n };\n\n const hasServices =\n services &&\n (Array.isArray(services)\n ? services.length > 0\n : Object.keys(services).length > 0);\n\n const servicesString = hasServices ? generateServices(services, context) : [];\n\n const enumsString = Array.from(enums.values()).map((enumDef) =>\n enumDef.join(\"\\n\")\n );\n\n const messagesString = Array.from(messages.entries()).map(\n ([name, fields]) =>\n `message ${name} {\\n${fields.map((field) => ` ${field}`).join(\"\\n\")}\\n}`\n );\n\n const content = [servicesString, enumsString, messagesString]\n .filter((strings) => !!strings.length)\n .map((strings) => strings.join(\"\\n\\n\"))\n .join(\"\\n\\n\");\n\n const protoDefinition = `\nsyntax = \"proto3\";\npackage ${packageName};\n\n${content}\n`;\n\n return protoDefinition.trim();\n};\n"],"mappings":";;;;;;;;;;;AAU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QAAQ,CAAC,KAAK,KAAK,CAAC,KAC1E;EAE4D,CAC1D,QAAQ,YAAY,CAAC,CAAC,QAAQ,OAAO,CACrC,KAAK,YAAY,QAAQ,KAAK,OAAO,CAAC,CACtC,KAAK,OAAO,CAMP;EAGe,MAAM"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@globalart/zod-to-proto",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.16",
|
|
4
4
|
"author": {
|
|
5
5
|
"name": "GlobalArt, Inc"
|
|
6
6
|
},
|
|
@@ -56,7 +56,7 @@
|
|
|
56
56
|
"prettier": "^3.6.2",
|
|
57
57
|
"release-it": "19.0.6",
|
|
58
58
|
"ts-node": "^10.9.2",
|
|
59
|
-
"tsdown": "0.16.
|
|
59
|
+
"tsdown": "0.16.6",
|
|
60
60
|
"typescript": "^5.9.3"
|
|
61
61
|
},
|
|
62
62
|
"dependencies": {
|