ondc-code-generator 0.0.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.
Files changed (139) hide show
  1. package/.idea/code-generator.iml +12 -0
  2. package/.idea/modules.xml +8 -0
  3. package/.idea/vcs.xml +6 -0
  4. package/LICENSE +21 -0
  5. package/README.md +0 -0
  6. package/custom-loader.js +4 -0
  7. package/dist/Generator/config-compiler.js +53 -0
  8. package/dist/Generator/config-validator.js +21 -0
  9. package/dist/Generator/generators/classes/abstract-generator.js +16 -0
  10. package/dist/Generator/generators/documentation/markdown-message-generator.js +37 -0
  11. package/dist/Generator/generators/documentation/md-generator.js +57 -0
  12. package/dist/Generator/generators/markdown-message-generator.js +25 -0
  13. package/dist/Generator/generators/python/py-generator.js +1 -0
  14. package/dist/Generator/generators/typescript/templates/json-path-utils.js +15 -0
  15. package/dist/Generator/generators/typescript/templates/validation-utils.js +91 -0
  16. package/dist/Generator/generators/typescript/ts-ast.js +50 -0
  17. package/dist/Generator/generators/typescript/ts-generator.js +184 -0
  18. package/dist/Generator/pipline.js +1 -0
  19. package/dist/Generator/validators/abstract-validator.js +6 -0
  20. package/dist/Generator/validators/config-validator.js +32 -0
  21. package/dist/Generator/validators/session-data-config/session-data-validator.js +35 -0
  22. package/dist/Generator/validators/tests-config/sub-validations.js +194 -0
  23. package/dist/Generator/validators/tests-config/test-list-validator.js +36 -0
  24. package/dist/Generator/validators/tests-config/test-validator.js +24 -0
  25. package/dist/Generator/validators/validation-error.js +9 -0
  26. package/dist/constants/operations.js +17 -0
  27. package/dist/constants/syntax.js +75 -0
  28. package/dist/index.js +39 -0
  29. package/dist/services/rename-later/main.js +1 -0
  30. package/dist/services/rename-later/parser.js +22 -0
  31. package/dist/services/rename-later/tokens.js +32 -0
  32. package/dist/services/rename-later/tokens1.js +33 -0
  33. package/dist/services/return-complier/ast-functions/compile-to-markdown.js +66 -0
  34. package/dist/services/return-complier/ast-functions/semantic-validations.js +30 -0
  35. package/dist/services/return-complier/ast.js +92 -0
  36. package/dist/services/return-complier/combined.js +7 -0
  37. package/dist/services/return-complier/parser.js +95 -0
  38. package/dist/services/return-complier/tokens.js +144 -0
  39. package/dist/services/schema-service.js +29 -0
  40. package/dist/types/build.js +1 -0
  41. package/dist/types/compiler-types.js +4 -0
  42. package/dist/types/config-types.js +1 -0
  43. package/dist/types/error-codes.js +1 -0
  44. package/dist/types/general-types.js +1 -0
  45. package/dist/utils/config-utils/json-schema-utils.js +91 -0
  46. package/dist/utils/config-utils/yaml.js +16 -0
  47. package/dist/utils/file-system.js +1 -0
  48. package/dist/utils/fs-utils.js +20 -0
  49. package/dist/utils/general-utils/string-utils.js +56 -0
  50. package/dist/utils/general-utils/test-object-utils.js +12 -0
  51. package/dist/utils/general-utils/validation-utils.js +21 -0
  52. package/dist/utils/json-path-utils/extract-string-paths.js +113 -0
  53. package/dist/utils/json-path-utils/paths.js +46 -0
  54. package/dist/utils/logger.js +41 -0
  55. package/docs/error-gen.md +33 -0
  56. package/docs/return-grammer.md +23 -0
  57. package/docs/sampleConfig.md +39 -0
  58. package/generated/L1-validations/api-tests/cancel.ts +569 -0
  59. package/generated/L1-validations/api-tests/confirm.ts +1162 -0
  60. package/generated/L1-validations/api-tests/init.ts +1063 -0
  61. package/generated/L1-validations/api-tests/on_cancel.ts +2069 -0
  62. package/generated/L1-validations/api-tests/on_confirm.ts +2219 -0
  63. package/generated/L1-validations/api-tests/on_init.ts +1949 -0
  64. package/generated/L1-validations/api-tests/on_search.ts +1574 -0
  65. package/generated/L1-validations/api-tests/on_select.ts +1723 -0
  66. package/generated/L1-validations/api-tests/on_status.ts +2221 -0
  67. package/generated/L1-validations/api-tests/on_update.ts +1969 -0
  68. package/generated/L1-validations/api-tests/search.ts +695 -0
  69. package/generated/L1-validations/api-tests/select.ts +994 -0
  70. package/generated/L1-validations/api-tests/status.ts +443 -0
  71. package/generated/L1-validations/api-tests/update.ts +898 -0
  72. package/generated/L1-validations/error.ts +64 -0
  73. package/generated/L1-validations/index.ts +138 -0
  74. package/generated/L1-validations/page/index.html +2118 -0
  75. package/generated/L1-validations/page/style.css +225 -0
  76. package/generated/L1-validations/readme.md +1779 -0
  77. package/generated/L1-validations/types/test-config.ts +27 -0
  78. package/generated/L1-validations/utils/json-path-utils.ts +17 -0
  79. package/generated/L1-validations/utils/validation-utils.ts +116 -0
  80. package/generated-structure/api-tests/search.ts +24 -0
  81. package/generated-structure/error.ts +0 -0
  82. package/generated-structure/index.ts +0 -0
  83. package/generated-structure/types/test-config.ts +21 -0
  84. package/nodemon.json +5 -0
  85. package/package.json +40 -0
  86. package/samples/build.yaml +24799 -0
  87. package/samples/output.md +91 -0
  88. package/samples/output.ts +27 -0
  89. package/samples/selections.json +216 -0
  90. package/samples/validation-config.json +3422 -0
  91. package/samples/x-validations.yaml +2893 -0
  92. package/src/constants/operations.ts +19 -0
  93. package/src/constants/syntax.ts +81 -0
  94. package/src/example.ts +25 -0
  95. package/src/generator/config-compiler.ts +122 -0
  96. package/src/generator/generators/classes/abstract-generator.ts +29 -0
  97. package/src/generator/generators/documentation/markdown-message-generator.ts +43 -0
  98. package/src/generator/generators/documentation/md-generator.ts +76 -0
  99. package/src/generator/generators/documentation/templates/index.mustache +36 -0
  100. package/src/generator/generators/documentation/templates/style.css +204 -0
  101. package/src/generator/generators/python/py-generator.ts +0 -0
  102. package/src/generator/generators/typescript/templates/api-test.mustache +7 -0
  103. package/src/generator/generators/typescript/templates/json-path-utils.ts +17 -0
  104. package/src/generator/generators/typescript/templates/schema-template.mustache +18 -0
  105. package/src/generator/generators/typescript/templates/test-config.mustache +28 -0
  106. package/src/generator/generators/typescript/templates/test-object.mustache +20 -0
  107. package/src/generator/generators/typescript/templates/validation-code.mustache +39 -0
  108. package/src/generator/generators/typescript/templates/validation-utils.ts +117 -0
  109. package/src/generator/generators/typescript/ts-ast.ts +72 -0
  110. package/src/generator/generators/typescript/ts-generator.ts +275 -0
  111. package/src/generator/validators/abstract-validator.ts +23 -0
  112. package/src/generator/validators/config-validator.ts +55 -0
  113. package/src/generator/validators/session-data-config/session-data-validator.ts +58 -0
  114. package/src/generator/validators/tests-config/sub-validations.ts +302 -0
  115. package/src/generator/validators/tests-config/test-list-validator.ts +59 -0
  116. package/src/generator/validators/tests-config/test-validator.ts +69 -0
  117. package/src/index.ts +2 -0
  118. package/src/services/return-complier/ast-functions/compile-to-markdown.ts +152 -0
  119. package/src/services/return-complier/ast-functions/semantic-validations.ts +44 -0
  120. package/src/services/return-complier/ast.ts +147 -0
  121. package/src/services/return-complier/combined.ts +8 -0
  122. package/src/services/return-complier/parser.ts +128 -0
  123. package/src/services/return-complier/tokens.ts +184 -0
  124. package/src/services/schema-service.ts +42 -0
  125. package/src/types/build.ts +51 -0
  126. package/src/types/compiler-types.ts +3 -0
  127. package/src/types/config-types.ts +27 -0
  128. package/src/types/error-codes.ts +6 -0
  129. package/src/types/general-types.ts +2 -0
  130. package/src/utils/config-utils/json-schema-utils.ts +150 -0
  131. package/src/utils/config-utils/yaml.ts +17 -0
  132. package/src/utils/fs-utils.ts +32 -0
  133. package/src/utils/general-utils/string-utils.ts +76 -0
  134. package/src/utils/general-utils/test-object-utils.ts +14 -0
  135. package/src/utils/general-utils/validation-utils.ts +30 -0
  136. package/src/utils/json-path-utils/extract-string-paths.ts +139 -0
  137. package/src/utils/json-path-utils/paths.ts +44 -0
  138. package/src/utils/logger.ts +53 -0
  139. package/tsconfig.json +17 -0
