APIClient.js 리팩터링

This commit is contained in:
현성필
2024-01-11 10:20:44 +09:00
parent f5ffe34aa3
commit 3a1dde476a
+55 -42
View File
@@ -9,44 +9,50 @@
class APIClient { class APIClient {
constructor() { constructor() {
window.globals = {}; window.globals = {};
this.globalVariables = window.globals;
this.variables = {}; this.variables = {};
this.abortController = null; this.abortController = null;
} }
async sendAPIRequest(model, preProcessCallback, consoleOutput) { async sendAPIRequest(model, preProcessCallback, consoleOutput) {
// try { try {
const updatedModel = await this.executePreRequestScript(model, consoleOutput); const updatedModel = await this.executePreRequestScript(model, consoleOutput);
let processedRequest = this.replacePlaceholdersWithVariables(updatedModel); let processedRequest = this.replacePlaceholdersWithVariables(updatedModel);
if (preProcessCallback) {
preProcessCallback(processedRequest); if (preProcessCallback) {
} preProcessCallback(processedRequest);
return this.makeAjaxRequest(processedRequest, model) }
.then(async response => { const response = await this.makeAjaxRequest(processedRequest, model, consoleOutput);
const responseData = await response.json(); // Parse the JSON response const responseData = await response.json(); // Parse the JSON response
if (!response.ok) {
const errorMessage = responseData.error || 'Unknown error'; if (!response.ok) {
throw new Error(`API 호출 중 오류가 발생했습니다. 사유[${errorMessage}]`); const errorMessage = responseData.error || 'Unknown error';
} throw new Error(`API 호출 중 오류가 발생했습니다. 사유[${errorMessage}]`);
return responseData; }
})
.then(data => { const finalResponse = this.formatResponse(responseData);
let response = {}; return this.executePostRequestScript(model.postRequestScript, finalResponse, consoleOutput)
response.body = data.body; .catch(error => {
response.status = data.status; $('.toast-body').text(error);
response.headers = {}; $('.toast').toast('show');
response.size = data.size; return finalResponse; // Return the original response even if there's an error in the post-request script
_.forEach(data.headers, (header) => {
response.headers[header.key] = header.value;
}); });
return this.executePostRequestScript(model.postRequestScript, response, consoleOutput)
.catch(error => { } catch (error) {
$('.toast-body').text(error); throw error;
$('.toast').toast('show'); }
return response; // Return the original response even if there's an error }
});
}); formatResponse(data) {
// Format response as needed before post-request script
let response = {};
response.body = data.body;
response.status = data.status;
response.headers = {};
response.size = data.size;
_.forEach(data.headers, (header) => {
response.headers[header.key] = header.value;
});
return response;
} }
abort() { abort() {
@@ -60,15 +66,22 @@ class APIClient {
this.abortController = new AbortController(); this.abortController = new AbortController();
const {signal} = this.abortController; const {signal} = this.abortController;
return fetch('/mgmt/api/test.do', { try {
method: 'POST', return fetch('/mgmt/api/test.do', {
headers: { method: 'POST',
'Content-Type': 'application/json', headers: {
'X-XSRF-TOKEN': $('meta[name="_csrf"]').attr('content') 'Content-Type': 'application/json',
}, 'X-XSRF-TOKEN': $('meta[name="_csrf"]').attr('content')
body: JSON.stringify(processedRequest), },
signal: signal // pass the signal to the fetch body: JSON.stringify(processedRequest),
}); signal: signal // pass the signal to the fetch
});
} catch (error) {
if (error.name !== 'AbortError') {
console.error('Network request failed:', error);
}
throw error;
}
} }
@@ -76,7 +89,7 @@ class APIClient {
let request = JSON.parse(JSON.stringify(original)); let request = JSON.parse(JSON.stringify(original));
let mergedVariables = {}; let mergedVariables = {};
_.forEach(this.globalVariables, (value, key) => { _.forEach(window.globals, (value, key) => {
mergedVariables[key] = value; mergedVariables[key] = value;
}); });