@dextinity/api-generator 2.0.0-canary-20260729062014
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/LICENSE +24 -0
- package/bin/api-generator.js +6 -0
- package/lib/apiGenerator.d.ts +1 -0
- package/lib/apiGenerator.js +7 -0
- package/lib/commands/generate/generate-command.d.ts +2 -0
- package/lib/commands/generate/generate-command.js +19 -0
- package/lib/commands/generate/generateCrud/build-options.d.ts +24 -0
- package/lib/commands/generate/generateCrud/build-options.js +144 -0
- package/lib/commands/generate/generateCrud/generate-crud.d.ts +18 -0
- package/lib/commands/generate/generateCrud/generate-crud.js +1092 -0
- package/lib/commands/generate/generateCrud/generate-enum-filter-dto.d.ts +2 -0
- package/lib/commands/generate/generateCrud/generate-enum-filter-dto.js +76 -0
- package/lib/commands/generate/generateCrud/generate-payload-object-types.d.ts +12 -0
- package/lib/commands/generate/generateCrud/generate-payload-object-types.js +38 -0
- package/lib/commands/generate/generateCrud/generate-service-hook-call.d.ts +8 -0
- package/lib/commands/generate/generateCrud/generate-service-hook-call.js +47 -0
- package/lib/commands/generate/generateCrudInput/generate-crud-input.d.ts +12 -0
- package/lib/commands/generate/generateCrudInput/generate-crud-input.js +499 -0
- package/lib/commands/generate/generateCrudSingle/generate-crud-single.d.ts +4 -0
- package/lib/commands/generate/generateCrudSingle/generate-crud-single.js +134 -0
- package/lib/commands/generate/generateFiles.d.ts +13 -0
- package/lib/commands/generate/generateFiles.js +81 -0
- package/lib/commands/generate/utils/build-name-variants.d.ts +9 -0
- package/lib/commands/generate/utils/build-name-variants.js +24 -0
- package/lib/commands/generate/utils/constants.d.ts +2 -0
- package/lib/commands/generate/utils/constants.js +5 -0
- package/lib/commands/generate/utils/find-hooks-service.d.ts +19 -0
- package/lib/commands/generate/utils/find-hooks-service.js +185 -0
- package/lib/commands/generate/utils/generate-imports-code.d.ts +5 -0
- package/lib/commands/generate/utils/generate-imports-code.js +32 -0
- package/lib/commands/generate/utils/ts-morph-helper.d.ts +18 -0
- package/lib/commands/generate/utils/ts-morph-helper.js +237 -0
- package/lib/commands/generate/utils/write-generated-file.d.ts +3 -0
- package/lib/commands/generate/utils/write-generated-file.js +113 -0
- package/lib/commands/generate/utils/write-generated-files.d.ts +7 -0
- package/lib/commands/generate/utils/write-generated-files.js +9 -0
- package/lib/commands/generate/watchMode/handleChildProcess.d.ts +2 -0
- package/lib/commands/generate/watchMode/handleChildProcess.js +26 -0
- package/lib/commands/generate/watchMode/watchMode.d.ts +6 -0
- package/lib/commands/generate/watchMode/watchMode.js +70 -0
- package/package.json +79 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
BSD 2-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023, Vivid Planet Software GmbH
|
|
4
|
+
|
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
|
7
|
+
|
|
8
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+
list of conditions and the following disclaimer.
|
|
10
|
+
|
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
+
this list of conditions and the following disclaimer in the documentation
|
|
13
|
+
and/or other materials provided with the distribution.
|
|
14
|
+
|
|
15
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
16
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
17
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
18
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
19
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
20
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
21
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
22
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
23
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
24
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const commander_1 = require("commander");
|
|
4
|
+
const generate_command_1 = require("./commands/generate/generate-command");
|
|
5
|
+
const program = new commander_1.Command();
|
|
6
|
+
program.addCommand(generate_command_1.generateCommand);
|
|
7
|
+
program.parse();
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateCommand = void 0;
|
|
4
|
+
const commander_1 = require("commander");
|
|
5
|
+
const generateFiles_1 = require("./generateFiles");
|
|
6
|
+
const watchMode_1 = require("./watchMode/watchMode");
|
|
7
|
+
exports.generateCommand = new commander_1.Command("generate")
|
|
8
|
+
.action(async (options) => {
|
|
9
|
+
if (options.watch) {
|
|
10
|
+
await (0, generateFiles_1.generateFiles)();
|
|
11
|
+
console.log("Watching for modified entities...");
|
|
12
|
+
await (0, watchMode_1.watchMode)();
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
(0, generateFiles_1.generateFiles)(options.file);
|
|
16
|
+
}
|
|
17
|
+
})
|
|
18
|
+
.option("-f, --file <file>", "path to entity file")
|
|
19
|
+
.option("-w, --watch", "Watch for changes");
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type CrudGeneratorOptions } from "@dextinity/cms-api";
|
|
2
|
+
import type { EntityMetadata } from "@mikro-orm/core";
|
|
3
|
+
export declare function buildSortProps(metadata: EntityMetadata<any>): string[];
|
|
4
|
+
export declare function buildOptions(metadata: EntityMetadata<any>, generatorOptions: CrudGeneratorOptions): {
|
|
5
|
+
crudSearchPropNames: import("@dextinity/cms-api/lib/common/helper/crud-generator.helper").CrudSearchField[];
|
|
6
|
+
hasSearchArg: boolean;
|
|
7
|
+
crudFilterProps: import("@mikro-orm/core").EntityProperty<any, any>[];
|
|
8
|
+
hasFilterArg: boolean;
|
|
9
|
+
crudSortProps: string[];
|
|
10
|
+
hasSortArg: boolean;
|
|
11
|
+
hasSlugProp: boolean;
|
|
12
|
+
hasPositionProp: boolean;
|
|
13
|
+
positionGroupProps: import("@mikro-orm/core").EntityProperty<any, any>[];
|
|
14
|
+
hasDeletedAtProp: boolean;
|
|
15
|
+
scopeProp: import("@mikro-orm/core").EntityProperty<any, any> | undefined;
|
|
16
|
+
skipScopeCheck: boolean;
|
|
17
|
+
argsClassName: string;
|
|
18
|
+
argsFileName: string;
|
|
19
|
+
hasPaging: boolean;
|
|
20
|
+
hasArgsClass: boolean;
|
|
21
|
+
blockProps: import("@mikro-orm/core").EntityProperty<any, any>[];
|
|
22
|
+
dedicatedResolverArgProps: import("@mikro-orm/core").EntityProperty<any, any>[];
|
|
23
|
+
targetDirectory: string;
|
|
24
|
+
};
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildSortProps = buildSortProps;
|
|
4
|
+
exports.buildOptions = buildOptions;
|
|
5
|
+
const node_path_1 = require("node:path");
|
|
6
|
+
const cms_api_1 = require("@dextinity/cms-api");
|
|
7
|
+
const build_name_variants_1 = require("../utils/build-name-variants");
|
|
8
|
+
const constants_1 = require("../utils/constants");
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
10
|
+
function buildFilterProps(metadata) {
|
|
11
|
+
const dedicatedResolverArgProps = buildDedicatedResolverArgProps(metadata);
|
|
12
|
+
const crudFilterProps = metadata.props.filter((prop) => (0, cms_api_1.hasCrudFieldFeature)(metadata.class, prop.name, "filter") &&
|
|
13
|
+
!prop.name.startsWith("scope_") &&
|
|
14
|
+
prop.name != "position" &&
|
|
15
|
+
(!prop.embedded || (0, cms_api_1.hasCrudFieldFeature)(metadata.class, prop.embedded[0], "filter")) && // the whole embeddable has filter disabled
|
|
16
|
+
(prop.enum ||
|
|
17
|
+
prop.type === "string" ||
|
|
18
|
+
prop.type === "text" ||
|
|
19
|
+
prop.type === "DecimalType" ||
|
|
20
|
+
prop.type === "number" ||
|
|
21
|
+
constants_1.integerTypes.includes(prop.type) ||
|
|
22
|
+
prop.type === "BooleanType" ||
|
|
23
|
+
prop.type === "boolean" ||
|
|
24
|
+
prop.type === "DateType" ||
|
|
25
|
+
prop.type === "Date" ||
|
|
26
|
+
prop.kind === "m:1" ||
|
|
27
|
+
prop.kind === "1:m" ||
|
|
28
|
+
prop.kind === "m:n" ||
|
|
29
|
+
prop.type === "EnumArrayType" ||
|
|
30
|
+
prop.type === "uuid") &&
|
|
31
|
+
!dedicatedResolverArgProps.some((dedicatedResolverArgProp) => dedicatedResolverArgProp.name == prop.name));
|
|
32
|
+
return crudFilterProps;
|
|
33
|
+
}
|
|
34
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
35
|
+
function buildSortProps(metadata) {
|
|
36
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
37
|
+
function directSortProps(metadata) {
|
|
38
|
+
return metadata.props
|
|
39
|
+
.filter((prop) => (0, cms_api_1.hasCrudFieldFeature)(metadata.class, prop.name, "sort") &&
|
|
40
|
+
!prop.name.startsWith("scope_") &&
|
|
41
|
+
!prop.primary &&
|
|
42
|
+
(!prop.embedded || (0, cms_api_1.hasCrudFieldFeature)(metadata.class, prop.embedded[0], "sort")) && // the whole embeddable has sort disabled
|
|
43
|
+
(prop.type === "string" ||
|
|
44
|
+
prop.type === "text" ||
|
|
45
|
+
prop.type === "DecimalType" ||
|
|
46
|
+
prop.type === "number" ||
|
|
47
|
+
constants_1.integerTypes.includes(prop.type) ||
|
|
48
|
+
prop.type === "BooleanType" ||
|
|
49
|
+
prop.type === "boolean" ||
|
|
50
|
+
prop.type === "DateType" ||
|
|
51
|
+
prop.type === "Date" ||
|
|
52
|
+
prop.kind === "m:1" ||
|
|
53
|
+
prop.type === "EnumArrayType" ||
|
|
54
|
+
prop.enum))
|
|
55
|
+
.map((prop) => {
|
|
56
|
+
return prop.name;
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
const crudSortProps = directSortProps(metadata);
|
|
60
|
+
metadata.props.forEach((prop) => {
|
|
61
|
+
if ((0, cms_api_1.hasCrudFieldFeature)(metadata.class, prop.name, "sort")) {
|
|
62
|
+
if (prop.primary) {
|
|
63
|
+
// primary only on root level
|
|
64
|
+
crudSortProps.push(prop.name);
|
|
65
|
+
}
|
|
66
|
+
else if ((prop.kind == "1:1" || prop.kind == "m:1") && prop.targetMeta) {
|
|
67
|
+
// add nested from relations, one level deep
|
|
68
|
+
crudSortProps.push(...directSortProps(prop.targetMeta).map((nestedProp) => `${prop.name}.${nestedProp}`));
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
return crudSortProps;
|
|
74
|
+
}
|
|
75
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
76
|
+
function buildDedicatedResolverArgProps(metadata) {
|
|
77
|
+
return metadata.props.filter((prop) => {
|
|
78
|
+
if ((0, cms_api_1.hasCrudFieldFeature)(metadata.class, prop.name, "dedicatedResolverArg")) {
|
|
79
|
+
if (prop.kind == "m:1") {
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
console.warn(`${metadata.className} ${prop.name} can't use dedicatedResolverArg as it's not a m:1 relation`);
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return false;
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
91
|
+
function buildOptions(metadata, generatorOptions) {
|
|
92
|
+
const { classNameSingular, classNamePlural, fileNameSingular, fileNamePlural } = (0, build_name_variants_1.buildNameVariants)(metadata);
|
|
93
|
+
const dedicatedResolverArgProps = buildDedicatedResolverArgProps(metadata);
|
|
94
|
+
const crudSearchPropNames = (0, cms_api_1.getCrudSearchFieldsFromMetadata)(metadata);
|
|
95
|
+
const hasSearchArg = crudSearchPropNames.length > 0;
|
|
96
|
+
const crudFilterProps = buildFilterProps(metadata);
|
|
97
|
+
const hasFilterArg = crudFilterProps.length > 0;
|
|
98
|
+
const crudSortProps = buildSortProps(metadata);
|
|
99
|
+
const hasSortArg = crudSortProps.length > 0;
|
|
100
|
+
const hasSlugProp = metadata.props.some((prop) => prop.name == "slug");
|
|
101
|
+
const scopeProp = metadata.props.find((prop) => prop.name == "scope");
|
|
102
|
+
if (scopeProp && !scopeProp.targetMeta) {
|
|
103
|
+
throw new Error("Scope prop has no targetMeta");
|
|
104
|
+
}
|
|
105
|
+
const hasDeletedAtProp = metadata.props.some((prop) => prop.name == "deletedAt");
|
|
106
|
+
const hasPositionProp = metadata.props.some((prop) => prop.name == "position");
|
|
107
|
+
const positionGroupPropNames = hasPositionProp
|
|
108
|
+
? (generatorOptions.position?.groupByFields ?? [
|
|
109
|
+
...(scopeProp ? [scopeProp.name] : []), // if there is a scope prop it's effecting position-group, if not groupByFields should be used
|
|
110
|
+
])
|
|
111
|
+
: [];
|
|
112
|
+
const positionGroupProps = hasPositionProp ? metadata.props.filter((prop) => positionGroupPropNames.includes(prop.name)) : [];
|
|
113
|
+
const scopedEntity = Reflect.getMetadata(cms_api_1.SCOPED_ENTITY_METADATA_KEY, metadata.class);
|
|
114
|
+
const skipScopeCheck = !scopeProp && !scopedEntity;
|
|
115
|
+
const argsClassName = `${classNameSingular != classNamePlural ? classNamePlural : `${classNamePlural}List`}Args`;
|
|
116
|
+
const argsFileName = `${fileNameSingular != fileNamePlural ? fileNamePlural : `${fileNameSingular}-list`}.args`;
|
|
117
|
+
const hasPaging = generatorOptions.paging !== false;
|
|
118
|
+
const hasArgsClass = hasPaging || !!scopeProp || dedicatedResolverArgProps.length > 0;
|
|
119
|
+
const blockProps = metadata.props.filter((prop) => {
|
|
120
|
+
return (0, cms_api_1.hasCrudFieldFeature)(metadata.class, prop.name, "input") && prop.type === "RootBlockType";
|
|
121
|
+
});
|
|
122
|
+
const targetDirectory = `${(0, node_path_1.dirname)(metadata.path).replace(/\/entities$/, "")}/generated`;
|
|
123
|
+
return {
|
|
124
|
+
crudSearchPropNames,
|
|
125
|
+
hasSearchArg,
|
|
126
|
+
crudFilterProps,
|
|
127
|
+
hasFilterArg,
|
|
128
|
+
crudSortProps,
|
|
129
|
+
hasSortArg,
|
|
130
|
+
hasSlugProp,
|
|
131
|
+
hasPositionProp,
|
|
132
|
+
positionGroupProps,
|
|
133
|
+
hasDeletedAtProp,
|
|
134
|
+
scopeProp,
|
|
135
|
+
skipScopeCheck,
|
|
136
|
+
argsClassName,
|
|
137
|
+
argsFileName,
|
|
138
|
+
hasPaging,
|
|
139
|
+
hasArgsClass,
|
|
140
|
+
blockProps,
|
|
141
|
+
dedicatedResolverArgProps,
|
|
142
|
+
targetDirectory,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type CrudGeneratorOptions } from "@dextinity/cms-api";
|
|
2
|
+
import { type EntityMetadata } from "@mikro-orm/postgresql";
|
|
3
|
+
import { type Imports } from "../utils/generate-imports-code";
|
|
4
|
+
import type { GeneratedFile } from "../utils/write-generated-files";
|
|
5
|
+
export declare function generateSortDto({ generatorOptions, metadata }: {
|
|
6
|
+
generatorOptions: CrudGeneratorOptions;
|
|
7
|
+
metadata: EntityMetadata<any>;
|
|
8
|
+
}): string;
|
|
9
|
+
export declare function generateInputHandling(options: {
|
|
10
|
+
mode: "create" | "update" | "updateNested";
|
|
11
|
+
inputName: string;
|
|
12
|
+
assignEntityCode: string;
|
|
13
|
+
excludeFields?: string[];
|
|
14
|
+
}, metadata: EntityMetadata<any>, generatorOptions: CrudGeneratorOptions, targetDirectory: string): {
|
|
15
|
+
code: string;
|
|
16
|
+
imports: Imports;
|
|
17
|
+
};
|
|
18
|
+
export declare function generateCrud(generatorOptionsParam: CrudGeneratorOptions, metadata: EntityMetadata<any>): Promise<GeneratedFile[]>;
|