From 461011b89434d36d31b1eb503502f794ad5c4dbb Mon Sep 17 00:00:00 2001 From: ivaosthu Date: Sat, 24 Nov 2018 15:02:31 +0100 Subject: [PATCH] chore(modernize): Modernize FeaturesController --- lib/routes/client-api/feature.js | 50 ++++++++++++++++++++------------ lib/routes/client-api/index.js | 4 +-- lib/routes/index.js | 4 +-- 3 files changed, 35 insertions(+), 23 deletions(-) diff --git a/lib/routes/client-api/feature.js b/lib/routes/client-api/feature.js index d425dc90b6..f545b54f98 100644 --- a/lib/routes/client-api/feature.js +++ b/lib/routes/client-api/feature.js @@ -9,26 +9,38 @@ const filter = (key, value) => { return array => array.filter(item => item[key].startsWith(value)); }; -exports.router = config => { - const router = Router(); - const { featureToggleStore } = config.stores; +class FeatureController { + constructor(config) { + const router = Router(); + this._router = router; + this.store = config.stores.featureToggleStore; - router.get('/', (req, res) => { + router.get('/', (req, res) => this.getAll(req, res)); + router.get('/:featureName', this.getFeatureToggle.bind(this)); + } + + async getAll(req, res) { const nameFilter = filter('name', req.query.namePrefix); - featureToggleStore - .getFeatures() - .then(nameFilter) - .then(features => res.json({ version, features })); - }); - router.get('/:featureName', (req, res) => { - featureToggleStore - .getFeature(req.params.featureName) - .then(feature => res.json(feature).end()) - .catch(() => - res.status(404).json({ error: 'Could not find feature' }) - ); - }); + const allFeatureToggles = await this.store.getFeatures(); + const features = nameFilter(allFeatureToggles); - return router; -}; + res.json({ version, features }); + } + + async getFeatureToggle(req, res) { + try { + const name = req.params.featureName; + const featureToggle = await this.store.getFeature(name); + res.json(featureToggle).end(); + } catch (err) { + res.status(404).json({ error: 'Could not find feature' }); + } + } + + router() { + return this._router; + } +} + +module.exports = FeatureController; diff --git a/lib/routes/client-api/index.js b/lib/routes/client-api/index.js index e7c61798d7..b9f8e6a6e5 100644 --- a/lib/routes/client-api/index.js +++ b/lib/routes/client-api/index.js @@ -1,7 +1,7 @@ 'use strict'; const { Router } = require('express'); -const features = require('./feature.js'); +const FeatureController = require('./feature.js'); const metrics = require('./metrics.js'); const register = require('./register.js'); @@ -22,7 +22,7 @@ exports.router = config => { res.json(apiDef); }); - router.use('/features', features.router(config)); + router.use('/features', new FeatureController(config).router()); router.use('/metrics', metrics.router(config)); router.use('/register', register.router(config)); diff --git a/lib/routes/index.js b/lib/routes/index.js index a941d1b1a8..f516eecec2 100644 --- a/lib/routes/index.js +++ b/lib/routes/index.js @@ -8,7 +8,7 @@ const version = module.exports.version; const adminApi = require('./admin-api'); const clientApi = require('./client-api'); -const clientFeatures = require('./client-api/feature.js'); +const FeatureController = require('./client-api/feature.js'); const HealthCheckController = require('./health-check'); const BackstageController = require('./backstage.js'); @@ -29,7 +29,7 @@ class IndexRouter { // legacy support (remove in 4.x) if (config.enableLegacyRoutes) { - router.use('/api/features', clientFeatures.router(config)); + router.use('/api/features', new FeatureController(config).router()); } }