@usebruno/js 0.11.0 → 0.12.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.11.0",
3
+ "version": "0.12.0",
4
4
  "license": "MIT",
5
5
  "main": "src/index.js",
6
6
  "files": [
package/src/bru.js CHANGED
@@ -1,14 +1,24 @@
1
1
  const Handlebars = require('handlebars');
2
2
  const { cloneDeep } = require('lodash');
3
+ const { uuid } = require('./utils');
3
4
 
4
5
  const variableNameRegex = /^[\w-.]*$/;
5
6
 
6
7
  class Bru {
7
- constructor(envVariables, collectionVariables, processEnvVars, collectionPath) {
8
+ constructor(
9
+ envVariables,
10
+ collectionVariables,
11
+ processEnvVars,
12
+ collectionPath,
13
+ historyLogger,
14
+ setHtmlVisualizationString
15
+ ) {
8
16
  this.envVariables = envVariables;
9
17
  this.collectionVariables = collectionVariables;
10
18
  this.processEnvVars = cloneDeep(processEnvVars || {});
11
19
  this.collectionPath = collectionPath;
20
+ this.historyLogger = historyLogger;
21
+ this.setHtmlVisualizationString = setHtmlVisualizationString;
12
22
  }
13
23
 
14
24
  _interpolateEnvVar = (str) => {
@@ -48,6 +58,15 @@ class Bru {
48
58
  throw new Error('Creating a env variable without specifying a name is not allowed.');
49
59
  }
50
60
 
61
+ if (this.historyLogger) {
62
+ this.historyLogger({
63
+ uid: uuid(),
64
+ type: 'setEnvVar()',
65
+ data: { key, value },
66
+ createdAt: new Date().toISOString()
67
+ });
68
+ }
69
+
51
70
  this.envVariables[key] = value;
52
71
  }
53
72
 
@@ -63,6 +82,15 @@ class Bru {
63
82
  );
64
83
  }
65
84
 
85
+ if (this.historyLogger) {
86
+ this.historyLogger({
87
+ uid: uuid(),
88
+ type: 'setVar()',
89
+ data: { key, value },
90
+ createdAt: new Date().toISOString()
91
+ });
92
+ }
93
+
66
94
  this.collectionVariables[key] = value;
67
95
  }
68
96
 
@@ -80,6 +108,10 @@ class Bru {
80
108
  setNextRequest(nextRequest) {
81
109
  this.nextRequest = nextRequest;
82
110
  }
111
+
112
+ visualize(htmlString) {
113
+ this.setHtmlVisualizationString(htmlString);
114
+ }
83
115
  }
84
116
 
85
117
  module.exports = Bru;
@@ -1,11 +1,14 @@
1
+ const { uuid } = require('./utils');
2
+
1
3
  class BrunoRequest {
2
- constructor(req) {
4
+ constructor(req, historyLogger) {
3
5
  this.req = req;
4
6
  this.url = req.url;
5
7
  this.method = req.method;
6
8
  this.headers = req.headers;
7
9
  this.body = req.data;
8
10
  this.timeout = req.timeout;
11
+ this.historyLogger = historyLogger;
9
12
  }
10
13
 
11
14
  getUrl() {
@@ -53,6 +56,14 @@ class BrunoRequest {
53
56
  }
54
57
 
55
58
  setHeader(name, value) {
59
+ if (this.historyLogger) {
60
+ this.historyLogger({
61
+ uid: uuid(),
62
+ type: 'setHeader()',
63
+ data: { name, value },
64
+ createdAt: new Date().toISOString()
65
+ });
66
+ }
56
67
  this.req.headers[name] = value;
57
68
  }
58
69
 
@@ -61,6 +72,14 @@ class BrunoRequest {
61
72
  }
62
73
 
63
74
  setBody(data) {
75
+ if (this.historyLogger) {
76
+ this.historyLogger({
77
+ uid: uuid(),
78
+ type: 'setBody()',
79
+ data: { body: data },
80
+ createdAt: new Date().toISOString()
81
+ });
82
+ }
64
83
  this.req.data = data;
65
84
  }
66
85
 
@@ -3,7 +3,7 @@ const chai = require('chai');
3
3
  const { nanoid } = require('nanoid');
4
4
  const Bru = require('../bru');
5
5
  const BrunoRequest = require('../bruno-request');
6
- const { evaluateJsTemplateLiteral, evaluateJsExpression, createResponseParser } = require('../utils');
6
+ const { evaluateJsTemplateLiteral, evaluateJsExpression, createResponseParser, uuid } = require('../utils');
7
7
  const { interpolateString } = require('../interpolate-string');
8
8
 
9
9
  const { expect } = chai;
@@ -199,14 +199,14 @@ const evaluateRhsOperand = (rhsOperand, operator, context) => {
199
199
  };
200
200
 
201
201
  class AssertRuntime {
202
- runAssertions(assertions, request, response, envVariables, collectionVariables, collectionPath) {
202
+ runAssertions(assertions, request, response, envVariables, collectionVariables, collectionPath, historyLogger) {
203
203
  const enabledAssertions = _.filter(assertions, (a) => a.enabled);
204
204
  if (!enabledAssertions.length) {
205
205
  return [];
206
206
  }
207
207
 
208
208
  const bru = new Bru(envVariables, collectionVariables);
209
- const req = new BrunoRequest(request);
209
+ const req = new BrunoRequest(request, historyLogger);
210
210
  const res = createResponseParser(response);
211
211
 
212
212
  const bruContext = {
@@ -339,6 +339,15 @@ class AssertRuntime {
339
339
  }
340
340
  }
341
341
 
342
+ if (historyLogger) {
343
+ historyLogger({
344
+ uid: uuid(),
345
+ type: 'network',
346
+ assertions: assertionResults,
347
+ createdAt: new Date().toISOString()
348
+ });
349
+ }
350
+
342
351
  return assertionResults;
343
352
  }
344
353
  }
@@ -42,10 +42,22 @@ class ScriptRuntime {
42
42
  collectionPath,
43
43
  onConsoleLog,
44
44
  processEnvVars,
45
- scriptingConfig
45
+ scriptingConfig,
46
+ historyLogger
46
47
  ) {
47
- const bru = new Bru(envVariables, collectionVariables, processEnvVars, collectionPath);
48
- const req = new BrunoRequest(request);
48
+ let htmlVisualizationString;
49
+ let setHtmlVisualizationString = (htmlString) => {
50
+ htmlVisualizationString = htmlString;
51
+ };
52
+ const bru = new Bru(
53
+ envVariables,
54
+ collectionVariables,
55
+ processEnvVars,
56
+ collectionPath,
57
+ historyLogger,
58
+ setHtmlVisualizationString
59
+ );
60
+ const req = new BrunoRequest(request, historyLogger);
49
61
  const allowScriptFilesystemAccess = get(scriptingConfig, 'filesystemAccess.allow', false);
50
62
  const moduleWhitelist = get(scriptingConfig, 'moduleWhitelist', []);
51
63
  const additionalContextRoots = get(scriptingConfig, 'additionalContextRoots', []);
@@ -126,6 +138,7 @@ class ScriptRuntime {
126
138
  request,
127
139
  envVariables: cleanJson(envVariables),
128
140
  collectionVariables: cleanJson(collectionVariables),
141
+ htmlVisualizationString,
129
142
  nextRequestName: bru.nextRequest
130
143
  };
131
144
  }
@@ -139,10 +152,22 @@ class ScriptRuntime {
139
152
  collectionPath,
140
153
  onConsoleLog,
141
154
  processEnvVars,
142
- scriptingConfig
155
+ scriptingConfig,
156
+ historyLogger
143
157
  ) {
144
- const bru = new Bru(envVariables, collectionVariables, processEnvVars, collectionPath);
145
- const req = new BrunoRequest(request);
158
+ let htmlVisualizationString;
159
+ let setHtmlVisualizationString = (htmlString) => {
160
+ htmlVisualizationString = htmlString;
161
+ };
162
+ const bru = new Bru(
163
+ envVariables,
164
+ collectionVariables,
165
+ processEnvVars,
166
+ collectionPath,
167
+ historyLogger,
168
+ setHtmlVisualizationString
169
+ );
170
+ const req = new BrunoRequest(request, historyLogger);
146
171
  const res = new BrunoResponse(response);
147
172
  const allowScriptFilesystemAccess = get(scriptingConfig, 'filesystemAccess.allow', false);
148
173
  const moduleWhitelist = get(scriptingConfig, 'moduleWhitelist', []);
@@ -220,6 +245,7 @@ class ScriptRuntime {
220
245
  response,
221
246
  envVariables: cleanJson(envVariables),
222
247
  collectionVariables: cleanJson(collectionVariables),
248
+ htmlVisualizationString,
223
249
  nextRequestName: bru.nextRequest
224
250
  };
225
251
  }
@@ -43,10 +43,11 @@ class TestRuntime {
43
43
  collectionPath,
44
44
  onConsoleLog,
45
45
  processEnvVars,
46
- scriptingConfig
46
+ scriptingConfig,
47
+ historyLogger
47
48
  ) {
48
- const bru = new Bru(envVariables, collectionVariables, processEnvVars, collectionPath);
49
- const req = new BrunoRequest(request);
49
+ const bru = new Bru(envVariables, collectionVariables, processEnvVars, collectionPath, historyLogger);
50
+ const req = new BrunoRequest(request, historyLogger);
50
51
  const res = new BrunoResponse(response);
51
52
  const allowScriptFilesystemAccess = get(scriptingConfig, 'filesystemAccess.allow', false);
52
53
  const moduleWhitelist = get(scriptingConfig, 'moduleWhitelist', []);
@@ -142,6 +143,15 @@ class TestRuntime {
142
143
  const asyncVM = vm.run(`module.exports = async () => { ${testsFile}}`, path.join(collectionPath, 'vm.js'));
143
144
  await asyncVM();
144
145
 
146
+ if (historyLogger) {
147
+ historyLogger({
148
+ uid: uuid.v4(),
149
+ type: 'network',
150
+ tests: cleanJson(__brunoTestResults.getResults()),
151
+ createdAt: new Date().toISOString()
152
+ });
153
+ }
154
+
145
155
  return {
146
156
  request,
147
157
  envVariables: cleanJson(envVariables),
@@ -4,14 +4,14 @@ const BrunoRequest = require('../bruno-request');
4
4
  const { evaluateJsTemplateLiteral, evaluateJsExpression, createResponseParser } = require('../utils');
5
5
 
6
6
  class VarsRuntime {
7
- runPreRequestVars(vars, request, envVariables, collectionVariables, collectionPath, processEnvVars) {
7
+ runPreRequestVars(vars, request, envVariables, collectionVariables, collectionPath, processEnvVars, historyLogger) {
8
8
  const enabledVars = _.filter(vars, (v) => v.enabled);
9
9
  if (!enabledVars.length) {
10
10
  return;
11
11
  }
12
12
 
13
- const bru = new Bru(envVariables, collectionVariables, processEnvVars);
14
- const req = new BrunoRequest(request);
13
+ const bru = new Bru(envVariables, collectionVariables, processEnvVars, null, historyLogger);
14
+ const req = new BrunoRequest(request, historyLogger);
15
15
 
16
16
  const bruContext = {
17
17
  bru,
@@ -34,14 +34,23 @@ class VarsRuntime {
34
34
  };
35
35
  }
36
36
 
37
- runPostResponseVars(vars, request, response, envVariables, collectionVariables, collectionPath, processEnvVars) {
37
+ runPostResponseVars(
38
+ vars,
39
+ request,
40
+ response,
41
+ envVariables,
42
+ collectionVariables,
43
+ collectionPath,
44
+ processEnvVars,
45
+ historyLogger
46
+ ) {
38
47
  const enabledVars = _.filter(vars, (v) => v.enabled);
39
48
  if (!enabledVars.length) {
40
49
  return;
41
50
  }
42
51
 
43
- const bru = new Bru(envVariables, collectionVariables, processEnvVars);
44
- const req = new BrunoRequest(request);
52
+ const bru = new Bru(envVariables, collectionVariables, processEnvVars, null, historyLogger);
53
+ const req = new BrunoRequest(request, historyLogger);
45
54
  const res = createResponseParser(response);
46
55
 
47
56
  const bruContext = {
package/src/utils.js CHANGED
@@ -1,5 +1,6 @@
1
1
  const jsonQuery = require('json-query');
2
2
  const { get } = require('@usebruno/query');
3
+ const { customAlphabet } = require('nanoid');
3
4
 
4
5
  const JS_KEYWORDS = `
5
6
  break case catch class const continue debugger default delete do
@@ -142,10 +143,20 @@ const cleanJson = (data) => {
142
143
  }
143
144
  };
144
145
 
146
+ // a customized version of nanoid without using _ and -
147
+ const uuid = () => {
148
+ // https://github.com/ai/nanoid/blob/main/url-alphabet/index.js
149
+ const urlAlphabet = 'useandom26T198340PX75pxJACKVERYMINDBUSHWOLFGQZbfghjklqvwyzrict';
150
+ const customNanoId = customAlphabet(urlAlphabet, 21);
151
+
152
+ return customNanoId();
153
+ };
154
+
145
155
  module.exports = {
146
156
  evaluateJsExpression,
147
157
  evaluateJsTemplateLiteral,
148
158
  createResponseParser,
149
159
  internalExpressionCache,
150
- cleanJson
160
+ cleanJson,
161
+ uuid
151
162
  };