feat: env variable substitutions

This commit is contained in:
DenserMeerkat
2024-06-16 22:14:27 +05:30
parent 41a7f2ccf9
commit ebe576a118
7 changed files with 108 additions and 8 deletions

View File

@ -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;
}