mirror of
https://github.com/usebruno/bruno.git
synced 2026-07-07 22:18:33 +00:00
feat: enhance support for prompt variables
This commit is contained in:
@@ -31,6 +31,59 @@ const onConsoleLog = (type, args) => {
|
||||
console[type](...args);
|
||||
};
|
||||
|
||||
const getCACertHostRegex = (domain) => {
|
||||
return '^https:\\/\\/' + domain.replaceAll('.', '\\.').replaceAll('*', '.*');
|
||||
};
|
||||
|
||||
/**
|
||||
* Extract prompt variables from a request
|
||||
* Tries to respect the hierarchy of the variables and avoid unnecessary prompts as much as possible
|
||||
* Note: TO BE CALLED ONLY AFTER THE PREPARE REQUEST
|
||||
*
|
||||
* @param {*} request - request object built by prepareRequest
|
||||
* @returns {string[]} An array of extracted prompt variables
|
||||
*/
|
||||
const extractPromptVariablesForRequest = ({ request, collection, envVariables, runtimeVariables, processEnvVars, brunoConfig }) => {
|
||||
const { vars, collectionVariables, folderVariables, requestVariables, ...requestObj } = request;
|
||||
|
||||
const allVariables = {
|
||||
...envVariables,
|
||||
...collectionVariables,
|
||||
...folderVariables,
|
||||
...requestVariables,
|
||||
...runtimeVariables,
|
||||
process: {
|
||||
env: {
|
||||
...processEnvVars
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const prompts = extractPromptVariables(requestObj);
|
||||
prompts.push(...extractPromptVariables(allVariables));
|
||||
|
||||
const interpolationOptions = {
|
||||
envVars: envVariables,
|
||||
runtimeVariables,
|
||||
processEnvVars
|
||||
};
|
||||
|
||||
// client certificate config
|
||||
const clientCertConfig = get(brunoConfig, 'clientCertificates.certs', []);
|
||||
for (let clientCert of clientCertConfig) {
|
||||
const domain = interpolateString(clientCert?.domain, interpolationOptions);
|
||||
if (domain) {
|
||||
const hostRegex = getCACertHostRegex(domain);
|
||||
if (request.url.match(hostRegex)) {
|
||||
prompts.push(...extractPromptVariables(clientCert));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// return unique prompt variables
|
||||
return Array.from(new Set(prompts));
|
||||
};
|
||||
|
||||
const runSingleRequest = async function (
|
||||
item,
|
||||
collectionPath,
|
||||
@@ -75,10 +128,11 @@ const runSingleRequest = async function (
|
||||
request = await prepareRequest(item, collection);
|
||||
|
||||
// Detect prompt variables before proceeding
|
||||
const promptVars = extractPromptVariables(request);
|
||||
const promptVars = extractPromptVariablesForRequest({ request, collection, envVariables, runtimeVariables, processEnvVars, brunoConfig });
|
||||
|
||||
if (promptVars.length > 0) {
|
||||
const errorMsg = 'Prompt variables detected in request. CLI execution is not supported for requests with prompt variables.';
|
||||
console.log(chalk.red(stripExtension(relativeItemPathname)) + chalk.dim(` (${errorMsg})`));
|
||||
const errorMsg = `Prompt variables detected in request. CLI execution is not supported for requests with prompt variables. \nPrompts: ${promptVars.join(', ')}`;
|
||||
console.log(chalk.yellow(stripExtension(relativeItemPathname) + ' Skipped:') + chalk.dim(` (${errorMsg})`));
|
||||
return {
|
||||
test: {
|
||||
filename: relativeItemPathname
|
||||
@@ -204,7 +258,7 @@ const runSingleRequest = async function (
|
||||
const domain = interpolateString(clientCert?.domain, interpolationOptions);
|
||||
const type = clientCert?.type || 'cert';
|
||||
if (domain) {
|
||||
const hostRegex = '^https:\\/\\/' + domain.replaceAll('.', '\\.').replaceAll('*', '.*');
|
||||
const hostRegex = getCACertHostRegex(domain);
|
||||
if (request.url.match(hostRegex)) {
|
||||
if (type === 'cert') {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user