2023-09-28 01:02:07 +02:00
|
|
|
const Handlebars = require('handlebars');
|
|
|
|
const { cloneDeep } = require('lodash');
|
|
|
|
|
2023-12-01 20:57:18 +01:00
|
|
|
const variableNameRegex = /^[\w-.]*$/;
|
2023-11-02 18:17:21 +01:00
|
|
|
|
2023-01-26 22:54:21 +01:00
|
|
|
class Bru {
|
2023-10-04 23:50:53 +02:00
|
|
|
constructor(envVariables, collectionVariables, processEnvVars, collectionPath) {
|
2023-09-06 17:06:55 +02:00
|
|
|
this.envVariables = envVariables;
|
|
|
|
this.collectionVariables = collectionVariables;
|
2023-09-28 01:02:07 +02:00
|
|
|
this.processEnvVars = cloneDeep(processEnvVars || {});
|
2023-10-04 23:50:53 +02:00
|
|
|
this.collectionPath = collectionPath;
|
2023-09-06 17:06:55 +02:00
|
|
|
}
|
|
|
|
|
2023-09-28 01:02:07 +02:00
|
|
|
_interpolateEnvVar = (str) => {
|
|
|
|
if (!str || !str.length || typeof str !== 'string') {
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
const template = Handlebars.compile(str, { noEscape: true });
|
|
|
|
|
|
|
|
return template({
|
|
|
|
process: {
|
|
|
|
env: {
|
|
|
|
...this.processEnvVars
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-10-04 23:50:53 +02:00
|
|
|
cwd() {
|
|
|
|
return this.collectionPath;
|
|
|
|
}
|
|
|
|
|
2023-09-06 17:06:55 +02:00
|
|
|
getEnvName() {
|
|
|
|
return this.envVariables.__name__;
|
2023-01-26 22:54:21 +01:00
|
|
|
}
|
|
|
|
|
2023-08-11 19:48:09 +02:00
|
|
|
getProcessEnv(key) {
|
2023-09-28 01:02:07 +02:00
|
|
|
return this.processEnvVars[key];
|
2023-08-11 19:48:09 +02:00
|
|
|
}
|
|
|
|
|
2023-01-29 00:19:31 +01:00
|
|
|
getEnvVar(key) {
|
2023-09-28 01:02:07 +02:00
|
|
|
return this._interpolateEnvVar(this.envVariables[key]);
|
2023-01-29 00:19:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
setEnvVar(key, value) {
|
2023-09-21 21:12:14 +02:00
|
|
|
if (!key) {
|
2023-11-18 04:48:40 +01:00
|
|
|
throw new Error('Creating a env variable without specifying a name is not allowed.');
|
2023-01-29 00:19:31 +01:00
|
|
|
}
|
|
|
|
|
2023-09-06 17:06:55 +02:00
|
|
|
this.envVariables[key] = value;
|
2023-01-26 22:54:21 +01:00
|
|
|
}
|
2023-02-01 13:26:13 +01:00
|
|
|
|
|
|
|
setVar(key, value) {
|
2023-09-21 21:12:14 +02:00
|
|
|
if (!key) {
|
2023-11-18 04:48:40 +01:00
|
|
|
throw new Error('Creating a variable without specifying a name is not allowed.');
|
2023-02-01 13:26:13 +01:00
|
|
|
}
|
|
|
|
|
2023-12-01 20:57:18 +01:00
|
|
|
if (variableNameRegex.test(key) === false) {
|
2023-10-25 23:13:37 +02:00
|
|
|
throw new Error(
|
|
|
|
`Variable name: "${key}" contains invalid characters!` +
|
2023-12-01 20:57:18 +01:00
|
|
|
' Names must only contain alpha-numeric characters, "-", "_", "."'
|
2023-10-25 23:13:37 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-09-06 17:06:55 +02:00
|
|
|
this.collectionVariables[key] = value;
|
2023-02-01 13:26:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getVar(key) {
|
2023-12-01 20:57:18 +01:00
|
|
|
if (variableNameRegex.test(key) === false) {
|
2023-10-25 23:13:37 +02:00
|
|
|
throw new Error(
|
|
|
|
`Variable name: "${key}" contains invalid characters!` +
|
2023-12-01 20:57:18 +01:00
|
|
|
' Names must only contain alpha-numeric characters, "-", "_", "."'
|
2023-10-25 23:13:37 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-09-06 17:06:55 +02:00
|
|
|
return this.collectionVariables[key];
|
2023-02-01 13:26:13 +01:00
|
|
|
}
|
2023-10-16 13:28:58 +02:00
|
|
|
|
|
|
|
setNextRequest(nextRequest) {
|
|
|
|
this.nextRequest = nextRequest;
|
|
|
|
}
|
2023-01-26 22:54:21 +01:00
|
|
|
}
|
|
|
|
|
2023-08-11 19:48:09 +02:00
|
|
|
module.exports = Bru;
|