api-core-lib 12.0.76 → 12.0.77

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/cli.cjs +171 -2
  2. package/package.json +1 -1
package/dist/cli.cjs CHANGED
@@ -74,6 +74,106 @@ var import_chalk = __toESM(require("chalk"), 1);
74
74
  var import_dotenv = __toESM(require("dotenv"), 1);
75
75
  var import_swagger_parser = __toESM(require("@apidevtools/swagger-parser"), 1);
76
76
  var import_openapi_types = __toESM(require_dist(), 1);
77
+
78
+ // src/generator/core/_propToZod.ts
79
+ function _propToZod(prop) {
80
+ let zodChain;
81
+ const getRuleMessage = (rule, defaultKey) => {
82
+ const customMessages = prop.errorMessages;
83
+ const message = customMessages?.[rule] || defaultKey;
84
+ return `{ "message": "${message}" }`;
85
+ };
86
+ switch (prop.type) {
87
+ case "string": {
88
+ if (prop.enum && prop.enum.length > 0) {
89
+ zodChain = `z.enum(${JSON.stringify(prop.enum)})`;
90
+ } else {
91
+ zodChain = "z.string()";
92
+ if (prop.isRequired) {
93
+ zodChain += `.min(1, ${getRuleMessage("minLength", "validation.string.nonempty")})`;
94
+ }
95
+ if (prop.maxLength !== void 0) {
96
+ zodChain += `.max(${prop.maxLength}, ${getRuleMessage("maxLength", "validation.string.max")})`;
97
+ }
98
+ if (prop.pattern) {
99
+ zodChain += `.regex(/${prop.pattern}/, ${getRuleMessage("pattern", "validation.string.regex")})`;
100
+ }
101
+ if (prop.format) {
102
+ const msg = getRuleMessage("format", `validation.string.${prop.format}`);
103
+ if (prop.format === "email") zodChain += `.email(${msg})`;
104
+ if (prop.format === "url") zodChain += `.url(${msg})`;
105
+ if (prop.format === "uuid") zodChain += `.uuid(${msg})`;
106
+ if (prop.format === "datetime") zodChain += `.datetime(${msg})`;
107
+ }
108
+ }
109
+ break;
110
+ }
111
+ case "integer": {
112
+ zodChain = `z.number().int(${getRuleMessage("integer", "validation.number.integer")})`;
113
+ if (prop.minimum !== void 0) {
114
+ zodChain += `.min(${prop.minimum}, ${getRuleMessage("minimum", "validation.number.min")})`;
115
+ }
116
+ if (prop.maximum !== void 0) {
117
+ zodChain += `.max(${prop.maximum}, ${getRuleMessage("maximum", "validation.number.max")})`;
118
+ }
119
+ break;
120
+ }
121
+ case "number": {
122
+ zodChain = "z.number()";
123
+ if (prop.minimum !== void 0) {
124
+ zodChain += `.min(${prop.minimum}, ${getRuleMessage("minimum", "validation.number.min")})`;
125
+ }
126
+ if (prop.maximum !== void 0) {
127
+ zodChain += `.max(${prop.maximum}, ${getRuleMessage("maximum", "validation.number.max")})`;
128
+ }
129
+ break;
130
+ }
131
+ case "boolean": {
132
+ zodChain = "z.boolean()";
133
+ break;
134
+ }
135
+ case "array": {
136
+ const itemSchema = prop.items ? _propToZod(prop.items) : "z.any()";
137
+ zodChain = `z.array(${itemSchema})`;
138
+ if (prop.minItems !== void 0) {
139
+ zodChain += `.min(${prop.minItems}, ${getRuleMessage("minItems", "validation.array.min")})`;
140
+ }
141
+ if (prop.maxItems !== void 0) {
142
+ zodChain += `.max(${prop.maxItems}, ${getRuleMessage("maxItems", "validation.array.max")})`;
143
+ }
144
+ break;
145
+ }
146
+ case "object": {
147
+ if (prop.properties && prop.properties.length > 0) {
148
+ const shape = prop.properties.map((p) => ` ${p.name}: ${_propToZod(p)}`).join(",\n");
149
+ zodChain = `z.object({
150
+ ${shape}
151
+ })`;
152
+ } else {
153
+ zodChain = "z.record(z.unknown())";
154
+ }
155
+ break;
156
+ }
157
+ default:
158
+ zodChain = "z.any()";
159
+ break;
160
+ }
161
+ if (prop.description) {
162
+ zodChain += `.describe(${JSON.stringify(prop.description)})`;
163
+ }
164
+ if (!prop.isRequired) {
165
+ zodChain += ".optional()";
166
+ }
167
+ if (prop.isNullable) {
168
+ zodChain += ".nullable()";
169
+ }
170
+ if (prop.defaultValue !== void 0) {
171
+ zodChain += `.default(${JSON.stringify(prop.defaultValue)})`;
172
+ }
173
+ return zodChain;
174
+ }
175
+
176
+ // src/generator/index.ts
77
177
  var DEBUG_MODE = process.env.DEBUG === "true";
