@usebruno/js
Advanced tools
Comparing version 0.15.0 to 0.16.0
{ | ||
"name": "@usebruno/js", | ||
"version": "0.15.0", | ||
"version": "0.16.0", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -8,12 +8,3 @@ const { cloneDeep } = require('lodash'); | ||
class Bru { | ||
constructor( | ||
envVariables, | ||
runtimeVariables, | ||
processEnvVars, | ||
collectionPath, | ||
historyLogger, | ||
setVisualizations, | ||
secretVariables, | ||
requestVariables | ||
) { | ||
constructor(envVariables, runtimeVariables, processEnvVars, collectionPath, historyLogger, setVisualizations, secretVariables, collectionVariables, folderVariables, requestVariables) { | ||
this.envVariables = envVariables || {}; | ||
@@ -23,2 +14,4 @@ this.runtimeVariables = runtimeVariables || {}; | ||
this.secretVariables = cloneDeep(secretVariables || {}); | ||
this.collectionVariables = collectionVariables || {}; | ||
this.folderVariables = folderVariables || {}; | ||
this.requestVariables = requestVariables || {}; | ||
@@ -36,3 +29,5 @@ this.collectionPath = collectionPath; | ||
const combinedVars = { | ||
...this.collectionVariables, | ||
...this.envVariables, | ||
...this.folderVariables, | ||
...this.requestVariables, | ||
@@ -99,3 +94,3 @@ ...this.runtimeVariables, | ||
`Variable name: "${key}" contains invalid characters!` + | ||
' Names must only contain alpha-numeric characters, "-", "_", "."' | ||
' Names must only contain alpha-numeric characters, "-", "_", "."' | ||
); | ||
@@ -120,3 +115,3 @@ } | ||
`Variable name: "${key}" contains invalid characters!` + | ||
' Names must only contain alpha-numeric characters, "-", "_", "."' | ||
' Names must only contain alpha-numeric characters, "-", "_", "."' | ||
); | ||
@@ -132,2 +127,10 @@ } | ||
getCollectionVar(key) { | ||
return this._interpolate(this.collectionVariables[key]); | ||
} | ||
getFolderVar(key) { | ||
return this._interpolate(this.folderVariables[key]); | ||
} | ||
getRequestVar(key) { | ||
@@ -167,3 +170,3 @@ return this._interpolate(this.requestVariables[key]); | ||
} | ||
sleep(ms) { | ||
@@ -170,0 +173,0 @@ return new Promise((resolve) => setTimeout(resolve, ms)); |
const { uuid } = require('./utils'); | ||
class BrunoRequest { | ||
/** | ||
* The following properties are available as shorthand: | ||
* - req.url | ||
* - req.method | ||
* - req.headers | ||
* - req.timeout | ||
* - req.body | ||
* | ||
* Above shorthands are useful for accessing the request properties directly in the scripts | ||
* It must be noted that the user cannot set these properties directly. | ||
* They should use the respective setter methods to set these properties. | ||
*/ | ||
constructor(req, historyLogger) { | ||
@@ -9,5 +21,16 @@ this.req = req; | ||
this.headers = req.headers; | ||
this.body = req.data; | ||
this.timeout = req.timeout; | ||
this.historyLogger = historyLogger; | ||
/** | ||
* We automatically parse the JSON body if the content type is JSON | ||
* This is to make it easier for the user to access the body directly | ||
* | ||
* It must be noted that the request data is always a string and is what gets sent over the network | ||
* If the user wants to access the raw data, they can use getBody({raw: true}) method | ||
*/ | ||
const isJson = this.hasJSONContentType(this.req.headers); | ||
if (isJson) { | ||
this.body = this.__safeParseJSON(req.data); | ||
} | ||
} | ||
@@ -20,2 +43,3 @@ | ||
setUrl(url) { | ||
this.url = url; | ||
this.req.url = url; | ||
@@ -45,2 +69,3 @@ } | ||
setMethod(method) { | ||
this.method = method; | ||
this.req.method = method; | ||
@@ -54,2 +79,3 @@ } | ||
setHeaders(headers) { | ||
this.headers = headers; | ||
this.req.headers = headers; | ||
@@ -71,10 +97,41 @@ } | ||
} | ||
this.headers[name] = value; | ||
this.req.headers[name] = value; | ||
} | ||
getBody() { | ||
hasJSONContentType(headers) { | ||
const contentType = headers?.['Content-Type'] || headers?.['content-type'] || ''; | ||
return contentType.includes('json'); | ||
} | ||
/** | ||
* Get the body of the request | ||
* | ||
* We automatically parse and return the JSON body if the content type is JSON | ||
* If the user wants the raw body, they can pass the raw option as true | ||
*/ | ||
getBody(options = {}) { | ||
if (options.raw) { | ||
return this.req.data; | ||
} | ||
const isJson = this.hasJSONContentType(this.req.headers); | ||
if (isJson) { | ||
return this.__safeParseJSON(this.req.data); | ||
} | ||
return this.req.data; | ||
} | ||
setBody(data) { | ||
/** | ||
* If the content type is JSON and if the data is an object | ||
* - We set the body property as the object itself | ||
* - We set the request data as the stringified JSON as it is what gets sent over the network | ||
* Otherwise | ||
* - We set the request data as the data itself | ||
* - We set the body property as the data itself | ||
* | ||
* If the user wants to override this behavior, they can pass the raw option as true | ||
*/ | ||
setBody(data, options = {}) { | ||
if (this.historyLogger) { | ||
@@ -88,3 +145,18 @@ this.historyLogger({ | ||
} | ||
if (options.raw) { | ||
this.req.data = data; | ||
this.body = data; | ||
return; | ||
} | ||
const isJson = this.hasJSONContentType(this.req.headers); | ||
if (isJson && this.__isObject(data)) { | ||
this.body = data; | ||
this.req.data = this.__safeStringifyJSON(data); | ||
return; | ||
} | ||
this.req.data = data; | ||
this.body = data; | ||
} | ||
@@ -101,6 +173,32 @@ | ||
setTimeout(timeout) { | ||
this.timeout = timeout; | ||
this.req.timeout = timeout; | ||
} | ||
__safeParseJSON(str) { | ||
try { | ||
return JSON.parse(str); | ||
} catch (e) { | ||
return str; | ||
} | ||
} | ||
__safeStringifyJSON(obj) { | ||
try { | ||
return JSON.stringify(obj); | ||
} catch (e) { | ||
return obj; | ||
} | ||
} | ||
__isObject(obj) { | ||
return obj !== null && typeof obj === 'object'; | ||
} | ||
disableParsingResponseJson() { | ||
this.req.__brunoDisableParsingResponseJson = true; | ||
} | ||
} | ||
module.exports = BrunoRequest; |
@@ -5,3 +5,3 @@ const { interpolate } = require('@usebruno/common'); | ||
str, | ||
{ envVariables = {}, runtimeVariables = {}, processEnvVars = {}, requestVariables = {} } | ||
{ envVariables = {}, runtimeVariables = {}, processEnvVars = {}, collectionVariables = {}, folderVariables = {}, requestVariables = {} } | ||
) => { | ||
@@ -13,3 +13,5 @@ if (!str || !str.length || typeof str !== 'string') { | ||
const combinedVars = { | ||
...collectionVariables, | ||
...envVariables, | ||
...folderVariables, | ||
...requestVariables, | ||
@@ -16,0 +18,0 @@ ...runtimeVariables, |
@@ -195,2 +195,4 @@ const _ = require('lodash'); | ||
const interpolationContext = { | ||
collectionVariables: context.bru.collectionVariables, | ||
folderVariables: context.bru.folderVariables, | ||
requestVariables: context.bru.requestVariables, | ||
@@ -242,2 +244,4 @@ runtimeVariables: context.bru.runtimeVariables, | ||
runAssertions(assertions, request, response, envVariables, runtimeVariables, processEnvVars, historyLogger, secretVariables) { | ||
const collectionVariables = request?.collectionVariables || {}; | ||
const folderVariables = request?.folderVariables || {}; | ||
const requestVariables = request?.requestVariables || {}; | ||
@@ -257,2 +261,4 @@ const enabledAssertions = _.filter(assertions, (a) => a.enabled); | ||
secretVariables, | ||
collectionVariables, | ||
folderVariables, | ||
requestVariables | ||
@@ -270,3 +276,5 @@ ); | ||
const context = { | ||
...collectionVariables, | ||
...envVariables, | ||
...folderVariables, | ||
...requestVariables, | ||
@@ -273,0 +281,0 @@ ...runtimeVariables, |
@@ -56,13 +56,6 @@ const { NodeVM } = require('@usebruno/vm2'); | ||
} | ||
const collectionVariables = request?.collectionVariables || {}; | ||
const folderVariables = request?.folderVariables || {}; | ||
const requestVariables = request?.requestVariables || {}; | ||
const bru = new Bru( | ||
envVariables, | ||
runtimeVariables, | ||
processEnvVars, | ||
collectionPath, | ||
historyLogger, | ||
setVisualizations, | ||
secretVariables, | ||
requestVariables | ||
); | ||
const bru = new Bru(envVariables, runtimeVariables, processEnvVars, collectionPath, historyLogger, setVisualizations, secretVariables, collectionVariables, folderVariables, requestVariables); | ||
const req = new BrunoRequest(request, historyLogger); | ||
@@ -189,13 +182,6 @@ const allowScriptFilesystemAccess = get(scriptingConfig, 'filesystemAccess.allow', false); | ||
} | ||
const collectionVariables = request?.collectionVariables || {}; | ||
const folderVariables = request?.folderVariables || {}; | ||
const requestVariables = request?.requestVariables || {}; | ||
const bru = new Bru( | ||
envVariables, | ||
runtimeVariables, | ||
processEnvVars, | ||
collectionPath, | ||
historyLogger, | ||
setVisualizations, | ||
secretVariables, | ||
requestVariables | ||
); | ||
const bru = new Bru(envVariables, runtimeVariables, processEnvVars, collectionPath, historyLogger, setVisualizations, secretVariables, collectionVariables, folderVariables, requestVariables); | ||
const req = new BrunoRequest(request, historyLogger); | ||
@@ -202,0 +188,0 @@ const res = new BrunoResponse(response); |
@@ -53,13 +53,6 @@ const { NodeVM } = require('@usebruno/vm2'); | ||
) { | ||
const collectionVariables = request?.collectionVariables || {}; | ||
const folderVariables = request?.folderVariables || {}; | ||
const requestVariables = request?.requestVariables || {}; | ||
const bru = new Bru( | ||
envVariables, | ||
runtimeVariables, | ||
processEnvVars, | ||
collectionPath, | ||
historyLogger, | ||
undefined, // setVisualizations | ||
secretVariables, | ||
requestVariables | ||
); | ||
const bru = new Bru(envVariables, runtimeVariables, processEnvVars, collectionPath, historyLogger, undefined, secretVariables, collectionVariables, folderVariables, requestVariables); | ||
const req = new BrunoRequest(request, historyLogger); | ||
@@ -99,4 +92,2 @@ const res = new BrunoResponse(response); | ||
// add 'await' prefix to the test function calls | ||
testsFile = appendAwaitToTestFunc(testsFile); | ||
@@ -103,0 +94,0 @@ const context = { |
const _ = require('lodash'); | ||
const Bru = require('../bru'); | ||
const BrunoRequest = require('../bruno-request'); | ||
const { evaluateJsTemplateLiteral, evaluateJsExpression, createResponseParser } = require('../utils'); | ||
const { evaluateJsExpression, createResponseParser } = require('../utils'); | ||
const { executeQuickJsVm } = require('../sandbox/quickjs'); | ||
const evaluateJsTemplateLiteralBasedOnRuntime = (literal, context, runtime) => { | ||
if (runtime === 'quickjs') { | ||
return executeQuickJsVm({ | ||
script: literal, | ||
context, | ||
scriptType: 'template-literal' | ||
}); | ||
} | ||
return evaluateJsTemplateLiteral(literal, context); | ||
}; | ||
const evaluateJsExpressionBasedOnRuntime = (expr, context, runtime, mode) => { | ||
@@ -38,52 +26,3 @@ if (runtime === 'quickjs') { | ||
runPreRequestVars(vars, request, envVariables, runtimeVariables, collectionPath, processEnvVars, historyLogger, secretVars = {}) { | ||
if (!request?.requestVariables) { | ||
request.requestVariables = {}; | ||
} | ||
const enabledVars = _.filter(vars, (v) => v.enabled); | ||
if (!enabledVars.length) { | ||
return; | ||
} | ||
const bru = new Bru( | ||
envVariables, | ||
runtimeVariables, | ||
processEnvVars, | ||
null, | ||
historyLogger, | ||
undefined, // setVisualizations | ||
secretVars, | ||
undefined, // requestVariables | ||
); | ||
const req = new BrunoRequest(request, historyLogger); | ||
const bruContext = { | ||
bru, | ||
req | ||
}; | ||
const context = { | ||
...envVariables, | ||
...runtimeVariables, | ||
...secretVars, | ||
...bruContext | ||
}; | ||
_.each(enabledVars, (v) => { | ||
const value = evaluateJsTemplateLiteralBasedOnRuntime(v.value, context, this.runtime); | ||
request?.requestVariables && (request.requestVariables[v.name] = value); | ||
}); | ||
} | ||
runPostResponseVars( | ||
vars, | ||
request, | ||
response, | ||
envVariables, | ||
runtimeVariables, | ||
collectionPath, | ||
processEnvVars, | ||
historyLogger, | ||
secretVars = {} | ||
) { | ||
runPostResponseVars(vars, request, response, envVariables, runtimeVariables, collectionPath, processEnvVars, historyLogger, secretVars = {}) { | ||
const requestVariables = request?.requestVariables || {}; | ||
@@ -90,0 +29,0 @@ const enabledVars = _.filter(vars, (v) => v.enabled); |
@@ -72,2 +72,14 @@ const { marshallToVm } = require('../utils'); | ||
let getFolderVar = vm.newFunction('getFolderVar', function (key) { | ||
return marshallToVm(bru.getFolderVar(vm.dump(key)), vm); | ||
}); | ||
vm.setProp(bruObject, 'getFolderVar', getFolderVar); | ||
getFolderVar.dispose(); | ||
let getCollectionVar = vm.newFunction('getCollectionVar', function (key) { | ||
return marshallToVm(bru.getCollectionVar(vm.dump(key)), vm); | ||
}); | ||
vm.setProp(bruObject, 'getCollectionVar', getCollectionVar); | ||
getCollectionVar.dispose(); | ||
const sleep = vm.newFunction('sleep', (timer) => { | ||
@@ -74,0 +86,0 @@ const t = vm.getString(timer); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
286908
3442