@@ -0,0 +1,92 @@
1
+ import { ReturnParserInstance } from "./parser.js";
2
+ export class AstBuilder extends ReturnParserInstance.getBaseCstVisitorConstructor() {
3
+ constructor() {
4
+ super();
5
+ this.validateVisitor();
6
+ }
7
+ returnStatement(ctx) {
8
+ return {
9
+ type: "returnStatement",
10
+ expression: this.visit(ctx.orExpression),
11
+ };
12
+ }
13
+ orExpression(ctx) {
14
+ let left = this.visit(ctx.lhs);
15
+ if (!ctx.operator)
16
+ return left;
17
+ if (ctx.operator) {
18
+ for (let i = 0; i < ctx.operator.length; i++) {
19
+ left = {
20
+ type: "binaryOperator",
21
+ operator: ctx.operator[i].image,
22
+ lhs: left,
23
+ rhs: this.visit(ctx.rhs[i]),
24
+ };
25
+ }
26
+ }
27
+ return left;
28
+ }
29
+ andExpression(ctx) {
30
+ let left = this.visit(ctx.lhs);
31
+ if (!ctx.operator)
32
+ return left;
33
+ if (ctx.operator) {
34
+ for (let i = 0; i < ctx.operator.length; i++) {
35
+ left = {
36
+ type: "binaryOperator",
37
+ operator: ctx.operator[i].image,
38
+ lhs: left,
39
+ rhs: this.visit(ctx.rhs[i]),
40
+ };
41
+ }
42
+ }
43
+ return left;
44
+ }
45
+ notExpression(ctx) {
46
+ if (ctx.operator) {
47
+ return {
48
+ type: "notOperator",
49
+ operator: ctx.operator[0].image,
50
+ expression: this.visit(ctx.parentheisizedExpression),
51
+ };
52
+ }
53
+ return this.visit(ctx.primaryExpression);
54
+ }
55
+ primaryExpression(ctx) {
56
+ if (ctx.parentheisizedExpression) {
57
+ return this.visit(ctx.parentheisizedExpression);
58
+ }
59
+ if (ctx.atomicCustomExpression) {
60
+ return this.visit(ctx.atomicCustomExpression);
61
+ }
62
+ throw new Error("Invalid primary expression");
63
+ }
64
+ parentheisizedExpression(ctx) {
65
+ return this.visit(ctx.returnStatement);
66
+ }
67
+ atomicCustomExpression(ctx) {
68
+ if (ctx.rhs) {
69
+ return {
70
+ type: "customBinaryFunction",
71
+ customFunction: ctx.function[0].image,
72
+ lhs: this.visitIdentifier(ctx.lhs[0]),
73
+ rhs: this.visitIdentifier(ctx.rhs[0]),
74
+ };
75
+ }
76
+ return {
77
+ type: "customUniaryFunction",
78
+ customFunction: ctx.function[0].image,
79
+ expression: this.visitIdentifier(ctx.lhs[0]),
80
+ };
81
+ }
82
+ visitIdentifier(token) {
83
+ return {
84
+ type: "identifier",
85
+ name: token.image,
86
+ };
87
+ }
88
+ }
89
+ export function buildAst(cst) {
90
+ const astBuilder = new AstBuilder();
91
+ return astBuilder.visit(cst);
92
+ }
@@ -0,0 +1,7 @@
1
+ import { buildAst } from "./ast.js";
2
+ import { parseReturnInput } from "./parser.js";
3
+ export function buildAstFromInput(input) {
4
+ const cst = parseReturnInput(input);
5
+ const ast = buildAst(cst);
6
+ return ast;
7
+ }
@@ -0,0 +1,95 @@
1
+ import { CstParser } from "chevrotain";
2
+ import { allTokens, AndOperator, CustomBinaryFunctions, CustomUniaryFunctions, Identifier, LParen, NotOperator, OrOperator, ReturnTokenize, RParen, } from "./tokens.js";
3
+ class ReturnParser extends CstParser {
4
+ constructor() {
5
+ super(allTokens);
6
+ const $ = this;
7
+ $.RULE("returnStatement", () => {
8
+ $.SUBRULE($.orExpression);
9
+ });
10
+ $.RULE("orExpression", () => {
11
+ $.SUBRULE($.andExpression, { LABEL: "lhs" });
12
+ $.MANY(() => {
13
+ $.CONSUME(OrOperator, { LABEL: "operator" });
14
+ $.SUBRULE2($.andExpression, { LABEL: "rhs" });
15
+ });
16
+ });
17
+ $.RULE("andExpression", () => {
18
+ $.SUBRULE($.notExpression, { LABEL: "lhs" });
19
+ $.MANY(() => {
20
+ $.CONSUME(AndOperator, { LABEL: "operator" });
21
+ $.SUBRULE2($.notExpression, { LABEL: "rhs" });
22
+ });
23
+ });
24
+ $.notExpression = $.RULE("notExpression", () => {
25
+ $.OR([
26
+ {
27
+ ALT: () => {
28
+ $.CONSUME(NotOperator, { LABEL: "operator" });
29
+ $.SUBRULE($.parentheisizedExpression);
30
+ },
31
+ },
32
+ {
33
+ ALT: () => {
34
+ $.SUBRULE($.primaryExpression);
35
+ },
36
+ },
37
+ ]);
38
+ });
39
+ $.RULE("primaryExpression", () => {
40
+ $.OR([
41
+ {
42
+ ALT: () => {
43
+ $.SUBRULE($.parentheisizedExpression);
44
+ },
45
+ },
46
+ {
47
+ ALT: () => {
48
+ $.SUBRULE($.atomicCustomExpression);
49
+ },
50
+ },
51
+ ]);
52
+ });
53
+ $.RULE("parentheisizedExpression", () => {
54
+ $.CONSUME(LParen);
55
+ $.SUBRULE($.returnStatement);
56
+ $.CONSUME(RParen);
57
+ });
58
+ $.RULE("atomicCustomExpression", () => {
59
+ $.OR([
60
+ {
61
+ ALT: () => {
62
+ $.CONSUME1(Identifier, { LABEL: "lhs" });
63
+ $.CONSUME(CustomBinaryFunctions, { LABEL: "function" });
64
+ $.CONSUME2(Identifier, { LABEL: "rhs" });
65
+ },
66
+ },
67
+ {
68
+ ALT: () => {
69
+ $.CONSUME3(Identifier, { LABEL: "lhs" });
70
+ $.CONSUME(CustomUniaryFunctions, { LABEL: "function" });
71
+ },
72
+ },
73
+ ]);
74
+ });
75
+ $.performSelfAnalysis();
76
+ }
77
+ }
78
+ export const ReturnParserInstance = new ReturnParser();
79
+ export function parseReturnInput(inputText) {
80
+ // Step 1: Tokenize the input
81
+ const lexResult = ReturnTokenize(inputText);
82
+ // Step 3: Set the parser's input
83
+ ReturnParserInstance.input = lexResult.tokens;
84
+ // Step 4: Parse the input starting from the 'returnStatement' rule
85
+ const cst = ReturnParserInstance.returnStatement();
86
+ // Step 5: Check for parsing errors
87
+ if (ReturnParserInstance.errors.length > 0) {
88
+ const parseErrors = ReturnParserInstance.errors
89
+ .map((error) => error.message)
90
+ .join("\n");
91
+ throw new Error(`Parsing errors detected:\n${parseErrors}`);
92
+ }
93
+ // If no errors, return the CST
94
+ return cst;
95
+ }
@@ -0,0 +1,144 @@
1
+ import { createToken, Lexer, } from "chevrotain";
2
+ export const Identifier = createToken({
3
+ name: "Identifier",
4
+ pattern: /[a-zA-Z_][a-zA-Z0-9_]*/,
5
+ });
6
+ export const CustomBinaryFunctions = createToken({
7
+ name: "CustomFunctions",
8
+ pattern: Lexer.NA,
9
+ });
10
+ export const CustomUniaryFunctions = createToken({
11
+ name: "CustomUniaryFunctions",
12
+ pattern: Lexer.NA,
13
+ });
14
+ export const AllIn = createToken({
15
+ name: "AllIn",
16
+ pattern: /all in/i,
17
+ categories: CustomBinaryFunctions,
18
+ label: "all in",
19
+ });
20
+ export const AnyIn = createToken({
21
+ name: "AnyIn",
22
+ pattern: /any in/i,
23
+ categories: CustomBinaryFunctions,
24
+ label: "any in",
25
+ });
26
+ export const AreUnique = createToken({
27
+ name: "AreUnique",
28
+ pattern: /are unique/i,
29
+ categories: CustomUniaryFunctions,
30
+ label: "are unique",
31
+ });
32
+ export const ArePresent = createToken({
33
+ name: "ArePresent",
34
+ pattern: /are present/i,
35
+ categories: CustomUniaryFunctions,
36
+ label: "are present",
37
+ });
38
+ export const FollowRegex = createToken({
39
+ name: "FollowRegex",
40
+ pattern: /follow regex/i,
41
+ categories: CustomBinaryFunctions,
42
+ label: "follow regex",
43
+ });
44
+ export const NoneIn = createToken({
45
+ name: "NoneIn",
46
+ pattern: /none in/i,
47
+ categories: CustomBinaryFunctions,
48
+ label: "none in",
49
+ });
50
+ export const EqualTo = createToken({
51
+ name: "EqualTo",
52
+ pattern: /equal to/i,
53
+ label: "equal to",
54
+ });
55
+ export const GreaterThan = createToken({
56
+ name: "GreaterThan",
57
+ pattern: /greater than/i,
58
+ label: "greater than",
59
+ });
60
+ export const LessThan = createToken({
61
+ name: "LessThan",
62
+ pattern: /less than/i,
63
+ label: "less than",
64
+ });
65
+ export const WhiteSpace = createToken({
66
+ name: "WhiteSpace",
67
+ pattern: /\s+/,
68
+ group: Lexer.SKIPPED,
69
+ });
70
+ export const LParen = createToken({
71
+ name: "LParen",
72
+ pattern: /\(/,
73
+ label: "(",
74
+ });
75
+ export const RParen = createToken({
76
+ name: "RParen",
77
+ pattern: /\)/,
78
+ label: ")",
79
+ });
80
+ export const BinaryOperator = createToken({
81
+ name: "BinaryOperator",
82
+ pattern: Lexer.NA,
83
+ });
84
+ export const AndOperator = createToken({
85
+ name: "AndOperator",
86
+ pattern: /&&/,
87
+ categories: BinaryOperator,
88
+ label: "&&",
89
+ });
90
+ export const OrOperator = createToken({
91
+ name: "OrOperator",
92
+ pattern: /\|\|/,
93
+ categories: BinaryOperator,
94
+ label: "||",
95
+ });
96
+ export const NotOperator = createToken({
97
+ name: "NotOperator",
98
+ pattern: /!/,
99
+ label: "!",
100
+ });
101
+ export const allTokens = [
102
+ WhiteSpace,
103
+ LParen,
104
+ RParen,
105
+ AndOperator,
106
+ OrOperator,
107
+ NotOperator,
108
+ AreUnique,
109
+ ArePresent,
110
+ AllIn,
111
+ AnyIn,
112
+ EqualTo,
113
+ GreaterThan,
114
+ LessThan,
115
+ FollowRegex,
116
+ NoneIn,
117
+ CustomUniaryFunctions,
118
+ CustomBinaryFunctions,
119
+ BinaryOperator,
120
+ Identifier,
121
+ ];
122
+ const lexerErrorMessageProvider = {
123
+ buildUnableToPopLexerModeMessage: function (token) {
124
+ return `Unable to pop Lexer Mode at line ${token.startLine}, column ${token.startColumn}. Token: ${token.image}.`;
125
+ },
126
+ buildUnexpectedCharactersMessage: function (fullText, startOffset, length, line, column) {
127
+ const unexpectedText = fullText.substring(startOffset, startOffset + length);
128
+ return `Unexpected character sequence: '${unexpectedText}' at line ${line}, column ${column}.`;
129
+ },
130
+ };
131
+ const ReturnLexer = new Lexer(allTokens, {
132
+ errorMessageProvider: lexerErrorMessageProvider,
133
+ positionTracking: "full",
134
+ });
135
+ export function ReturnTokenize(inputText) {
136
+ const lexerResult = ReturnLexer.tokenize(inputText);
137
+ if (lexerResult.errors.length > 0) {
138
+ const errorMessages = lexerResult.errors
139
+ .map((error) => error.message)
140
+ .join("\n");
141
+ throw new Error(`Lexing errors encountered:\n${errorMessages}`);
142
+ }
143
+ return lexerResult;
144
+ }
@@ -0,0 +1,29 @@
1
+ import { removeRequiredAndEnum } from "../utils/config-utils/json-schema-utils.js";
2
+ import { getAllJsonPaths } from "../utils/json-path-utils/extract-string-paths.js";
3
+ export class SchemaExtactionService {
4
+ constructor() {
5
+ this.extractSchemas = async (data, removeRequired, removeEnums) => {
6
+ const paths = data.paths;
7
+ const apis = Object.keys(paths).map((key) => {
8
+ return key.split("/")[1];
9
+ });
10
+ const output = { paths: {} };
11
+ for (const targetApi of apis) {
12
+ const existingSchema = paths[`/${targetApi}`].post.requestBody.content["application/json"]
13
+ .schema;
14
+ output["response"] =
15
+ paths[`/${targetApi}`].post.responses.default.content["application/json"].schema;
16
+ const filtteredSchema = removeRequiredAndEnum(existingSchema, removeEnums, removeRequired);
17
+ output[targetApi] = filtteredSchema;
18
+ }
19
+ return output;
20
+ };
21
+ this.extractPossiblePaths = (schemas) => {
22
+ const paths = {};
23
+ for (const [key, schema] of Object.entries(schemas)) {
24
+ paths[key] = getAllJsonPaths(schema);
25
+ }
26
+ return paths;
27
+ };
28
+ }
29
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,4 @@
1
+ export var SupportedLanguages;
2
+ (function (SupportedLanguages) {
3
+ SupportedLanguages["Typescript"] = "typescript";
4
+ })(SupportedLanguages || (SupportedLanguages = {}));
@@ -0,0 +1 @@
1
+ import { ConfigSyntax, TestObjectSyntax } from "../constants/syntax.js";
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,91 @@
1
+ /**
2
+ * Recursively removes all "required" and "enum" properties from a JSON Schema.
3
+ * @param schema - The JSON Schema object to process.
4
+ * @returns A new JSON Schema object with "required" and "enum" properties removed.
5
+ */
6
+ export function removeRequiredAndEnum(schema, removeEnums = true, removeRequired = true) {
7
+ if (typeof schema === "boolean") {
8
+ // Boolean schemas are returned as is
9
+ return schema;
10
+ }
11
+ // Create a shallow copy to avoid mutating the original schema
12
+ const cleanedSchema = { ...schema };
13
+ // Remove "required" and "enum" properties
14
+ if (removeRequired) {
15
+ delete cleanedSchema.required;
16
+ }
17
+ if (removeEnums) {
18
+ delete cleanedSchema.enum;
19
+ }
20
+ // Helper function to process child schemas
21
+ const processSchema = (subSchema) => {
22
+ return removeRequiredAndEnum(subSchema, removeEnums, removeRequired);
23
+ };
24
+ // Process "properties"
25
+ if (cleanedSchema.properties) {
26
+ const newProperties = {};
27
+ for (const key in cleanedSchema.properties) {
28
+ newProperties[key] = processSchema(cleanedSchema.properties[key]);
29
+ }
30
+ cleanedSchema.properties = newProperties;
31
+ }
32
+ // Process "patternProperties"
33
+ if (cleanedSchema.patternProperties) {
34
+ const newPatternProperties = {};
35
+ for (const key in cleanedSchema.patternProperties) {
36
+ newPatternProperties[key] = processSchema(cleanedSchema.patternProperties[key]);
37
+ }
38
+ cleanedSchema.patternProperties = newPatternProperties;
39
+ }
40
+ // Process "additionalProperties"
41
+ if (typeof cleanedSchema.additionalProperties === "object" &&
42
+ cleanedSchema.additionalProperties !== null) {
43
+ cleanedSchema.additionalProperties = removeRequiredAndEnum(cleanedSchema.additionalProperties, removeEnums, removeRequired);
44
+ }
45
+ // Process "items"
46
+ if (cleanedSchema.items) {
47
+ if (Array.isArray(cleanedSchema.items)) {
48
+ cleanedSchema.items = cleanedSchema.items.map((item) => removeRequiredAndEnum(item, removeEnums, removeRequired));
49
+ }
50
+ else {
51
+ cleanedSchema.items = removeRequiredAndEnum(cleanedSchema.items, removeEnums, removeRequired);
52
+ }
53
+ }
54
+ // Process "additionalItems"
55
+ if (typeof cleanedSchema.additionalItems === "object" &&
56
+ cleanedSchema.additionalItems !== null) {
57
+ cleanedSchema.additionalItems = removeRequiredAndEnum(cleanedSchema.additionalItems, removeEnums, removeRequired);
58
+ }
59
+ // Process combiners: "allOf", "anyOf", "oneOf"
60
+ ["allOf", "anyOf", "oneOf"].forEach((combiner) => {
61
+ if (cleanedSchema[combiner] &&
62
+ Array.isArray(cleanedSchema[combiner])) {
63
+ // @ts-ignore
64
+ cleanedSchema[combiner] = cleanedSchema[combiner].map((subSchema) => removeRequiredAndEnum(subSchema, removeEnums, removeRequired));
65
+ }
66
+ });
67
+ // Process "not"
68
+ if (cleanedSchema.not) {
69
+ cleanedSchema.not = removeRequiredAndEnum(cleanedSchema.not, removeEnums, removeRequired);
70
+ }
71
+ // Process "definitions"
72
+ if (cleanedSchema.definitions) {
73
+ const newDefinitions = {};
74
+ for (const key in cleanedSchema.definitions) {
75
+ newDefinitions[key] = processSchema(cleanedSchema.definitions[key]);
76
+ }
77
+ cleanedSchema.definitions = newDefinitions;
78
+ }
79
+ // Process "if", "then", "else"
80
+ ["if", "then", "else"].forEach((keyword) => {
81
+ if (cleanedSchema[keyword]) {
82
+ // @ts-ignore
83
+ cleanedSchema[keyword] = removeRequiredAndEnum(cleanedSchema[keyword], removeEnums, removeRequired);
84
+ }
85
+ });
86
+ // Process "contains"
87
+ if (cleanedSchema.contains) {
88
+ cleanedSchema.contains = removeRequiredAndEnum(cleanedSchema.contains, removeEnums, removeRequired);
89
+ }
90
+ return cleanedSchema;
91
+ }
@@ -0,0 +1,16 @@
1
+ import yaml from "js-yaml";
2
+ import RefParser from "@apidevtools/json-schema-ref-parser";
3
+ export async function loadAndDereferenceYaml(yamlData) {
4
+ const raw = yaml.load(yamlData);
5
+ const data = (await dereferenceSchema(raw));
6
+ return data;
7
+ }
8
+ async function dereferenceSchema(schema) {
9
+ try {
10
+ const dereferencedSchema = await RefParser.dereference(schema);
11
+ return dereferencedSchema;
12
+ }
13
+ catch (error) {
14
+ console.error("Error dereferencing schema:", error);
15
+ }
16
+ }
@@ -0,0 +1 @@
1
+ "use strict";
@@ -0,0 +1,20 @@
1
+ import fs from "fs-extra";
2
+ import * as path from "path";
3
+ import prettier from "prettier";
4
+ import logger from "./logger.js";
5
+ export function writeFileWithFsExtra(rootPath, relativeFilePath, content) {
6
+ // Resolve the full file path
7
+ const filePath = path.resolve(rootPath, relativeFilePath);
8
+ logger.debug(`Writing file to ${filePath}`);
9
+ fs.outputFileSync(filePath, content);
10
+ }
11
+ export async function formatCode(code, lang) {
12
+ return await prettier.format(code, {
13
+ parser: lang,
14
+ tabWidth: 4,
15
+ });
16
+ }
17
+ export async function writeAndFormatCode(rootPath, relativeFilePath, content, lang) {
18
+ const formattedCode = await formatCode(content, lang);
19
+ writeFileWithFsExtra(rootPath, relativeFilePath, formattedCode);
20
+ }
@@ -0,0 +1,56 @@
1
+ import { nodeReservedKeywords } from "../../constants/syntax.js";
2
+ import { getVariablesFromTest } from "./test-object-utils.js";
3
+ /**
4
+ * Check if the string matches the pattern: lowercase letters and underscores only, no leading or trailing underscores
5
+ *
6
+ * @param str
7
+ * @returns true if the string is in snake_case, false otherwise
8
+ */
9
+ export function isSnakeCase(str) {
10
+ const snakeCasePattern = /^[a-z0-9]+(_[a-z0-9]+)*$/;
11
+ return snakeCasePattern.test(str);
12
+ }
13
+ export function isValidVariableName(input) {
14
+ // Regular expression to match valid variable names
15
+ const validVariableNameRegex = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
16
+ // Check if input matches the regex
17
+ const matchesPattern = validVariableNameRegex.test(input);
18
+ const isNotReservedKeyword = !nodeReservedKeywords.has(input);
19
+ return matchesPattern && isNotReservedKeyword;
20
+ }
21
+ /**
22
+ * Adds '>' at the beginning of each line in the given Markdown string.
23
+ *
24
+ * @param {string} markdown - The input Markdown string.
25
+ * @returns {string} - The modified Markdown string with '>' added to each line.
26
+ */
27
+ export function addBlockquoteToMarkdown(markdown) {
28
+ return markdown
29
+ .split("\n") // Split the input string into an array of lines.
30
+ .map((line) => ">" + (line.trim() !== "" ? " " + line : "")) // Add '>' in front of each line.
31
+ .join("\n"); // Join the array of lines back into a single string.
32
+ }
33
+ export function ConvertArrayToStringsInTestObject(testObject) {
34
+ const variables = getVariablesFromTest(testObject);
35
+ const testDuplicate = { ...testObject };
36
+ for (const variable of variables) {
37
+ if (Array.isArray(testObject[variable])) {
38
+ let vals = testObject[variable].map((v) => `"${v}"`).join(", ");
39
+ vals = vals.replace(/"/g, `"`);
40
+ testDuplicate[variable] = `[${vals}]`;
41
+ }
42
+ }
43
+ return testDuplicate;
44
+ }
45
+ export function ConvertArrayToString(arr) {
46
+ let vals = arr.map((v) => `"${v}"`).join(", ");
47
+ vals = vals.replace(/"/g, `"`);
48
+ return `[${vals}]`;
49
+ }
50
+ export function addTabToMarkdown(markdown) {
51
+ // Split the markdown into lines, add a tab to each line, and rejoin
52
+ return markdown
53
+ .split("\n") // Split the input into individual lines
54
+ .map((line) => `\t${line}`) // Add a tab character at the beginning of each line
55
+ .join("\n"); // Rejoin the lines into a single string
56
+ }
@@ -0,0 +1,12 @@
1
+ import { TestObjectSyntax } from "../../constants/syntax.js";
2
+ export function getVariablesFromTest(testObject) {
3
+ const variables = [];
4
+ const keys = Object.keys(testObject);
5
+ for (const key of keys) {
6
+ if (Object.values(TestObjectSyntax).includes(key)) {
7
+ continue;
8
+ }
9
+ variables.push(key);
10
+ }
11
+ return variables;
12
+ }
@@ -0,0 +1,21 @@
1
+ export function isPrimitive(value) {
2
+ return (value === null ||
3
+ ["string", "number", "boolean", "symbol", "bigint"].includes(typeof value));
4
+ }
5
+ export function isValidVariableValueType(line) {
6
+ if (typeof line === "string") {
7
+ return true;
8
+ }
9
+ return Array.isArray(line) && line.every((item) => isPrimitive(item));
10
+ }
11
+ export function getExternalVariables(sessionData) {
12
+ const variables = [];
13
+ Object.values(sessionData).forEach((session) => {
14
+ Object.keys(session).forEach((key) => {
15
+ if (!variables.includes(key)) {
16
+ variables.push(key);
17
+ }
18
+ });
19
+ });
20
+ return variables;
21
+ }