@jmm-devkit/ngx-form-generator 1.2.3 → 1.3.1
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/generator-cli.js +11 -1
- package/dist/generator-lib.d.ts +2 -1
- package/dist/generator-lib.js +55 -34
- package/package.json +1 -1
package/dist/generator-cli.js
CHANGED
|
@@ -28,6 +28,11 @@ async function main() {
|
|
|
28
28
|
alias: ['f', 'outFile'],
|
|
29
29
|
description: 'Generated file name',
|
|
30
30
|
type: 'string'
|
|
31
|
+
})
|
|
32
|
+
.option('max-depth', {
|
|
33
|
+
alias: ['d', 'maxDepth'],
|
|
34
|
+
description: 'Maximum depth of the generated forms',
|
|
35
|
+
type: 'number'
|
|
31
36
|
})
|
|
32
37
|
.help()
|
|
33
38
|
.wrap(null)
|
|
@@ -37,7 +42,12 @@ async function main() {
|
|
|
37
42
|
.example('npx ngx-form-generator -i swagger.json -o project/form/src/lib')
|
|
38
43
|
.alias('help', 'h').argv;
|
|
39
44
|
const spec = await (0, generator_lib_1.loadSpec)(argv['input-spec']);
|
|
40
|
-
const
|
|
45
|
+
const maxDepth = argv['max-depth'];
|
|
46
|
+
if (maxDepth !== undefined && (isNaN(maxDepth) || maxDepth < 1)) {
|
|
47
|
+
console.error('Error: max-depth must be a number greater than 0');
|
|
48
|
+
process.exit(1);
|
|
49
|
+
}
|
|
50
|
+
const file = (0, generator_lib_1.makeForm)(spec, maxDepth);
|
|
41
51
|
let fileName = argv['file-name'] || (0, generator_lib_1.makeFileName)(spec) || 'forms.ts';
|
|
42
52
|
if (argv.output) {
|
|
43
53
|
fileName = (0, path_1.join)(argv.output, fileName);
|
package/dist/generator-lib.d.ts
CHANGED
|
@@ -10,9 +10,10 @@ import { OpenAPI } from 'openapi-types';
|
|
|
10
10
|
/**
|
|
11
11
|
* Generates Angular ReactiveForms from an OpenAPI v2 or v3 spec.
|
|
12
12
|
* @param spec - The OpenAPI document.
|
|
13
|
+
* @param maxDepth - The maximum depth of the generated forms.
|
|
13
14
|
* @returns A string representing the generated forms.
|
|
14
15
|
*/
|
|
15
|
-
export declare function makeForm(spec: OpenAPI.Document): string;
|
|
16
|
+
export declare function makeForm(spec: OpenAPI.Document, maxDepth?: number): string;
|
|
16
17
|
/**
|
|
17
18
|
* Adds a validation rule for a form field.
|
|
18
19
|
* @param rule - The validation rule to add.
|
package/dist/generator-lib.js
CHANGED
|
@@ -19,14 +19,17 @@ const swagger_parser_1 = __importDefault(require("@apidevtools/swagger-parser"))
|
|
|
19
19
|
const DEFAULT_RULES = [rules_1.requiredRule, rules_1.patternRule, rules_1.minLengthRule, rules_1.maxLengthRule, rules_1.emailRule, rules_1.minimumRule, rules_1.maximumRule];
|
|
20
20
|
const NEEDED_IMPORTS = `import { FormGroup, FormControl, Validators, FormArray } from '@angular/forms'; \n`;
|
|
21
21
|
let rules = [...DEFAULT_RULES];
|
|
22
|
+
let MAX_DEPTH = 2;
|
|
22
23
|
/**
|
|
23
24
|
* Generates Angular ReactiveForms from an OpenAPI v2 or v3 spec.
|
|
24
25
|
* @param spec - The OpenAPI document.
|
|
26
|
+
* @param maxDepth - The maximum depth of the generated forms.
|
|
25
27
|
* @returns A string representing the generated forms.
|
|
26
28
|
*/
|
|
27
|
-
function makeForm(spec) {
|
|
29
|
+
function makeForm(spec, maxDepth) {
|
|
28
30
|
var _a;
|
|
29
31
|
let definitions;
|
|
32
|
+
MAX_DEPTH = maxDepth !== null && maxDepth !== void 0 ? maxDepth : MAX_DEPTH;
|
|
30
33
|
if ('definitions' in spec) {
|
|
31
34
|
definitions = spec.definitions;
|
|
32
35
|
}
|
|
@@ -40,11 +43,11 @@ function makeForm(spec) {
|
|
|
40
43
|
throw new Error('Cannot find schemas/definitions');
|
|
41
44
|
}
|
|
42
45
|
let body = NEEDED_IMPORTS + '\n\n';
|
|
43
|
-
Object.
|
|
44
|
-
if (
|
|
45
|
-
body += makeDefinition(key,
|
|
46
|
+
for (const [key, value] of Object.entries(definitions)) {
|
|
47
|
+
if (value) {
|
|
48
|
+
body += makeDefinition(key, value);
|
|
46
49
|
}
|
|
47
|
-
}
|
|
50
|
+
}
|
|
48
51
|
return prettier_1.default.format(body, { parser: 'typescript', singleQuote: true });
|
|
49
52
|
}
|
|
50
53
|
exports.makeForm = makeForm;
|
|
@@ -66,38 +69,56 @@ function makeDefinition(definitionName, definition) {
|
|
|
66
69
|
* @returns An array of strings representing the form fields.
|
|
67
70
|
*/
|
|
68
71
|
function makeFieldsBody(definition, depth) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
return fields;
|
|
72
|
+
if (depth >= MAX_DEPTH)
|
|
73
|
+
return [];
|
|
72
74
|
depth++;
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
75
|
+
return [...extractPropertiesFields(definition, depth), ...extractAllOfFields(definition, depth)];
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Extracts fields from the properties of a given OpenAPI definition.
|
|
79
|
+
* @param definition - The OpenAPI definition object.
|
|
80
|
+
* @param depth - The current depth of recursion.
|
|
81
|
+
* @returns An array of strings representing the form fields.
|
|
82
|
+
*/
|
|
83
|
+
function extractPropertiesFields(definition, depth) {
|
|
84
|
+
var _a;
|
|
85
|
+
if (!('properties' in definition) || !definition.properties)
|
|
86
|
+
return [];
|
|
87
|
+
const fields = [];
|
|
88
|
+
for (const [fieldName, fieldValue] of Object.entries(definition.properties)) {
|
|
89
|
+
const field = makeField(fieldName, fieldValue, !!((_a = definition.required) === null || _a === void 0 ? void 0 : _a.includes(fieldName)), depth);
|
|
90
|
+
if (field !== '') {
|
|
91
|
+
fields.push(field);
|
|
92
|
+
}
|
|
83
93
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
94
|
+
return fields;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Extracts fields from the allOf properties of a given OpenAPI definition.
|
|
98
|
+
* @param definition - The OpenAPI definition object.
|
|
99
|
+
* @param depth - The current depth of recursion.
|
|
100
|
+
* @returns An array of strings representing the form fields.
|
|
101
|
+
*/
|
|
102
|
+
function extractAllOfFields(definition, depth) {
|
|
103
|
+
var _a, _b, _c;
|
|
104
|
+
if (!('allOf' in definition) || !Array.isArray(definition.allOf))
|
|
105
|
+
return [];
|
|
106
|
+
const fields = [];
|
|
107
|
+
for (const subSchema of definition.allOf) {
|
|
108
|
+
if ('$ref' in subSchema) {
|
|
109
|
+
const refName = subSchema.$ref.split('/').pop();
|
|
110
|
+
const refSchema = ((_a = definition.definitions) === null || _a === void 0 ? void 0 : _a[refName]) || ((_c = (_b = definition.components) === null || _b === void 0 ? void 0 : _b.schemas) === null || _c === void 0 ? void 0 : _c[refName]);
|
|
111
|
+
if (refSchema) {
|
|
112
|
+
fields.push(...makeFieldsBody(refSchema, depth));
|
|
99
113
|
}
|
|
100
|
-
}
|
|
114
|
+
}
|
|
115
|
+
else if ('type' in subSchema && subSchema.type === 'object') {
|
|
116
|
+
subSchema.required = definition.required;
|
|
117
|
+
fields.push(...makeFieldsBody(subSchema, depth));
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
fields.push(...makeFieldsBody(subSchema, depth));
|
|
121
|
+
}
|
|
101
122
|
}
|
|
102
123
|
return fields;
|
|
103
124
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jmm-devkit/ngx-form-generator",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.1",
|
|
4
4
|
"description": "Generates an Angular ReactiveForm from a Swagger or OpenAPI definition",
|
|
5
5
|
"main": "dist/generator-lib.js",
|
|
6
6
|
"repository": "github:jmm-devkit/ngx-form-generator",
|