fix rendering of environment-backed variables
This commit is contained in:
@@ -179,7 +179,7 @@ function parseHttpBlock(text) {
|
||||
};
|
||||
}
|
||||
|
||||
function formatVariableValue(value) {
|
||||
function formatVariableValue(value, renderContext = {}) {
|
||||
if (value === null || value === undefined) {
|
||||
return '';
|
||||
}
|
||||
@@ -188,10 +188,11 @@ function formatVariableValue(value) {
|
||||
if (value.trim() === '') {
|
||||
return '';
|
||||
}
|
||||
if (/\s/.test(value)) {
|
||||
return `"${value.replace(/"/g, '\\"')}"`;
|
||||
const renderedValue = renderContext.renderValue ? renderContext.renderValue(value) : value;
|
||||
if (/\s/.test(renderedValue)) {
|
||||
return `"${renderedValue.replace(/"/g, '\\"')}"`;
|
||||
}
|
||||
return value;
|
||||
return renderedValue;
|
||||
}
|
||||
|
||||
return JSON.stringify(value);
|
||||
@@ -306,7 +307,7 @@ export function mergeRequestConfig(base, updates) {
|
||||
return merged;
|
||||
}
|
||||
|
||||
function buildRequestContent(request, requestName, requestConfig = {}, dotenvVariables = new Set()) {
|
||||
export function buildRequestContent(request, requestName, requestConfig = {}, dotenvVariables = new Set()) {
|
||||
const lines = [];
|
||||
const variableDefinitions = [];
|
||||
const commentedVariableDefinitions = [];
|
||||
@@ -478,7 +479,7 @@ function buildRequestContent(request, requestName, requestConfig = {}, dotenvVar
|
||||
if (commentedVariableDefinitions.length > 0) {
|
||||
lines.push(`# Other variables for ${requestName}`);
|
||||
for (const variable of commentedVariableDefinitions) {
|
||||
lines.push(`# ${sanitizeVarName(variable.name)} = ${formatVariableValue(variable.value)}`);
|
||||
lines.push(`# ${sanitizeVarName(variable.name)} = ${formatVariableValue(variable.value, { renderValue })}`);
|
||||
}
|
||||
lines.push('');
|
||||
}
|
||||
@@ -486,7 +487,7 @@ function buildRequestContent(request, requestName, requestConfig = {}, dotenvVar
|
||||
if (parameterVariableDefinitions.length > 0) {
|
||||
lines.push(`# Parameter variables for ${requestName}`);
|
||||
for (const variable of parameterVariableDefinitions) {
|
||||
lines.push(`@${sanitizeVarName(variable.name)} = ${formatVariableValue(variable.value)}`);
|
||||
lines.push(`@${sanitizeVarName(variable.name)} = ${formatVariableValue(variable.value, { renderValue })}`);
|
||||
}
|
||||
lines.push('');
|
||||
}
|
||||
@@ -524,7 +525,7 @@ function buildRequestContent(request, requestName, requestConfig = {}, dotenvVar
|
||||
if (variableDefinitions.length > 0) {
|
||||
lines.push(`# Variables for ${requestName}`);
|
||||
for (const variable of variableDefinitions) {
|
||||
lines.push(`@${sanitizeVarName(variable.name)} = ${formatVariableValue(variable.value)}`);
|
||||
lines.push(`@${sanitizeVarName(variable.name)} = ${formatVariableValue(variable.value, { renderValue })}`);
|
||||
}
|
||||
lines.push('');
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { getRequestConfigForFile, mergeRequestConfig } from './generate-http-docs.js';
|
||||
import { buildRequestContent, getRequestConfigForFile, mergeRequestConfig } from './generate-http-docs.js';
|
||||
|
||||
test('does not inherit parent auth when a child config has no auth override', () => {
|
||||
const parentAuth = { type: 'bearer', token: 'parent-token' };
|
||||
@@ -32,3 +32,16 @@ test('reads auth inherit from a Bruno-style http block', () => {
|
||||
|
||||
assert.deepEqual(config.auth, { type: 'bearer', token: 'parent-token' });
|
||||
});
|
||||
|
||||
test('renders dotenv placeholders in generated variable definitions', () => {
|
||||
const request = {
|
||||
url: 'https://example.test',
|
||||
params: [
|
||||
{ name: 'username', value: '{{elixFormsApiUsername}}', type: 'path' },
|
||||
],
|
||||
};
|
||||
|
||||
const output = buildRequestContent(request, 'Logout', {}, new Set(['elixFormsApiUsername']));
|
||||
|
||||
assert.match(output, /@username = "\{\{\$dotenv elixFormsApiUsername\}\}"/);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user