k6-cucumber-steps 1.2.18 → 1.2.19

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,62 +1,74 @@
1
- /**
2
- * @module resolveBody
3
- * @description
4
- * This module resolves placeholders in a template string using environment variables,
5
- * Faker templates, and JSON file includes.
6
- * It supports the following types of placeholders:
7
- * 1. Environment variables: {{username}} will be replaced with the value of process.env.username.
8
- * 2. Faker templates: {{faker.internet.email}} will be replaced with a randomly generated email address.
9
- * 3. JSON file includes: <address.json> will be replaced with the contents of the address.json file.
10
- * */
11
1
  const fs = require("fs");
12
2
  const path = require("path");
13
- const faker = require("@faker-js/faker");
14
-
15
- module.exports = function resolveBody(template, env) {
16
- let result = template;
17
- console.log("Resolving body template:", template);
18
- console.log("Resolved body result:", result);
19
- // Replace env vars like {{username}}
20
- result = result.replace(/{{(\w+)}}/g, (_, key) => {
21
- if (!(key in env)) {
22
- console.warn(`Missing environment variable for placeholder: {{${key}}}`);
23
- }
24
- return env[key] || "";
25
- });
26
-
27
- // Support faker templates like {{faker.internet.email}}
28
- result = result.replace(/{{faker\.([\w.]+)}}/g, (_, methodPath) => {
29
- const parts = methodPath.split(".");
30
- let fn = faker;
31
- for (const part of parts) {
32
- fn = fn?.[part]; // Use optional chaining to avoid errors
33
- if (!fn) {
34
- throw new Error(`Invalid Faker template: {{faker.${methodPath}}}`);
3
+ const faker = require("@faker-js/faker").faker;
4
+
5
+ /**
6
+ * Recursively resolves all placeholders in an object.
7
+ * @param {*} data - The parsed JSON (object, array, string, etc)
8
+ * @param {object} env - Environment variables + aliases
9
+ * @returns {*} - Fully resolved structure
10
+ */
11
+ function resolveDeep(data, env) {
12
+ if (typeof data === "string") {
13
+ // 1. Resolve env vars like {{username}}
14
+ data = data.replace(/{{(\w+)}}/g, (_, key) => {
15
+ return env[key] || "";
16
+ });
17
+
18
+ // 2. Resolve faker like {{faker.internet.email}}
19
+ data = data.replace(/{{faker\.([\w.]+)}}/g, (_, methodPath) => {
20
+ const parts = methodPath.split(".");
21
+ let fn = faker;
22
+ for (const part of parts) {
23
+ fn = fn?.[part];
24
+ if (!fn) throw new Error(`Invalid Faker method: faker.${methodPath}`);
35
25
  }
26
+ return typeof fn === "function" ? fn() : fn;
27
+ });
28
+
29
+ // 3. Replace JSON file includes like <address.json>
30
+ data = data.replace(/<([\w-]+\.json)>/g, (_, fileName) => {
31
+ const filePath = path.join(__dirname, "../payloads", fileName);
32
+ if (!fs.existsSync(filePath)) {
33
+ throw new Error(`Payload file not found: ${fileName}`);
34
+ }
35
+ const fileContent = fs.readFileSync(filePath, "utf-8");
36
+ return fileContent.trim();
37
+ });
38
+
39
+ return data;
40
+ }
41
+
42
+ if (Array.isArray(data)) {
43
+ return data.map((item) => resolveDeep(item, env));
44
+ }
45
+
46
+ if (typeof data === "object" && data !== null) {
47
+ const resolved = {};
48
+ for (const key in data) {
49
+ resolved[key] = resolveDeep(data[key], env);
36
50
  }
37
- if (typeof fn !== "function") {
38
- throw new Error(`Invalid Faker template: {{faker.${methodPath}}}`);
39
- }
40
- return fn();
41
- });
42
-
43
- // Replace JSON file includes like <address.json>
44
- result = result.replace(/<([\w.]+\.json)>/g, (_, fileName) => {
45
- const filePath = path.join(__dirname, "../payloads", fileName);
46
- if (!fs.existsSync(filePath)) {
47
- throw new Error(`Payload file not found: ${fileName}`);
48
- }
49
- try {
50
- return JSON.stringify(JSON.parse(fs.readFileSync(filePath, "utf-8")));
51
- } catch (error) {
52
- throw new Error(`Failed to parse JSON file: ${fileName}`);
53
- }
54
- });
51
+ return resolved;
52
+ }
55
53
 
54
+ return data;
55
+ }
56
+
57
+ /**
58
+ * Resolves JSON templates using env vars, Faker, and JSON includes.
59
+ * @param {string} template - The raw JSON string template
60
+ * @param {object} env - Object containing environment variables or aliases
61
+ * @returns {object} - Fully resolved JS object
62
+ */
63
+ module.exports = function resolveBody(template, env = {}) {
56
64
  try {
57
- return JSON.parse(result);
58
- } catch (error) {
59
- console.error("Failed to parse resolved body to JSON:", result);
60
- throw new Error("Invalid JSON body after resolving placeholders.");
65
+ const parsed = JSON.parse(template);
66
+ return resolveDeep(parsed, env);
67
+ } catch (err) {
68
+ console.error(
69
+ "❌ Failed to parse input JSON before resolving:",
70
+ err.message
71
+ );
72
+ throw new Error("Invalid JSON template provided to resolveBody");
61
73
  }
62
74
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "k6-cucumber-steps",
3
- "version": "1.2.18",
3
+ "version": "1.2.19",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "repository": {