mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-31 00:16:47 +01:00
chore(modernize): Modernize FeaturesController
This commit is contained in:
parent
00217bfe14
commit
afae3317de
@ -9,26 +9,38 @@ const filter = (key, value) => {
|
|||||||
return array => array.filter(item => item[key].startsWith(value));
|
return array => array.filter(item => item[key].startsWith(value));
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.router = config => {
|
class FeatureController {
|
||||||
const router = Router();
|
constructor(config) {
|
||||||
const { featureToggleStore } = config.stores;
|
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);
|
const nameFilter = filter('name', req.query.namePrefix);
|
||||||
featureToggleStore
|
|
||||||
.getFeatures()
|
|
||||||
.then(nameFilter)
|
|
||||||
.then(features => res.json({ version, features }));
|
|
||||||
});
|
|
||||||
|
|
||||||
router.get('/:featureName', (req, res) => {
|
const allFeatureToggles = await this.store.getFeatures();
|
||||||
featureToggleStore
|
const features = nameFilter(allFeatureToggles);
|
||||||
.getFeature(req.params.featureName)
|
|
||||||
.then(feature => res.json(feature).end())
|
|
||||||
.catch(() =>
|
|
||||||
res.status(404).json({ error: 'Could not find feature' })
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
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;
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const { Router } = require('express');
|
const { Router } = require('express');
|
||||||
const features = require('./feature.js');
|
const FeatureController = require('./feature.js');
|
||||||
const metrics = require('./metrics.js');
|
const metrics = require('./metrics.js');
|
||||||
const register = require('./register.js');
|
const register = require('./register.js');
|
||||||
|
|
||||||
@ -22,7 +22,7 @@ exports.router = config => {
|
|||||||
res.json(apiDef);
|
res.json(apiDef);
|
||||||
});
|
});
|
||||||
|
|
||||||
router.use('/features', features.router(config));
|
router.use('/features', new FeatureController(config).router());
|
||||||
router.use('/metrics', metrics.router(config));
|
router.use('/metrics', metrics.router(config));
|
||||||
router.use('/register', register.router(config));
|
router.use('/register', register.router(config));
|
||||||
|
|
||||||
|
@ -8,7 +8,7 @@ const version = module.exports.version;
|
|||||||
|
|
||||||
const adminApi = require('./admin-api');
|
const adminApi = require('./admin-api');
|
||||||
const clientApi = require('./client-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 HealthCheckController = require('./health-check');
|
||||||
const BackstageController = require('./backstage.js');
|
const BackstageController = require('./backstage.js');
|
||||||
@ -29,7 +29,7 @@ class IndexRouter {
|
|||||||
|
|
||||||
// legacy support (remove in 4.x)
|
// legacy support (remove in 4.x)
|
||||||
if (config.enableLegacyRoutes) {
|
if (config.enableLegacyRoutes) {
|
||||||
router.use('/api/features', clientFeatures.router(config));
|
router.use('/api/features', new FeatureController(config).router());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user