APIClient variable 처리 수정

This commit is contained in:
현성필
2024-02-19 15:18:14 +09:00
parent a77c7a6965
commit 47d997866a
3 changed files with 17 additions and 17 deletions
+16 -12
View File
@@ -12,7 +12,6 @@ class APIClient {
constructor() {
window.globals = {};
this.variables = {};
this.abortController = null;
this.contextPath = document.querySelector('meta[name="context-path"]').getAttribute('content');
@@ -31,8 +30,9 @@ class APIClient {
async sendAPIRequest(model, preProcessCallback, consoleOutput) {
try {
const updatedModel = await this.executePreRequestScript(model, consoleOutput);
let processedRequest = this.replacePlaceholdersWithVariables(updatedModel);
let variables = {};
const updatedModel = await this.executePreRequestScript(model, variables, consoleOutput);
let processedRequest = this.replacePlaceholdersWithVariables(updatedModel, variables);
if (preProcessCallback) {
preProcessCallback(processedRequest);
@@ -46,7 +46,7 @@ class APIClient {
}
const finalResponse = this.formatResponse(responseData);
return this.executePostRequestScript(model.postRequestScript, finalResponse, consoleOutput).catch(error => {
return this.executePostRequestScript(model.postRequestScript, variables, processedRequest, finalResponse, consoleOutput).catch(error => {
$('.toast-body').text(error);
$('.toast').toast('show');
return finalResponse; // Return the original response even if there's an error in the post-request script
@@ -99,7 +99,7 @@ class APIClient {
}
}
replacePlaceholdersWithVariables(original) {
replacePlaceholdersWithVariables(original, variables) {
let request = JSON.parse(JSON.stringify(original));
let mergedVariables = {};
@@ -107,8 +107,8 @@ class APIClient {
mergedVariables[key] = value;
});
if (this.variables) {
_.forEach(this.variables, (value, key) => {
if (variables) {
_.forEach(variables, (value, key) => {
mergedVariables[key] = value;
});
}
@@ -139,12 +139,12 @@ class APIClient {
return request;
}
executePreRequestScript(model, consoleOutput) {
executePreRequestScript(model, variables, consoleOutput) {
return new Promise((resolve, reject) => {
let script = model.preRequestScript;
let resultFrame = document.getElementById('preRequest');
this.variables = {};
window.preRequestComplete = (updatedModel, error) => {
if (error) {
@@ -154,7 +154,7 @@ class APIClient {
}
};
window.temp_variable = this.variables;
window.temp_variable = variables;
window.currentRequest = model;
window.consoleOutput = consoleOutput;
resultFrame.srcdoc = `<script>
@@ -179,11 +179,13 @@ class APIClient {
});
}
executePostRequestScript(script, response, consoleOutput) {
executePostRequestScript(script, variables, request, response, consoleOutput) {
return new Promise((resolve, reject) => {
let resultFrame = document.getElementById('postRequest');
window.request = request;
window.response = response;
window.temp_variable = this.variables;
window.temp_variable = variables;
window.postRequestComplete = (response, error) => {
if (error) {
reject(error);
@@ -201,7 +203,9 @@ class APIClient {
};
window.globals = window.parent.globals;
window.variables = window.parent.temp_variable;
window.request = window.parent.request;
window.response = window.parent.response;
try {
${script}
window.parent.postRequestComplete(window.response, null);
-4
View File
@@ -178,7 +178,6 @@ class APITester {
</div>
</div>
</div>
<!-- <div class="resizer" data-direction="vertical"></div>-->
<div class="api_response" style="flex: 1">
<div class="mt-1">
<span>Response</span>
@@ -719,9 +718,6 @@ class APITester {
model.postRequestScript = postRequestEditor.getValue();
});
// apiRequestTabContent.find('.resizer').each(function(index, ele) {
// resizable(ele, () => {this.resizeEditor();});
// });
}
renderResponse(tabIndex) {