Streamline management of API collections #1

Merged
administrator merged 43 commits from feature/script-autogenerate-http into main 2026-08-19 14:54:42 +02:00
2 changed files with 23 additions and 9 deletions
Showing only changes of commit 42b8f337af - Show all commits
+9 -8
View File
@@ -179,7 +179,7 @@ function parseHttpBlock(text) {
}; };
} }
function formatVariableValue(value) { function formatVariableValue(value, renderContext = {}) {
if (value === null || value === undefined) { if (value === null || value === undefined) {
return ''; return '';
} }
@@ -188,10 +188,11 @@ function formatVariableValue(value) {
if (value.trim() === '') { if (value.trim() === '') {
return ''; return '';
} }
if (/\s/.test(value)) { const renderedValue = renderContext.renderValue ? renderContext.renderValue(value) : value;
return `"${value.replace(/"/g, '\\"')}"`; if (/\s/.test(renderedValue)) {
return `"${renderedValue.replace(/"/g, '\\"')}"`;
} }
return value; return renderedValue;
} }
return JSON.stringify(value); return JSON.stringify(value);
@@ -306,7 +307,7 @@ export function mergeRequestConfig(base, updates) {
return merged; return merged;
} }
function buildRequestContent(request, requestName, requestConfig = {}, dotenvVariables = new Set()) { export function buildRequestContent(request, requestName, requestConfig = {}, dotenvVariables = new Set()) {
const lines = []; const lines = [];
const variableDefinitions = []; const variableDefinitions = [];
const commentedVariableDefinitions = []; const commentedVariableDefinitions = [];
@@ -478,7 +479,7 @@ function buildRequestContent(request, requestName, requestConfig = {}, dotenvVar
if (commentedVariableDefinitions.length > 0) { if (commentedVariableDefinitions.length > 0) {
lines.push(`# Other variables for ${requestName}`); lines.push(`# Other variables for ${requestName}`);
for (const variable of commentedVariableDefinitions) { for (const variable of commentedVariableDefinitions) {
lines.push(`# ${sanitizeVarName(variable.name)} = ${formatVariableValue(variable.value)}`); lines.push(`# ${sanitizeVarName(variable.name)} = ${formatVariableValue(variable.value, { renderValue })}`);
} }
lines.push(''); lines.push('');
} }
@@ -486,7 +487,7 @@ function buildRequestContent(request, requestName, requestConfig = {}, dotenvVar
if (parameterVariableDefinitions.length > 0) { if (parameterVariableDefinitions.length > 0) {
lines.push(`# Parameter variables for ${requestName}`); lines.push(`# Parameter variables for ${requestName}`);
for (const variable of parameterVariableDefinitions) { for (const variable of parameterVariableDefinitions) {
lines.push(`@${sanitizeVarName(variable.name)} = ${formatVariableValue(variable.value)}`); lines.push(`@${sanitizeVarName(variable.name)} = ${formatVariableValue(variable.value, { renderValue })}`);
} }
lines.push(''); lines.push('');
} }
@@ -524,7 +525,7 @@ function buildRequestContent(request, requestName, requestConfig = {}, dotenvVar
if (variableDefinitions.length > 0) { if (variableDefinitions.length > 0) {
lines.push(`# Variables for ${requestName}`); lines.push(`# Variables for ${requestName}`);
for (const variable of variableDefinitions) { for (const variable of variableDefinitions) {
lines.push(`@${sanitizeVarName(variable.name)} = ${formatVariableValue(variable.value)}`); lines.push(`@${sanitizeVarName(variable.name)} = ${formatVariableValue(variable.value, { renderValue })}`);
} }
lines.push(''); lines.push('');
} }
+14 -1
View File
@@ -3,7 +3,7 @@ import assert from 'node:assert/strict';
import fs from 'node:fs'; import fs from 'node:fs';
import os from 'node:os'; import os from 'node:os';
import path from 'node:path'; 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', () => { test('does not inherit parent auth when a child config has no auth override', () => {
const parentAuth = { type: 'bearer', token: 'parent-token' }; 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' }); 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\}\}"/);
});