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.
- package/lib/helpers/resolveBody.js +66 -54
- package/package.json +1 -1
|
@@ -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
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
throw new Error(`Invalid Faker
|
|
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
|
-
|
|
38
|
-
|
|
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
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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
|
};
|