@usebruno/js 0.8.0 → 0.9.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.8.0",
3
+ "version": "0.9.0",
4
4
  "license": "MIT",
5
5
  "main": "src/index.js",
6
6
  "files": [
@@ -5,6 +5,7 @@ class BrunoRequest {
5
5
  this.method = req.method;
6
6
  this.headers = req.headers;
7
7
  this.body = req.data;
8
+ this.timeout = req.timeout;
8
9
  }
9
10
 
10
11
  getUrl() {
@@ -50,6 +51,14 @@ class BrunoRequest {
50
51
  setMaxRedirects(maxRedirects) {
51
52
  this.req.maxRedirects = maxRedirects;
52
53
  }
54
+
55
+ getTimeout() {
56
+ return this.req.timeout;
57
+ }
58
+
59
+ setTimeout(timeout) {
60
+ this.req.timeout = timeout;
61
+ }
53
62
  }
54
63
 
55
64
  module.exports = BrunoRequest;
@@ -0,0 +1,55 @@
1
+ const Handlebars = require('handlebars');
2
+ const { forOwn, cloneDeep } = require('lodash');
3
+
4
+ const interpolateEnvVars = (str, processEnvVars) => {
5
+ if (!str || !str.length || typeof str !== 'string') {
6
+ return str;
7
+ }
8
+
9
+ const template = Handlebars.compile(str, { noEscape: true });
10
+
11
+ return template({
12
+ process: {
13
+ env: {
14
+ ...processEnvVars
15
+ }
16
+ }
17
+ });
18
+ };
19
+
20
+ const interpolateString = (str, { envVariables, collectionVariables, processEnvVars }) => {
21
+ if (!str || !str.length || typeof str !== 'string') {
22
+ return str;
23
+ }
24
+
25
+ processEnvVars = processEnvVars || {};
26
+ collectionVariables = collectionVariables || {};
27
+
28
+ // we clone envVariables because we don't want to modify the original object
29
+ envVariables = envVariables ? cloneDeep(envVariables) : {};
30
+
31
+ // envVariables can inturn have values as {{process.env.VAR_NAME}}
32
+ // so we need to interpolate envVariables first with processEnvVars
33
+ forOwn(envVariables, (value, key) => {
34
+ envVariables[key] = interpolateEnvVars(value, processEnvVars);
35
+ });
36
+
37
+ const template = Handlebars.compile(str, { noEscape: true });
38
+
39
+ // collectionVariables take precedence over envVariables
40
+ const combinedVars = {
41
+ ...envVariables,
42
+ ...collectionVariables,
43
+ process: {
44
+ env: {
45
+ ...processEnvVars
46
+ }
47
+ }
48
+ };
49
+
50
+ return template(combinedVars);
51
+ };
52
+
53
+ module.exports = {
54
+ interpolateString
55
+ };
@@ -4,8 +4,10 @@ const { nanoid } = require('nanoid');
4
4
  const Bru = require('../bru');
5
5
  const BrunoRequest = require('../bruno-request');
6
6
  const { evaluateJsTemplateLiteral, evaluateJsExpression, createResponseParser } = require('../utils');
7
+ const { interpolateString } = require('../interpolate-string');
7
8
 
8
9
  const { expect } = chai;
10
+ chai.use(require('chai-string'));
9
11
  chai.use(function (chai, utils) {
10
12
  // Custom assertion for checking if a variable is JSON
11
13
  chai.Assertion.addProperty('json', function () {
@@ -160,17 +162,27 @@ const evaluateRhsOperand = (rhsOperand, operator, context) => {
160
162
  return;
161
163
  }
162
164
 
165
+ const interpolationContext = {
166
+ collectionVariables: context.bru.collectionVariables,
167
+ envVariables: context.bru.envVariables,
168
+ processEnvVars: context.bru.processEnvVars
169
+ };
170
+
163
171
  // gracefully allow both a,b as well as [a, b]
164
172
  if (operator === 'in' || operator === 'notIn') {
165
173
  if (rhsOperand.startsWith('[') && rhsOperand.endsWith(']')) {
166
174
  rhsOperand = rhsOperand.substring(1, rhsOperand.length - 1);
167
175
  }
168
176
 
169
- return rhsOperand.split(',').map((v) => evaluateJsTemplateLiteral(v.trim(), context));
177
+ return rhsOperand
178
+ .split(',')
179
+ .map((v) => evaluateJsTemplateLiteral(interpolateString(v.trim(), interpolationContext), context));
170
180
  }
171
181
 
172
182
  if (operator === 'between') {
173
- const [lhs, rhs] = rhsOperand.split(',').map((v) => evaluateJsTemplateLiteral(v.trim(), context));
183
+ const [lhs, rhs] = rhsOperand
184
+ .split(',')
185
+ .map((v) => evaluateJsTemplateLiteral(interpolateString(v.trim(), interpolationContext), context));
174
186
  return [lhs, rhs];
175
187
  }
176
188
 
@@ -180,10 +192,10 @@ const evaluateRhsOperand = (rhsOperand, operator, context) => {
180
192
  rhsOperand = rhsOperand.substring(1, rhsOperand.length - 1);
181
193
  }
182
194
 
183
- return rhsOperand;
195
+ return interpolateString(rhsOperand, interpolationContext);
184
196
  }
185
197
 
186
- return evaluateJsTemplateLiteral(rhsOperand, context);
198
+ return evaluateJsTemplateLiteral(interpolateString(rhsOperand, interpolationContext), context);
187
199
  };
188
200
 
189
201
  class AssertRuntime {
@@ -268,7 +280,7 @@ class AssertRuntime {
268
280
  expect(lhs).to.endWith(rhs);
269
281
  break;
270
282
  case 'between':
271
- const [min, max] = value.split(',');
283
+ const [min, max] = rhs;
272
284
  expect(lhs).to.be.within(min, max);
273
285
  break;
274
286
  case 'isEmpty':
@@ -15,6 +15,7 @@ const BrunoResponse = require('../bruno-response');
15
15
  const { cleanJson } = require('../utils');
16
16
 
17
17
  // Inbuilt Library Support
18
+ const ajv = require('ajv');
18
19
  const atob = require('atob');
19
20
  const btoa = require('btoa');
20
21
  const lodash = require('lodash');
@@ -39,7 +40,7 @@ class ScriptRuntime {
39
40
  collectionPath,
40
41
  onConsoleLog,
41
42
  processEnvVars,
42
- scriptingConfig,
43
+ scriptingConfig
43
44
  ) {
44
45
  const bru = new Bru(envVariables, collectionVariables, processEnvVars, collectionPath);
45
46
  const req = new BrunoRequest(request);
@@ -48,7 +49,7 @@ class ScriptRuntime {
48
49
 
49
50
  const whitelistedModules = {};
50
51
 
51
- for(let module of moduleWhitelist) {
52
+ for (let module of moduleWhitelist) {
52
53
  try {
53
54
  whitelistedModules[module] = require(module);
54
55
  } catch (e) {
@@ -70,6 +71,7 @@ class ScriptRuntime {
70
71
  };
71
72
  context.console = {
72
73
  log: customLogger('log'),
74
+ debug: customLogger('debug'),
73
75
  info: customLogger('info'),
74
76
  warn: customLogger('warn'),
75
77
  error: customLogger('error')
@@ -93,6 +95,7 @@ class ScriptRuntime {
93
95
  punycode,
94
96
  zlib,
95
97
  // 3rd party libs
98
+ ajv,
96
99
  atob,
97
100
  btoa,
98
101
  lodash,
@@ -136,7 +139,7 @@ class ScriptRuntime {
136
139
 
137
140
  const whitelistedModules = {};
138
141
 
139
- for(let module of moduleWhitelist) {
142
+ for (let module of moduleWhitelist) {
140
143
  try {
141
144
  whitelistedModules[module] = require(module);
142
145
  } catch (e) {
@@ -182,6 +185,7 @@ class ScriptRuntime {
182
185
  punycode,
183
186
  zlib,
184
187
  // 3rd party libs
188
+ ajv,
185
189
  atob,
186
190
  btoa,
187
191
  lodash,
@@ -192,7 +196,7 @@ class ScriptRuntime {
192
196
  'node-fetch': fetch,
193
197
  'crypto-js': CryptoJS,
194
198
  ...whitelistedModules,
195
- fs: allowScriptFilesystemAccess ? fs : undefined,
199
+ fs: allowScriptFilesystemAccess ? fs : undefined
196
200
  }
197
201
  }
198
202
  });
@@ -18,13 +18,15 @@ const TestResults = require('../test-results');
18
18
  const { cleanJson } = require('../utils');
19
19
 
20
20
  // Inbuilt Library Support
21
+ const ajv = require('ajv');
21
22
  const atob = require('atob');
22
- const axios = require('axios');
23
23
  const btoa = require('btoa');
24
24
  const lodash = require('lodash');
25
25
  const moment = require('moment');
26
26
  const uuid = require('uuid');
27
27
  const nanoid = require('nanoid');
28
+ const axios = require('axios');
29
+ const fetch = require('node-fetch');
28
30
  const CryptoJS = require('crypto-js');
29
31
 
30
32
  class TestRuntime {
@@ -49,7 +51,7 @@ class TestRuntime {
49
51
 
50
52
  const whitelistedModules = {};
51
53
 
52
- for(let module of moduleWhitelist) {
54
+ for (let module of moduleWhitelist) {
53
55
  try {
54
56
  whitelistedModules[module] = require(module);
55
57
  } catch (e) {
@@ -111,14 +113,16 @@ class TestRuntime {
111
113
  punycode,
112
114
  zlib,
113
115
  // 3rd party libs
114
- atob,
115
- axios,
116
+ ajv,
116
117
  btoa,
118
+ atob,
117
119
  lodash,
118
120
  moment,
119
121
  uuid,
120
122
  nanoid,
123
+ axios,
121
124
  chai,
125
+ 'node-fetch': fetch,
122
126
  'crypto-js': CryptoJS,
123
127
  ...whitelistedModules,
124
128
  fs: allowScriptFilesystemAccess ? fs : undefined