ondc-code-generator 0.2.2 → 0.2.4
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.
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export default function normalizeKeys(obj: any) {
|
|
2
|
+
if (Array.isArray(obj)) {
|
|
3
|
+
// Find all keys across all objects in the array
|
|
4
|
+
const allKeys = new Set();
|
|
5
|
+
obj.forEach((item) => {
|
|
6
|
+
if (typeof item === "object" && item !== null) {
|
|
7
|
+
Object.keys(item).forEach((key) => allKeys.add(key));
|
|
8
|
+
}
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
// Add missing keys with null
|
|
12
|
+
return obj.map((item) => {
|
|
13
|
+
if (typeof item === "object" && item !== null) {
|
|
14
|
+
const newItem = { ...item };
|
|
15
|
+
allKeys.forEach((key: any) => {
|
|
16
|
+
if (!(key in newItem)) {
|
|
17
|
+
newItem[key] = null;
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
// Recursively normalize nested objects/arrays
|
|
21
|
+
for (const k in newItem) {
|
|
22
|
+
newItem[k] = normalizeKeys(newItem[k]);
|
|
23
|
+
}
|
|
24
|
+
return newItem;
|
|
25
|
+
}
|
|
26
|
+
return item;
|
|
27
|
+
});
|
|
28
|
+
} else if (typeof obj === "object" && obj !== null) {
|
|
29
|
+
const newObj: any = {};
|
|
30
|
+
for (const key in obj) {
|
|
31
|
+
newObj[key] = normalizeKeys(obj[key]);
|
|
32
|
+
}
|
|
33
|
+
return newObj;
|
|
34
|
+
}
|
|
35
|
+
return obj; // primitive values
|
|
36
|
+
}
|
|
@@ -38,10 +38,12 @@ export class TypescriptGenerator extends CodeGenerator {
|
|
|
38
38
|
const jsonPathUtilsCode = readFileSync(path.resolve(__dirname, "./templates/json-path-utils.mustache"), "utf-8");
|
|
39
39
|
const validtionUtils = readFileSync(path.resolve(__dirname, "./templates/validation-utils.mustache"), "utf-8");
|
|
40
40
|
const typesTemplate = readFileSync(path.resolve(__dirname, "./templates/test-config.mustache"), "utf-8");
|
|
41
|
+
const normalizerTemplate = readFileSync(path.resolve(__dirname, "./templates/json-normalizer.mustache"), "utf-8");
|
|
41
42
|
const typesCode = Mustache.render(typesTemplate, {
|
|
42
43
|
externalData: this.getExternalKeys(),
|
|
43
44
|
});
|
|
44
45
|
writeAndFormatCode(this.rootPath, "./utils/json-path-utils.ts", jsonPathUtilsCode, "typescript");
|
|
46
|
+
writeAndFormatCode(this.rootPath, "./utils/json-normalizer.ts", normalizerTemplate, "typescript");
|
|
45
47
|
writeAndFormatCode(this.rootPath, "./utils/validation-utils.ts", validtionUtils, "typescript");
|
|
46
48
|
writeAndFormatCode(this.rootPath, "./types/test-config.ts", typesCode, "typescript");
|
|
47
49
|
await this.generateValidationCode();
|
|
@@ -161,10 +163,11 @@ export class TypescriptGenerator extends CodeGenerator {
|
|
|
161
163
|
.join("\n");
|
|
162
164
|
const masterFunction = `
|
|
163
165
|
export function perform${functionName}(action: string, payload: any,allErrors = false, externalData = {}) {
|
|
166
|
+
const normalizedPayload = normalizeKeys(payload);
|
|
164
167
|
switch (action) {
|
|
165
168
|
${apis
|
|
166
169
|
.map((api) => `case "${api}": return ${api}({
|
|
167
|
-
payload:
|
|
170
|
+
payload: normalizedPayload,
|
|
168
171
|
externalData: externalData,
|
|
169
172
|
config: {
|
|
170
173
|
runAllValidations: allErrors,
|
|
@@ -175,7 +178,8 @@ export class TypescriptGenerator extends CodeGenerator {
|
|
|
175
178
|
throw new Error("Action not found");
|
|
176
179
|
}
|
|
177
180
|
}`;
|
|
178
|
-
return `
|
|
181
|
+
return `
|
|
182
|
+
import normalizeKeys from "./utils/json-normalizer";
|
|
179
183
|
${importsCode}
|
|
180
184
|
${masterFunction}
|
|
181
185
|
`;
|
package/dist/index.js
CHANGED
|
@@ -16,7 +16,7 @@ export { ConfigCompiler };
|
|
|
16
16
|
// const buildYaml = readFileSync(buildPath, "utf-8");
|
|
17
17
|
// const valConfig = JSON.parse(readFileSync(valConfigPath, "utf-8"));
|
|
18
18
|
// await compiler.initialize(buildYaml);
|
|
19
|
-
// await compiler.generateCode(valConfig);
|
|
19
|
+
// await compiler.generateCode(valConfig, "L1-validations");
|
|
20
20
|
// };
|
|
21
21
|
// (async () => {
|
|
22
22
|
// await main();
|