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
Showing only changes of commit ba01e76c75 - Show all commits
+54 -2
View File
@@ -297,6 +297,8 @@ function getRequestConfigForFile(yamlFile, sourceDir) {
function buildRequestContent(request, requestName, requestConfig = {}, dotenvVariables = new Set()) {
const lines = [];
const variableDefinitions = [];
const commentedVariableDefinitions = [];
const parameterVariableDefinitions = [];
const seenVariables = new Set();
const addVariable = (name, value) => {
@@ -311,12 +313,44 @@ function buildRequestContent(request, requestName, requestConfig = {}, dotenvVar
variableDefinitions.push({ name: normalized, value });
};
const addParameterVariable = (name, value) => {
if (!name) {
return;
}
const normalized = String(name).trim();
if (!normalized || seenVariables.has(normalized) || dotenvVariables.has(normalized)) {
return;
}
seenVariables.add(normalized);
parameterVariableDefinitions.push({ name: normalized, value });
};
const addCommentedVariable = (name, value) => {
if (!name) {
return;
}
const normalized = String(name).trim();
if (!normalized) { // || seenVariables.has(normalized) || dotenvVariables.has(normalized)) {
return;
}
// seenVariables.add(normalized);
commentedVariableDefinitions.push({ name: normalized, value });
};
const addReferencedVariables = (value, fallbackValue = 'YOUR_VALUE_HERE') => {
for (const placeholder of collectPlaceholders(String(value))) {
addVariable(placeholder, fallbackValue);
}
};
const addParameterVariables = (name, value) => {
addParameterVariable(name, value);
};
const addCommentedVariables = (name, value) => {
addCommentedVariable(name, value);
};
const renderValue = (value) => {
if (typeof value !== 'string') {
return value;
@@ -364,6 +398,7 @@ function buildRequestContent(request, requestName, requestConfig = {}, dotenvVar
const disabled = String(param.disabled).toLowerCase() === 'true';
if (disabled) {
addCommentedVariables(name, value);
continue;
}
@@ -372,7 +407,8 @@ function buildRequestContent(request, requestName, requestConfig = {}, dotenvVar
if (type === 'header') {
headers.push({ name, value: renderValue(value) });
} else {
queryParams.push({ name, value: renderValue(value) });
queryParams.push({ name, value: `{{${name}}}` });
addParameterVariables(name, value);
}
}
@@ -402,6 +438,22 @@ 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('');
}
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('');
}
if (variableDefinitions.length > 0) {
lines.push(`# Variables for ${requestName}`);
for (const variable of variableDefinitions) {
@@ -524,7 +576,7 @@ function writeEnvironmentTemplates(sourceDir, outputRoot) {
}
}
const templateContent = variableNames.length > 0 ? `${variableNames.map(v => `${v}={{${v}}}`).join('\n')}\n` : '';
const templateContent = variableNames.length > 0 ? `${variableNames.map(v => `${v}=EDIT_VALUE_HERE`).join('\n')}\n` : '';
fs.writeFileSync(path.join(targetDir, '.env.template'), templateContent, 'utf8');
dotenvVariablesByTarget.set(targetDir, new Set(variableNames));
}