@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.cjs CHANGED
@@ -3688,10 +3688,15 @@ function resolveInstantiatedReturnType(cls, methodName) {
3688
3688
 
3689
3689
  // src/discovery/filter-for.ts
3690
3690
  function resolveMixinEntityClass(mixin, project) {
3691
- const entityArg = mixin?.namedClassArgs?.entity ?? mixin?.classArgs[0];
3692
- if (!entityArg) return void 0;
3693
- const file = project.getSourceFile(entityArg.filePath) ?? project.addSourceFileAtPathIfExists(entityArg.filePath);
3694
- return file?.getClass(entityArg.name);
3691
+ return resolveClassRef(mixin?.namedClassArgs?.entity ?? mixin?.classArgs[0], project);
3692
+ }
3693
+ function resolveMixinFilterClass(mixin, project) {
3694
+ return resolveClassRef(mixin?.namedClassArgs?.filter, project);
3695
+ }
3696
+ function resolveClassRef(ref, project) {
3697
+ if (!ref) return void 0;
3698
+ const file = project.getSourceFile(ref.filePath) ?? project.addSourceFileAtPathIfExists(ref.filePath);
3699
+ return file?.getClass(ref.name);
3695
3700
  }
3696
3701
  function classifyFilterForHint(typeInit) {
3697
3702
  if (import_ts_morph7.Node.isStringLiteral(typeInit)) {
@@ -3791,31 +3796,55 @@ function extractApplyFilterInfo(method, sourceFile, project, mixin) {
3791
3796
  }
3792
3797
  const filterClassName = filterClassArg.getText();
3793
3798
  const resolved = factoryStaticClass ? void 0 : findType(filterClassName, declFile, project);
3794
- const classDecl = factoryStaticClass ?? (resolved?.kind === "class" ? resolved.decl : resolveLocalClassDeclaration(filterClassArg));
3795
- if (classDecl) {
3796
- let fieldTypes = extractClassPropertyTypes(classDecl, project);
3797
- if (fieldTypes.length === 0) {
3798
- fieldTypes = extractFilterableEntityFields(classDecl, project, mixinEntity);
3799
- }
3800
- const filterForHints = extractFilterForHints(classDecl, project);
3801
- if (filterForHints.size > 0) {
3802
- const byName = new Map(fieldTypes.map((f) => [f.name, f]));
3803
- for (const [key, classified] of filterForHints) {
3804
- byName.set(key, toFilterFieldType(key, classified));
3805
- }
3806
- fieldTypes = [...byName.values()];
3807
- }
3808
- if (fieldTypes.length === 0) return null;
3809
- const fieldNames = fieldTypes.map((f) => f.name);
3799
+ const declaredClass = factoryStaticClass ?? (resolved?.kind === "class" ? resolved.decl : resolveLocalClassDeclaration(filterClassArg));
3800
+ const ownRoute = !mixin || declFile.getFilePath() !== mixin.factoryFilePath;
3801
+ const namedByRoute = ownRoute && import_ts_morph7.Node.isIdentifier(filterClassArg) ? declaredClass : void 0;
3802
+ for (const candidate of orderFilterCandidates({
3803
+ namedByRoute,
3804
+ supplied: resolveMixinFilterClass(mixin, project),
3805
+ declared: declaredClass
3806
+ })) {
3807
+ const fieldTypes = collectFilterFields(candidate, project, mixinEntity);
3808
+ if (fieldTypes.length === 0) continue;
3810
3809
  return {
3811
- fieldNames,
3810
+ fieldNames: fieldTypes.map((f) => f.name),
3812
3811
  fieldTypes,
3813
3812
  source
3814
3813
  };
3815
3814
  }
3815
+ if (declaredClass) return null;
3816
3816
  }
3817
3817
  return null;
3818
3818
  }
3819
+ function orderFilterCandidates(sources) {
3820
+ const ordered = [];
3821
+ const seen = /* @__PURE__ */ new Set();
3822
+ const push = (decl, preferDeclaredEntity) => {
3823
+ if (!decl || seen.has(decl)) return;
3824
+ seen.add(decl);
3825
+ ordered.push({ decl, preferDeclaredEntity });
3826
+ };
3827
+ push(sources.namedByRoute, true);
3828
+ push(sources.supplied, true);
3829
+ push(sources.declared, false);
3830
+ return ordered;
3831
+ }
3832
+ function collectFilterFields(candidate, project, mixinEntity) {
3833
+ const { decl, preferDeclaredEntity } = candidate;
3834
+ let fieldTypes = extractClassPropertyTypes(decl, project);
3835
+ if (fieldTypes.length === 0) {
3836
+ fieldTypes = extractFilterableEntityFields(decl, project, mixinEntity, preferDeclaredEntity);
3837
+ }
3838
+ const filterForHints = extractFilterForHints(decl, project);
3839
+ if (filterForHints.size > 0) {
3840
+ const byName = new Map(fieldTypes.map((f) => [f.name, f]));
3841
+ for (const [key, classified] of filterForHints) {
3842
+ byName.set(key, toFilterFieldType(key, classified));
3843
+ }
3844
+ fieldTypes = [...byName.values()];
3845
+ }
3846
+ return fieldTypes;
3847
+ }
3819
3848
  var RELATION_DECORATORS = /* @__PURE__ */ new Set(["OneToMany", "ManyToOne", "ManyToMany", "OneToOne"]);
3820
3849
  function collectEntityFields(entityDecl, sourceFile, project, prefix, visited) {
3821
3850
  const entityName = entityDecl.getName() ?? "";
@@ -3894,21 +3923,53 @@ function resolveDeclaredEntity(optionsArg, filterClass, project) {
3894
3923
  if (!resolved || resolved.kind !== "class") return void 0;
3895
3924
  return resolved.decl;
3896
3925
  }
3897
- function extractFilterableEntityFields(filterClass, project, entityOverride) {
3926
+ function readFieldNameList(optionsArg, key) {
3927
+ if (!import_ts_morph7.Node.isObjectLiteralExpression(optionsArg)) return void 0;
3928
+ const prop = optionsArg.getProperty(key);
3929
+ if (!prop || !import_ts_morph7.Node.isPropertyAssignment(prop)) return void 0;
3930
+ const init = prop.getInitializer();
3931
+ if (!init || !import_ts_morph7.Node.isArrayLiteralExpression(init)) return void 0;
3932
+ const names = /* @__PURE__ */ new Set();
3933
+ for (const el of init.getElements()) {
3934
+ if (import_ts_morph7.Node.isStringLiteral(el)) {
3935
+ names.add(el.getLiteralValue());
3936
+ continue;
3937
+ }
3938
+ if (import_ts_morph7.Node.isObjectLiteralExpression(el)) {
3939
+ const fieldProp = el.getProperty("field");
3940
+ if (fieldProp && import_ts_morph7.Node.isPropertyAssignment(fieldProp)) {
3941
+ const fieldInit = fieldProp.getInitializer();
3942
+ if (fieldInit && import_ts_morph7.Node.isStringLiteral(fieldInit)) {
3943
+ names.add(fieldInit.getLiteralValue());
3944
+ continue;
3945
+ }
3946
+ }
3947
+ }
3948
+ return void 0;
3949
+ }
3950
+ return names;
3951
+ }
3952
+ function narrowToDeclaredFields(fields, optionsArg) {
3953
+ const allowed = readFieldNameList(optionsArg, "allowed");
3954
+ const blocked = readFieldNameList(optionsArg, "blocked");
3955
+ if (!allowed && !blocked) return fields;
3956
+ return fields.filter(
3957
+ (f) => (!allowed || allowed.has(f.name)) && !(blocked?.has(f.name) ?? false)
3958
+ );
3959
+ }
3960
+ function extractFilterableEntityFields(filterClass, project, entityOverride, preferDeclaredEntity = false) {
3898
3961
  const filterableDecorator = filterClass.getDecorators().find((d) => d.getName() === "Filterable");
3899
3962
  if (!filterableDecorator) return [];
3900
3963
  const args = filterableDecorator.getArguments();
3901
3964
  if (args.length === 0) return [];
3902
3965
  const optionsArg = args[0];
3903
3966
  if (!import_ts_morph7.Node.isObjectLiteralExpression(optionsArg)) return [];
3904
- const entityDecl = entityOverride ?? resolveDeclaredEntity(optionsArg, filterClass, project);
3967
+ const declaredEntity = resolveDeclaredEntity(optionsArg, filterClass, project);
3968
+ const entityDecl = preferDeclaredEntity ? declaredEntity ?? entityOverride : entityOverride ?? declaredEntity;
3905
3969
  if (!entityDecl) return [];
3906
- const fields = collectEntityFields(
3907
- entityDecl,
3908
- entityDecl.getSourceFile(),
3909
- project,
3910
- "",
3911
- /* @__PURE__ */ new Set()
3970
+ const fields = narrowToDeclaredFields(
3971
+ collectEntityFields(entityDecl, entityDecl.getSourceFile(), project, "", /* @__PURE__ */ new Set()),
3972
+ optionsArg
3912
3973
  );
3913
3974
  const relationsDecorator = filterClass.getDecorators().find((d) => d.getName() === "Relations");
3914
3975
  if (relationsDecorator) {
@@ -5256,7 +5317,7 @@ function createChainModuleRenderer(opts) {
5256
5317
  }
5257
5318
 
5258
5319
  // src/index.ts
5259
- var VERSION = "0.19.0";
5320
+ var VERSION = "0.20.0";
5260
5321
  // Annotate the CommonJS export names for ESM import in node:
5261
5322
  0 && (module.exports = {
5262
5323
  CodegenError,