ondc-code-generator 0.2.1 → 0.2.3

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
+ }
@@ -30,6 +30,7 @@ if(invalidResults.length > 0) {
30
30
  const validate = {{{returnStatement}}}
31
31
 
32
32
  if(!validate){
33
+ delete testObj._EXTERNAL;
33
34
  return [{
34
35
  valid: false,
35
36
  code: {{errorCode}},
@@ -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: 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
@@ -1,23 +1,20 @@
1
1
  import { ConfigCompiler } from "./generator/config-compiler.js";
2
2
  export { ConfigCompiler };
3
- // import { readFileSync } from "fs";
4
- // import path from "path";
5
- // import { fileURLToPath } from "url";
6
- // const __filename = fileURLToPath(import.meta.url);
7
- // const __dirname = path.dirname(__filename);
8
- // import { SupportedLanguages } from "./types/compiler-types.js";
9
- // const main = async () => {
10
- // const compiler = new ConfigCompiler(SupportedLanguages.Typescript);
11
- // const buildPath = path.resolve(__dirname, "../samples/build.yaml");
12
- // const valConfigPath = path.resolve(
13
- // __dirname,
14
- // "../samples/validation-config.json"
15
- // );
16
- // const buildYaml = readFileSync(buildPath, "utf-8");
17
- // const valConfig = JSON.parse(readFileSync(valConfigPath, "utf-8"));
18
- // await compiler.initialize(buildYaml);
19
- // await compiler.generateCode(valConfig);
20
- // };
21
- // (async () => {
22
- // await main();
23
- // })();
3
+ import { readFileSync } from "fs";
4
+ import path from "path";
5
+ import { fileURLToPath } from "url";
6
+ const __filename = fileURLToPath(import.meta.url);
7
+ const __dirname = path.dirname(__filename);
8
+ import { SupportedLanguages } from "./types/compiler-types.js";
9
+ const main = async () => {
10
+ const compiler = new ConfigCompiler(SupportedLanguages.Typescript);
11
+ const buildPath = path.resolve(__dirname, "../samples/build.yaml");
12
+ const valConfigPath = path.resolve(__dirname, "../samples/validation-config.json");
13
+ const buildYaml = readFileSync(buildPath, "utf-8");
14
+ const valConfig = JSON.parse(readFileSync(valConfigPath, "utf-8"));
15
+ await compiler.initialize(buildYaml);
16
+ await compiler.generateCode(valConfig, "L1-validations");
17
+ };
18
+ (async () => {
19
+ await main();
20
+ })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ondc-code-generator",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "description": "generate code from build.yaml ",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",