@dudousxd/nestjs-codegen 0.19.0 → 0.21.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/CHANGELOG.md +79 -0
- package/dist/cli/main.cjs +95 -34
- package/dist/cli/main.cjs.map +1 -1
- package/dist/cli/main.js +95 -34
- package/dist/cli/main.js.map +1 -1
- package/dist/index.cjs +95 -34
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +95 -34
- package/dist/index.js.map +1 -1
- package/dist/nest/index.cjs +95 -34
- package/dist/nest/index.cjs.map +1 -1
- package/dist/nest/index.js +95 -34
- package/dist/nest/index.js.map +1 -1
- package/package.json +1 -1
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
|
-
|
|
3692
|
-
|
|
3693
|
-
|
|
3694
|
-
return
|
|
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)) {
|
|
@@ -3766,7 +3771,7 @@ function extractFilterForHints(classDecl, project) {
|
|
|
3766
3771
|
}
|
|
3767
3772
|
return hints;
|
|
3768
3773
|
}
|
|
3769
|
-
function extractApplyFilterInfo(method, sourceFile, project, mixin) {
|
|
3774
|
+
function extractApplyFilterInfo(method, sourceFile, project, mixin, controllerClass) {
|
|
3770
3775
|
const declFile = method.getSourceFile();
|
|
3771
3776
|
const mixinEntity = resolveMixinEntityClass(mixin, project);
|
|
3772
3777
|
for (const param of method.getParameters()) {
|
|
@@ -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
|
|
3795
|
-
|
|
3796
|
-
|
|
3797
|
-
|
|
3798
|
-
|
|
3799
|
-
|
|
3800
|
-
|
|
3801
|
-
|
|
3802
|
-
|
|
3803
|
-
|
|
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 || (controllerClass ? method.getParent() === controllerClass : 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
|
|
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
|
|
3967
|
+
const declaredEntity = resolveDeclaredEntity(optionsArg, filterClass, project);
|
|
3968
|
+
const entityDecl = preferDeclaredEntity ? declaredEntity ?? entityOverride : entityOverride ?? declaredEntity;
|
|
3905
3969
|
if (!entityDecl) return [];
|
|
3906
|
-
const fields =
|
|
3907
|
-
entityDecl,
|
|
3908
|
-
|
|
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) {
|
|
@@ -4327,9 +4388,9 @@ function unwrapNamedContainer(node, names) {
|
|
|
4327
4388
|
}
|
|
4328
4389
|
return node;
|
|
4329
4390
|
}
|
|
4330
|
-
function extractDtoContract(method, sourceFile, project, mixin) {
|
|
4391
|
+
function extractDtoContract(method, sourceFile, project, mixin, controllerClass) {
|
|
4331
4392
|
let body = extractBodyType(method, sourceFile, project);
|
|
4332
|
-
const filterInfo = extractApplyFilterInfo(method, sourceFile, project, mixin);
|
|
4393
|
+
const filterInfo = extractApplyFilterInfo(method, sourceFile, project, mixin, controllerClass);
|
|
4333
4394
|
const query = extractQueryType(method, sourceFile, project);
|
|
4334
4395
|
const uploads = extractUploadedFiles(method);
|
|
4335
4396
|
const multipartBody = uploads.fields ? `{ ${uploads.fields} }` : null;
|
|
@@ -4892,7 +4953,7 @@ function extractDtoRoute(args) {
|
|
|
4892
4953
|
const methodName = method.getName();
|
|
4893
4954
|
const classAs = readAsDecorator(cls, `class ${className}`);
|
|
4894
4955
|
const methodAs = readAsDecorator(method, `${className}.${methodName}`);
|
|
4895
|
-
const dtoContract = extractDtoContract(method, sourceFile, project, mixin);
|
|
4956
|
+
const dtoContract = extractDtoContract(method, sourceFile, project, mixin, cls);
|
|
4896
4957
|
const mixinResponse = mixin ? resolveInstantiatedReturnType(cls, methodName) : void 0;
|
|
4897
4958
|
return buildRoute({
|
|
4898
4959
|
className,
|
|
@@ -5256,7 +5317,7 @@ function createChainModuleRenderer(opts) {
|
|
|
5256
5317
|
}
|
|
5257
5318
|
|
|
5258
5319
|
// src/index.ts
|
|
5259
|
-
var VERSION = "0.
|
|
5320
|
+
var VERSION = "0.21.0";
|
|
5260
5321
|
// Annotate the CommonJS export names for ESM import in node:
|
|
5261
5322
|
0 && (module.exports = {
|
|
5262
5323
|
CodegenError,
|