Streamline management of API collections #1
+1
-1
@@ -10,4 +10,4 @@ node_modules
|
|||||||
Thumbs.db
|
Thumbs.db
|
||||||
|
|
||||||
# Automatically generated stuff
|
# Automatically generated stuff
|
||||||
autodocs/**/*
|
autogen/**/*
|
||||||
@@ -7,6 +7,7 @@ import { fileURLToPath } from 'url';
|
|||||||
|
|
||||||
const interpolationVariableRegex = /^{{(.*?)}}$/
|
const interpolationVariableRegex = /^{{(.*?)}}$/
|
||||||
const DEFAULT_ENV_VAR_VALUE = 'EDIT_VALUE_HERE'
|
const DEFAULT_ENV_VAR_VALUE = 'EDIT_VALUE_HERE'
|
||||||
|
const VARIABLE_NAME_VALUE_SEPARATOR = '='
|
||||||
const __filename = fileURLToPath(import.meta.url);
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
const __dirname = path.dirname(__filename);
|
const __dirname = path.dirname(__filename);
|
||||||
|
|
||||||
@@ -181,12 +182,12 @@ function parseHttpBlock(text) {
|
|||||||
|
|
||||||
function formatVariableValue(value, renderContext = {}) {
|
function formatVariableValue(value, renderContext = {}) {
|
||||||
if (value === null || value === undefined) {
|
if (value === null || value === undefined) {
|
||||||
return '';
|
return "''";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof value === 'string') {
|
if (typeof value === 'string') {
|
||||||
if (value.trim() === '') {
|
if (value.trim() === '') {
|
||||||
return '';
|
return "''";
|
||||||
}
|
}
|
||||||
const renderedValue = renderContext.renderValue ? renderContext.renderValue(value) : value;
|
const renderedValue = renderContext.renderValue ? renderContext.renderValue(value) : value;
|
||||||
if (/\s/.test(renderedValue)) {
|
if (/\s/.test(renderedValue)) {
|
||||||
@@ -456,8 +457,7 @@ export function buildRequestContent(request, requestName, requestConfig = {}, do
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (requestConfig.auth)
|
if (requestConfig.auth) {
|
||||||
{
|
|
||||||
if (requestConfig.auth.type === 'bearer') {
|
if (requestConfig.auth.type === 'bearer') {
|
||||||
addReferencedVariables(requestConfig.auth.token ?? '');
|
addReferencedVariables(requestConfig.auth.token ?? '');
|
||||||
addHeader('Authorization', `Bearer ${renderValue(requestConfig.auth.token ?? '')}`);
|
addHeader('Authorization', `Bearer ${renderValue(requestConfig.auth.token ?? '')}`);
|
||||||
@@ -479,17 +479,15 @@ export function buildRequestContent(request, requestName, requestConfig = {}, do
|
|||||||
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, { renderValue })}`);
|
lines.push(`# ${sanitizeVarName(variable.name)}${VARIABLE_NAME_VALUE_SEPARATOR}${formatVariableValue(variable.value, { renderValue })}`);
|
||||||
}
|
}
|
||||||
lines.push('');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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, { renderValue })}`);
|
lines.push(`@${sanitizeVarName(variable.name)}${VARIABLE_NAME_VALUE_SEPARATOR}${formatVariableValue(variable.value, { renderValue })}`);
|
||||||
}
|
}
|
||||||
lines.push('');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let requestBody = '';
|
let requestBody = '';
|
||||||
@@ -525,9 +523,8 @@ export function buildRequestContent(request, requestName, requestConfig = {}, do
|
|||||||
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, { renderValue })}`);
|
lines.push(`@${sanitizeVarName(variable.name)}${VARIABLE_NAME_VALUE_SEPARATOR}${formatVariableValue(variable.value, { renderValue })}`);
|
||||||
}
|
}
|
||||||
lines.push('');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const method = (request.method || 'GET').toUpperCase();
|
const method = (request.method || 'GET').toUpperCase();
|
||||||
@@ -540,6 +537,7 @@ export function buildRequestContent(request, requestName, requestConfig = {}, do
|
|||||||
requestUrl = `${requestUrl}${separator}${param.name}=${param.value}`;
|
requestUrl = `${requestUrl}${separator}${param.name}=${param.value}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lines.push('');
|
||||||
lines.push(`${method} ${requestUrl}`);
|
lines.push(`${method} ${requestUrl}`);
|
||||||
for (const header of headers) {
|
for (const header of headers) {
|
||||||
lines.push(`${header.name}: ${header.value}`);
|
lines.push(`${header.name}: ${header.value}`);
|
||||||
@@ -677,7 +675,7 @@ function main() {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const outputRoot = path.join(workspaceRoot, 'autodocs', 'http', collection.name);
|
const outputRoot = path.join(workspaceRoot, 'autogen', 'httpyac', collection.name);
|
||||||
ensureDir(outputRoot);
|
ensureDir(outputRoot);
|
||||||
const dotenvVariablesByTarget = writeEnvironmentTemplates(sourceDir, outputRoot);
|
const dotenvVariablesByTarget = writeEnvironmentTemplates(sourceDir, outputRoot);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user