179 lines
6.3 KiB
JavaScript
179 lines
6.3 KiB
JavaScript
/**
|
|
* 처리 순서
|
|
* 1. preRequestScript 실행 -> executePreRequestScript
|
|
* 2. 변수 치환 -> replacePlaceholdersWithVariables
|
|
* 3. API 호출 -> sendAPIRequest
|
|
* 4. postRequestScript 실행 -> executePostRequestScript
|
|
*/
|
|
|
|
class APIClient {
|
|
constructor() {
|
|
window.globals = {};
|
|
this.globalVariables = window.globals;
|
|
this.variables = {};
|
|
this.abortController = null;
|
|
}
|
|
|
|
async sendAPIRequest(model, preProcessCallback) {
|
|
// try {
|
|
const updatedModel = await this.executePreRequestScript(model);
|
|
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) {
|
|
// If response is not OK, use the parsed error information
|
|
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;
|
|
});
|
|
return this.executePostRequestScript(model.postRequestScript, response)
|
|
.catch(error => {
|
|
$('.toast-body').text(error);
|
|
$('.toast').toast('show');
|
|
return response; // Return the original response even if there's an error
|
|
});
|
|
});
|
|
}
|
|
|
|
abort() {
|
|
if (this.abortController) {
|
|
this.abortController.abort(); // abort the fetch
|
|
this.abortController = null; // reset the controller
|
|
}
|
|
}
|
|
|
|
makeAjaxRequest(processedRequest, model) {
|
|
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
|
|
});
|
|
}
|
|
|
|
|
|
replacePlaceholdersWithVariables(original) {
|
|
let request = JSON.parse(JSON.stringify(original));
|
|
|
|
let mergedVariables = {};
|
|
_.forEach(this.globalVariables, (value, key) => {
|
|
mergedVariables[key] = value;
|
|
});
|
|
|
|
if (this.variables) {
|
|
_.forEach(this.variables, (value, key) => {
|
|
mergedVariables[key] = value;
|
|
});
|
|
}
|
|
|
|
_.forEach(mergedVariables, (value, key) => {
|
|
const placeholder = `{{${key}}}`;
|
|
// Replace in path
|
|
request.path = request.path.replace(new RegExp(placeholder, 'g'), value);
|
|
|
|
// Replace in requestBody
|
|
if (request.requestBody) {
|
|
request.requestBody = request.requestBody.replace(new RegExp(placeholder, 'g'), value);
|
|
}
|
|
|
|
// Replace in headers
|
|
if (request.headers) {
|
|
let replaced = JSON.stringify(request.headers).replace(new RegExp(placeholder, 'g'), value);
|
|
request.headers = JSON.parse(replaced);
|
|
}
|
|
|
|
// Replace in queryParams
|
|
if (request.queryParams) {
|
|
let replaced = JSON.stringify(request.queryParams).replace(new RegExp(placeholder, 'g'), value);
|
|
request.queryParams = JSON.parse(replaced);
|
|
}
|
|
});
|
|
|
|
return request;
|
|
}
|
|
|
|
executePreRequestScript(model) {
|
|
return new Promise((resolve, reject) => {
|
|
let script = model.preRequestScript;
|
|
let resultFrame = document.getElementById('preRequest');
|
|
|
|
this.variables = {};
|
|
model.variables.forEach((item) => {
|
|
if (item.enabled) {
|
|
this.variables[item.key] = item.value;
|
|
}
|
|
});
|
|
|
|
window.preRequestComplete = (updatedModel, error) => {
|
|
if (error) {
|
|
reject(error);
|
|
} else {
|
|
resolve(updatedModel);
|
|
}
|
|
};
|
|
|
|
window.temp_variable = this.variables;
|
|
window.currentRequest = model;
|
|
resultFrame.srcdoc = `<script>
|
|
window.globals = window.parent.globals;
|
|
window.variables = window.parent.temp_variable;
|
|
window.request = window.parent.currentRequest;
|
|
|
|
try {
|
|
${script}
|
|
window.parent.preRequestComplete(window.request, null);
|
|
} catch (error) {
|
|
window.parent.preRequestComplete(null, error);
|
|
}
|
|
</script>`;
|
|
});
|
|
}
|
|
|
|
executePostRequestScript(script, response) {
|
|
return new Promise((resolve, reject) => {
|
|
let resultFrame = document.getElementById('postRequest');
|
|
window.response = response;
|
|
window.temp_variable = this.variables;
|
|
window.postRequestComplete = (response, error) => {
|
|
console.log(response)
|
|
if (error) {
|
|
reject(error);
|
|
} else {
|
|
resolve(response);
|
|
}
|
|
};
|
|
|
|
resultFrame.srcdoc = `<script>
|
|
window.globals = window.parent.globals;
|
|
window.variables = window.parent.temp_variable;
|
|
window.response = window.parent.response;
|
|
try {
|
|
${script}
|
|
window.parent.postRequestComplete(window.response, null);
|
|
} catch (error) {
|
|
window.parent.postRequestComplete(window.response, error.toString());
|
|
}
|
|
</script>`;
|
|
});
|
|
}
|
|
} |