1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-01 00:08:27 +01:00
unleash.unleash/lib/routes/client-api/feature.js
ivaosthu 8e30b7643e fix(clientApi): Add namePrefix paramter to /api/client/features
Allows the client to limit the response to name with the given prefix.
2018-11-16 18:37:48 +01:00

35 lines
919 B
JavaScript

'use strict';
const { Router } = require('express');
const version = 1;
const filter = (key, value) => {
if (!key || !value) return array => array;
return array => array.filter(item => item[key].startsWith(value));
};
exports.router = config => {
const router = Router();
const { featureToggleStore } = config.stores;
router.get('/', (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' })
);
});
return router;
};