@dudousxd/nestjs-codegen 0.7.1 → 0.9.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.
@@ -1,2 +1,2 @@
1
- export { m as ApiClientLayer, n as ApiHeaderContribution, o as ApiModuleDeps, C as CodegenExtension, p as EmittedFile, E as ExtensionContext, L as LeafModel, q as RequestModel, s as RequestShape, t as defineExtension, u as requestShape } from '../index-DgIAN5k5.cjs';
1
+ export { m as ApiClientLayer, n as ApiHeaderContribution, o as ApiModuleDeps, C as CodegenExtension, p as EmittedFile, E as ExtensionContext, L as LeafModel, q as RequestModel, s as RequestShape, t as defineExtension, u as requestShape } from '../index-SJc0gya_.cjs';
2
2
  import 'ts-morph';
@@ -1,2 +1,2 @@
1
- export { m as ApiClientLayer, n as ApiHeaderContribution, o as ApiModuleDeps, C as CodegenExtension, p as EmittedFile, E as ExtensionContext, L as LeafModel, q as RequestModel, s as RequestShape, t as defineExtension, u as requestShape } from '../index-DgIAN5k5.js';
1
+ export { m as ApiClientLayer, n as ApiHeaderContribution, o as ApiModuleDeps, C as CodegenExtension, p as EmittedFile, E as ExtensionContext, L as LeafModel, q as RequestModel, s as RequestShape, t as defineExtension, u as requestShape } from '../index-SJc0gya_.js';
2
2
  import 'ts-morph';
@@ -204,6 +204,15 @@ type ValidationOption = 'zod' | 'valibot' | 'arktype' | ValidationAdapter;
204
204
  declare function resolveAdapter(option: ValidationOption): ValidationAdapter;
205
205
 
