@dudousxd/nestjs-codegen 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -453,10 +453,7 @@ function extractSchemaFromDto(classDecl, sourceFile, project) {
453
453
  depth: 0
454
454
  };
455
455
  const root = buildObject(classDecl, sourceFile, ctx);
456
- for (const schemaName of ctx.recursiveSchemas) {
457
- ctx.named.set(schemaName, { kind: "unknown", note: "recursive type \u2014 not expanded" });
458
- }
459
- return { root, named: ctx.named, warnings: ctx.warnings };
456
+ return { root, named: ctx.named, warnings: ctx.warnings, recursive: ctx.recursiveSchemas };
460
457
  }
461
458
  function buildObject(classDecl, classFile, ctx) {
462
459
  const props = classDecl.getProperties();
@@ -476,7 +473,7 @@ function buildProperty(prop, classFile, ctx) {
476
473
  const dec = (n) => decorators.get(n);
477
474
  const typeNode = prop.getTypeNode();
478
475
  const typeText = typeNode?.getText() ?? "unknown";
479
- const isArrayType = !!typeNode && typeNode.getText().endsWith("[]");
476
+ const isArrayType = !!typeNode && import_ts_morph2.Node.isArrayTypeNode(typeNode);
480
477
  const typeRefName = resolveTypeFactoryName(dec("Type"));
481
478
  if (has("ValidateNested") || typeRefName) {
482
479
  const childName = typeRefName ?? singularClassName(typeText);
@@ -607,18 +604,27 @@ function baseFromType(typeText, isArrayType) {
607
604
  }
608
605
  }
609
606
  function buildNestedReference(className, fromFile, ctx) {
610
- if (ctx.visiting.has(className) || ctx.depth >= 8) {
607
+ if (ctx.visiting.has(className)) {
611
608
  const reserved = ctx.emittedClasses.get(className) ?? aliasFor(className, ctx);
612
609
  ctx.emittedClasses.set(className, reserved);
613
610
  ctx.recursiveSchemas.add(reserved);
614
611
  if (!ctx.warnedDecorators.has(`recursive:${reserved}`)) {
615
612
  ctx.warnedDecorators.add(`recursive:${reserved}`);
616
- const msg = `${className} is a recursive type and was not expanded; the generated schema uses unknown for it.`;
613
+ const msg = `${className} is a recursive type; the generated schema validates it via a lazy self-reference.`;
617
614
  ctx.warnings.push(msg);
618
615
  console.warn(`[nestjs-codegen] ${msg}`);
619
616
  }
620
617
  return { kind: "lazyRef", name: reserved };
621
618
  }
619
+ if (ctx.depth >= 8) {
620
+ if (!ctx.warnedDecorators.has(`deep:${className}`)) {
621
+ ctx.warnedDecorators.add(`deep:${className}`);
622
+ const msg = `${className} nesting is too deep to expand; the generated schema uses unknown for it.`;
623
+ ctx.warnings.push(msg);
624
+ console.warn(`[nestjs-codegen] ${msg}`);
625
+ }
626
+ return { kind: "unknown", note: "nesting too deep \u2014 not expanded" };
627
+ }
622
628
  const existing = ctx.emittedClasses.get(className);
623
629
  if (existing) return { kind: "ref", name: existing };
624
630
  const schemaName = aliasFor(className, ctx);
@@ -2947,6 +2953,8 @@ function buildFormsFileWithAdapter(routes, outDir, adapter, config) {
2947
2953
  }
2948
2954
  const { globalSchemas, renamesByEntry } = planNestedSchemas(entries);
2949
2955
  const irNamed = /* @__PURE__ */ new Map();
2956
+ const irTypeAliases = /* @__PURE__ */ new Map();
2957
+ const irAnnotations = /* @__PURE__ */ new Map();
2950
2958
  const decls = [];
2951
2959
  const mapEntries = [];
2952
2960
  let used = false;
@@ -2954,6 +2962,8 @@ function buildFormsFileWithAdapter(routes, outDir, adapter, config) {
2954
2962
  if (src.schema) {
2955
2963
  const r = adapter.renderModule(src.schema);
2956
2964
  for (const [n, t] of r.namedNestedSchemas) irNamed.set(n, t);
2965
+ if (r.namedTypeAliases) for (const [n, t] of r.namedTypeAliases) irTypeAliases.set(n, t);
2966
+ if (r.namedAnnotations) for (const [n, a] of r.namedAnnotations) irAnnotations.set(n, a);
2957
2967
  return { text: r.schemaText };
2958
2968
  }
2959
2969
  if (src.zodText) {
@@ -3027,7 +3037,13 @@ function buildFormsFileWithAdapter(routes, outDir, adapter, config) {
3027
3037
  for (const [n, t] of irNamed) if (!allNested.has(n)) allNested.set(n, t);
3028
3038
  if (allNested.size > 0) {
3029
3039
  lines.push("// Hoisted nested schemas (shared across endpoints).");
3030
- for (const [n, t] of allNested) lines.push(`const ${n} = ${t};`);
3040
+ for (const [n, alias] of irTypeAliases) {
3041
+ if (allNested.has(n)) lines.push(`${alias};`);
3042
+ }
3043
+ for (const [n, t] of allNested) {
3044
+ const annotation = irAnnotations.get(n);
3045
+ lines.push(`const ${n}${annotation ? `: ${annotation}` : ""} = ${t};`);
3046
+ }
3031
3047
  lines.push("");
3032
3048
  }
3033
3049
  lines.push(...decls);