|
|
|
@ -1,81 +1,44 @@
|
|
|
|
|
const currentURL = window.location.href; // Get the current URL
|
|
|
|
|
const baseURL = currentURL.split('/').slice(0, -1).join('/'); // Extract base URL
|
|
|
|
|
const completeURL = baseURL + '/request'; |
|
|
|
|
|
|
|
|
|
async function makeRequest(cell) { |
|
|
|
|
$('#request-container').empty(); |
|
|
|
|
// Extrai as opções do step
|
|
|
|
|
const tempVars = cell.attr('properties/temporaryVars'); |
|
|
|
|
const oldUrl = formataTempVars(cell.attr('properties/text'), tempVars); |
|
|
|
|
const url = new URL(oldUrl); |
|
|
|
|
const method = cell.attr('properties/method').toUpperCase(); |
|
|
|
|
const queryParams = cell.attr('properties/queryParams') || []; |
|
|
|
|
const authorization = cell.attr('properties/authorization') || null; |
|
|
|
|
const headers = cell.attr('properties/headers') || []; |
|
|
|
|
const body = cell.attr('properties/body') || null; |
|
|
|
|
|
|
|
|
|
// Constrói a URL com os parâmetros de consulta (query params)
|
|
|
|
|
if (queryParams.length > 0) { |
|
|
|
|
queryParams.forEach(param => { |
|
|
|
|
url.searchParams.append( |
|
|
|
|
formataTempVars(param.key, tempVars), |
|
|
|
|
formataTempVars(param.value, tempVars) |
|
|
|
|
); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Configura os headers
|
|
|
|
|
const fetchHeaders = new Headers(); |
|
|
|
|
if (authorization) { |
|
|
|
|
fetchHeaders.append('Authorization', formataTempVars(authorization, tempVars)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
headers.forEach(header => { |
|
|
|
|
fetchHeaders.append( |
|
|
|
|
formataTempVars(header.key, tempVars), |
|
|
|
|
formataTempVars(header.value, tempVars) |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
var data = getBlockById(jsonBlocks, cell.id); |
|
|
|
|
var jsonData = JSON.stringify(data); |
|
|
|
|
|
|
|
|
|
$.ajax({ |
|
|
|
|
url: completeURL, |
|
|
|
|
type: "POST", |
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json', |
|
|
|
|
'X-CSRF-TOKEN': csrfToken |
|
|
|
|
}, |
|
|
|
|
data: jsonData, |
|
|
|
|
success: function (response) { |
|
|
|
|
response = JSON.parse(response); |
|
|
|
|
const httpCode = response.statusCode; |
|
|
|
|
const data = response.data; |
|
|
|
|
const json = { "statusCode": httpCode, "data": data }; |
|
|
|
|
const requestResponse = `<code><pre id="pre-code-area" style="border: 1px solid #7A7A7A; overflow-y: hidden">${JSON.stringify(json, null, 2)}</pre></code>`; |
|
|
|
|
$('#request-container').append(requestResponse); |
|
|
|
|
|
|
|
|
|
const jsonvars = getJsonPaths(json); |
|
|
|
|
const datalist = document.getElementById('itemList'); |
|
|
|
|
|
|
|
|
|
jsonvars.forEach(item => { |
|
|
|
|
const option = document.createElement('option'); |
|
|
|
|
option.value = item; |
|
|
|
|
datalist.appendChild(option); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
return { httpCode, data }; |
|
|
|
|
}, |
|
|
|
|
error: function (res) { |
|
|
|
|
alert('Nao foi possivel retirar de pausa.') |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
if (method === 'PUT' && body) { |
|
|
|
|
fetchHeaders.append('Content-Type', 'application/json'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Configura as opções da requisição
|
|
|
|
|
const fetchOptions = { |
|
|
|
|
method: method, |
|
|
|
|
headers: fetchHeaders, |
|
|
|
|
redirect: 'follow' |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
// Adiciona o corpo da requisição se for PUT
|
|
|
|
|
if (method === 'PUT' && body) { |
|
|
|
|
fetchOptions.body = JSON.stringify(body); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
// Executa a requisição
|
|
|
|
|
const response = await fetch(url.toString(), fetchOptions); |
|
|
|
|
const httpCode = response.status; |
|
|
|
|
const data = await response.json(); |
|
|
|
|
const json = {"statusCode" : httpCode, "data" : data}; |
|
|
|
|
const requestReponse = ` <code ><pre id="pre-code-area" style="border: 1px solid #7A7A7A; overflow-y: hidden">${JSON.stringify(json, null, 2)}</pre></code>`; |
|
|
|
|
$('#request-container').append(requestReponse); |
|
|
|
|
|
|
|
|
|
const jsonvars = getJsonPaths(json); |
|
|
|
|
const datalist = document.getElementById('itemList') |
|
|
|
|
|
|
|
|
|
jsonvars.forEach(item => { |
|
|
|
|
const option = document.createElement('option'); |
|
|
|
|
option.value = item; |
|
|
|
|
datalist.appendChild(option); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
return { httpCode, data }; |
|
|
|
|
} catch (error) { |
|
|
|
|
const json = {"statusCode" : null, "data" : null}; |
|
|
|
|
console.error('Error:', error); |
|
|
|
|
const requestReponse = ` <code ><pre id="pre-code-area" style="border: 1px solid #7A7A7A; overflow-y: hidden">${JSON.stringify(json, null, 2)}</pre></code>`; |
|
|
|
|
$('#request-container').append(requestReponse); |
|
|
|
|
return { httpCode: null, data: null, error }; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function formataTempVars(string, vars) { |
|
|
|
@ -143,4 +106,26 @@ function getJsonPaths(obj, path = '') {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return paths; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function getBlockById(jsonData, blockId) { |
|
|
|
|
// Parse the JSON string if needed
|
|
|
|
|
if (typeof jsonData === 'string') { |
|
|
|
|
jsonData = JSON.parse(jsonData); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Loop through each element in the "blocks" array
|
|
|
|
|
for (const element of jsonData) { |
|
|
|
|
if (element.hasOwnProperty('blocks')) { |
|
|
|
|
for (const block of element.blocks) { |
|
|
|
|
if (block.id === blockId) { |
|
|
|
|
return block; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Block not found
|
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|