@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.
package/dist/cli/main.js CHANGED
@@ -1251,6 +1251,8 @@ function buildFormsFileWithAdapter(routes, outDir, adapter, config) {
1251
1251
  }
1252
1252
  const { globalSchemas, renamesByEntry } = planNestedSchemas(entries);
1253
1253
  const irNamed = /* @__PURE__ */ new Map();
1254
+ const irTypeAliases = /* @__PURE__ */ new Map();
1255
+ const irAnnotations = /* @__PURE__ */ new Map();
1254
1256
  const decls = [];
1255
1257
  const mapEntries = [];
1256
1258
  let used = false;
@@ -1258,6 +1260,8 @@ function buildFormsFileWithAdapter(routes, outDir, adapter, config) {
1258
1260
  if (src.schema) {
1259
1261
  const r = adapter.renderModule(src.schema);
1260
1262
  for (const [n, t] of r.namedNestedSchemas) irNamed.set(n, t);
1263
+ if (r.namedTypeAliases) for (const [n, t] of r.namedTypeAliases) irTypeAliases.set(n, t);
1264
+ if (r.namedAnnotations) for (const [n, a] of r.namedAnnotations) irAnnotations.set(n, a);
1261
1265
  return { text: r.schemaText };
1262
1266
  }
1263
1267
  if (src.zodText) {
@@ -1331,7 +1335,13 @@ function buildFormsFileWithAdapter(routes, outDir, adapter, config) {
1331
1335
  for (const [n, t] of irNamed) if (!allNested.has(n)) allNested.set(n, t);
1332
1336
  if (allNested.size > 0) {
1333
1337
  lines.push("// Hoisted nested schemas (shared across endpoints).");
1334
- for (const [n, t] of allNested) lines.push(`const ${n} = ${t};`);
1338
+ for (const [n, alias] of irTypeAliases) {
1339
+ if (allNested.has(n)) lines.push(`${alias};`);
1340
+ }
1341
+ for (const [n, t] of allNested) {
1342
+ const annotation = irAnnotations.get(n);
1343
+ lines.push(`const ${n}${annotation ? `: ${annotation}` : ""} = ${t};`);
1344
+ }
1335
1345
  lines.push("");
1336
1346
  }
1337
1347
  lines.push(...decls);
@@ -1883,10 +1893,7 @@ function extractSchemaFromDto(classDecl, sourceFile, project) {
1883
1893
  depth: 0
1884
1894
  };
1885
1895
  const root = buildObject(classDecl, sourceFile, ctx);
1886
- for (const schemaName of ctx.recursiveSchemas) {
1887
- ctx.named.set(schemaName, { kind: "unknown", note: "recursive type \u2014 not expanded" });
1888
- }
1889
- return { root, named: ctx.named, warnings: ctx.warnings };
1896
+ return { root, named: ctx.named, warnings: ctx.warnings, recursive: ctx.recursiveSchemas };
1890
1897
  }
1891
1898
  function buildObject(classDecl, classFile, ctx) {
1892
1899
  const props = classDecl.getProperties();
@@ -1906,7 +1913,7 @@ function buildProperty(prop, classFile, ctx) {
1906
1913
  const dec = (n) => decorators.get(n);
1907
1914
  const typeNode = prop.getTypeNode();
1908
1915
  const typeText = typeNode?.getText() ?? "unknown";
1909
- const isArrayType = !!typeNode && typeNode.getText().endsWith("[]");
1916
+ const isArrayType = !!typeNode && Node3.isArrayTypeNode(typeNode);
1910
1917
  const typeRefName = resolveTypeFactoryName(dec("Type"));
1911
1918
  if (has("ValidateNested") || typeRefName) {
1912
1919
  const childName = typeRefName ?? singularClassName(typeText);
@@ -2037,18 +2044,27 @@ function baseFromType(typeText, isArrayType) {
2037
2044
  }
2038
2045
  }
2039
2046
  function buildNestedReference(className, fromFile, ctx) {
2040
- if (ctx.visiting.has(className) || ctx.depth >= 8) {
2047
+ if (ctx.visiting.has(className)) {
2041
2048
  const reserved = ctx.emittedClasses.get(className) ?? aliasFor(className, ctx);
2042
2049
  ctx.emittedClasses.set(className, reserved);
2043
2050
  ctx.recursiveSchemas.add(reserved);
2044
2051
  if (!ctx.warnedDecorators.has(`recursive:${reserved}`)) {
2045
2052
  ctx.warnedDecorators.add(`recursive:${reserved}`);
2046
- const msg = `${className} is a recursive type and was not expanded; the generated schema uses unknown for it.`;
2053
+ const msg = `${className} is a recursive type; the generated schema validates it via a lazy self-reference.`;
2047
2054
  ctx.warnings.push(msg);
2048
2055
  console.warn(`[nestjs-codegen] ${msg}`);
2049
2056
  }
2050
2057
  return { kind: "lazyRef", name: reserved };
2051
2058
  }
2059
+ if (ctx.depth >= 8) {
2060
+ if (!ctx.warnedDecorators.has(`deep:${className}`)) {
2061
+ ctx.warnedDecorators.add(`deep:${className}`);
2062
+ const msg = `${className} nesting is too deep to expand; the generated schema uses unknown for it.`;
2063
+ ctx.warnings.push(msg);
2064
+ console.warn(`[nestjs-codegen] ${msg}`);
2065
+ }
2066
+ return { kind: "unknown", note: "nesting too deep \u2014 not expanded" };
2067
+ }
2052
2068
  const existing = ctx.emittedClasses.get(className);
2053
2069
  if (existing) return { kind: "ref", name: existing };
2054
2070
  const schemaName = aliasFor(className, ctx);
@@ -3459,7 +3475,7 @@ async function watch(config, onChange) {
3459
3475
  }
3460
3476
 
3461
3477
  // src/index.ts
3462
- var VERSION = "0.3.0";
3478
+ var VERSION = "0.4.0";
3463
3479
 
3464
3480
  // src/cli/codegen.ts
3465
3481
  async function runCodegen(opts = {}) {