mirror of
https://github.com/foss42/apidash.git
synced 2025-05-21 00:09:55 +08:00
feat: env variable substitutions
This commit is contained in:
@ -35,3 +35,53 @@ List<EnvironmentVariableModel> getEnvironmentSecrets(
|
||||
(removeEmptyModels ? element != kEnvironmentSecretEmptyModel : true))
|
||||
.toList();
|
||||
}
|
||||
|
||||
String? substituteVariables(
|
||||
String? input,
|
||||
Map<String?, List<EnvironmentVariableModel>> envMap,
|
||||
String? activeEnvironmentId) {
|
||||
if (input == null) return null;
|
||||
|
||||
final Map<String, String> combinedMap = {};
|
||||
final activeEnv = envMap[activeEnvironmentId] ?? [];
|
||||
final globalEnv = envMap[kGlobalEnvironmentId] ?? [];
|
||||
|
||||
for (var variable in globalEnv) {
|
||||
combinedMap[variable.key] = variable.value;
|
||||
}
|
||||
for (var variable in activeEnv) {
|
||||
combinedMap[variable.key] = variable.value;
|
||||
}
|
||||
|
||||
final regex = RegExp(r'{{(.*?)}}');
|
||||
String result = input.replaceAllMapped(regex, (match) {
|
||||
final key = match.group(1)?.trim() ?? '';
|
||||
return combinedMap[key] ?? '';
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
HttpRequestModel substituteHttpRequestModel(
|
||||
HttpRequestModel httpRequestModel,
|
||||
Map<String?, List<EnvironmentVariableModel>> envMap,
|
||||
String? activeEnvironmentId) {
|
||||
var newRequestModel = httpRequestModel.copyWith(
|
||||
url: substituteVariables(
|
||||
httpRequestModel.url,
|
||||
envMap,
|
||||
activeEnvironmentId,
|
||||
)!,
|
||||
headers: httpRequestModel.headers?.map((header) {
|
||||
return header.copyWith(
|
||||
value: substituteVariables(header.value, envMap, activeEnvironmentId),
|
||||
);
|
||||
}).toList(),
|
||||
params: httpRequestModel.params?.map((param) {
|
||||
return param.copyWith(
|
||||
value: substituteVariables(param.value, envMap, activeEnvironmentId),
|
||||
);
|
||||
}).toList(),
|
||||
);
|
||||
return newRequestModel;
|
||||
}
|
||||
|
Reference in New Issue
Block a user