api-client/helpers/preRequest.js

21 lines
670 B
JavaScript
Raw Normal View History

2019-10-30 01:28:44 +00:00
export default function getEnvironmentVariablesFromScript(script) {
2020-02-24 18:44:50 +00:00
let _variables = {}
2019-10-30 01:28:44 +00:00
// the pw object is the proxy by which pre-request scripts can pass variables to the request.
// for security and control purposes, this is the only way a pre-request script should modify variables.
let pw = {
environment: {
2020-02-24 18:44:50 +00:00
set: (key, value) => (_variables[key] = value),
2019-10-30 01:28:44 +00:00
},
env: {
2020-02-24 18:44:50 +00:00
set: (key, value) => (_variables[key] = value),
},
2019-10-30 01:28:44 +00:00
// globals that the script is allowed to have access to.
2020-02-24 18:44:50 +00:00
}
2019-10-30 01:28:44 +00:00
// run pre-request script within this function so that it has access to the pw object.
new Function("pw", script)(pw)
2019-10-30 01:28:44 +00:00
2020-02-24 18:44:50 +00:00
return _variables
2019-10-30 01:28:44 +00:00
}