Skip to content

Commit

Permalink
Add our plugin
Browse files Browse the repository at this point in the history
With our plugin we make an instance of GlobalNotifierLib available for use anywhere in the app.

Because we're adding a global variable we have to let labrc. It has checks to help stop global variables being inadvertently created.
  • Loading branch information
Cruikshanks committed Jan 31, 2023
1 parent 0e36451 commit 67aa036
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .labrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ module.exports = {
'__asyncGenerator', '__asyncDelegator', '__asyncValues', '__makeTemplateObject', '__importStar', '__importDefault',
'__classPrivateFieldGet', '__classPrivateFieldSet',
// We also ignore globals exposed by global-agent:
'GLOBAL_AGENT','ROARR'
'GLOBAL_AGENT','ROARR',
// GlobalNotifier is added by us a global in a server plugin. It's how we make logging available anywhere in the app
// whilst avoiding having to pass it around
'GlobalNotifier'
].join(',')
}
17 changes: 17 additions & 0 deletions app/plugins/global-notifier.plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict'

/**
* Plugin to add a globally available notifier for logging and sending exceptions to Errbit
* @module GlobalNotifierPlugin
*/

const GlobalNotifierLib = require('../lib/global-notifier.lib.js')

const GlobalNotifierPlugin = {
name: 'global-notifier',
register: (server, _options) => {
global.GlobalNotifier = new GlobalNotifierLib(server.logger, server.app.airbrake)
}
}

module.exports = GlobalNotifierPlugin
2 changes: 2 additions & 0 deletions app/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const AirbrakePlugin = require('./plugins/airbrake.plugin.js')
const BlippPlugin = require('./plugins/blipp.plugin.js')
const ChargingModuleTokenCachePlugin = require('./plugins/charging-module-token-cache.plugin.js')
const ErrorPagesPlugin = require('./plugins/error-pages.plugin.js')
const GlobalNotifierPlugin = require('./plugins/global-notifier.plugin.js')
const HapiPinoPlugin = require('./plugins/hapi-pino.plugin.js')
const RequestNotifierPlugin = require('./plugins/request-notifier.plugin.js')
const RouterPlugin = require('./plugins/router.plugin.js')
Expand All @@ -21,6 +22,7 @@ const registerPlugins = async (server) => {
await server.register(RouterPlugin)
await server.register(HapiPinoPlugin())
await server.register(AirbrakePlugin)
await server.register(GlobalNotifierPlugin)
await server.register(ChargingModuleTokenCachePlugin)
await server.register(ErrorPagesPlugin)
await server.register(RequestNotifierPlugin)
Expand Down
31 changes: 31 additions & 0 deletions test/plugins/global-notifier.plugin.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')

const { describe, it, beforeEach } = exports.lab = Lab.script()
const { expect } = Code

// Test helpers
const GlobalNotifierLib = require('../../app/lib/global-notifier.lib.js')

// For running our service
const { init } = require('../../app/server.js')

describe('Global Notifier plugin', () => {
beforeEach(async () => {
// Create server before each test
await init()
})

describe('Global Notifier Plugin', () => {
describe('when the server is initialised', () => {
it('makes an instance of GlobalNotifierLib available globally', async () => {
const result = global.GlobalNotifier

expect(result).to.be.an.instanceOf(GlobalNotifierLib)
})
})
})
})

0 comments on commit 67aa036

Please sign in to comment.