@dudousxd/nestjs-codegen 0.5.1 → 0.6.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 { 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);
@@ -2301,7 +2304,6 @@ function resolveImportedType(name, sourceFile, project) {
2301
2304
  if (!namedImport) continue;
2302
2305
  const moduleSpecifier = importDecl.getModuleSpecifierValue();
2303
2306
  const candidates = resolveModuleSpecifier(moduleSpecifier, sourceFile, project);
2304
- if (candidates.length === 0) continue;
2305
2307
  for (const candidate of candidates) {
2306
2308
  let importedFile = project.getSourceFile(candidate);
2307
2309
  if (!importedFile) {
@@ -2316,9 +2318,25 @@ function resolveImportedType(name, sourceFile, project) {
2316
2318
  const viaReExport = resolveReExportedType(name, importedFile, project, /* @__PURE__ */ new Set());
2317
2319
  if (viaReExport) return viaReExport;
2318
2320
  }
2321
+ if (candidates.length === 0) {
2322
+ const viaCompiler = resolveBareSpecifierType(name, importDecl, project);
2323
+ if (viaCompiler) return viaCompiler;
2324
+ }
2319
2325
  }
2320
2326
  return resolveReExportedType(name, sourceFile, project, /* @__PURE__ */ new Set());
2321
2327
  }
2328
+ function resolveBareSpecifierType(name, importDecl, project) {
2329
+ let target;
2330
+ try {
2331
+ target = importDecl.getModuleSpecifierSourceFile();
2332
+ } catch {
2333
+ return null;
2334
+ }
2335
+ if (!target) return null;
2336
+ const direct = findTypeInFile(name, target);
2337
+ if (direct) return direct;
2338
+ return resolveReExportedType(name, target, project, /* @__PURE__ */ new Set());
2339
+ }
2322
2340
  function resolveReExportedType(name, file, project, seen) {
2323
2341
  const filePath = file.getFilePath();
2324
2342
  if (seen.has(filePath)) return null;
@@ -2602,37 +2620,28 @@ function buildProperty(prop, classFile, ctx) {
2602
2620
  const typeNode = prop.getTypeNode();
2603
2621
  const typeText = typeNode?.getText() ?? "unknown";
2604
2622
  const isArrayType = !!typeNode && import_ts_morph4.Node.isArrayTypeNode(typeNode);
2623
+ const asField = (child) => applyPresence(
2624
+ has("IsArray") || isArrayType ? { kind: "array", element: child } : child,
2625
+ decorators
2626
+ );
2605
2627
  const discriminator = resolveDiscriminator(dec("Type"));
2606
2628
  if (discriminator) {
2607
2629
  const options = discriminator.subTypes.map(
2608
2630
  (name) => buildNestedReference(name, classFile, ctx)
2609
2631
  );
2610
- const unionNode = {
2611
- kind: "union",
2612
- options,
2613
- discriminator: discriminator.property
2614
- };
2615
- const wrapArray = has("IsArray") || isArrayType;
2616
- const node2 = wrapArray ? { kind: "array", element: unionNode } : unionNode;
2617
- return applyPresence(node2, decorators);
2632
+ return asField({ kind: "union", options, discriminator: discriminator.property });
2618
2633
  }
2619
2634
  const propTypeParam = singularClassName(typeText);
2620
2635
  if (propTypeParam && ctx.typeBindings.has(propTypeParam)) {
2621
2636
  const bound = ctx.typeBindings.get(propTypeParam);
2622
- const childNode = buildNestedReference(bound, classFile, ctx);
2623
- const wrapArray = has("IsArray") || isArrayType;
2624
- const node2 = wrapArray ? { kind: "array", element: childNode } : childNode;
2625
- return applyPresence(node2, decorators);
2637
+ return asField(buildNestedReference(bound, classFile, ctx));
2626
2638
  }
2627
2639
  const typeRefName = resolveTypeFactoryName(dec("Type"));
2628
2640
  if (has("ValidateNested") || typeRefName) {
2629
2641
  const typeArgs = genericTypeArgNames(typeNode);
2630
2642
  const childName = typeRefName ?? singularClassName(typeText);
2631
2643
  if (childName) {
2632
- const childNode = buildNestedReference(childName, classFile, ctx, typeArgs);
2633
- const wrapArray = has("IsArray") || isArrayType;
2634
- const node2 = wrapArray ? { kind: "array", element: childNode } : childNode;
2635
- return applyPresence(node2, decorators);
2644
+ return asField(buildNestedReference(childName, classFile, ctx, typeArgs));
2636
2645
  }
2637
2646
  }
2638
2647
  let base = baseFromType(typeText, isArrayType);
@@ -4536,8 +4545,42 @@ function renderTsType(node, ctx) {
4536
4545
  }
4537
4546
  }
4538
4547
 
4548
+ // src/adapters/chain-module.ts
4549
+ function typeNameFor(schemaName) {
4550
+ return schemaName.replace(/Schema(_\d+)?$/, "$1");
4551
+ }
4552
+ function toObjectKey2(name) {
4553
+ return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name) ? name : JSON.stringify(name);
4554
+ }
4555
+ function createChainModuleRenderer(opts) {
4556
+ const { render, recursiveAnnotation } = opts;
4557
+ return (mod) => {
4558
+ const ctx = { named: mod.named };
4559
+ const recursive = mod.recursive ?? /* @__PURE__ */ new Set();
4560
+ const tctx = { named: mod.named, recursive, typeNameFor };
4561
+ const namedNestedSchemas = /* @__PURE__ */ new Map();
4562
+ const namedTypeAliases = /* @__PURE__ */ new Map();
4563
+ const namedAnnotations = /* @__PURE__ */ new Map();
4564
+ for (const [name, node] of mod.named) {
4565
+ namedNestedSchemas.set(name, render(node, ctx));
4566
+ if (recursive.has(name)) {
4567
+ const typeName = typeNameFor(name);
4568
+ namedTypeAliases.set(name, `type ${typeName} = ${renderTsType(node, tctx)}`);
4569
+ namedAnnotations.set(name, recursiveAnnotation(typeName));
4570
+ }
4571
+ }
4572
+ return {
4573
+ schemaText: render(mod.root, ctx),
4574
+ namedNestedSchemas,
4575
+ namedTypeAliases,
4576
+ namedAnnotations,
4577
+ warnings: mod.warnings
4578
+ };
4579
+ };
4580
+ }
4581
+
4539
4582
  // src/index.ts
4540
- var VERSION = "0.5.1";
4583
+ var VERSION = "0.6.0";
4541
4584
  // Annotate the CommonJS export names for ESM import in node:
4542
4585
  0 && (module.exports = {
4543
4586
  CodegenError,
@@ -4546,6 +4589,7 @@ var VERSION = "0.5.1";
4546
4589
  acquireLock,
4547
4590
  buildMocksFile,
4548
4591
  buildOpenApiSpec,
4592
+ createChainModuleRenderer,
4549
4593
  defineConfig,
4550
4594
  discoverContractsFast,
4551
4595
  emitApi,
@@ -4561,6 +4605,8 @@ var VERSION = "0.5.1";
4561
4605
  resolveConfig,
4562
4606
  schemaModuleToJsonSchema,
4563
4607
  schemaNodeToJsonSchema,
4608
+ toObjectKey,
4609
+ typeNameFor,
4564
4610
  watch
4565
4611
  });
4566
4612
  //# sourceMappingURL=index.cjs.map