Socket Client UI 기본 골격 작성

This commit is contained in:
현성필
2024-04-17 22:03:02 +09:00
parent 95924af31a
commit 7d97628d38
27 changed files with 2244 additions and 570 deletions
+55 -29
View File
@@ -28,35 +28,6 @@ class APIClient {
return this.contextPath + this.CLIENT_URL;
}
async sendAPIRequest(model, preProcessCallback, consoleOutput) {
try {
let variables = {};
const updatedModel = await this.executePreRequestScript(model, variables, consoleOutput);
let processedRequest = this.replacePlaceholdersWithVariables(updatedModel, variables);
if (preProcessCallback) {
preProcessCallback(processedRequest);
}
const response = await this.makeAjaxRequest(processedRequest, model, consoleOutput);
const responseData = await response.json(); // Parse the JSON response
if (!response.ok) {
const errorMessage = responseData.error || 'Unknown error';
throw new Error(`API 호출 중 오류가 발생했습니다. 사유[${errorMessage}]`);
}
const finalResponse = this.formatResponse(responseData);
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
});
} catch (error) {
throw error;
}
}
formatResponse(data) {
// Format response as needed before post-request script
let response = {};
@@ -99,6 +70,61 @@ class APIClient {
}
}
async processPreScriptAndVariables(model, value, consoleOutput){
let variables = {};
const updatedModel = await this.executePreRequestScript(model, variables, consoleOutput);
return this.replacePlaceholderValueWithVariables(updatedModel, value, variables);
}
replacePlaceholderValueWithVariables(model, originalValue, variables) {
let mergedVariables = {};
_.forEach(window.globals, (value, key) => {
mergedVariables[key] = value;
});
if (variables) {
_.forEach(variables, (value, key) => {
mergedVariables[key] = value;
});
}
_.forEach(mergedVariables, (value, key) => {
const placeholder = `{{${key}}}`;
// Replace in path
originalValue = originalValue.replace(new RegExp(placeholder, 'g'), value);
});
return originalValue;
}
async sendAPIRequest(model, preProcessCallback, consoleOutput) {
try {
let variables = {};
const updatedModel = await this.executePreRequestScript(model, variables, consoleOutput);
let processedRequest = this.replacePlaceholdersWithVariables(updatedModel, variables);
if (preProcessCallback) {
preProcessCallback(processedRequest);
}
const response = await this.makeAjaxRequest(processedRequest, model, consoleOutput);
const responseData = await response.json(); // Parse the JSON response
if (!response.ok) {
const errorMessage = responseData.error || 'Unknown error';
throw new Error(`API 호출 중 오류가 발생했습니다. 사유[${errorMessage}]`);
}
const finalResponse = this.formatResponse(responseData);
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
});
} catch (error) {
throw error;
}
}
replacePlaceholdersWithVariables(original, variables) {
let request = JSON.parse(JSON.stringify(original));