206
206
  interface UserConfig {
207
+ /**
208
+ * Print schema-translation advisories to the terminal (e.g. "@X is not
209
+ * translatable to a client validation schema", "T is a recursive type"). These
210
+ * are always preserved in generated output as `// warning:` comments regardless;
211
+ * this only controls the duplicate terminal chatter, which is off by default.
212
+ *
213
+ * @default false
214
+ */
215
+ debug?: boolean;
207
216
  /**
208
217
  * Codegen extensions, applied in order. Each may augment the route IR
209
218
  * (`transformRoutes`), contribute extra output files (`emitFiles`), and — once a
@@ -356,6 +365,7 @@ interface ResolvedMocksConfig {
356
365
  baseUrl: string;
357
366
  }
358
367
  interface ResolvedConfig {
368
+ debug: boolean;
359
369
  extensions: CodegenExtension[];
360
370
  validation: ValidationAdapter;
361
371
  pages: ResolvedPagesConfig | null;
@@ -204,6 +204,15 @@ type ValidationOption = 'zod' | 'valibot' | 'arktype' | ValidationAdapter;
204
204
  declare function resolveAdapter(option: ValidationOption): ValidationAdapter;
205
205
 
206
206
  interface UserConfig {
207
+ /**
208
+ * Print schema-translation advisories to the terminal (e.g. "@X is not
209
+ * translatable to a client validation schema", "T is a recursive type"). These
210
+ * are always preserved in generated output as `// warning:` comments regardless;
211
+ * this only controls the duplicate terminal chatter, which is off by default.
212
+ *
213
+ * @default false
214
+ */
215
+ debug?: boolean;
207
216
  /**
208
217
  * Codegen extensions, applied in order. Each may augment the route IR
209
218
  * (`transformRoutes`), contribute extra output files (`emitFiles`), and — once a
@@ -356,6 +365,7 @@ interface ResolvedMocksConfig {
356
365
  baseUrl: string;
357
366
  }
358
367
  interface ResolvedConfig {
368
+ debug: boolean;
359
369
  extensions: CodegenExtension[];
360
370
  validation: ValidationAdapter;
361
371
  pages: ResolvedPagesConfig | null;
package/dist/index.cjs CHANGED
@@ -173,6 +173,7 @@ function applyDefaults(userConfig, cwd) {
173
173
  };
174
174
  }
175
175
  return {
176
+ debug: userConfig.debug ?? false,
176
177
  extensions: userConfig.extensions ?? [],
177
178
  // Non-null: validateUserConfig() above throws when `validation` is absent.
178
179
  validation: resolveAdapter(userConfig.validation),
@@ -2142,8 +2143,18 @@ function buildEmpty() {
2142
2143
  ].join("\n");
2143
2144
  }
2144
2145
 
2146
+ // src/util/debug-log.ts
2147
+ var debugEnabled = false;
2148
+ function setCodegenDebug(enabled) {
2149
+ debugEnabled = enabled;
2150
+ }
2151
+ function debugWarn(message) {
2152
+ if (debugEnabled) console.warn(`[nestjs-codegen] ${message}`);
2153
+ }
2154
+
2145
2155
  // src/generate.ts
2146
2156
  async function generate(config, inputRoutes = []) {
2157
+ setCodegenDebug(config.debug);
2147
2158
  const extensions = config.extensions ?? [];
2148
2159
  let routes = inputRoutes;
2149
2160
  const ctx = createExtensionContext(config, () => routes);
@@ -2739,7 +2750,7 @@ function buildProperty(prop, classFile, ctx) {
2739
2750
  ctx.warnedDecorators.add(name);
2740
2751
  const msg = `@${name} is not translatable to a client validation schema and was skipped (server-only validation).`;
2741
2752
  ctx.warnings.push(msg);
2742
- console.warn(`[nestjs-codegen] ${msg}`);
2753
+ debugWarn(msg);
2743
2754
  }
2744
2755
  }
2745
2756
  }
@@ -2790,7 +2801,7 @@ function buildNestedReference(className, fromFile, ctx, typeArgs = []) {
2790
2801
  ctx.warnedDecorators.add(`recursive:${reserved}`);
2791
2802
  const msg = `${className} is a recursive type; the generated schema validates it via a lazy self-reference.`;
2792
2803
  ctx.warnings.push(msg);
2793
- console.warn(`[nestjs-codegen] ${msg}`);
2804
+ debugWarn(msg);
2794
2805
  }
2795
2806
  return { kind: "lazyRef", name: reserved };
2796
2807
  }
@@ -2799,7 +2810,7 @@ function buildNestedReference(className, fromFile, ctx, typeArgs = []) {
2799
2810
  ctx.warnedDecorators.add(`deep:${className}`);
2800
2811
  const msg = `${className} nesting is too deep to expand; the generated schema uses unknown for it.`;
2801
2812
  ctx.warnings.push(msg);
2802
- console.warn(`[nestjs-codegen] ${msg}`);
2813
+ debugWarn(msg);
2803
2814
  }
2804
2815
  return { kind: "unknown", note: "nesting too deep \u2014 not expanded" };
2805
2816
  }
@@ -2940,7 +2951,7 @@ function enumSchemaFromDecorator(decorator, classFile, ctx) {
2940
2951
  if (!ctx.warnedDecorators.has(`IsEnum:${name}`)) {
2941
2952
  ctx.warnedDecorators.add(`IsEnum:${name}`);
2942
2953
  ctx.warnings.push(msg);
2943
- console.warn(`[nestjs-codegen] ${msg}`);
2954
+ debugWarn(msg);
2944
2955
  }
2945
2956
  return { kind: "unknown", note: `@IsEnum(${name}): enum not resolvable to literals` };
2946
2957
  }
@@ -3570,14 +3581,33 @@ function extractQueryType(method, sourceFile, project) {
3570
3581
  for (const param of method.getParameters()) {
3571
3582
  const queryDecorator = param.getDecorators().find((d) => d.getName() === "Query");
3572
3583
  if (!queryDecorator) continue;
3573
- const queryArgs = queryDecorator.getArguments();
3574
- if (queryArgs.length > 0) continue;
3584
+ if (queryDecorator.getArguments().length > 0) continue;
3575
3585
  const typeNode = param.getTypeNode();
3576
3586
  if (typeNode) {
3577
3587
  return resolveTypeNodeToString(typeNode, sourceFile, project, 3);
3578
3588
  }
3579
3589
  }
3580
- return null;
3590
+ const entries = [];
3591
+ for (const param of method.getParameters()) {
3592
+ const queryDecorator = param.getDecorators().find((d) => d.getName() === "Query");
3593
+ if (!queryDecorator) continue;
3594
+ const queryArgs = queryDecorator.getArguments();
3595
+ const nameArg = queryArgs[0];
3596
+ if (!nameArg || !import_ts_morph7.Node.isStringLiteral(nameArg)) continue;
3597
+ const queryName = nameArg.getLiteralValue();
3598
+ const typeNode = param.getTypeNode();
3599
+ const queryType = typeNode ? resolveTypeNodeToString(typeNode, sourceFile, project, 3) : "string";
3600
+ entries.push(`${queryName}${isParamOptional(param) ? "?" : ""}: ${queryType}`);
3601
+ }
3602
+ return entries.length > 0 ? `{ ${entries.join("; ")} }` : null;
3603
+ }
3604
+ function isParamOptional(param) {
3605
+ if (param.hasQuestionToken() || param.hasInitializer()) return true;
3606
+ const typeNode = param.getTypeNode();
3607
+ if (typeNode && import_ts_morph7.Node.isUnionTypeNode(typeNode)) {
3608
+ return typeNode.getTypeNodes().some((t) => t.getKind() === import_ts_morph7.SyntaxKind.UndefinedKeyword);
3609
+ }
3610
+ return false;
3581
3611
  }
3582
3612
  function extractParamsType(method, sourceFile, project) {
3583
3613
  const entries = [];
@@ -4598,7 +4628,7 @@ function createChainModuleRenderer(opts) {
4598
4628
  }
4599
4629
 
4600
4630
  // src/index.ts
4601
- var VERSION = "0.7.1";
4631
+ var VERSION = "0.9.0";
4602
4632
  // Annotate the CommonJS export names for ESM import in node:
4603
4633
  0 && (module.exports = {
4604
4634
  CodegenError,