From 9bc36158608087dc0d4cc5270d26d22f9a087848 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20P=C3=A9rez=20Mart=C3=ADn?= Date: Tue, 30 Jan 2018 08:58:11 +0100 Subject: [PATCH 1/2] Allow setting custom log endpoint in browser script --- example/index.js | 3 ++- example/views/main.html | 5 +++++ lib/browser/script.js | 14 ++++++++++---- test/test.browser-log.js | 24 ++++++++++++++++++++++++ 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/example/index.js b/example/index.js index 6617253..63dc645 100644 --- a/example/index.js +++ b/example/index.js @@ -22,7 +22,8 @@ const startServer = async function() { await server.register({ plugin: require('../'), options: { - serveScript: true + serveScript: true, + endpoint: '/api/errors-logs' } }); diff --git a/example/views/main.html b/example/views/main.html index 4aba83d..11bb9e1 100644 --- a/example/views/main.html +++ b/example/views/main.html @@ -9,6 +9,11 @@

Hello

$(function(){ var logger = window.hapiLogger || {}; + logger.configure({ + debug: false, + logEndpoint: 'api/errors-logs' + }); + logger.sendLog({test: 'yes'}, function(err, data) { }); diff --git a/lib/browser/script.js b/lib/browser/script.js index db3fe76..5d4d6d0 100644 --- a/lib/browser/script.js +++ b/lib/browser/script.js @@ -3,8 +3,10 @@ var HapiBrowserLogger = function() { var self = this; - this.debug = false; - this.logEndpoint = '/api/browser-log'; + const defaults = { + debug: false, + logEndpoint: '/api/browser-log' + }; var getAjaxHandler = function() { var xmlhttp; @@ -16,6 +18,10 @@ return xmlhttp; }; + this.configure = function (config = {}) { + this.config = Object.assign(defaults, config); + }; + this.sendLog = function(tags, data, cb) { if (!Array.isArray(tags)) { cb = data; @@ -26,7 +32,7 @@ data.userAgent = navigator.userAgent; data.location = window.location.href; - if (this.debug) { + if (this.config.debug) { console.log('hapi-error-log', tags, data); if(cb) { cb(null, data); @@ -53,7 +59,7 @@ var dataString = JSON.stringify({ data: data, tags: tags }); - ajax.open('POST', this.logEndpoint, true); + ajax.open('POST', this.config.logEndpoint, true); ajax.setRequestHeader('Content-type', 'application/json'); ajax.send(dataString); }; diff --git a/test/test.browser-log.js b/test/test.browser-log.js index 75ce07e..f1799b6 100644 --- a/test/test.browser-log.js +++ b/test/test.browser-log.js @@ -197,6 +197,30 @@ lab.test('ignore not valid', { timeout: 5000 }, async () => { code.expect(statements.length).to.equal(1); }); +lab.test('custom logs endpoint', { timeout: 5000 }, async () => { + const statements = []; + + server.events.on('log', (logObj) => { + statements.push(logObj.data); + }); + + await server.register({ + plugin: hapiSlow, + options: { + endpoint: '/api/custom-errors-endpoint' + } + }); + + const response = await server.inject({ + method: 'POST', + url: '/api/custom-errors-endpoint', + payload + }); + + code.expect(response.statusCode).to.equal(200); + code.expect(statements.length).to.equal(1); +}); + lab.test('serves script', { timeout: 5000 }, async () => { await server.register(require('inert')); await server.register({ From 53ff3d74b15a96d7fe19e819d29f9224ed8b6161 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20P=C3=A9rez=20Mart=C3=ADn?= Date: Tue, 30 Jan 2018 11:21:00 +0100 Subject: [PATCH 2/2] Replace Object.assign as it is not supported in some IE versions --- lib/browser/script.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/browser/script.js b/lib/browser/script.js index 5d4d6d0..3f2786c 100644 --- a/lib/browser/script.js +++ b/lib/browser/script.js @@ -3,7 +3,7 @@ var HapiBrowserLogger = function() { var self = this; - const defaults = { + this.config = { debug: false, logEndpoint: '/api/browser-log' }; @@ -18,8 +18,8 @@ return xmlhttp; }; - this.configure = function (config = {}) { - this.config = Object.assign(defaults, config); + this.configure = function (config) { + Object.keys(config).forEach(prop => this.config[prop] = config[prop]); }; this.sendLog = function(tags, data, cb) {