78
178
  var debugLog = (title, data) => DEBUG_MODE && console.log(import_chalk.default.yellow(`
79
179
  [DEBUG: ${title}]`), import_util.default.inspect(data, { depth: 5, colors: true }));
@@ -276,10 +376,79 @@ ${prop.example ? ` * @example ${JSON.stringify(prop.example)}
276
376
  import_fs.default.writeFileSync(import_path.default.join(moduleOutputPath, "types.ts"), typesContent);
277
377
  console.log(import_chalk.default.gray(` \u2713 types.ts`));
278
378
  indexContent.push(`export * from './types';`);
279
- let validationContent = "";
379
+ const validationFileHeader = `// This file is auto-generated.
380
+ import { z, ZodIssueCode, ZodError } from 'zod';
381
+ ${enumsToImport.length > 0 ? `import { ${enumsToImport.join(", ")} } from './enums';
382
+ ` : ""}
383
+ // =============================================================================
384
+ // I. \u0625\u0639\u062F\u0627\u062F \u062E\u0631\u064A\u0637\u0629 \u0623\u062E\u0637\u0627\u0621 \u0639\u0627\u0644\u0645\u064A\u0629 (Global Error Map) \u0644\u062F\u0639\u0645 \u0627\u0644\u062A\u0631\u062C\u0645\u0629
385
+ // =============================================================================
386
+ //
387
+ // \u0645\u0644\u0627\u062D\u0638\u0629 \u0647\u0627\u0645\u0629: \u0641\u064A \u062A\u0637\u0628\u064A\u0642 \u062D\u0642\u064A\u0642\u064A\u060C \u064A\u062C\u0628 \u062A\u0639\u0631\u064A\u0641 \u0647\u0630\u0647 \u0627\u0644\u062E\u0631\u064A\u0637\u0629 \u0645\u0631\u0629 \u0648\u0627\u062D\u062F\u0629 \u0641\u0642\u0637
388
+ // \u0641\u064A \u0646\u0642\u0637\u0629 \u0627\u0644\u062F\u062E\u0648\u0644 \u0644\u062A\u0637\u0628\u064A\u0642\u0643 (entry-point). \u062A\u0645 \u0648\u0636\u0639\u0647\u0627 \u0647\u0646\u0627 \u0644\u0644\u062A\u0648\u0636\u064A\u062D.
389
+ //
390
+ // \u0627\u0644\u0647\u062F\u0641: \u0631\u0628\u0637 \u0645\u0641\u0627\u062A\u064A\u062D \u0627\u0644\u062A\u0631\u062C\u0645\u0629 (\u0645\u062B\u0644 "validation.required") \u0627\u0644\u062A\u064A \u062A\u0645 \u062A\u0648\u0644\u064A\u062F\u0647\u0627
391
+ // \u0641\u064A \u0627\u0644\u0645\u062E\u0637\u0637\u0627\u062A \u0623\u062F\u0646\u0627\u0647 \u0628\u0631\u0633\u0627\u0626\u0644 \u062D\u0642\u064A\u0642\u064A\u0629 \u064A\u0645\u0643\u0646 \u0639\u0631\u0636\u0647\u0627 \u0644\u0644\u0645\u0633\u062A\u062E\u062F\u0645.
392
+
393
+ // 1. \u0645\u062B\u0627\u0644 \u0628\u0633\u064A\u0637 \u0644\u062F\u0627\u0644\u0629 \u062A\u0631\u062C\u0645\u0629 (\u0641\u064A \u062A\u0637\u0628\u064A\u0642\u0643 \u0633\u062A\u0633\u062A\u062E\u062F\u0645 \u0645\u0643\u062A\u0628\u0629 \u0645\u062B\u0644 i18next)
394
+ const t = (key: string, args?: any): string => {
395
+ // \u0647\u0646\u0627 \u062A\u0636\u0639 \u0645\u0646\u0637\u0642 \u0627\u0644\u062A\u0631\u062C\u0645\u0629 \u0627\u0644\u062D\u0642\u064A\u0642\u064A \u0627\u0644\u062E\u0627\u0635 \u0628\u0643.
396
+ // \u0647\u0630\u0627 \u0645\u062C\u0631\u062F \u0645\u062B\u0627\u0644 \u0628\u0633\u064A\u0637.
397
+ if (key === 'validation.string.nonempty') return '\u0647\u0630\u0627 \u0627\u0644\u062D\u0642\u0644 \u0645\u0637\u0644\u0648\u0628 \u0648\u0644\u0627 \u064A\u0645\u0643\u0646 \u0623\u0646 \u064A\u0643\u0648\u0646 \u0641\u0627\u0631\u063A\u064B\u0627.';
398
+ if (key === 'validation.string.email') return '\u0627\u0644\u0631\u062C\u0627\u0621 \u0625\u062F\u062E\u0627\u0644 \u0628\u0631\u064A\u062F \u0625\u0644\u0643\u062A\u0631\u0648\u0646\u064A \u0635\u0627\u0644\u062D.';
399
+ if (key === 'validation.required') return '\u0647\u0630\u0627 \u0627\u0644\u062D\u0642\u0644 \u0625\u0644\u0632\u0627\u0645\u064A.';
400
+ // \u0631\u0633\u0627\u0644\u0629 \u0627\u062D\u062A\u064A\u0627\u0637\u064A\u0629
401
+ return key.split('.').pop()?.replace(/_/g, ' ') || key;
402
+ };
403
+
404
+ // 2. \u062A\u0639\u0631\u064A\u0641 \u062E\u0631\u064A\u0637\u0629 \u0627\u0644\u0623\u062E\u0637\u0627\u0621 \u0627\u0644\u0645\u062E\u0635\u0635\u0629
405
+ const customErrorMap: z.ZodErrorMap = (issue, ctx) => {
406
+ if (issue.message) {
407
+ // \u0625\u0630\u0627 \u0643\u0627\u0646\u062A \u0627\u0644\u0631\u0633\u0627\u0644\u0629 \u0647\u064A \u0645\u0641\u062A\u0627\u062D \u062A\u0631\u062C\u0645\u0629\u060C \u0642\u0645 \u0628\u062A\u0631\u062C\u0645\u062A\u0647\u0627
408
+ if (issue.message.startsWith('validation.')) {
409
+ return { message: t(issue.message) };
410
+ }
411
+ // \u0648\u0625\u0644\u0627\u060C \u0641\u0647\u064A \u0631\u0633\u0627\u0644\u0629 \u0645\u062E\u0635\u0635\u0629 \u0645\u0628\u0627\u0634\u0631\u0629
412
+ return { message: issue.message };
413
+ }
414
+
415
+ // \u062A\u0631\u062C\u0645\u0629 \u0627\u0644\u0623\u062E\u0637\u0627\u0621 \u0627\u0644\u0627\u0641\u062A\u0631\u0627\u0636\u064A\u0629 \u0627\u0644\u0639\u0627\u0645\u0629
416
+ if (issue.code === ZodIssueCode.invalid_type) {
417
+ return { message: t('validation.required') };
418
+ }
419
+
420
+ return { message: ctx.defaultError };
421
+ };
422
+
423
+ // 3. \u0642\u0645 \u0628\u062A\u0639\u064A\u064A\u0646 \u0627\u0644\u062E\u0631\u064A\u0637\u0629 \u0627\u0644\u0645\u062E\u0635\u0635\u0629 \u0639\u0627\u0644\u0645\u064A\u064B\u0627 (\u064A\u062C\u0628 \u0639\u0645\u0644 \u0630\u0644\u0643 \u0645\u0631\u0629 \u0648\u0627\u062D\u062F\u0629 \u0641\u064A \u0627\u0644\u062A\u0637\u0628\u064A\u0642)
424
+ z.setErrorMap(customErrorMap);
425
+
426
+ // =============================================================================
427
+ // II. \u0645\u062E\u0637\u0637\u0627\u062A \u0627\u0644\u062A\u062D\u0642\u0642 \u0627\u0644\u0645\u0648\u0644\u062F\u0629 \u062A\u0644\u0642\u0627\u0626\u064A\u064B\u0627 (Generated Validation Schemas)
428
+ // =============================================================================
429
+ `;
430
+ let validationContent = validationFileHeader;
431
+ for (const typeName of schemasToImport) {
432
+ const parsedSchema = allSchemas.get(typeName);
433
+ if (parsedSchema) {
434
+ let zodShape = parsedSchema.properties.map((p) => ` ${p.name}: ${_propToZod(p)}`).join(",\n");
435
+ validationContent += `
436
+ /**
437
+ * Schema for validating ${typeName} objects.
438
+ * @description ${parsedSchema.description || ""}
439
+ */
440
+ `;
441
+ validationContent += `export const ${typeName}Schema = z.object({
442
+ ${zodShape}
443
+ });
444
+ `;
445
+ validationContent += `export type ${typeName}Schema = z.infer<typeof ${typeName}Schema>;
446
+ `;
447
+ }
448
+ }
280
449
  if (schemasToImport.length > 0) {
281
450
  const firstSchemaName = schemasToImport[0];
282
- validationContent = `
451
+ validationContent += `
283
452
  // =============================================================================
284
453
  // III. \u0645\u062B\u0627\u0644 \u0639\u0644\u0649 \u0643\u064A\u0641\u064A\u0629 \u0627\u0633\u062A\u062E\u062F\u0627\u0645 \u0627\u0644\u0645\u062E\u0637\u0637 \u0641\u064A \u0627\u0644\u0643\u0648\u062F
285
454
  // =============================================================================
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "api-core-lib",
3
- "version": "12.0.76",
3
+ "version": "12.0.77",
4
4
  "description": "A flexible and powerful API client library for modern web applications.",
5
5
  "type": "module",
6
6
  "exports": {