2017-06-28 10:20:22 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const { Router } = require('express');
|
2018-11-24 15:45:22 +01:00
|
|
|
const { filter } = require('./util');
|
2017-06-28 10:20:22 +02:00
|
|
|
|
|
|
|
const version = 1;
|
|
|
|
|
2018-11-24 15:02:31 +01:00
|
|
|
class FeatureController {
|
2018-11-29 21:01:53 +01:00
|
|
|
constructor({ featureToggleStore }) {
|
2018-11-24 15:02:31 +01:00
|
|
|
const router = Router();
|
|
|
|
this._router = router;
|
2018-11-29 21:01:53 +01:00
|
|
|
this.toggleStore = featureToggleStore;
|
2017-06-28 10:20:22 +02:00
|
|
|
|
2018-11-24 15:02:31 +01:00
|
|
|
router.get('/', (req, res) => this.getAll(req, res));
|
|
|
|
router.get('/:featureName', this.getFeatureToggle.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
async getAll(req, res) {
|
2018-11-16 15:56:14 +01:00
|
|
|
const nameFilter = filter('name', req.query.namePrefix);
|
2018-11-24 15:02:31 +01:00
|
|
|
|
2018-11-29 21:01:53 +01:00
|
|
|
const allFeatureToggles = await this.toggleStore.getFeatures();
|
2018-11-24 15:02:31 +01:00
|
|
|
const features = nameFilter(allFeatureToggles);
|
|
|
|
|
|
|
|
res.json({ version, features });
|
|
|
|
}
|
|
|
|
|
|
|
|
async getFeatureToggle(req, res) {
|
|
|
|
try {
|
|
|
|
const name = req.params.featureName;
|
2018-11-29 21:01:53 +01:00
|
|
|
const featureToggle = await this.toggleStore.getFeature(name);
|
2018-11-24 15:02:31 +01:00
|
|
|
res.json(featureToggle).end();
|
|
|
|
} catch (err) {
|
|
|
|
res.status(404).json({ error: 'Could not find feature' });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
router() {
|
|
|
|
return this._router;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = FeatureController;
|