시나리오
This commit is contained in:
@@ -1,272 +0,0 @@
|
||||
export const fillPadding = async (apiRequest, value, dataLength) => {
|
||||
let paddedValue = value; // In real app, this would be processed by APIClient
|
||||
while (paddedValue.length < dataLength) {
|
||||
paddedValue = ' ' + paddedValue;
|
||||
}
|
||||
return paddedValue;
|
||||
};
|
||||
|
||||
class APIClient {
|
||||
constructor() {
|
||||
// Initialize global variables store
|
||||
window.globals = window.globals || {};
|
||||
this.abortController = null;
|
||||
this.contextPath = document.querySelector('meta[name="context-path"]')?.getAttribute('content') || '/';
|
||||
|
||||
if (!this.contextPath.endsWith('/')) {
|
||||
this.contextPath += '/';
|
||||
}
|
||||
}
|
||||
|
||||
getClientUrl() {
|
||||
return `${this.contextPath}mgmt/api/test.do`;
|
||||
}
|
||||
|
||||
// Format response for consistency
|
||||
formatResponse(data) {
|
||||
return {
|
||||
body: data.body,
|
||||
status: data.status,
|
||||
headers: data.headers.reduce((acc, header) => {
|
||||
acc[header.key] = header.value;
|
||||
return acc;
|
||||
}, {}),
|
||||
size: data.size
|
||||
};
|
||||
}
|
||||
|
||||
// Abort ongoing requests
|
||||
abort() {
|
||||
if (this.abortController) {
|
||||
this.abortController.abort();
|
||||
this.abortController = null;
|
||||
}
|
||||
}
|
||||
|
||||
// Make the actual API request
|
||||
async makeAjaxRequest(processedRequest) {
|
||||
this.abortController = new AbortController();
|
||||
|
||||
try {
|
||||
const response = await fetch(this.getClientUrl(), {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-XSRF-TOKEN': document.querySelector('meta[name="_csrf"]')?.getAttribute('content')
|
||||
},
|
||||
body: JSON.stringify(processedRequest),
|
||||
signal: this.abortController.signal
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Network request failed');
|
||||
}
|
||||
|
||||
return response;
|
||||
|
||||
} catch (error) {
|
||||
if (error.name === 'AbortError') {
|
||||
// Handle abort silently
|
||||
return null;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// Process pre-script and variables
|
||||
async processPreScriptAndVariables(model, value, consoleOutput) {
|
||||
const variables = {};
|
||||
const updatedModel = await this.executePreRequestScript(model, variables, consoleOutput);
|
||||
return this.replacePlaceholderValueWithVariables(updatedModel, value, variables);
|
||||
}
|
||||
|
||||
// Replace placeholder values with actual variables
|
||||
replacePlaceholderValueWithVariables(model, originalValue, variables = {}) {
|
||||
const mergedVariables = {
|
||||
...window.globals,
|
||||
...variables
|
||||
};
|
||||
|
||||
return Object.entries(mergedVariables).reduce((value, [key, replacement]) => {
|
||||
const placeholder = `{{${key}}}`;
|
||||
return value.replace(new RegExp(placeholder, 'g'), replacement);
|
||||
}, originalValue);
|
||||
}
|
||||
|
||||
// Main method to send API requests
|
||||
async sendAPIRequest(model, preProcessCallback, consoleOutput) {
|
||||
try {
|
||||
// Execute pre-request script and get variables
|
||||
const variables = {};
|
||||
const updatedModel = await this.executePreRequestScript(model, variables, consoleOutput);
|
||||
|
||||
// Process request with variables
|
||||
const processedRequest = this.replacePlaceholdersWithVariables(updatedModel, variables);
|
||||
|
||||
// Allow pre-processing if callback provided
|
||||
if (preProcessCallback) {
|
||||
preProcessCallback(processedRequest);
|
||||
}
|
||||
|
||||
// Make the request
|
||||
const response = await this.makeAjaxRequest(processedRequest, consoleOutput);
|
||||
if (!response) return null;
|
||||
|
||||
const responseData = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(responseData.error || 'Unknown error');
|
||||
}
|
||||
|
||||
// Format response
|
||||
const finalResponse = this.formatResponse(responseData);
|
||||
|
||||
// Execute post-request script
|
||||
return this.executePostRequestScript(
|
||||
model.postRequestScript,
|
||||
variables,
|
||||
processedRequest,
|
||||
finalResponse,
|
||||
consoleOutput
|
||||
).catch(error => {
|
||||
console.error('Post-request script error:', error);
|
||||
return finalResponse;
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
throw new Error(`API 호출 중 오류가 발생했습니다. 사유[${error.message}]`);
|
||||
}
|
||||
}
|
||||
|
||||
// Replace placeholders in request with variables
|
||||
replacePlaceholdersWithVariables(original, variables = {}) {
|
||||
const request = JSON.parse(JSON.stringify(original));
|
||||
const mergedVariables = {
|
||||
...window.globals,
|
||||
...variables
|
||||
};
|
||||
|
||||
Object.entries(mergedVariables).forEach(([key, value]) => {
|
||||
const placeholder = new RegExp(`{{${key}}}`, 'g');
|
||||
|
||||
// Replace in path
|
||||
request.path = request.path.replace(placeholder, value);
|
||||
|
||||
// Replace in request body
|
||||
if (request.requestBody) {
|
||||
request.requestBody = request.requestBody.replace(placeholder, value);
|
||||
}
|
||||
|
||||
// Replace in headers
|
||||
if (request.headers?.length) {
|
||||
request.headers = JSON.parse(
|
||||
JSON.stringify(request.headers).replace(placeholder, value)
|
||||
);
|
||||
}
|
||||
|
||||
// Replace in query params
|
||||
if (request.queryParams?.length) {
|
||||
request.queryParams = JSON.parse(
|
||||
JSON.stringify(request.queryParams).replace(placeholder, value)
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
// Execute pre-request script in an iframe
|
||||
executePreRequestScript(model, variables, consoleOutput) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const resultFrame = document.getElementById('preRequest');
|
||||
if (!resultFrame) {
|
||||
console.warn('preRequest iframe not found');
|
||||
resolve(model);
|
||||
return;
|
||||
}
|
||||
|
||||
window.preRequestComplete = (updatedModel, error) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve(updatedModel);
|
||||
}
|
||||
};
|
||||
|
||||
window.temp_variable = variables;
|
||||
window.currentRequest = model;
|
||||
window.consoleOutput = consoleOutput;
|
||||
|
||||
resultFrame.srcdoc = `
|
||||
<script>
|
||||
var oldLog = console.log;
|
||||
console.log = function(message) {
|
||||
if(window.parent.consoleOutput)
|
||||
window.parent.consoleOutput(message);
|
||||
oldLog.apply(console, arguments);
|
||||
};
|
||||
|
||||
window.globals = window.parent.globals;
|
||||
window.variables = window.parent.temp_variable;
|
||||
window.request = window.parent.currentRequest;
|
||||
|
||||
try {
|
||||
${model.preRequestScript || ''}
|
||||
window.parent.preRequestComplete(window.request, null);
|
||||
} catch (error) {
|
||||
window.parent.preRequestComplete(null, error);
|
||||
}
|
||||
</script>
|
||||
`;
|
||||
});
|
||||
}
|
||||
|
||||
// Execute post-request script in an iframe
|
||||
executePostRequestScript(script, variables, request, response, consoleOutput) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const resultFrame = document.getElementById('postRequest');
|
||||
if (!resultFrame) {
|
||||
console.warn('postRequest iframe not found');
|
||||
resolve(response);
|
||||
return;
|
||||
}
|
||||
|
||||
window.request = request;
|
||||
window.response = response;
|
||||
window.temp_variable = variables;
|
||||
window.postRequestComplete = (response, error) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve(response);
|
||||
}
|
||||
};
|
||||
window.consoleOutput = consoleOutput;
|
||||
|
||||
resultFrame.srcdoc = `
|
||||
<script>
|
||||
var oldLog = console.log;
|
||||
console.log = function(message) {
|
||||
if(window.parent.consoleOutput)
|
||||
window.parent.consoleOutput(message);
|
||||
oldLog.apply(console, arguments);
|
||||
};
|
||||
|
||||
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);
|
||||
} catch (error) {
|
||||
window.parent.postRequestComplete(window.response, error.toString());
|
||||
}
|
||||
</script>
|
||||
`;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export default APIClient;
|
||||
Reference in New Issue
Block a user