@usebruno/js 0.30.0 → 0.31.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@usebruno/js",
3
- "version": "0.30.0",
3
+ "version": "0.31.0",
4
4
  "license": "MIT",
5
5
  "main": "src/index.js",
6
6
  "files": [
@@ -15,7 +15,7 @@
15
15
  "sandbox:bundle-libraries": "node ./src/sandbox/bundle-libraries.js"
16
16
  },
17
17
  "dependencies": {
18
- "@usebruno/common": "0.5.0",
18
+ "@usebruno/common": "0.6.0",
19
19
  "@usebruno/crypto-js": "^3.1.9",
20
20
  "@usebruno/query": "0.1.0",
21
21
  "ajv": "^8.12.0",
package/src/bru.js CHANGED
@@ -1,12 +1,11 @@
1
1
  const { cloneDeep } = require('lodash');
2
2
  const { uuid } = require('./utils');
3
- const { interpolate } = require('@usebruno/common');
3
+ const { interpolate: _interpolate } = require('@usebruno/common');
4
4
 
5
5
  const variableNameRegex = /^[\w-.]*$/;
6
6
 
7
7
  class Bru {
8
- constructor(envVariables, runtimeVariables, processEnvVars, collectionPath, historyLogger, setVisualizations, secretVariables, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables, oauth2CredentialVariables, iterationDetails) {
9
-
8
+ constructor(envVariables, runtimeVariables, processEnvVars, collectionPath, historyLogger, setVisualizations, secretVariables, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables, oauth2CredentialVariables, iterationDetails, collectionName) {
10
9
  this.envVariables = envVariables || {};
11
10
  this.runtimeVariables = runtimeVariables || {};
12
11
  this.processEnvVars = cloneDeep(processEnvVars || {});
@@ -19,7 +18,7 @@ class Bru {
19
18
  this.collectionPath = collectionPath;
20
19
  this.historyLogger = historyLogger;
21
20
  this.setVisualizations = setVisualizations;
22
-
21
+ this.collectionName = collectionName;
23
22
  this.runner = {
24
23
  skipRequest: () => {
25
24
  this.skipRequest = true;
@@ -38,10 +37,10 @@ class Bru {
38
37
  }
39
38
 
40
39
 
41
- _interpolate = (str) => {
42
- if (!str || !str.length || typeof str !== 'string') {
43
- return str;
44
- }
40
+ interpolate = (strOrObj) => {
41
+ if (!strOrObj) return strOrObj;
42
+ const isObj = typeof strOrObj === 'object';
43
+ const strToInterpolate = isObj ? JSON.stringify(strOrObj) : strOrObj;
45
44
 
46
45
  const combinedVars = {
47
46
  ...this.globalEnvironmentVariables,
@@ -58,7 +57,8 @@ class Bru {
58
57
  }
59
58
  };
60
59
 
61
- return interpolate(str, combinedVars);
60
+ const interpolatedStr = _interpolate(strToInterpolate, combinedVars);
61
+ return isObj ? JSON.parse(interpolatedStr) : interpolatedStr;
62
62
  };
63
63
 
64
64
  cwd() {
@@ -78,7 +78,7 @@ class Bru {
78
78
  }
79
79
 
80
80
  getEnvVar(key) {
81
- return this._interpolate(this.envVariables[key]);
81
+ return this.interpolate(this.envVariables[key]);
82
82
  }
83
83
 
84
84
  setEnvVar(key, value) {
@@ -103,7 +103,7 @@ class Bru {
103
103
  }
104
104
 
105
105
  getGlobalEnvVar(key) {
106
- return this._interpolate(this.globalEnvironmentVariables[key]);
106
+ return this.interpolate(this.globalEnvironmentVariables[key]);
107
107
  }
108
108
 
109
109
  setGlobalEnvVar(key, value) {
@@ -115,7 +115,7 @@ class Bru {
115
115
  }
116
116
 
117
117
  getOauth2CredentialVar(key) {
118
- return this._interpolate(this.oauth2CredentialVariables[key]);
118
+ return this.interpolate(this.oauth2CredentialVariables[key]);
119
119
  }
120
120
 
121
121
  hasVar(key) {
@@ -130,7 +130,7 @@ class Bru {
130
130
  if (variableNameRegex.test(key) === false) {
131
131
  throw new Error(
132
132
  `Variable name: "${key}" contains invalid characters!` +
133
- ' Names must only contain alpha-numeric characters, "-", "_", "."'
133
+ ' Names must only contain alpha-numeric characters, "-", "_", "."'
134
134
  );
135
135
  }
136
136
 
@@ -150,11 +150,11 @@ class Bru {
150
150
  if (variableNameRegex.test(key) === false) {
151
151
  throw new Error(
152
152
  `Variable name: "${key}" contains invalid characters!` +
153
- ' Names must only contain alpha-numeric characters, "-", "_", "."'
153
+ ' Names must only contain alpha-numeric characters, "-", "_", "."'
154
154
  );
155
155
  }
156
156
 
157
- return this._interpolate(this.runtimeVariables[key]);
157
+ return this.interpolate(this.runtimeVariables[key]);
158
158
  }
159
159
 
160
160
  deleteVar(key) {
@@ -170,15 +170,15 @@ class Bru {
170
170
  }
171
171
 
172
172
  getCollectionVar(key) {
173
- return this._interpolate(this.collectionVariables[key]);
173
+ return this.interpolate(this.collectionVariables[key]);
174
174
  }
175
175
 
176
176
  getFolderVar(key) {
177
- return this._interpolate(this.folderVariables[key]);
177
+ return this.interpolate(this.folderVariables[key]);
178
178
  }
179
179
 
180
180
  getRequestVar(key) {
181
- return this._interpolate(this.requestVariables[key]);
181
+ return this.interpolate(this.requestVariables[key]);
182
182
  }
183
183
 
184
184
  setNextRequest(nextRequest) {
@@ -215,6 +215,10 @@ class Bru {
215
215
  sleep(ms) {
216
216
  return new Promise((resolve) => setTimeout(resolve, ms));
217
217
  }
218
+
219
+ getCollectionName() {
220
+ return this.collectionName;
221
+ }
218
222
  }
219
223
 
220
224
  class IterationDataManager {
@@ -21,7 +21,7 @@ class BrunoRequest {
21
21
  this.headers = req.headers;
22
22
  this.timeout = req.timeout;
23
23
  this.historyLogger = historyLogger;
24
-
24
+ this.name = req.name;
25
25
  /**
26
26
  * We automatically parse the JSON body if the content type is JSON
27
27
  * This is to make it easier for the user to access the body directly
@@ -198,6 +198,10 @@ class BrunoRequest {
198
198
  getExecutionMode() {
199
199
  return this.req.__bruno__executionMode;
200
200
  }
201
+
202
+ getName() {
203
+ return this.req.name;
204
+ }
201
205
  }
202
206
 
203
207
  module.exports = BrunoRequest;
@@ -51,7 +51,8 @@ class ScriptRuntime {
51
51
  scriptingConfig,
52
52
  historyLogger,
53
53
  secretVariables,
54
- runRequestByItemPathname
54
+ runRequestByItemPathname,
55
+ collectionName
55
56
  ) {
56
57
  let visualizations = [];
57
58
  let setVisualizations = (data) => {
@@ -64,7 +65,7 @@ class ScriptRuntime {
64
65
  const requestVariables = request?.requestVariables || {};
65
66
  const iterationDetails = request?.runnerIterationDetails || {};
66
67
  // TODO please clean this up
67
- const bru = new Bru(envVariables, runtimeVariables, processEnvVars, collectionPath, historyLogger, setVisualizations, secretVariables, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables, oauth2CredentialVariables, iterationDetails);
68
+ const bru = new Bru(envVariables, runtimeVariables, processEnvVars, collectionPath, historyLogger, setVisualizations, secretVariables, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables, oauth2CredentialVariables, iterationDetails, collectionName);
68
69
  const req = new BrunoRequest(request);
69
70
  const allowScriptFilesystemAccess = get(scriptingConfig, 'filesystemAccess.allow', false);
70
71
  const moduleWhitelist = get(scriptingConfig, 'moduleWhitelist', []);
@@ -105,7 +106,7 @@ class ScriptRuntime {
105
106
  };
106
107
  }
107
108
 
108
- if(runRequestByItemPathname) {
109
+ if (runRequestByItemPathname) {
109
110
  context.bru.runRequest = runRequestByItemPathname;
110
111
  }
111
112
 
@@ -159,7 +160,7 @@ class ScriptRuntime {
159
160
  chai,
160
161
  'node-fetch': fetch,
161
162
  'crypto-js': CryptoJS,
162
- 'xml2js': xml2js,
163
+ xml2js: xml2js,
163
164
  cheerio,
164
165
  tv4,
165
166
  ...whitelistedModules,
@@ -195,7 +196,8 @@ class ScriptRuntime {
195
196
  scriptingConfig,
196
197
  historyLogger,
197
198
  secretVariables,
198
- runRequestByItemPathname
199
+ runRequestByItemPathname,
200
+ collectionName
199
201
  ) {
200
202
  let visualizations = [];
201
203
  let setVisualizations = (data) => {
@@ -207,7 +209,7 @@ class ScriptRuntime {
207
209
  const folderVariables = request?.folderVariables || {};
208
210
  const requestVariables = request?.requestVariables || {};
209
211
  const iterationDetails = request?.runnerIterationDetails || {};
210
- const bru = new Bru(envVariables, runtimeVariables, processEnvVars, collectionPath, historyLogger, setVisualizations, secretVariables, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables, oauth2CredentialVariables, iterationDetails);
212
+ const bru = new Bru(envVariables, runtimeVariables, processEnvVars, collectionPath, historyLogger, setVisualizations, secretVariables, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables, oauth2CredentialVariables, iterationDetails, collectionName);
211
213
  const req = new BrunoRequest(request);
212
214
  const res = new BrunoResponse(response);
213
215
  const allowScriptFilesystemAccess = get(scriptingConfig, 'filesystemAccess.allow', false);
@@ -250,7 +252,7 @@ class ScriptRuntime {
250
252
  };
251
253
  }
252
254
 
253
- if(runRequestByItemPathname) {
255
+ if (runRequestByItemPathname) {
254
256
  context.bru.runRequest = runRequestByItemPathname;
255
257
  }
256
258
 
@@ -71,7 +71,8 @@ class TestRuntime {
71
71
  scriptingConfig,
72
72
  historyLogger,
73
73
  secretVariables,
74
- runRequestByItemPathname
74
+ runRequestByItemPathname,
75
+ collectionName
75
76
  ) {
76
77
  const globalEnvironmentVariables = request?.globalEnvironmentVariables || {};
77
78
  const oauth2CredentialVariables = request?.oauth2CredentialVariables || {};
@@ -80,7 +81,7 @@ class TestRuntime {
80
81
  const requestVariables = request?.requestVariables || {};
81
82
  const iterationDetails = request?.runnerIterationDetails || {};
82
83
  const assertionResults = request?.assertionResults || [];
83
- const bru = new Bru(envVariables, runtimeVariables, processEnvVars, collectionPath, historyLogger, undefined, secretVariables, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables, oauth2CredentialVariables, iterationDetails);
84
+ const bru = new Bru(envVariables, runtimeVariables, processEnvVars, collectionPath, historyLogger, undefined, secretVariables, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables, oauth2CredentialVariables, iterationDetails, collectionName);
84
85
  const req = new BrunoRequest(request, historyLogger);
85
86
  const res = new BrunoResponse(response);
86
87
  const allowScriptFilesystemAccess = get(scriptingConfig, 'filesystemAccess.allow', false);
@@ -18,12 +18,24 @@ const addBruShimToContext = (vm, bru) => {
18
18
  vm.setProp(bruObject, 'getEnvName', getEnvName);
19
19
  getEnvName.dispose();
20
20
 
21
+ let getCollectionName = vm.newFunction('getCollectionName', function () {
22
+ return marshallToVm(bru.getCollectionName(), vm);
23
+ });
24
+ vm.setProp(bruObject, 'getCollectionName', getCollectionName);
25
+ getCollectionName.dispose();
26
+
21
27
  let getProcessEnv = vm.newFunction('getProcessEnv', function (key) {
22
28
  return marshallToVm(bru.getProcessEnv(vm.dump(key)), vm);
23
29
  });
24
30
  vm.setProp(bruObject, 'getProcessEnv', getProcessEnv);
25
31
  getProcessEnv.dispose();
26
32
 
33
+ let interpolate = vm.newFunction('interpolate', function (str) {
34
+ return marshallToVm(bru.interpolate(vm.dump(str)), vm);
35
+ });
36
+ vm.setProp(bruObject, 'interpolate', interpolate);
37
+ interpolate.dispose();
38
+
27
39
  let hasEnvVar = vm.newFunction('hasEnvVar', function (key) {
28
40
  return marshallToVm(bru.hasEnvVar(vm.dump(key)), vm);
29
41
  });
@@ -193,7 +205,8 @@ const addBruShimToContext = (vm, bru) => {
193
205
 
194
206
  let getTestResults = vm.newFunction('getTestResults', () => {
195
207
  const promise = vm.newPromise();
196
- bru.getTestResults()
208
+ bru
209
+ .getTestResults()
197
210
  .then((results) => {
198
211
  promise.resolve(marshallToVm(cleanJson(results), vm));
199
212
  })
@@ -214,7 +227,8 @@ const addBruShimToContext = (vm, bru) => {
214
227
 
215
228
  let getAssertionResults = vm.newFunction('getAssertionResults', () => {
216
229
  const promise = vm.newPromise();
217
- bru.getAssertionResults()
230
+ bru
231
+ .getAssertionResults()
218
232
  .then((results) => {
219
233
  promise.resolve(marshallToVm(cleanJson(results), vm));
220
234
  })
@@ -235,7 +249,8 @@ const addBruShimToContext = (vm, bru) => {
235
249
 
236
250
  let runRequestHandle = vm.newFunction('runRequest', (args) => {
237
251
  const promise = vm.newPromise();
238
- bru.runRequest(vm.dump(args))
252
+ bru
253
+ .runRequest(vm.dump(args))
239
254
  .then((response) => {
240
255
  const { status, headers, data, dataBuffer, size, statusText } = response || {};
241
256
  promise.resolve(marshallToVm(cleanJson({ status, statusText, headers, data, dataBuffer, size }), vm));
@@ -8,18 +8,21 @@ const addBrunoRequestShimToContext = (vm, req) => {
8
8
  const headers = marshallToVm(req.getHeaders(), vm);
9
9
  const body = marshallToVm(req.getBody(), vm);
10
10
  const timeout = marshallToVm(req.getTimeout(), vm);
11
+ const name = marshallToVm(req.getName(), vm);
11
12
 
12
13
  vm.setProp(reqObject, 'url', url);
13
14
  vm.setProp(reqObject, 'method', method);
14
15
  vm.setProp(reqObject, 'headers', headers);
15
16
  vm.setProp(reqObject, 'body', body);
16
17
  vm.setProp(reqObject, 'timeout', timeout);
18
+ vm.setProp(reqObject, 'name', name);
17
19
 
18
20
  url.dispose();
19
21
  method.dispose();
20
22
  headers.dispose();
21
23
  body.dispose();
22
24
  timeout.dispose();
25
+ name.dispose();
23
26
 
24
27
  let getUrl = vm.newFunction('getUrl', function () {
25
28
  return marshallToVm(req.getUrl(), vm);
@@ -45,6 +48,12 @@ const addBrunoRequestShimToContext = (vm, req) => {
45
48
  vm.setProp(reqObject, 'getAuthMode', getAuthMode);
46
49
  getAuthMode.dispose();
47
50
 
51
+ let getName = vm.newFunction('getName', function () {
52
+ return marshallToVm(req.getName(), vm);
53
+ });
54
+ vm.setProp(reqObject, 'getName', getName);
55
+ getName.dispose();
56
+
48
57
  let setMethod = vm.newFunction('setMethod', function (method) {
49
58
  req.setMethod(vm.dump(method));
50
59
  });