@dudousxd/nestjs-codegen 0.5.0 → 0.5.2

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 { l as ApiClientLayer, m as ApiHeaderContribution, n as ApiModuleDeps, C as CodegenExtension, o as EmittedFile, E as ExtensionContext, L as LeafModel, p as RequestModel, q as RequestShape, s as defineExtension, t as requestShape } from '../index-B0mS84Jj.cjs';
1
+ export { l as ApiClientLayer, m as ApiHeaderContribution, n as ApiModuleDeps, C as CodegenExtension, o as EmittedFile, E as ExtensionContext, L as LeafModel, p as RequestModel, q as RequestShape, s as defineExtension, t as requestShape } from '../index-_qRai4M3.cjs';
2
2
  import 'ts-morph';
@@ -1,2 +1,2 @@
1
- export { l as ApiClientLayer, m as ApiHeaderContribution, n as ApiModuleDeps, C as CodegenExtension, o as EmittedFile, E as ExtensionContext, L as LeafModel, p as RequestModel, q as RequestShape, s as defineExtension, t as requestShape } from '../index-B0mS84Jj.js';
1
+ export { l as ApiClientLayer, m as ApiHeaderContribution, n as ApiModuleDeps, C as CodegenExtension, o as EmittedFile, E as ExtensionContext, L as LeafModel, p as RequestModel, q as RequestShape, s as defineExtension, t as requestShape } from '../index-_qRai4M3.js';
2
2
  import 'ts-morph';
@@ -631,4 +631,4 @@ declare function requestShape(route: RouteDescriptor): RequestShape;
631
631
  /** Identity helper for authoring extensions with full type inference. */
632
632
  declare function defineExtension(ext: CodegenExtension): CodegenExtension;
633
633
 
634
- export { type AdapterUsage as A, type CodegenExtension as C, type ExtensionContext as E, type LeafModel as L, type NumberCheck as N, type ResolvedConfig as R, type SchemaNode as S, type TypeRef as T, type UserConfig as U, type ValidationAdapter as V, type RouteDescriptor as a, type SchemaModule as b, type ResolvedFormsConfig as c, type ContractDescriptor as d, type ContractSource as e, type ControllerRef as f, type RenderContext as g, type RenderedModule as h, type ScopeConfig as i, type StringCheck as j, type ValidationOption as k, type ApiClientLayer as l, type ApiHeaderContribution as m, type ApiModuleDeps as n, type EmittedFile as o, type RequestModel as p, type RequestShape as q, resolveAdapter as r, defineExtension as s, requestShape as t };
634
+ export { type AdapterUsage as A, type CodegenExtension as C, type ExtensionContext as E, type LeafModel as L, type NumberCheck as N, type ResolvedConfig as R, type SchemaNode as S, type TypeRef as T, type UserConfig as U, type ValidationAdapter as V, type RouteDescriptor as a, type RenderContext as b, type SchemaModule as c, type RenderedModule as d, type ResolvedFormsConfig as e, type ContractDescriptor as f, type ContractSource as g, type ControllerRef as h, type ScopeConfig as i, type StringCheck as j, type ValidationOption as k, type ApiClientLayer as l, type ApiHeaderContribution as m, type ApiModuleDeps as n, type EmittedFile as o, type RequestModel as p, type RequestShape as q, resolveAdapter as r, defineExtension as s, requestShape as t };
@@ -631,4 +631,4 @@ declare function requestShape(route: RouteDescriptor): RequestShape;
631
631
  /** Identity helper for authoring extensions with full type inference. */
632
632
  declare function defineExtension(ext: CodegenExtension): CodegenExtension;
633
633
 
