@dudousxd/nestjs-codegen 0.19.0 → 0.20.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/index.d.cts CHANGED
@@ -382,6 +382,6 @@ interface FastDiscoveryOptions {
382
382
  }
383
383
  declare function discoverContractsFast(opts: FastDiscoveryOptions): Promise<RouteDescriptor[]>;
384
384
 
385
- declare const VERSION = "0.19.0";
385
+ declare const VERSION = "0.20.0";
386
386
 
387
387
  export { type ChainModuleRendererOptions, CodegenError, type CodegenManifest, ConfigError, DriftGuardError, type EntryPoint, type FastDiscoveryOptions, type JsonSchema, type MocksEmitOptions, type OpenApiDocument, type OpenApiEmitOptions, type OpenApiInfo, RenderContext, RenderedModule, ResolvedConfig, RouteDescriptor, SchemaModule, SchemaNode, type TsTypeContext, UserConfig, VERSION, ValidationAdapter, type WatchOptions, type Watcher, acquireLock, buildMocksFile, buildOpenApiSpec, createChainModuleRenderer, defineConfig, discoverContractsFast, emitApi, emitForms, emitMocks, emitOpenApi, emitRoutes, extractSchemaFromDto, generate, loadConfig, renderTsType, resolveConfig, schemaModuleToJsonSchema, schemaNodeToJsonSchema, toObjectKey, typeNameFor, watch };
package/dist/index.d.ts CHANGED
@@ -382,6 +382,6 @@ interface FastDiscoveryOptions {
382
382
  }
383
383
  declare function discoverContractsFast(opts: FastDiscoveryOptions): Promise<RouteDescriptor[]>;
384
384
 
385
- declare const VERSION = "0.19.0";
385
+ declare const VERSION = "0.20.0";
386
386
 
387
387
  export { type ChainModuleRendererOptions, CodegenError, type CodegenManifest, ConfigError, DriftGuardError, type EntryPoint, type FastDiscoveryOptions, type JsonSchema, type MocksEmitOptions, type OpenApiDocument, type OpenApiEmitOptions, type OpenApiInfo, RenderContext, RenderedModule, ResolvedConfig, RouteDescriptor, SchemaModule, SchemaNode, type TsTypeContext, UserConfig, VERSION, ValidationAdapter, type WatchOptions, type Watcher, acquireLock, buildMocksFile, buildOpenApiSpec, createChainModuleRenderer, defineConfig, discoverContractsFast, emitApi, emitForms, emitMocks, emitOpenApi, emitRoutes, extractSchemaFromDto, generate, loadConfig, renderTsType, resolveConfig, schemaModuleToJsonSchema, schemaNodeToJsonSchema, toObjectKey, typeNameFor, watch };
