From 42a4df8aae074bd360b2b07ae0f8c5ad21066a7e Mon Sep 17 00:00:00 2001 From: ivaosthu Date: Fri, 30 Nov 2018 10:11:36 +0100 Subject: [PATCH] chore(modernize): Spin out base class for Controllers --- lib/routes/admin-api/archive.js | 15 ++++------ lib/routes/admin-api/event.js | 50 ++++++++++++++++----------------- lib/routes/admin-api/index.js | 30 +++++++++----------- lib/routes/controller.js | 26 +++++++++++++++++ 4 files changed, 69 insertions(+), 52 deletions(-) create mode 100644 lib/routes/controller.js diff --git a/lib/routes/admin-api/archive.js b/lib/routes/admin-api/archive.js index 91cfbd5aaf..8e535af252 100644 --- a/lib/routes/admin-api/archive.js +++ b/lib/routes/admin-api/archive.js @@ -1,20 +1,19 @@ 'use strict'; -const { Router } = require('express'); +const Controller = require('../controller'); const logger = require('../../logger')('/admin-api/archive.js'); const { FEATURE_REVIVED } = require('../../event-type'); const extractUser = require('../../extract-user'); -class ArchiveController { +class ArchiveController extends Controller { constructor({ featureToggleStore, eventStore }) { - const router = Router(); - this._router = router; + super(); this.featureToggleStore = featureToggleStore; this.eventStore = eventStore; - router.get('/features', this.getArchivedFeatures.bind(this)); - router.post('/revive/:name', this.reviveFeatureToggle.bind(this)); + this.get('/features', this.getArchivedFeatures); + this.post('/revive/:name', this.reviveFeatureToggle); } async getArchivedFeatures(req, res) { @@ -37,10 +36,6 @@ class ArchiveController { return res.status(500).end(); } } - - router() { - return this._router; - } } module.exports = ArchiveController; diff --git a/lib/routes/admin-api/event.js b/lib/routes/admin-api/event.js index f5842d5bfb..e0a6a187c8 100644 --- a/lib/routes/admin-api/event.js +++ b/lib/routes/admin-api/event.js @@ -1,35 +1,35 @@ 'use strict'; -const { Router } = require('express'); +const Controller = require('../controller'); const eventDiffer = require('../../event-differ'); const version = 1; -module.exports.router = function(config) { - const { eventStore } = config.stores; - const router = Router(); +class EventController extends Controller { + constructor({ eventStore }) { + super(); + this.eventStore = eventStore; + this.get('/', this.getEvents); + this.get('/:name', this.getEventsForToggle); + } - router.get('/', (req, res) => { - eventStore.getEvents().then(events => { - eventDiffer.addDiffs(events); - res.json({ version, events }); - }); - }); + async getEvents(req, res) { + const events = await this.eventStore.getEvents(); + eventDiffer.addDiffs(events); + res.json({ version, events }); + } - router.get('/:name', (req, res) => { + async getEventsForToggle(req, res) { const toggleName = req.params.name; - eventStore.getEventsFilterByName(toggleName).then(events => { - if (events) { - eventDiffer.addDiffs(events); - res.json({ - toggleName, - events, - }); - } else { - res.status(404).json({ error: 'Could not find events' }); - } - }); - }); + const events = await this.eventStore.getEventsFilterByName(toggleName); - return router; -}; + if (events) { + eventDiffer.addDiffs(events); + res.json({ toggleName, events }); + } else { + res.status(404).json({ error: 'Could not find events' }); + } + } +} + +module.exports = EventController; diff --git a/lib/routes/admin-api/index.js b/lib/routes/admin-api/index.js index 5920c51790..f789a8f415 100644 --- a/lib/routes/admin-api/index.js +++ b/lib/routes/admin-api/index.js @@ -1,36 +1,32 @@ 'use strict'; -const { Router } = require('express'); - +const Controller = require('../controller'); const features = require('./feature.js'); const ArchiveController = require('./archive.js'); -const events = require('./event.js'); +const EventController = require('./event.js'); const strategies = require('./strategy'); const metrics = require('./metrics'); const user = require('./user'); const apiDef = require('./api-def.json'); -class AdminApi { +class AdminApi extends Controller { constructor(config) { - const router = Router(); - this._router = router; + super(); - router.get('/', (req, res) => res.json(apiDef)); - router.use('/features', features.router(config)); - router.use('/archive', new ArchiveController(config.stores).router()); - router.use('/strategies', strategies.router(config)); - router.use('/events', events.router(config)); - router.use('/metrics', metrics.router(config)); - router.use('/user', user.router(config)); + const stores = config.stores; + + this.app.get('/', this.index); + this.app.use('/features', features.router(config)); + this.app.use('/archive', new ArchiveController(stores).router()); + this.app.use('/strategies', strategies.router(config)); + this.app.use('/events', new EventController(stores).router()); + this.app.use('/metrics', metrics.router(config)); + this.app.use('/user', user.router(config)); } index(req, res) { res.json(apiDef); } - - router() { - return this._router; - } } module.exports = AdminApi; diff --git a/lib/routes/controller.js b/lib/routes/controller.js new file mode 100644 index 0000000000..5787a82ab1 --- /dev/null +++ b/lib/routes/controller.js @@ -0,0 +1,26 @@ +'use strict'; + +const { Router } = require('express'); +/** + * Base class for Controllers to standardize binding to express Router. + */ +class Controller { + constructor() { + const router = Router(); + this.app = router; + } + + get(path, handler) { + this.app.get(path, handler.bind(this)); + } + + post(path, handler) { + this.app.post(path, handler.bind(this)); + } + + router() { + return this.app; + } +} + +module.exports = Controller;