@jmm-devkit/ngx-form-generator 1.3.0 → 1.3.2

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.
@@ -43,11 +43,11 @@ function makeForm(spec, maxDepth) {
43
43
  throw new Error('Cannot find schemas/definitions');
44
44
  }
45
45
  let body = NEEDED_IMPORTS + '\n\n';
46
- Object.keys(definitions).forEach(key => {
47
- if (definitions === null || definitions === void 0 ? void 0 : definitions[key]) {
48
- body += makeDefinition(key, definitions[key]);
46
+ for (const [key, value] of Object.entries(definitions)) {
47
+ if (value) {
48
+ body += makeDefinition(key, value);
49
49
  }
50
- });
50
+ }
51
51
  return prettier_1.default.format(body, { parser: 'typescript', singleQuote: true });
52
52
  }
53
53
  exports.makeForm = makeForm;
@@ -59,7 +59,7 @@ exports.makeForm = makeForm;
59
59
  */
60
60
  function makeDefinition(definitionName, definition) {
61
61
  return `export const ${(0, camelcase_1.default)(definitionName)}Form = () => new FormGroup({
62
- ${makeFieldsBody(definition, 0)}
62
+ ${makeFieldsBody(Object.assign({}, definition), 0)}
63
63
  });\n`;
64
64
  }
65
65
  /**
@@ -69,38 +69,58 @@ function makeDefinition(definitionName, definition) {
69
69
  * @returns An array of strings representing the form fields.
70
70
  */
71
71
  function makeFieldsBody(definition, depth) {
72
- const fields = [];
73
72
  if (depth >= MAX_DEPTH)
74
- return fields;
73
+ return [];
75
74
  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
- });
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
+ }
86
93
  }
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));
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, _d, _e;
104
+ if (!('allOf' in definition) || !Array.isArray(definition.allOf))
105
+ return [];
106
+ const fields = [];
107
+ const definitionRequired = (_a = definition.required) !== null && _a !== void 0 ? _a : [];
108
+ for (const subSchema of definition.allOf) {
109
+ if ('$ref' in subSchema) {
110
+ const refName = subSchema.$ref.split('/').pop();
111
+ 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]);
112
+ if (refSchema) {
113
+ refSchema.required = [...refSchema.required, ...definitionRequired];
114
+ fields.push(...makeFieldsBody(refSchema, depth));
102
115
  }
103
- });
116
+ }
117
+ else if ('type' in subSchema && subSchema.type === 'object') {
118
+ subSchema.required = [...((_e = subSchema.required) !== null && _e !== void 0 ? _e : []), ...definitionRequired];
119
+ fields.push(...makeFieldsBody(subSchema, depth));
120
+ }
121
+ else {
122
+ fields.push(...makeFieldsBody(subSchema, depth));
123
+ }
104
124
  }
105
125
  return fields;
106
126
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jmm-devkit/ngx-form-generator",
3
- "version": "1.3.0",
3
+ "version": "1.3.2",
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",