diff --git a/src/server/http/index.js b/src/server/http/index.js index 9502af0d2add8..f0c542c41f08d 100644 --- a/src/server/http/index.js +++ b/src/server/http/index.js @@ -10,7 +10,7 @@ module.exports = function (kbnServer, server, config) { server = kbnServer.server = new Hapi.Server(); - const urlLookup = require('./urlLookup')(server); + const shortUrlLookup = require('./short_url_lookup')(server); // Create a new connection var connectionOptions = { @@ -160,7 +160,7 @@ module.exports = function (kbnServer, server, config) { method: 'GET', path: '/goto/{urlId}', handler: async function (request, reply) { - const url = await urlLookup.getUrl(request.params.urlId); + const url = await shortUrlLookup.getUrl(request.params.urlId); reply().redirect(url); } }); @@ -169,7 +169,7 @@ module.exports = function (kbnServer, server, config) { method: 'POST', path: '/shorten', handler: async function (request, reply) { - const urlId = await urlLookup.generateUrlId(request.payload.url); + const urlId = await shortUrlLookup.generateUrlId(request.payload.url); reply(urlId); } }); diff --git a/src/server/http/urlLookup.js b/src/server/http/short_url_lookup.js similarity index 89% rename from src/server/http/urlLookup.js rename to src/server/http/short_url_lookup.js index 18e094a08462a..a67accada2ca8 100644 --- a/src/server/http/urlLookup.js +++ b/src/server/http/short_url_lookup.js @@ -11,8 +11,8 @@ export default function (server) { id: urlId, body: { doc: { - 'access-date': new Date(), - 'access-count': urlDoc._source['access-count'] + 1 + 'accessDate': new Date(), + 'accessCount': urlDoc._source.accessCount + 1 } } }); @@ -35,7 +35,7 @@ export default function (server) { resolve(response); }) .catch(err => { - resolve(); + reject(); }); }); @@ -52,9 +52,9 @@ export default function (server) { id: urlId, body: { url, - 'access-count': 0, - 'create-date': new Date(), - 'access-date': new Date() + 'accessCount': 0, + 'createDate': new Date(), + 'accessDate': new Date() } }) .then(response => { diff --git a/src/ui/public/share/index.js b/src/ui/public/share/index.js index 1e587a0fd7452..5cb9cd42a81b8 100644 --- a/src/ui/public/share/index.js +++ b/src/ui/public/share/index.js @@ -1,57 +1,55 @@ -define(function (require) { - const app = require('ui/modules').get('kibana'); - - app.directive('share', function (Private) { - const urlShortener = Private(require('./url_shortener')); - - return { - restrict: 'E', - scope: { objectType: '@' }, - template: require('ui/share/index.html'), - controller: function ($scope, $rootScope, $location, $http) { - $scope.shortUrlsLoading = false; - - $scope.$watch('getUrl()', function (url) { - $scope.url = url; - $scope.embedUrl = url.replace('?', '?embed&'); - - //when the url changes we want to hide any generated short urls - $scope.shortenUrls = false; - $scope.shortUrl = undefined; - }); - - $scope.$watch('shortenUrls', enabled => { - if (!!enabled) { - if ($scope.shortUrl) { +const app = require('ui/modules').get('kibana'); + +app.directive('share', function (Private) { + const urlShortener = Private(require('./url_shortener')); + + return { + restrict: 'E', + scope: { objectType: '@' }, + template: require('ui/share/index.html'), + controller: function ($scope, $rootScope, $location, $http) { + $scope.shortUrlsLoading = false; + + $scope.$watch('getUrl()', function (url) { + $scope.url = url; + $scope.embedUrl = url.replace('?', '?embed&'); + + //when the url changes we want to hide any generated short urls + $scope.shortenUrls = false; + $scope.shortUrl = undefined; + }); + + $scope.$watch('shortenUrls', enabled => { + if (!!enabled) { + if ($scope.shortUrl) { + $scope.shortUrlsLoading = false; + } else { + $scope.shortUrlsLoading = true; + + const linkPromise = urlShortener.shortenUrl($scope.url) + .then(shortUrl => { + $scope.shortUrl = shortUrl; + }); + + const embedPromise = urlShortener.shortenUrl($scope.embedUrl) + .then(shortUrl => { + $scope.shortEmbedUrl = shortUrl; + }); + + Promise.all([linkPromise, embedPromise]) + .then(() => { $scope.shortUrlsLoading = false; - } else { - $scope.shortUrlsLoading = true; - - const linkPromise = urlShortener.shortenUrl($scope.url) - .then(shortUrl => { - $scope.shortUrl = shortUrl; - }); - - const embedPromise = urlShortener.shortenUrl($scope.embedUrl) - .then(shortUrl => { - $scope.shortEmbedUrl = shortUrl; - }); - - Promise.all([linkPromise, embedPromise]) - .then(() => { - $scope.shortUrlsLoading = false; - }) - .catch(err => { - $scope.shortenUrls = false; - }); - } + }) + .catch(err => { + $scope.shortenUrls = false; + }); } - }); - - $scope.getUrl = function () { - return $location.absUrl(); - }; - } - }; - }); + } + }); + + $scope.getUrl = function () { + return $location.absUrl(); + }; + } + }; }); diff --git a/src/ui/public/share/url_shortener.js b/src/ui/public/share/url_shortener.js index 98411fd2bcd6c..3e29500d8d2f0 100644 --- a/src/ui/public/share/url_shortener.js +++ b/src/ui/public/share/url_shortener.js @@ -1,26 +1,24 @@ -define(function (require) { - return function createUrlShortener(Notifier, $http, $location) { - const notify = new Notifier({ - location: 'Url Shortener' - }); - const baseUrl = `${$location.protocol()}://${$location.host()}:${$location.port()}`; +export default function createUrlShortener(Notifier, $http, $location) { + const notify = new Notifier({ + location: 'Url Shortener' + }); + const baseUrl = `${$location.protocol()}://${$location.host()}:${$location.port()}`; - async function shortenUrl(url) { - const relativeUrl = url.replace(baseUrl, ''); - const formData = { url: relativeUrl }; + async function shortenUrl(url) { + const relativeUrl = url.replace(baseUrl, ''); + const formData = { url: relativeUrl }; - try { - const result = await $http.post('/shorten', formData); + try { + const result = await $http.post('/shorten', formData); - return `${baseUrl}/goto/${result.data}`; - } catch (err) { - notify.error(err); - throw err; - } + return `${baseUrl}/goto/${result.data}`; + } catch (err) { + notify.error(err); + throw err; } + } - return { - shortenUrl - }; + return { + shortenUrl }; -}); +};