159 lines
5.3 KiB
JavaScript
159 lines
5.3 KiB
JavaScript
export const useVariableSubstitution = () => {
|
|
const { environments, selectedEnvironment } = useApiPlatform()
|
|
|
|
// Get the currently selected environment
|
|
const currentEnvironment = computed(() => {
|
|
if (!selectedEnvironment.value) return null
|
|
return environments.value.find(env => env.id === selectedEnvironment.value)
|
|
})
|
|
|
|
// Create a variables map for quick lookup
|
|
const variablesMap = computed(() => {
|
|
if (!currentEnvironment.value) return {}
|
|
|
|
const map = {}
|
|
currentEnvironment.value.variables.forEach(variable => {
|
|
if (variable.key && variable.value) {
|
|
map[variable.key] = variable.value
|
|
}
|
|
})
|
|
return map
|
|
})
|
|
|
|
// Replace variables in a string ({{variableName}})
|
|
const substituteVariables = (text) => {
|
|
if (!text || typeof text !== 'string') return text
|
|
if (!currentEnvironment.value) return text
|
|
|
|
return text.replace(/\{\{([^}]+)\}\}/g, (match, variableName) => {
|
|
const trimmedName = variableName.trim()
|
|
return variablesMap.value[trimmedName] || match
|
|
})
|
|
}
|
|
|
|
// Process an entire request object and substitute all variables
|
|
const processRequest = (requestData) => {
|
|
if (!currentEnvironment.value) return requestData
|
|
|
|
const processed = JSON.parse(JSON.stringify(requestData))
|
|
|
|
// Substitute URL
|
|
processed.url = substituteVariables(processed.url)
|
|
|
|
// Substitute headers
|
|
if (processed.headers && Array.isArray(processed.headers)) {
|
|
processed.headers.forEach(header => {
|
|
header.key = substituteVariables(header.key)
|
|
header.value = substituteVariables(header.value)
|
|
})
|
|
}
|
|
|
|
// Substitute params
|
|
if (processed.params && Array.isArray(processed.params)) {
|
|
processed.params.forEach(param => {
|
|
param.key = substituteVariables(param.key)
|
|
param.value = substituteVariables(param.value)
|
|
})
|
|
}
|
|
|
|
// Substitute auth values
|
|
if (processed.auth) {
|
|
if (processed.auth.bearer) {
|
|
processed.auth.bearer = substituteVariables(processed.auth.bearer)
|
|
}
|
|
if (processed.auth.basic) {
|
|
processed.auth.basic.username = substituteVariables(processed.auth.basic.username)
|
|
processed.auth.basic.password = substituteVariables(processed.auth.basic.password)
|
|
}
|
|
if (processed.auth.apiKey) {
|
|
processed.auth.apiKey.key = substituteVariables(processed.auth.apiKey.key)
|
|
processed.auth.apiKey.value = substituteVariables(processed.auth.apiKey.value)
|
|
}
|
|
if (processed.auth.oauth2) {
|
|
processed.auth.oauth2.authUrl = substituteVariables(processed.auth.oauth2.authUrl)
|
|
processed.auth.oauth2.accessTokenUrl = substituteVariables(processed.auth.oauth2.accessTokenUrl)
|
|
processed.auth.oauth2.clientId = substituteVariables(processed.auth.oauth2.clientId)
|
|
processed.auth.oauth2.clientSecret = substituteVariables(processed.auth.oauth2.clientSecret)
|
|
processed.auth.oauth2.redirectUri = substituteVariables(processed.auth.oauth2.redirectUri)
|
|
}
|
|
}
|
|
|
|
// Substitute body content
|
|
if (processed.requestBody) {
|
|
if (processed.requestBody.raw) {
|
|
processed.requestBody.raw = substituteVariables(processed.requestBody.raw)
|
|
}
|
|
if (processed.requestBody.formData && Array.isArray(processed.requestBody.formData)) {
|
|
processed.requestBody.formData.forEach(item => {
|
|
item.key = substituteVariables(item.key)
|
|
item.value = substituteVariables(item.value)
|
|
})
|
|
}
|
|
if (processed.requestBody.urlEncoded && Array.isArray(processed.requestBody.urlEncoded)) {
|
|
processed.requestBody.urlEncoded.forEach(item => {
|
|
item.key = substituteVariables(item.key)
|
|
item.value = substituteVariables(item.value)
|
|
})
|
|
}
|
|
}
|
|
|
|
return processed
|
|
}
|
|
|
|
// Find all variables used in a text string
|
|
const findVariables = (text) => {
|
|
if (!text || typeof text !== 'string') return []
|
|
|
|
const matches = text.match(/\{\{([^}]+)\}\}/g)
|
|
if (!matches) return []
|
|
|
|
return matches.map(match => {
|
|
const variableName = match.replace(/[{}]/g, '').trim()
|
|
return {
|
|
name: variableName,
|
|
found: !!variablesMap.value[variableName],
|
|
value: variablesMap.value[variableName] || null
|
|
}
|
|
})
|
|
}
|
|
|
|
// Get all variables used in the current request
|
|
const getRequestVariables = (requestData) => {
|
|
const variables = new Set()
|
|
|
|
// Check URL
|
|
findVariables(requestData.url).forEach(v => variables.add(JSON.stringify(v)))
|
|
|
|
// Check headers
|
|
if (requestData.headers) {
|
|
requestData.headers.forEach(header => {
|
|
findVariables(header.key).forEach(v => variables.add(JSON.stringify(v)))
|
|
findVariables(header.value).forEach(v => variables.add(JSON.stringify(v)))
|
|
})
|
|
}
|
|
|
|
// Check params
|
|
if (requestData.params) {
|
|
requestData.params.forEach(param => {
|
|
findVariables(param.key).forEach(v => variables.add(JSON.stringify(v)))
|
|
findVariables(param.value).forEach(v => variables.add(JSON.stringify(v)))
|
|
})
|
|
}
|
|
|
|
// Check body
|
|
if (requestData.body?.raw) {
|
|
findVariables(requestData.body.raw).forEach(v => variables.add(JSON.stringify(v)))
|
|
}
|
|
|
|
return Array.from(variables).map(v => JSON.parse(v))
|
|
}
|
|
|
|
return {
|
|
currentEnvironment,
|
|
variablesMap,
|
|
substituteVariables,
|
|
processRequest,
|
|
findVariables,
|
|
getRequestVariables
|
|
}
|
|
}
|