From 057499176da695c19e6f5336f9099f8a8dbbf402 Mon Sep 17 00:00:00 2001 From: Edward Callahan Date: Sat, 25 May 2019 13:37:50 -0400 Subject: [PATCH] Updated to new HApi structure. Supported versions >=es6.6. Also changed kibana version to 7.0.1 in order to support the newest release. --- package.json | 4 +-- server/routes.js | 92 +++++++++++++++++++++++------------------------- 2 files changed, 47 insertions(+), 49 deletions(-) diff --git a/package.json b/package.json index b74b73d..5f47593 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,10 @@ { "name": "kibana_api", "version": "0.6.2", - "description": "This plugin allow you to crete visualization dynamiclly", + "description": "This plugin allow you to crete visualization dynamically", "main": "index.js", "kibana": { - "version": "6.5.2" + "version": "7.0.1" }, "repository": { "type": "git", diff --git a/server/routes.js b/server/routes.js index dfe1798..eebaff8 100644 --- a/server/routes.js +++ b/server/routes.js @@ -25,16 +25,19 @@ export default function (server) { path: '/api/visStructure', method: 'GET', - handler(req, reply) { + handler(req, h) { var file = __dirname + '/visStruct.json'; - jsonfile.readFile(file, function (err, obj) { - if (err) - console.dir(err) - reply(obj); - }) + return new Promise((resolve, reject) => { + jsonfile.readFile(file, (err, obj) => { + if(err) + reject(err); + else + resolve(obj); + }); + }) } }); @@ -43,10 +46,8 @@ export default function (server) { path: '/api/getIndexPatternId', method: 'POST', - handler(req, reply) { - getIndexPatternId(req).then((indexPattern) => { - reply(indexPattern); - }); + handler(req, h) { + return getIndexPatternId(req); } }); @@ -55,15 +56,9 @@ export default function (server) { path: '/api/createVis/createVisByVisState', method: 'POST', - handler(req, reply) { + handler(req, h) { - callWithRequest(req, 'bulk', {body: getBulkBody(req.payload, server.config().get('kibana.index'))}).then(function (response) { - reply(response); - }, - function (error) { - reply(error); - } - ); + return callWithRequest(req, 'bulk', {body: getBulkBody(req.payload, server.config().get('kibana.index'))}); } }); @@ -72,36 +67,37 @@ export default function (server) { path: '/api/createIndexPattern', method: 'POST', - handler(req, reply) { - getIndexPatternId(req).then((indexPattern) => { + handler(req, h) { + return getIndexPatternId(req) + .then((indexPattern) => { if (indexPattern.length == 0) { let options = { headers: {'content-type': 'application/json', 'kbn-version': getKibanVersion()}, url: req.headers.referer.split('app')[0] + 'api/saved_objects/index-pattern', json: {attributes: {timeFieldName: req.payload.timeFieldName, title: req.payload.title}} }; - request.post(options, function (error, response, body) { - try { - if (error) { - console.log(error); - reply(error); - } - else { - reply(body); + return new Promise((resolve, reject) => { + request.post(options, function (error, response, body) { + try { + if (error) { + console.log(error); + reject(error); + } + else { + resolve(body); + } + } catch (e) { + console.log(e); + return reject(e); + } - } catch (e) { - console.log(e); - reply(e); - - } + }); }); } else { - reply({}); + return Promise.resolve({}); } }) - - } }); @@ -109,8 +105,8 @@ export default function (server) { path: '/api/setIndexPattern', method: 'POST', - handler(req, reply) { - getIndexPatternId(req).then((indexPattern) => { + handler(req, h) { + return getIndexPatternId(req).then((indexPattern) => { let urlHeader = req.headers.referer.split('app')[0] + 'api/'; let url = urlHeader + 'kibana/settings/defaultIndex'; let options = { @@ -118,15 +114,17 @@ export default function (server) { url: url, json: {value: indexPattern[0].id} }; - request.post(options, function (error, response, body) { - if (error) { - console.log(error); - reply(error); - } - console.log(body); - - reply(body); - + return new Promise((resolve, reject) => { + request.post(options, function (error, response, body) { + if (error) { + console.log(error); + reject(error); + } + console.log(body); + + resolve(body); + + }); }); });