1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00

chore(modernize): Modernize FeaturesController

This commit is contained in:
ivaosthu 2018-11-24 15:02:31 +01:00 committed by Ivar Conradi Østhus
parent 2dde9c4b95
commit 461011b894
3 changed files with 35 additions and 23 deletions

View File

@ -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;

View File

@ -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));

View File

@ -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());
}
}