diff --git a/src/main/resources/static/js/APIClient.js b/src/main/resources/static/js/APIClient.js index 35ba671..ecfd7aa 100644 --- a/src/main/resources/static/js/APIClient.js +++ b/src/main/resources/static/js/APIClient.js @@ -9,44 +9,50 @@ class APIClient { constructor() { window.globals = {}; - this.globalVariables = window.globals; this.variables = {}; this.abortController = null; } - async sendAPIRequest(model, preProcessCallback, consoleOutput) { - // try { - const updatedModel = await this.executePreRequestScript(model, consoleOutput); - let processedRequest = this.replacePlaceholdersWithVariables(updatedModel); - if (preProcessCallback) { - preProcessCallback(processedRequest); - } - return this.makeAjaxRequest(processedRequest, model) - .then(async response => { - const responseData = await response.json(); // Parse the JSON response - if (!response.ok) { - const errorMessage = responseData.error || 'Unknown error'; - throw new Error(`API 호출 중 오류가 발생했습니다. 사유[${errorMessage}]`); - } - return responseData; - }) - .then(data => { - 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; + try { + const updatedModel = await this.executePreRequestScript(model, consoleOutput); + let processedRequest = this.replacePlaceholdersWithVariables(updatedModel); + + 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, 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 }); - return this.executePostRequestScript(model.postRequestScript, response, consoleOutput) - .catch(error => { - $('.toast-body').text(error); - $('.toast').toast('show'); - return response; // Return the original response even if there's an error - }); - }); + + } catch (error) { + throw 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() { @@ -60,15 +66,22 @@ class APIClient { this.abortController = new AbortController(); const {signal} = this.abortController; - return fetch('/mgmt/api/test.do', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'X-XSRF-TOKEN': $('meta[name="_csrf"]').attr('content') - }, - body: JSON.stringify(processedRequest), - signal: signal // pass the signal to the fetch - }); + try { + return fetch('/mgmt/api/test.do', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-XSRF-TOKEN': $('meta[name="_csrf"]').attr('content') + }, + 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 mergedVariables = {}; - _.forEach(this.globalVariables, (value, key) => { + _.forEach(window.globals, (value, key) => { mergedVariables[key] = value; });