@jmm-devkit/ngx-form-generator 1.4.0 → 1.5.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/file-utils.js +5 -0
- package/dist/generator-cli.js +11 -9
- package/dist/generator-lib.d.ts +1 -1
- package/dist/generator-lib.js +18 -15
- package/dist/index.d.ts +3 -3
- package/dist/index.js +3 -3
- package/dist/rules.js +0 -1
- package/package.json +5 -2
package/dist/file-utils.js
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
import * as fs from 'node:fs';
|
|
2
|
+
import * as path from 'node:path';
|
|
2
3
|
/**
|
|
3
4
|
* Saves the generated file
|
|
4
5
|
* @param content - The content of the file to save.
|
|
5
6
|
* @param fileName - The name of the file to save.
|
|
6
7
|
*/
|
|
7
8
|
export function saveFile(content, fileName) {
|
|
9
|
+
const dir = path.dirname(fileName);
|
|
10
|
+
if (!fs.existsSync(dir)) {
|
|
11
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
12
|
+
}
|
|
8
13
|
fs.writeFileSync(fileName, content);
|
|
9
14
|
}
|
package/dist/generator-cli.js
CHANGED
|
@@ -6,17 +6,18 @@
|
|
|
6
6
|
*
|
|
7
7
|
* Copyright (c) 2020 Verizon
|
|
8
8
|
*/
|
|
9
|
-
import { saveFile } from './file-utils';
|
|
10
|
-
import { makeForm, makeFileName, loadSpec } from './generator-lib';
|
|
9
|
+
import { saveFile } from './file-utils.js';
|
|
10
|
+
import { makeForm, makeFileName, loadSpec } from './generator-lib.js';
|
|
11
11
|
import { join } from 'node:path';
|
|
12
|
-
|
|
12
|
+
import yargs from 'yargs';
|
|
13
|
+
import { hideBin } from 'yargs/helpers';
|
|
13
14
|
async function main() {
|
|
14
|
-
const argv = yargs
|
|
15
|
+
const argv = await yargs(hideBin(process.argv))
|
|
15
16
|
.option('input-spec', {
|
|
16
17
|
alias: ['i', 'swaggerUrl'],
|
|
17
18
|
description: 'Location of the OpenAPI spec as a URL or file path',
|
|
18
19
|
type: 'string',
|
|
19
|
-
|
|
20
|
+
demandOption: true,
|
|
20
21
|
})
|
|
21
22
|
.option('output', {
|
|
22
23
|
alias: ['o', 'outDir'],
|
|
@@ -36,10 +37,11 @@ async function main() {
|
|
|
36
37
|
.help()
|
|
37
38
|
.wrap(null)
|
|
38
39
|
.usage('Generates Angular ReactiveForms from an OpenAPI v2 or v3 spec.\n\n Usage: $0 -i <spec> -o <path>')
|
|
39
|
-
.example('ngx-form-generator -i https://petstore.swagger.io/v2/swagger.json -o petstore-forms')
|
|
40
|
-
.example('ngx-form-generator -i https://petstore.swagger.io/v2/swagger.yaml -o petstore-forms')
|
|
41
|
-
.example('npx ngx-form-generator -i swagger.json -o project/form/src/lib')
|
|
42
|
-
.alias('help', 'h')
|
|
40
|
+
.example('ngx-form-generator -i https://petstore.swagger.io/v2/swagger.json -o petstore-forms', 'Generate forms from a remote JSON spec')
|
|
41
|
+
.example('ngx-form-generator -i https://petstore.swagger.io/v2/swagger.yaml -o petstore-forms', 'Generate forms from a remote YAML spec')
|
|
42
|
+
.example('npx ngx-form-generator -i swagger.json -o project/form/src/lib', 'Generate forms from a local JSON spec')
|
|
43
|
+
.alias('help', 'h')
|
|
44
|
+
.parse();
|
|
43
45
|
const spec = await loadSpec(argv['input-spec']);
|
|
44
46
|
const maxDepth = argv['max-depth'];
|
|
45
47
|
if (maxDepth !== undefined && (Number.isNaN(maxDepth) || maxDepth < 1)) {
|
package/dist/generator-lib.d.ts
CHANGED
package/dist/generator-lib.js
CHANGED
|
@@ -7,9 +7,8 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import * as prettier from 'prettier';
|
|
9
9
|
import camelcase from 'camelcase';
|
|
10
|
-
import { requiredRule, patternRule, minLengthRule, maxLengthRule, emailRule, minimumRule, maximumRule, } from './rules';
|
|
11
|
-
import
|
|
12
|
-
import { OpenAPIV3 } from 'openapi-types';
|
|
10
|
+
import { requiredRule, patternRule, minLengthRule, maxLengthRule, emailRule, minimumRule, maximumRule, } from './rules.js';
|
|
11
|
+
import SwaggerParser from '@apidevtools/swagger-parser';
|
|
13
12
|
const DEFAULT_RULES = [requiredRule, patternRule, minLengthRule, maxLengthRule, emailRule, minimumRule, maximumRule];
|
|
14
13
|
const NEEDED_IMPORTS = `import { FormGroup, FormControl, Validators, FormArray } from '@angular/forms'; \n`;
|
|
15
14
|
let rules = [...DEFAULT_RULES];
|
|
@@ -21,13 +20,14 @@ let MAX_DEPTH = 2;
|
|
|
21
20
|
* @returns A string representing the generated forms.
|
|
22
21
|
*/
|
|
23
22
|
export async function makeForm(spec, maxDepth) {
|
|
23
|
+
var _a;
|
|
24
24
|
let definitions;
|
|
25
|
-
MAX_DEPTH = maxDepth
|
|
25
|
+
MAX_DEPTH = maxDepth !== null && maxDepth !== void 0 ? maxDepth : MAX_DEPTH;
|
|
26
26
|
if ('definitions' in spec) {
|
|
27
27
|
definitions = spec.definitions;
|
|
28
28
|
}
|
|
29
29
|
else if ('components' in spec) {
|
|
30
|
-
definitions = spec.components
|
|
30
|
+
definitions = (_a = spec.components) === null || _a === void 0 ? void 0 : _a.schemas;
|
|
31
31
|
}
|
|
32
32
|
else {
|
|
33
33
|
throw new Error('Cannot find schemas/definitions');
|
|
@@ -50,8 +50,8 @@ export async function makeForm(spec, maxDepth) {
|
|
|
50
50
|
* @returns A string representing the form definition.
|
|
51
51
|
*/
|
|
52
52
|
function makeDefinition(definitionName, definition) {
|
|
53
|
-
return `export const ${camelcase(definitionName)}Form = () => new FormGroup({
|
|
54
|
-
${makeFieldsBody({
|
|
53
|
+
return `export const ${camelcase(definitionName)}Form = () => new FormGroup({
|
|
54
|
+
${makeFieldsBody(Object.assign({}, definition), 0)}
|
|
55
55
|
});\n`;
|
|
56
56
|
}
|
|
57
57
|
/**
|
|
@@ -73,11 +73,12 @@ function makeFieldsBody(definition, depth) {
|
|
|
73
73
|
* @returns An array of strings representing the form fields.
|
|
74
74
|
*/
|
|
75
75
|
function extractPropertiesFields(definition, depth) {
|
|
76
|
+
var _a;
|
|
76
77
|
if (!('properties' in definition) || !definition.properties)
|
|
77
78
|
return [];
|
|
78
79
|
const fields = [];
|
|
79
80
|
for (const [fieldName, fieldValue] of Object.entries(definition.properties)) {
|
|
80
|
-
const field = makeField(fieldName, fieldValue, !!definition.required
|
|
81
|
+
const field = makeField(fieldName, fieldValue, !!((_a = definition.required) === null || _a === void 0 ? void 0 : _a.includes(fieldName)), depth);
|
|
81
82
|
if (field !== '') {
|
|
82
83
|
fields.push(field);
|
|
83
84
|
}
|
|
@@ -91,21 +92,22 @@ function extractPropertiesFields(definition, depth) {
|
|
|
91
92
|
* @returns An array of strings representing the form fields.
|
|
92
93
|
*/
|
|
93
94
|
function extractAllOfFields(definition, depth) {
|
|
95
|
+
var _a, _b, _c, _d, _e;
|
|
94
96
|
if (!('allOf' in definition) || !Array.isArray(definition.allOf))
|
|
95
97
|
return [];
|
|
96
98
|
const fields = [];
|
|
97
|
-
const definitionRequired = definition.required
|
|
99
|
+
const definitionRequired = (_a = definition.required) !== null && _a !== void 0 ? _a : [];
|
|
98
100
|
for (const subSchema of definition.allOf) {
|
|
99
101
|
if ('$ref' in subSchema) {
|
|
100
102
|
const refName = subSchema.$ref.split('/').pop();
|
|
101
|
-
const refSchema = definition.definitions
|
|
103
|
+
const refSchema = ((_b = definition.definitions) === null || _b === void 0 ? void 0 : _b[refName]) || ((_d = (_c = definition.components) === null || _c === void 0 ? void 0 : _c.schemas) === null || _d === void 0 ? void 0 : _d[refName]);
|
|
102
104
|
if (refSchema) {
|
|
103
105
|
refSchema.required = [...refSchema.required, ...definitionRequired];
|
|
104
106
|
fields.push(...makeFieldsBody(refSchema, depth));
|
|
105
107
|
}
|
|
106
108
|
}
|
|
107
109
|
else if ('type' in subSchema && subSchema.type === 'object') {
|
|
108
|
-
subSchema.required = [...(subSchema.required
|
|
110
|
+
subSchema.required = [...((_e = subSchema.required) !== null && _e !== void 0 ? _e : []), ...definitionRequired];
|
|
109
111
|
fields.push(...makeFieldsBody(subSchema, depth));
|
|
110
112
|
}
|
|
111
113
|
else {
|
|
@@ -147,8 +149,9 @@ function makeField(fieldName, property, isRequired, depth) {
|
|
|
147
149
|
* @returns A string representing the form array field.
|
|
148
150
|
*/
|
|
149
151
|
function makeArrayField(fieldName, property, isRequired, depth) {
|
|
152
|
+
var _a;
|
|
150
153
|
const itemDefinition = property.items;
|
|
151
|
-
const minItems = property['minItems']
|
|
154
|
+
const minItems = (_a = property['minItems']) !== null && _a !== void 0 ? _a : 1;
|
|
152
155
|
const items = [];
|
|
153
156
|
if (itemDefinition['type'] === 'object') {
|
|
154
157
|
for (let i = 0; i <= minItems; i++) {
|
|
@@ -224,7 +227,8 @@ export function resetRules() {
|
|
|
224
227
|
* @returns A string representing the file name or undefined if no title is available.
|
|
225
228
|
*/
|
|
226
229
|
export function makeFileName(swagger) {
|
|
227
|
-
|
|
230
|
+
var _a;
|
|
231
|
+
if ((_a = swagger.info) === null || _a === void 0 ? void 0 : _a.title) {
|
|
228
232
|
return `${camelcase(swagger.info.title)}.ts`;
|
|
229
233
|
}
|
|
230
234
|
}
|
|
@@ -234,6 +238,5 @@ export function makeFileName(swagger) {
|
|
|
234
238
|
* @returns A promise that resolves to the dereferenced OpenAPI document.
|
|
235
239
|
*/
|
|
236
240
|
export async function loadSpec(fileOrUrlPath) {
|
|
237
|
-
|
|
238
|
-
return parser.dereference(fileOrUrlPath);
|
|
241
|
+
return SwaggerParser.dereference(fileOrUrlPath);
|
|
239
242
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * from './
|
|
2
|
-
export * from './
|
|
3
|
-
export * from './
|
|
1
|
+
export * from './file-utils.js';
|
|
2
|
+
export * from './generator-lib.js';
|
|
3
|
+
export * from './rules.js';
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * from './
|
|
2
|
-
export * from './
|
|
3
|
-
export * from './
|
|
1
|
+
export * from './file-utils.js';
|
|
2
|
+
export * from './generator-lib.js';
|
|
3
|
+
export * from './rules.js';
|
package/dist/rules.js
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jmm-devkit/ngx-form-generator",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.1",
|
|
4
4
|
"description": "Generates an Angular ReactiveForm from a Swagger or OpenAPI definition",
|
|
5
|
+
"type": "module",
|
|
5
6
|
"main": "dist/generator-lib.js",
|
|
6
7
|
"repository": "github:jmm-devkit/ngx-form-generator",
|
|
7
8
|
"bin": {
|
|
@@ -33,8 +34,10 @@
|
|
|
33
34
|
},
|
|
34
35
|
"dependencies": {
|
|
35
36
|
"@apidevtools/swagger-parser": "^12.1.0",
|
|
37
|
+
"@types/yargs": "^17.0.35",
|
|
36
38
|
"camelcase": "^9.0.0",
|
|
37
39
|
"openapi-types": "^12.1.3",
|
|
38
|
-
"prettier": "^3.8.1"
|
|
40
|
+
"prettier": "^3.8.1",
|
|
41
|
+
"yargs": "^18.0.0"
|
|
39
42
|
}
|
|
40
43
|
}
|