@jmm-devkit/ngx-form-generator 1.2.2 → 1.3.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/generator-cli.js +11 -1
- package/dist/generator-lib.d.ts +2 -1
- package/dist/generator-lib.js +36 -11
- package/package.json +79 -79
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
|
}
|
|
@@ -66,17 +69,39 @@ function makeDefinition(definitionName, definition) {
|
|
|
66
69
|
* @returns An array of strings representing the form fields.
|
|
67
70
|
*/
|
|
68
71
|
function makeFieldsBody(definition, depth) {
|
|
69
|
-
if (!('properties' in definition) || !definition.properties || depth > 2)
|
|
70
|
-
return [];
|
|
71
|
-
depth++;
|
|
72
72
|
const fields = [];
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
73
|
+
if (depth >= MAX_DEPTH)
|
|
74
|
+
return fields;
|
|
75
|
+
depth++;
|
|
76
|
+
const hasProperties = 'properties' in definition && definition.properties;
|
|
77
|
+
if (hasProperties && definition.properties) {
|
|
78
|
+
// Process each property in the definition
|
|
79
|
+
Object.keys(definition.properties).forEach(fieldName => {
|
|
80
|
+
var _a, _b;
|
|
81
|
+
const field = makeField(fieldName, (_a = definition.properties) === null || _a === void 0 ? void 0 : _a[fieldName], !!((_b = definition.required) === null || _b === void 0 ? void 0 : _b.includes(fieldName)), depth);
|
|
82
|
+
if (field !== '') {
|
|
83
|
+
fields.push(field);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
// Handle allOf by merging properties from referenced schemas
|
|
88
|
+
const hasAllOf = 'allOf' in definition && Array.isArray(definition.allOf);
|
|
89
|
+
if (hasAllOf && definition.allOf) {
|
|
90
|
+
definition.allOf.forEach((subSchema) => {
|
|
91
|
+
var _a, _b, _c;
|
|
92
|
+
// If the subSchema is a reference, resolve it from definitions
|
|
93
|
+
if ('$ref' in subSchema) {
|
|
94
|
+
const refName = subSchema.$ref.split('/').pop();
|
|
95
|
+
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]);
|
|
96
|
+
if (refSchema) {
|
|
97
|
+
fields.push(...makeFieldsBody(refSchema, depth));
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
fields.push(...makeFieldsBody(subSchema, depth));
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
}
|
|
80
105
|
return fields;
|
|
81
106
|
}
|
|
82
107
|
/**
|
package/package.json
CHANGED
|
@@ -1,79 +1,79 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@jmm-devkit/ngx-form-generator",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "Generates an Angular ReactiveForm from a Swagger or OpenAPI definition",
|
|
5
|
-
"main": "dist/generator-lib.js",
|
|
6
|
-
"repository": "github:jmm-devkit/ngx-form-generator",
|
|
7
|
-
"bin": {
|
|
8
|
-
"ngx-form-generator": "dist/generator-cli.js"
|
|
9
|
-
},
|
|
10
|
-
"scripts": {
|
|
11
|
-
"test": "npm run build && jasmine dist/*.spec.js",
|
|
12
|
-
"build": "tsc --project .\\",
|
|
13
|
-
"lint": "eslint . --ext .ts",
|
|
14
|
-
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s && git add CHANGELOG.md"
|
|
15
|
-
},
|
|
16
|
-
"author": "Martin McWhorter <martin@mcwhorter.org> (https://github.com/martinmcwhorter)",
|
|
17
|
-
"license": "MIT",
|
|
18
|
-
"keywords": [
|
|
19
|
-
"angular",
|
|
20
|
-
"validation",
|
|
21
|
-
"form",
|
|
22
|
-
"reactive-forms",
|
|
23
|
-
"swagger",
|
|
24
|
-
"openapi",
|
|
25
|
-
"typescript"
|
|
26
|
-
],
|
|
27
|
-
"devDependencies": {
|
|
28
|
-
"@commitlint/cli": "^17.1.2",
|
|
29
|
-
"@commitlint/config-conventional": "^17.0.3",
|
|
30
|
-
"@types/camelcase": "^4.1.0",
|
|
31
|
-
"@types/jasmine": "^4.3.0",
|
|
32
|
-
"@types/node-fetch": "^2.5.5",
|
|
33
|
-
"@types/prettier": "^1.19.1",
|
|
34
|
-
"@types/yaml": "^1.2.0",
|
|
35
|
-
"@types/yargs": "^17.0.13",
|
|
36
|
-
"@typescript-eslint/eslint-plugin": "^5.0.0",
|
|
37
|
-
"@typescript-eslint/parser": "^5.0.0",
|
|
38
|
-
"commitiquette": "^1.2.1",
|
|
39
|
-
"commitizen": "^4.2.4",
|
|
40
|
-
"eslint": "^8.0.1",
|
|
41
|
-
"eslint-config-prettier": "^6.10.1",
|
|
42
|
-
"husky": "^7.0.4",
|
|
43
|
-
"jasmine": "^4.4.0",
|
|
44
|
-
"lint-staged": "^13.0.3",
|
|
45
|
-
"openapi-types": "^7.0.1",
|
|
46
|
-
"typescript": "^4.8.4"
|
|
47
|
-
},
|
|
48
|
-
"dependencies": {
|
|
49
|
-
"@apidevtools/swagger-parser": "^10.0.2",
|
|
50
|
-
"camelcase": "^5.0.0",
|
|
51
|
-
"prettier": "^1.19.1",
|
|
52
|
-
"ts-morph": "^16.0.0",
|
|
53
|
-
"yaml": "^2.1.3",
|
|
54
|
-
"yargs": "^17.6.0"
|
|
55
|
-
},
|
|
56
|
-
"husky": {
|
|
57
|
-
"hooks": {
|
|
58
|
-
"pre-commit": "lint-staged",
|
|
59
|
-
"prepare-commit-msg": "exec < /dev/tty && git cz --hook || true",
|
|
60
|
-
"commit-msg": "commitLint -E HUSKY_GIT_PARAMS"
|
|
61
|
-
}
|
|
62
|
-
},
|
|
63
|
-
"lint-staged": {
|
|
64
|
-
"*.{js,css,json,md}": [
|
|
65
|
-
"prettier --write",
|
|
66
|
-
"git add"
|
|
67
|
-
],
|
|
68
|
-
"*.ts": [
|
|
69
|
-
"eslint --fix",
|
|
70
|
-
"prettier --write",
|
|
71
|
-
"git add"
|
|
72
|
-
]
|
|
73
|
-
},
|
|
74
|
-
"config": {
|
|
75
|
-
"commitizen": {
|
|
76
|
-
"path": "commitiquette"
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@jmm-devkit/ngx-form-generator",
|
|
3
|
+
"version": "1.3.0",
|
|
4
|
+
"description": "Generates an Angular ReactiveForm from a Swagger or OpenAPI definition",
|
|
5
|
+
"main": "dist/generator-lib.js",
|
|
6
|
+
"repository": "github:jmm-devkit/ngx-form-generator",
|
|
7
|
+
"bin": {
|
|
8
|
+
"ngx-form-generator": "dist/generator-cli.js"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"test": "npm run build && jasmine dist/*.spec.js",
|
|
12
|
+
"build": "tsc --project .\\",
|
|
13
|
+
"lint": "eslint . --ext .ts",
|
|
14
|
+
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s && git add CHANGELOG.md"
|
|
15
|
+
},
|
|
16
|
+
"author": "Martin McWhorter <martin@mcwhorter.org> (https://github.com/martinmcwhorter)",
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"keywords": [
|
|
19
|
+
"angular",
|
|
20
|
+
"validation",
|
|
21
|
+
"form",
|
|
22
|
+
"reactive-forms",
|
|
23
|
+
"swagger",
|
|
24
|
+
"openapi",
|
|
25
|
+
"typescript"
|
|
26
|
+
],
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@commitlint/cli": "^17.1.2",
|
|
29
|
+
"@commitlint/config-conventional": "^17.0.3",
|
|
30
|
+
"@types/camelcase": "^4.1.0",
|
|
31
|
+
"@types/jasmine": "^4.3.0",
|
|
32
|
+
"@types/node-fetch": "^2.5.5",
|
|
33
|
+
"@types/prettier": "^1.19.1",
|
|
34
|
+
"@types/yaml": "^1.2.0",
|
|
35
|
+
"@types/yargs": "^17.0.13",
|
|
36
|
+
"@typescript-eslint/eslint-plugin": "^5.0.0",
|
|
37
|
+
"@typescript-eslint/parser": "^5.0.0",
|
|
38
|
+
"commitiquette": "^1.2.1",
|
|
39
|
+
"commitizen": "^4.2.4",
|
|
40
|
+
"eslint": "^8.0.1",
|
|
41
|
+
"eslint-config-prettier": "^6.10.1",
|
|
42
|
+
"husky": "^7.0.4",
|
|
43
|
+
"jasmine": "^4.4.0",
|
|
44
|
+
"lint-staged": "^13.0.3",
|
|
45
|
+
"openapi-types": "^7.0.1",
|
|
46
|
+
"typescript": "^4.8.4"
|
|
47
|
+
},
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"@apidevtools/swagger-parser": "^10.0.2",
|
|
50
|
+
"camelcase": "^5.0.0",
|
|
51
|
+
"prettier": "^1.19.1",
|
|
52
|
+
"ts-morph": "^16.0.0",
|
|
53
|
+
"yaml": "^2.1.3",
|
|
54
|
+
"yargs": "^17.6.0"
|
|
55
|
+
},
|
|
56
|
+
"husky": {
|
|
57
|
+
"hooks": {
|
|
58
|
+
"pre-commit": "lint-staged",
|
|
59
|
+
"prepare-commit-msg": "exec < /dev/tty && git cz --hook || true",
|
|
60
|
+
"commit-msg": "commitLint -E HUSKY_GIT_PARAMS"
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
"lint-staged": {
|
|
64
|
+
"*.{js,css,json,md}": [
|
|
65
|
+
"prettier --write",
|
|
66
|
+
"git add"
|
|
67
|
+
],
|
|
68
|
+
"*.ts": [
|
|
69
|
+
"eslint --fix",
|
|
70
|
+
"prettier --write",
|
|
71
|
+
"git add"
|
|
72
|
+
]
|
|
73
|
+
},
|
|
74
|
+
"config": {
|
|
75
|
+
"commitizen": {
|
|
76
|
+
"path": "commitiquette"
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|