2017-06-28 10:20:22 +02:00
|
|
|
'use strict';
|
|
|
|
|
2018-12-03 08:59:13 +01:00
|
|
|
const Controller = require('../controller');
|
2018-11-24 15:45:22 +01:00
|
|
|
const { filter } = require('./util');
|
2017-06-28 10:20:22 +02:00
|
|
|
|
|
|
|
const version = 1;
|
|
|
|
|
2018-12-03 08:59:13 +01:00
|
|
|
class FeatureController extends Controller {
|
2018-11-29 21:01:53 +01:00
|
|
|
constructor({ featureToggleStore }) {
|
2018-12-03 08:59:13 +01:00
|
|
|
super();
|
2018-11-29 21:01:53 +01:00
|
|
|
this.toggleStore = featureToggleStore;
|
2017-06-28 10:20:22 +02:00
|
|
|
|
2018-12-03 08:59:13 +01:00
|
|
|
this.get('/', this.getAll);
|
|
|
|
this.get('/:featureName', this.getFeatureToggle);
|
2018-11-24 15:02:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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' });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = FeatureController;
|