Fix DevTools request (#1496)

This commit is contained in:
Juanka Rodríguez 2019-06-03 13:41:54 +02:00 committed by Jesús Ángel
parent 11c6032227
commit 50b15be17a
2 changed files with 67 additions and 42 deletions

View File

@ -83,7 +83,7 @@ export class DevToolsController {
}
);
// Register plugin for code mirror
CodeMirror.commands.autocomplete = function(cm) {
CodeMirror.commands.autocomplete = function (cm) {
CodeMirror.showHint(cm, CodeMirror.hint.dictionaryHint, {
completeSingle: false
});
@ -335,7 +335,7 @@ export class DevToolsController {
this.apiInputBox.setSize('auto', '100%');
this.apiInputBox.model = [];
this.getAvailableMethods();
this.apiInputBox.on('keyup', function(cm, e) {
this.apiInputBox.on('keyup', function (cm, e) {
if (!ExcludedIntelliSenseTriggerKeys[(e.keyCode || e.which).toString()]) {
cm.execCommand('autocomplete', null, {
completeSingle: false
@ -359,22 +359,22 @@ export class DevToolsController {
this.highlightGroup(currentGroup);
// Register our custom Codemirror hint plugin.
CodeMirror.registerHelper('hint', 'dictionaryHint', function(editor) {
CodeMirror.registerHelper('hint', 'dictionaryHint', function (editor) {
const model = editor.model;
function getDictionary(line, word) {
let hints = [];
const exp = line.split(/\s+/g);
if (exp[0] && exp[0].match(/^(?:GET|PUT|POST|DELETE).*$/)) {
let method = model.find(function(item) {
let method = model.find(function (item) {
return item.method === exp[0];
});
const forbidChars = /^[^?{]+$/;
if (method && !exp[2] && forbidChars.test(word)) {
method.endpoints.forEach(function(endpoint) {
method.endpoints.forEach(function (endpoint) {
endpoint.path = endpoint.name;
if (endpoint.args && endpoint.args.length > 0) {
let argSubs = [];
endpoint.args.forEach(function(arg) {
endpoint.args.forEach(function (arg) {
const pathSplitted = endpoint.name.split('/');
const arrayIdx = pathSplitted.indexOf(arg.name);
const wordSplitted = word.split('/');
@ -386,7 +386,7 @@ export class DevToolsController {
}
});
let auxPath = endpoint.name;
argSubs.forEach(function(arg) {
argSubs.forEach(function (arg) {
auxPath = auxPath.replace(arg.id, arg.value);
});
endpoint.path = auxPath;
@ -407,34 +407,34 @@ export class DevToolsController {
const whiteSpace = /\s/;
while (end < curLine.length && !whiteSpace.test(curLine.charAt(end)))
++end;
while (start && !whiteSpace.test(curLine.charAt(start - 1))) --start;
while (start && !whiteSpace.test(curLine.charAt(start - 1)))--start;
const curWord = start !== end && curLine.slice(start, end);
return {
list: (!curWord
? []
: getDictionary(curLine, curWord).filter(function(item) {
return item.toUpperCase().includes(curWord.toUpperCase());
})
: getDictionary(curLine, curWord).filter(function (item) {
return item.toUpperCase().includes(curWord.toUpperCase());
})
).sort(),
from: CodeMirror.Pos(cur.line, start),
to: CodeMirror.Pos(cur.line, end)
};
});
const evtDocument = this.$document[0];
$('.wz-dev-column-separator').mousedown(function(e) {
$('.wz-dev-column-separator').mousedown(function (e) {
e.preventDefault();
$('.wz-dev-column-separator').addClass('active');
const leftOrigWidth = $('#wz-dev-left-column').width();
const rightOrigWidth = $('#wz-dev-right-column').width();
$(evtDocument).mousemove(function(e) {
const leftWidth = e.pageX - 215 + 14;
$(evtDocument).mousemove(function (e) {
const leftWidth = e.pageX - 85 + 14;
let rightWidth = leftOrigWidth - leftWidth;
$('#wz-dev-left-column').css('width', leftWidth);
$('#wz-dev-right-column').css('width', rightOrigWidth + rightWidth);
});
});
$(evtDocument).mouseup(function() {
$(evtDocument).mouseup(function () {
$('.wz-dev-column-separator').removeClass('active');
$(evtDocument).unbind('mousemove');
});
@ -448,6 +448,28 @@ export class DevToolsController {
'style',
'width: calc(70% - 7px); !important'
);
dynamicHeight();
};
const dynamicHeight = () => {
const self = this;
const window = this.$window;
setTimeout(function () {
const windows = $(window).height();
$('#wz-dev-left-column').height(
windows - (self.getPosition($('#wz-dev-left-column')[0]).y + 20)
);
$('.wz-dev-column-separator').height(
windows - (self.getPosition($('.wz-dev-column-separator')[0]).y + 20)
);
$('#wz-dev-right-column').height(
windows - (self.getPosition($('#wz-dev-right-column')[0]).y + 20)
);
$('.wz-dev-column-separator span').height(
windows -
(self.getPosition($('.wz-dev-column-separator span')[0]).y + 20)
);
}, 1);
};
}
@ -461,10 +483,10 @@ export class DevToolsController {
const desiredGroup = firstTime
? this.groups.filter(item => item.requestText)
: this.groups.filter(
item =>
item.requestText &&
(item.end >= selection.line && item.start <= selection.line)
);
item =>
item.requestText &&
(item.end >= selection.line && item.start <= selection.line)
);
// Place play button at first line from the selected group
const cords = this.apiInputBox.cursorCoords({
@ -520,12 +542,12 @@ export class DevToolsController {
const method = desiredGroup.requestText.startsWith('GET')
? 'GET'
: desiredGroup.requestText.startsWith('POST')
? 'POST'
: desiredGroup.requestText.startsWith('PUT')
? 'PUT'
: desiredGroup.requestText.startsWith('DELETE')
? 'DELETE'
: 'GET';
? 'POST'
: desiredGroup.requestText.startsWith('PUT')
? 'PUT'
: desiredGroup.requestText.startsWith('DELETE')
? 'DELETE'
: 'GET';
let requestCopy = desiredGroup.requestText.includes(method)
? desiredGroup.requestText.split(method)[1].trim()
@ -560,20 +582,9 @@ export class DevToolsController {
if (typeof extra.pretty !== 'undefined') delete extra.pretty;
if (typeof JSONraw.pretty !== 'undefined') delete JSONraw.pretty;
let path = '';
if (method === 'GET' || method === 'POST') {
// Assign inline parameters
for (const key in extra) JSONraw[key] = extra[key];
path = req.includes('?') ? req.split('?')[0] : req;
} else {
path =
typeof JSONraw === 'object' && Object.keys(JSONraw).length
? `${req}${req.includes('?') ? '&' : '?'}${queryString.unescape(
queryString.stringify(JSONraw)
)}`
: req;
JSONraw = {};
}
// Assign inline parameters
for (const key in extra) JSONraw[key] = extra[key];
const path = req.includes('?') ? req.split('?')[0] : req;
if (typeof JSONraw === 'object') JSONraw.devTools = true;
if (!firstTime) {

View File

@ -27,7 +27,7 @@ import { cleanKeys } from '../../util/remove-key';
import { apiRequestList } from '../../util/api-request-list';
import * as ApiHelper from '../lib/api-helper';
import { Queue } from '../jobs/queue';
import querystring from 'querystring';
export class WazuhApiCtrl {
/**
* Constructor
@ -708,8 +708,22 @@ export class WazuhApiCtrl {
return ErrorResponse('ERROR3099', 3099, 500, reply);
}
}
log('wazuh-api:makeRequest', `${method} ${fullUrl}`, 'debug');
const response = await needle(method, fullUrl, data, options);
let fixedUrl = false;
if (method === 'DELETE') {
fixedUrl = `${
fullUrl.includes('?') ? fullUrl.split('?')[0] : fullUrl
}?${querystring.stringify(data)}`;
}
log('wazuh-api:makeRequest', `${method} ${fixedUrl || fullUrl}`, 'debug');
const response = await needle(
method,
fixedUrl || fullUrl,
fixedUrl ? null : data,
options
);
const responseBody = (response || {}).body || {};
const responseData = responseBody.data || false;