2023-09-28 01:02:07 +02:00
|
|
|
const { cloneDeep } = require('lodash');
|
2024-06-21 07:45:23 +02:00
|
|
|
const { interpolate } = require('@usebruno/common');
|
2023-09-28 01:02:07 +02:00
|
|
|
|
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 {
|
2024-09-03 17:48:38 +02:00
|
|
|
constructor(envVariables, runtimeVariables, processEnvVars, collectionPath, collectionVariables, folderVariables, requestVariables) {
|
2024-06-21 07:45:23 +02:00
|
|
|
this.envVariables = envVariables || {};
|
2024-07-17 13:51:03 +02:00
|
|
|
this.runtimeVariables = runtimeVariables || {};
|
2023-09-28 01:02:07 +02:00
|
|
|
this.processEnvVars = cloneDeep(processEnvVars || {});
|
2024-09-03 17:48:38 +02:00
|
|
|
this.collectionVariables = collectionVariables || {};
|
|
|
|
this.folderVariables = folderVariables || {};
|
2024-07-01 10:45:25 +02:00
|
|
|
this.requestVariables = requestVariables || {};
|
2023-10-04 23:50:53 +02:00
|
|
|
this.collectionPath = collectionPath;
|
2023-09-06 17:06:55 +02:00
|
|
|
}
|
|
|
|
|
2024-06-21 07:45:23 +02:00
|
|
|
_interpolate = (str) => {
|
2023-09-28 01:02:07 +02:00
|
|
|
if (!str || !str.length || typeof str !== 'string') {
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2024-06-21 07:45:23 +02:00
|
|
|
const combinedVars = {
|
2024-09-03 17:48:38 +02:00
|
|
|
...this.collectionVariables,
|
2024-06-21 07:45:23 +02:00
|
|
|
...this.envVariables,
|
2024-09-03 17:48:38 +02:00
|
|
|
...this.folderVariables,
|
2024-07-01 10:45:25 +02:00
|
|
|
...this.requestVariables,
|
2024-07-17 13:51:03 +02:00
|
|
|
...this.runtimeVariables,
|
2023-09-28 01:02:07 +02:00
|
|
|
process: {
|
|
|
|
env: {
|
|
|
|
...this.processEnvVars
|
|
|
|
}
|
|
|
|
}
|
2024-06-21 07:45:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return interpolate(str, combinedVars);
|
2023-09-28 01:02:07 +02:00
|
|
|
};
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-07-04 09:51:27 +02:00
|
|
|
hasEnvVar(key) {
|
|
|
|
return Object.hasOwn(this.envVariables, key);
|
|
|
|
}
|
|
|
|
|
2023-01-29 00:19:31 +01:00
|
|
|
getEnvVar(key) {
|
2024-06-21 07:45:23 +02:00
|
|
|
return this._interpolate(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
|
|
|
|
2024-07-04 09:51:27 +02:00
|
|
|
hasVar(key) {
|
2024-07-17 13:51:03 +02:00
|
|
|
return Object.hasOwn(this.runtimeVariables, key);
|
2024-07-04 09:51:27 +02: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!` +
|
2024-09-03 17:48:38 +02:00
|
|
|
' Names must only contain alpha-numeric characters, "-", "_", "."'
|
2023-10-25 23:13:37 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-07-17 13:51:03 +02:00
|
|
|
this.runtimeVariables[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!` +
|
2024-09-03 17:48:38 +02:00
|
|
|
' Names must only contain alpha-numeric characters, "-", "_", "."'
|
2023-10-25 23:13:37 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-07-17 13:51:03 +02:00
|
|
|
return this._interpolate(this.runtimeVariables[key]);
|
2023-02-01 13:26:13 +01:00
|
|
|
}
|
2023-10-16 13:28:58 +02:00
|
|
|
|
2024-07-04 09:51:27 +02:00
|
|
|
deleteVar(key) {
|
2024-07-17 13:51:03 +02:00
|
|
|
delete this.runtimeVariables[key];
|
2024-07-04 09:51:27 +02:00
|
|
|
}
|
|
|
|
|
2024-10-01 20:15:05 +02:00
|
|
|
deleteAllVars() {
|
|
|
|
for (let key in this.runtimeVariables) {
|
|
|
|
if (this.runtimeVariables.hasOwnProperty(key)) {
|
|
|
|
delete this.runtimeVariables[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-03 17:48:38 +02:00
|
|
|
getCollectionVar(key) {
|
|
|
|
return this._interpolate(this.collectionVariables[key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
getFolderVar(key) {
|
|
|
|
return this._interpolate(this.folderVariables[key]);
|
|
|
|
}
|
|
|
|
|
2024-07-01 10:45:25 +02:00
|
|
|
getRequestVar(key) {
|
|
|
|
return this._interpolate(this.requestVariables[key]);
|
|
|
|
}
|
|
|
|
|
2023-10-16 13:28:58 +02:00
|
|
|
setNextRequest(nextRequest) {
|
|
|
|
this.nextRequest = nextRequest;
|
|
|
|
}
|
2024-08-21 09:22:49 +02:00
|
|
|
|
|
|
|
sleep(ms) {
|
|
|
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
|
|
}
|
2023-01-26 22:54:21 +01:00
|
|
|
}
|
|
|
|
|
2023-08-11 19:48:09 +02:00
|
|
|
module.exports = Bru;
|