ondc-code-generator 0.4.5 → 0.4.6
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.
|
@@ -1,36 +1,101 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
1
|
+
type JSONObject = Record<string, any>;
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Normalize keys so that:
|
|
5
|
+
* - All objects with the same property name (e.g., "price") share the union of keys seen anywhere.
|
|
6
|
+
* - All objects inside the same array share the union of keys at that array level.
|
|
7
|
+
* Missing keys are filled with `null`.
|
|
8
|
+
*/
|
|
9
|
+
export default function normalizeKeys(input: any): any {
|
|
10
|
+
// 1) Collect templates by property name: e.g., "price" -> Set<"currency" | "value" | ...>
|
|
11
|
+
const templatesByPropName: Record<string, Set<string>> = Object.create(null);
|
|
12
|
+
|
|
13
|
+
const collectTemplates = (node: any): void => {
|
|
14
|
+
if (Array.isArray(node)) {
|
|
15
|
+
// Recurse into array items
|
|
16
|
+
node.forEach(collectTemplates);
|
|
17
|
+
|
|
18
|
+
// Also collect a per-array union (used later when applying)
|
|
19
|
+
// We won't store this globally; we'll recompute per-array on apply.
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
if (node && typeof node === "object") {
|
|
23
|
+
// For each property: if it's an object (non-array), record its keys under the property name
|
|
24
|
+
for (const [k, v] of Object.entries(node)) {
|
|
25
|
+
if (v && typeof v === "object" && !Array.isArray(v)) {
|
|
26
|
+
const set = (templatesByPropName[k] ||= new Set<string>());
|
|
27
|
+
Object.keys(v).forEach((childKey) => set.add(childKey));
|
|
28
|
+
}
|
|
29
|
+
collectTemplates(v);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
// 2) Apply templates and within-array unions
|
|
35
|
+
const applyTemplates = (node: any): any => {
|
|
36
|
+
if (Array.isArray(node)) {
|
|
37
|
+
// Compute union of keys across all object elements for this array level
|
|
38
|
+
const arrayUnion = new Set<string>();
|
|
39
|
+
for (const item of node) {
|
|
40
|
+
if (item && typeof item === "object" && !Array.isArray(item)) {
|
|
41
|
+
Object.keys(item).forEach((k) => arrayUnion.add(k));
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return node.map((item) => {
|
|
46
|
+
if (item && typeof item === "object" && !Array.isArray(item)) {
|
|
47
|
+
// Ensure array-level union keys
|
|
48
|
+
const next: JSONObject = { ...item };
|
|
49
|
+
arrayUnion.forEach((k) => {
|
|
50
|
+
if (!(k in next)) next[k] = null;
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// Now apply templates per property name for nested objects
|
|
54
|
+
for (const [k, v] of Object.entries(next)) {
|
|
55
|
+
// If this property holds an object, align it to the template for that property name
|
|
56
|
+
if (v && typeof v === "object" && !Array.isArray(v)) {
|
|
57
|
+
next[k] = fillFromTemplate(k, v);
|
|
58
|
+
} else {
|
|
59
|
+
next[k] = applyTemplates(v);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return next;
|
|
63
|
+
}
|
|
64
|
+
return applyTemplates(item);
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (node && typeof node === "object") {
|
|
69
|
+
const out: JSONObject = {};
|
|
70
|
+
for (const [k, v] of Object.entries(node)) {
|
|
71
|
+
if (v && typeof v === "object" && !Array.isArray(v)) {
|
|
72
|
+
// Align object to the template for this property name (k)
|
|
73
|
+
out[k] = fillFromTemplate(k, v);
|
|
74
|
+
} else {
|
|
75
|
+
out[k] = applyTemplates(v);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return out;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return node; // primitives unchanged
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
// Helper: apply the template for a given property name `prop`
|
|
85
|
+
const fillFromTemplate = (prop: string, obj: any): any => {
|
|
86
|
+
const templ = templatesByPropName[prop];
|
|
87
|
+
// Recurse first on children so nested arrays/objects also normalize
|
|
88
|
+
const base: JSONObject = applyTemplates(obj);
|
|
89
|
+
if (!templ) return base; // no known template keys for this prop
|
|
90
|
+
|
|
91
|
+
const filled: JSONObject = { ...base };
|
|
92
|
+
templ.forEach((k) => {
|
|
93
|
+
if (!(k in filled)) filled[k] = null;
|
|
94
|
+
});
|
|
95
|
+
return filled;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
// Run passes
|
|
99
|
+
collectTemplates(input);
|
|
100
|
+
return applyTemplates(input);
|
|
101
|
+
}
|
|
@@ -163,7 +163,7 @@ export class TypescriptGenerator extends CodeGenerator {
|
|
|
163
163
|
.join("\n");
|
|
164
164
|
const masterFunction = `
|
|
165
165
|
export function perform${functionName}(action: string, payload: any,allErrors = false, externalData : any = {}) {
|
|
166
|
-
const normalizedPayload = normalizeKeys(payload);
|
|
166
|
+
const normalizedPayload = normalizeKeys(JSON.parse(JSON.stringify(payload)));
|
|
167
167
|
externalData._SELF = normalizedPayload;
|
|
168
168
|
switch (action) {
|
|
169
169
|
${apis
|