package/dist/index.js CHANGED
@@ -3642,10 +3642,15 @@ function resolveInstantiatedReturnType(cls, methodName) {
3642
3642
 
3643
3643
  // src/discovery/filter-for.ts
3644
3644
  function resolveMixinEntityClass(mixin, project) {
3645
- const entityArg = mixin?.namedClassArgs?.entity ?? mixin?.classArgs[0];
3646
- if (!entityArg) return void 0;
3647
- const file = project.getSourceFile(entityArg.filePath) ?? project.addSourceFileAtPathIfExists(entityArg.filePath);
3648
- return file?.getClass(entityArg.name);
3645
+ return resolveClassRef(mixin?.namedClassArgs?.entity ?? mixin?.classArgs[0], project);
3646
+ }
3647
+ function resolveMixinFilterClass(mixin, project) {
3648
+ return resolveClassRef(mixin?.namedClassArgs?.filter, project);
3649
+ }
3650
+ function resolveClassRef(ref, project) {
3651
+ if (!ref) return void 0;
3652
+ const file = project.getSourceFile(ref.filePath) ?? project.addSourceFileAtPathIfExists(ref.filePath);
3653
+ return file?.getClass(ref.name);
3649
3654
  }
3650
3655
  function classifyFilterForHint(typeInit) {
3651
3656
  if (Node6.isStringLiteral(typeInit)) {
@@ -3745,31 +3750,55 @@ function extractApplyFilterInfo(method, sourceFile, project, mixin) {
3745
3750
  }
3746
3751
  const filterClassName = filterClassArg.getText();
3747
3752
  const resolved = factoryStaticClass ? void 0 : findType(filterClassName, declFile, project);
3748
- const classDecl = factoryStaticClass ?? (resolved?.kind === "class" ? resolved.decl : resolveLocalClassDeclaration(filterClassArg));
3749
- if (classDecl) {
3750
- let fieldTypes = extractClassPropertyTypes(classDecl, project);
3751
- if (fieldTypes.length === 0) {
3752
- fieldTypes = extractFilterableEntityFields(classDecl, project, mixinEntity);
3753
- }
3754
- const filterForHints = extractFilterForHints(classDecl, project);
3755
- if (filterForHints.size > 0) {
3756
- const byName = new Map(fieldTypes.map((f) => [f.name, f]));
3757
- for (const [key, classified] of filterForHints) {
3758
- byName.set(key, toFilterFieldType(key, classified));
3759
- }
3760
- fieldTypes = [...byName.values()];
3761
- }
3762
- if (fieldTypes.length === 0) return null;
3763
- const fieldNames = fieldTypes.map((f) => f.name);
3753
+ const declaredClass = factoryStaticClass ?? (resolved?.kind === "class" ? resolved.decl : resolveLocalClassDeclaration(filterClassArg));
3754
+ const ownRoute = !mixin || declFile.getFilePath() !== mixin.factoryFilePath;
3755
+ const namedByRoute = ownRoute && Node6.isIdentifier(filterClassArg) ? declaredClass : void 0;
3756
+ for (const candidate of orderFilterCandidates({
3757
+ namedByRoute,
3758
+ supplied: resolveMixinFilterClass(mixin, project),
3759
+ declared: declaredClass
3760
+ })) {
3761
+ const fieldTypes = collectFilterFields(candidate, project, mixinEntity);
3762
+ if (fieldTypes.length === 0) continue;
3764
3763
  return {
3765
- fieldNames,
3764
+ fieldNames: fieldTypes.map((f) => f.name),
3766
3765
  fieldTypes,
3767
3766
  source
3768
3767
  };
3769
3768
  }
3769
+ if (declaredClass) return null;
3770
3770
  }
3771
3771
  return null;
3772
3772
  }
3773
+ function orderFilterCandidates(sources) {
3774
+ const ordered = [];
3775
+ const seen = /* @__PURE__ */ new Set();
3776
+ const push = (decl, preferDeclaredEntity) => {
3777
+ if (!decl || seen.has(decl)) return;
3778
+ seen.add(decl);
3779
+ ordered.push({ decl, preferDeclaredEntity });
3780
+ };
3781
+ push(sources.namedByRoute, true);
3782
+ push(sources.supplied, true);
3783
+ push(sources.declared, false);
3784
+ return ordered;
3785
+ }
3786
+ function collectFilterFields(candidate, project, mixinEntity) {
3787
+ const { decl, preferDeclaredEntity } = candidate;
3788
+ let fieldTypes = extractClassPropertyTypes(decl, project);
3789
+ if (fieldTypes.length === 0) {
3790
+ fieldTypes = extractFilterableEntityFields(decl, project, mixinEntity, preferDeclaredEntity);
3791
+ }
3792
+ const filterForHints = extractFilterForHints(decl, project);
3793
+ if (filterForHints.size > 0) {
3794
+ const byName = new Map(fieldTypes.map((f) => [f.name, f]));
3795
+ for (const [key, classified] of filterForHints) {
3796
+ byName.set(key, toFilterFieldType(key, classified));
3797
+ }
3798
+ fieldTypes = [...byName.values()];
3799
+ }
3800
+ return fieldTypes;
3801
+ }
3773
3802
  var RELATION_DECORATORS = /* @__PURE__ */ new Set(["OneToMany", "ManyToOne", "ManyToMany", "OneToOne"]);
3774
3803
  function collectEntityFields(entityDecl, sourceFile, project, prefix, visited) {
3775
3804
  const entityName = entityDecl.getName() ?? "";
@@ -3848,21 +3877,53 @@ function resolveDeclaredEntity(optionsArg, filterClass, project) {
3848
3877
  if (!resolved || resolved.kind !== "class") return void 0;
3849
3878
  return resolved.decl;
3850
3879
  }
3851
- function extractFilterableEntityFields(filterClass, project, entityOverride) {
3880
+ function readFieldNameList(optionsArg, key) {
3881
+ if (!Node6.isObjectLiteralExpression(optionsArg)) return void 0;
3882
+ const prop = optionsArg.getProperty(key);
3883
+ if (!prop || !Node6.isPropertyAssignment(prop)) return void 0;
3884
+ const init = prop.getInitializer();
3885
+ if (!init || !Node6.isArrayLiteralExpression(init)) return void 0;
3886
+ const names = /* @__PURE__ */ new Set();
3887
+ for (const el of init.getElements()) {
3888
+ if (Node6.isStringLiteral(el)) {
3889
+ names.add(el.getLiteralValue());
3890
+ continue;
3891
+ }
3892
+ if (Node6.isObjectLiteralExpression(el)) {
3893
+ const fieldProp = el.getProperty("field");
3894
+ if (fieldProp && Node6.isPropertyAssignment(fieldProp)) {
3895
+ const fieldInit = fieldProp.getInitializer();
3896
+ if (fieldInit && Node6.isStringLiteral(fieldInit)) {
3897
+ names.add(fieldInit.getLiteralValue());
3898
+ continue;
3899
+ }
3900
+ }
3901
+ }
3902
+ return void 0;
3903
+ }
3904
+ return names;
3905
+ }
3906
+ function narrowToDeclaredFields(fields, optionsArg) {
3907
+ const allowed = readFieldNameList(optionsArg, "allowed");
3908
+ const blocked = readFieldNameList(optionsArg, "blocked");
3909
+ if (!allowed && !blocked) return fields;
3910
+ return fields.filter(
3911
+ (f) => (!allowed || allowed.has(f.name)) && !(blocked?.has(f.name) ?? false)
3912
+ );
3913
+ }
3914
+ function extractFilterableEntityFields(filterClass, project, entityOverride, preferDeclaredEntity = false) {
3852
3915
  const filterableDecorator = filterClass.getDecorators().find((d) => d.getName() === "Filterable");
3853
3916
  if (!filterableDecorator) return [];
3854
3917
  const args = filterableDecorator.getArguments();
3855
3918
  if (args.length === 0) return [];
3856
3919
  const optionsArg = args[0];
3857
3920
  if (!Node6.isObjectLiteralExpression(optionsArg)) return [];
3858
- const entityDecl = entityOverride ?? resolveDeclaredEntity(optionsArg, filterClass, project);
3921
+ const declaredEntity = resolveDeclaredEntity(optionsArg, filterClass, project);
3922
+ const entityDecl = preferDeclaredEntity ? declaredEntity ?? entityOverride : entityOverride ?? declaredEntity;
3859
3923
  if (!entityDecl) return [];
3860
- const fields = collectEntityFields(
3861
- entityDecl,
3862
- entityDecl.getSourceFile(),
3863
- project,
3864
- "",
3865
- /* @__PURE__ */ new Set()
3924
+ const fields = narrowToDeclaredFields(
3925
+ collectEntityFields(entityDecl, entityDecl.getSourceFile(), project, "", /* @__PURE__ */ new Set()),
3926
+ optionsArg
3866
3927
  );
3867
3928
  const relationsDecorator = filterClass.getDecorators().find((d) => d.getName() === "Relations");
3868
3929
  if (relationsDecorator) {
@@ -5210,7 +5271,7 @@ function createChainModuleRenderer(opts) {
5210
5271
  }
5211
5272
 
5212
5273
  // src/index.ts
5213
- var VERSION = "0.19.0";
5274
+ var VERSION = "0.20.0";
5214
5275
  export {
5215
5276
  CodegenError,
5216
5277
  ConfigError,