634
- export { type AdapterUsage as A, type CodegenExtension as C, type ExtensionContext as E, type LeafModel as L, type NumberCheck as N, type ResolvedConfig as R, type SchemaNode as S, type TypeRef as T, type UserConfig as U, type ValidationAdapter as V, type RouteDescriptor as a, type SchemaModule as b, type ResolvedFormsConfig as c, type ContractDescriptor as d, type ContractSource as e, type ControllerRef as f, type RenderContext as g, type RenderedModule as h, type ScopeConfig as i, type StringCheck as j, type ValidationOption as k, type ApiClientLayer as l, type ApiHeaderContribution as m, type ApiModuleDeps as n, type EmittedFile as o, type RequestModel as p, type RequestShape as q, resolveAdapter as r, defineExtension as s, requestShape as t };
634
+ export { type AdapterUsage as A, type CodegenExtension as C, type ExtensionContext as E, type LeafModel as L, type NumberCheck as N, type ResolvedConfig as R, type SchemaNode as S, type TypeRef as T, type UserConfig as U, type ValidationAdapter as V, type RouteDescriptor as a, type RenderContext as b, type SchemaModule as c, type RenderedModule as d, type ResolvedFormsConfig as e, type ContractDescriptor as f, type ContractSource as g, type ControllerRef as h, type ScopeConfig as i, type StringCheck as j, type ValidationOption as k, type ApiClientLayer as l, type ApiHeaderContribution as m, type ApiModuleDeps as n, type EmittedFile as o, type RequestModel as p, type RequestShape as q, resolveAdapter as r, defineExtension as s, requestShape as t };
package/dist/index.cjs CHANGED
@@ -36,6 +36,7 @@ __export(src_exports, {
36
36
  acquireLock: () => acquireLock,
37
37
  buildMocksFile: () => buildMocksFile,
38
38
  buildOpenApiSpec: () => buildOpenApiSpec,
39
+ createChainModuleRenderer: () => createChainModuleRenderer,
39
40
  defineConfig: () => defineConfig,
40
41
  discoverContractsFast: () => discoverContractsFast,
41
42
  emitApi: () => emitApi,
@@ -51,6 +52,8 @@ __export(src_exports, {
51
52
  resolveConfig: () => resolveConfig,
52
53
  schemaModuleToJsonSchema: () => schemaModuleToJsonSchema,
53
54
  schemaNodeToJsonSchema: () => schemaNodeToJsonSchema,
55
+ toObjectKey: () => toObjectKey2,
56
+ typeNameFor: () => typeNameFor,
54
57
  watch: () => watch
55
58
  });
56
59
  module.exports = __toCommonJS(src_exports);
@@ -1216,18 +1219,27 @@ function refRootIdentifier(refName) {
1216
1219
  function hasSource(src) {
1217
1220
  return !!(src.schema || src.zodText || src.zodRef);
1218
1221
  }
1222
+ function escapeRegExp(s) {
1223
+ return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
1224
+ }
1225
+ var wordBoundaryRegexCache = /* @__PURE__ */ new Map();
1226
+ function wordBoundaryRegex(token) {
1227
+ let re = wordBoundaryRegexCache.get(token);
1228
+ if (re === void 0) {
1229
+ re = new RegExp(`\\b${escapeRegExp(token)}\\b`, "g");
1230
+ wordBoundaryRegexCache.set(token, re);
1231
+ }
1232
+ return re;
1233
+ }
1219
1234
  function applyRenames(text, renames) {
1220
1235
  if (!renames || renames.size === 0) return text;
1221
1236
  let out = text;
1222
1237
  for (const [from, to] of renames) {
1223
1238
  if (from === to) continue;
1224
- out = out.replace(new RegExp(`\\b${escapeRegExp(from)}\\b`, "g"), to);
1239
+ out = out.replace(wordBoundaryRegex(from), to);
1225
1240
  }
1226
1241
  return out;
1227
1242
  }
1228
- function escapeRegExp(s) {
1229
- return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
1230
- }
1231
1243
  function isSelfReferential(name, text) {
1232
1244
  return new RegExp(`\\b${escapeRegExp(name)}\\b`).test(text);
1233
1245
  }
@@ -1239,7 +1251,23 @@ function planNestedSchemas(entries) {
1239
1251
  const local = Object.entries(entry.nestedSchemas);
1240
1252
  if (local.length === 0) continue;
1241
1253
  const rename = /* @__PURE__ */ new Map();
1242
- for (const [name] of local) rename.set(name, name);
1254
+ const renameValues = /* @__PURE__ */ new Set();
1255
+ const setRename = (key, value) => {
1256
+ const prev = rename.get(key);
1257
+ rename.set(key, value);
1258
+ if (prev !== void 0 && prev !== value) {
1259
+ let stillUsed = false;
1260
+ for (const v of rename.values()) {
1261
+ if (v === prev) {
1262
+ stillUsed = true;
1263
+ break;
1264
+ }
1265
+ }
1266
+ if (!stillUsed) renameValues.delete(prev);
1267
+ }
1268
+ renameValues.add(value);
1269
+ };
1270
+ for (const [name] of local) setRename(name, name);
1243
1271
  const textFor = (name) => {
1244
1272
  const raw = entry.nestedSchemas?.[name] ?? "";
1245
1273
  return applyRenames(raw, rename);
@@ -1257,11 +1285,11 @@ function planNestedSchemas(entries) {
1257
1285
  if (existing === text) continue;
1258
1286
  let i = 2;
1259
1287
  let candidate = `${name}_${i}`;
1260
- while (globalSchemas.has(candidate) && globalSchemas.get(candidate) !== textFor(name) || [...rename.values()].includes(candidate)) {
1288
+ while (globalSchemas.has(candidate) && globalSchemas.get(candidate) !== textFor(name) || renameValues.has(candidate)) {
1261
1289
  i += 1;
1262
1290
  candidate = `${name}_${i}`;
1263
1291
  }
1264
- rename.set(name, candidate);
1292
+ setRename(name, candidate);
1265
1293
  changed = true;
1266
1294
  }
1267
1295
  }
@@ -2549,6 +2577,7 @@ function extractSchemaFromDto(classDecl, sourceFile, project) {
2549
2577
  warnings: [],
2550
2578
  warnedDecorators: /* @__PURE__ */ new Set(),
2551
2579
  emittedClasses: /* @__PURE__ */ new Map(),
2580
+ usedSchemaNames: /* @__PURE__ */ new Set(),
2552
2581
  visiting: /* @__PURE__ */ new Set(),
2553
2582
  recursiveSchemas: /* @__PURE__ */ new Set(),
2554
2583
  depth: 0,
@@ -2576,37 +2605,28 @@ function buildProperty(prop, classFile, ctx) {
2576
2605
  const typeNode = prop.getTypeNode();
2577
2606
  const typeText = typeNode?.getText() ?? "unknown";
2578
2607
  const isArrayType = !!typeNode && import_ts_morph4.Node.isArrayTypeNode(typeNode);
2608
+ const asField = (child) => applyPresence(
2609
+ has("IsArray") || isArrayType ? { kind: "array", element: child } : child,
2610
+ decorators
2611
+ );
2579
2612
  const discriminator = resolveDiscriminator(dec("Type"));
2580
2613
  if (discriminator) {
2581
2614
  const options = discriminator.subTypes.map(
2582
2615
  (name) => buildNestedReference(name, classFile, ctx)
2583
2616
  );
2584
- const unionNode = {
2585
- kind: "union",
2586
- options,
2587
- discriminator: discriminator.property
2588
- };
2589
- const wrapArray = has("IsArray") || isArrayType;
2590
- const node2 = wrapArray ? { kind: "array", element: unionNode } : unionNode;
2591
- return applyPresence(node2, decorators);
2617
+ return asField({ kind: "union", options, discriminator: discriminator.property });
2592
2618
  }
2593
2619
  const propTypeParam = singularClassName(typeText);
2594
2620
  if (propTypeParam && ctx.typeBindings.has(propTypeParam)) {
2595
2621
  const bound = ctx.typeBindings.get(propTypeParam);
2596
- const childNode = buildNestedReference(bound, classFile, ctx);
2597
- const wrapArray = has("IsArray") || isArrayType;
2598
- const node2 = wrapArray ? { kind: "array", element: childNode } : childNode;
2599
- return applyPresence(node2, decorators);
2622
+ return asField(buildNestedReference(bound, classFile, ctx));
2600
2623
  }
2601
2624
  const typeRefName = resolveTypeFactoryName(dec("Type"));
2602
2625
  if (has("ValidateNested") || typeRefName) {
2603
2626
  const typeArgs = genericTypeArgNames(typeNode);
2604
2627
  const childName = typeRefName ?? singularClassName(typeText);
2605
2628
  if (childName) {
2606
- const childNode = buildNestedReference(childName, classFile, ctx, typeArgs);
2607
- const wrapArray = has("IsArray") || isArrayType;
2608
- const node2 = wrapArray ? { kind: "array", element: childNode } : childNode;
2609
- return applyPresence(node2, decorators);
2629
+ return asField(buildNestedReference(childName, classFile, ctx, typeArgs));
2610
2630
  }
2611
2631
  }
2612
2632
  let base = baseFromType(typeText, isArrayType);
@@ -2734,6 +2754,7 @@ function buildNestedReference(className, fromFile, ctx, typeArgs = []) {
2734
2754
  if (ctx.visiting.has(cacheKey)) {
2735
2755
  const reserved = ctx.emittedClasses.get(cacheKey) ?? aliasFor(schemaBase, ctx);
2736
2756
  ctx.emittedClasses.set(cacheKey, reserved);
2757
+ ctx.usedSchemaNames.add(reserved);
2737
2758
  ctx.recursiveSchemas.add(reserved);
2738
2759
  if (!ctx.warnedDecorators.has(`recursive:${reserved}`)) {
2739
2760
  ctx.warnedDecorators.add(`recursive:${reserved}`);
@@ -2767,6 +2788,7 @@ function buildNestedReference(className, fromFile, ctx, typeArgs = []) {
2767
2788
  });
2768
2789
  for (const [k, v] of newBindings) ctx.typeBindings.set(k, v);
2769
2790
  ctx.emittedClasses.set(cacheKey, schemaName);
2791
+ ctx.usedSchemaNames.add(schemaName);
2770
2792
  ctx.visiting.add(cacheKey);
2771
2793
  ctx.depth += 1;
2772
2794
  const childNode = buildObject(resolved.decl, resolved.file, ctx);
@@ -2774,15 +2796,14 @@ function buildNestedReference(className, fromFile, ctx, typeArgs = []) {
2774
2796
  ctx.visiting.delete(cacheKey);
2775
2797
  for (const [k] of newBindings) ctx.typeBindings.delete(k);
2776
2798
  ctx.named.set(schemaName, childNode);
2799
+ ctx.usedSchemaNames.add(schemaName);
2777
2800
  return { kind: "ref", name: schemaName };
2778
2801
  }
2779
2802
  function aliasFor(className, ctx) {
2780
2803
  const baseName = `${className}Schema`;
2781
2804
  let candidate = baseName;
2782
2805
  let i = 1;
2783
- const used = new Set(ctx.named.keys());
2784
- for (const v of ctx.emittedClasses.values()) used.add(v);
2785
- while (used.has(candidate)) {
2806
+ while (ctx.usedSchemaNames.has(candidate)) {
2786
2807
  candidate = `${baseName}_${i}`;
2787
2808
  i += 1;
2788
2809
  }
@@ -4509,8 +4530,42 @@ function renderTsType(node, ctx) {
4509
4530
  }
4510
4531
  }
4511
4532
 
4533
+ // src/adapters/chain-module.ts
4534
+ function typeNameFor(schemaName) {
4535
+ return schemaName.replace(/Schema(_\d+)?$/, "$1");
4536
+ }
4537
+ function toObjectKey2(name) {
4538
+ return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name) ? name : JSON.stringify(name);
4539
+ }
4540
+ function createChainModuleRenderer(opts) {
4541
+ const { render, recursiveAnnotation } = opts;
4542
+ return (mod) => {
4543
+ const ctx = { named: mod.named };
4544
+ const recursive = mod.recursive ?? /* @__PURE__ */ new Set();
4545
+ const tctx = { named: mod.named, recursive, typeNameFor };
4546
+ const namedNestedSchemas = /* @__PURE__ */ new Map();
4547
+ const namedTypeAliases = /* @__PURE__ */ new Map();
4548
+ const namedAnnotations = /* @__PURE__ */ new Map();
4549
+ for (const [name, node] of mod.named) {
4550
+ namedNestedSchemas.set(name, render(node, ctx));
4551
+ if (recursive.has(name)) {
4552
+ const typeName = typeNameFor(name);
4553
+ namedTypeAliases.set(name, `type ${typeName} = ${renderTsType(node, tctx)}`);
4554
+ namedAnnotations.set(name, recursiveAnnotation(typeName));
4555
+ }
4556
+ }
4557
+ return {
4558
+ schemaText: render(mod.root, ctx),
4559
+ namedNestedSchemas,
4560
+ namedTypeAliases,
4561
+ namedAnnotations,
4562
+ warnings: mod.warnings
4563
+ };
4564
+ };
4565
+ }
4566
+
4512
4567
  // src/index.ts
4513
- var VERSION = "0.5.0";
4568
+ var VERSION = "0.5.2";
4514
4569
  // Annotate the CommonJS export names for ESM import in node:
4515
4570
  0 && (module.exports = {
4516
4571
  CodegenError,
@@ -4519,6 +4574,7 @@ var VERSION = "0.5.0";
4519
4574
  acquireLock,
4520
4575
  buildMocksFile,
4521
4576
  buildOpenApiSpec,
4577
+ createChainModuleRenderer,
4522
4578
  defineConfig,
4523
4579
  discoverContractsFast,
4524
4580
  emitApi,
@@ -4534,6 +4590,8 @@ var VERSION = "0.5.0";
4534
4590
  resolveConfig,
4535
4591
  schemaModuleToJsonSchema,
4536
4592
  schemaNodeToJsonSchema,
4593
+ toObjectKey,
4594
+ typeNameFor,
4537
4595
  watch
4538
4596
  });
4539
4597
  //# sourceMappingURL=index.cjs.map