2016-06-18 21:53:18 +02:00
|
|
|
'use strict';
|
2016-10-26 10:43:11 +02:00
|
|
|
|
2016-06-20 10:27:19 +02:00
|
|
|
const logger = require('../logger');
|
2016-10-27 20:51:21 +02:00
|
|
|
const eventType = require('../event-type');
|
|
|
|
const NameExistsError = require('../error/name-exists-error');
|
|
|
|
const NotFoundError = require('../error/notfound-error');
|
|
|
|
const ValidationError = require('../error/validation-error.js');
|
|
|
|
const validateRequest = require('../error/validate-request');
|
|
|
|
const extractUser = require('../extract-user');
|
2014-10-20 15:12:30 +02:00
|
|
|
|
2016-07-02 16:25:30 +02:00
|
|
|
const legacyFeatureMapper = require('../helper/legacy-feature-mapper');
|
2016-09-05 16:50:52 +02:00
|
|
|
const version = 1;
|
2016-07-02 16:25:30 +02:00
|
|
|
|
2016-11-05 15:08:00 +01:00
|
|
|
const handleErrors = (req, res, error) => {
|
|
|
|
switch (error.constructor) {
|
|
|
|
case NotFoundError:
|
|
|
|
return res
|
|
|
|
.status(404)
|
|
|
|
.end();
|
|
|
|
case NameExistsError:
|
|
|
|
return res
|
|
|
|
.status(403)
|
|
|
|
.json([{ msg: `A feature named '${req.body.name}' already exists.` }])
|
|
|
|
.end();
|
|
|
|
case ValidationError:
|
|
|
|
return res
|
|
|
|
.status(400)
|
|
|
|
.json(req.validationErrors())
|
|
|
|
.end();
|
|
|
|
default:
|
|
|
|
logger.error('Server failed executing request', error);
|
|
|
|
return res
|
|
|
|
.status(500)
|
|
|
|
.end();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-01 22:53:09 +02:00
|
|
|
module.exports = function (app, config) {
|
2016-11-05 14:08:47 +01:00
|
|
|
const { featureToggleStore, eventStore } = config.stores;
|
2016-05-01 22:53:09 +02:00
|
|
|
|
2016-06-18 21:53:18 +02:00
|
|
|
app.get('/features', (req, res) => {
|
2016-11-05 10:16:48 +01:00
|
|
|
featureToggleStore.getFeatures()
|
2016-07-02 16:25:30 +02:00
|
|
|
.then((features) => features.map(legacyFeatureMapper.addOldFields))
|
2016-09-05 16:50:52 +02:00
|
|
|
.then(features => res.json({ version, features }));
|
2014-10-20 15:12:30 +02:00
|
|
|
});
|
|
|
|
|
2016-06-18 21:53:18 +02:00
|
|
|
app.get('/features/:featureName', (req, res) => {
|
2016-11-05 10:16:48 +01:00
|
|
|
featureToggleStore.getFeature(req.params.featureName)
|
2016-07-02 16:25:30 +02:00
|
|
|
.then(legacyFeatureMapper.addOldFields)
|
|
|
|
.then(feature => res.json(feature).end())
|
|
|
|
.catch(() => res.status(404).json({ error: 'Could not find feature' }));
|
2014-10-20 15:12:30 +02:00
|
|
|
});
|
|
|
|
|
2016-11-05 11:37:59 +01:00
|
|
|
app.post('/features-validate', (req, res) => {
|
|
|
|
req.checkBody('name', 'Name is required').notEmpty();
|
|
|
|
req.checkBody('name', 'Name must match format ^[0-9a-zA-Z\\.\\-]+$').matches(/^[0-9a-zA-Z\\.\\-]+$/i);
|
|
|
|
|
|
|
|
validateRequest(req)
|
|
|
|
.then(validateFormat)
|
|
|
|
.then(validateUniqueName)
|
|
|
|
.then(() => res.status(201).end())
|
2016-11-05 15:08:00 +01:00
|
|
|
.catch(error => handleErrors(req, res, error));
|
2016-11-05 11:37:59 +01:00
|
|
|
});
|
|
|
|
|
2016-06-18 21:53:18 +02:00
|
|
|
app.post('/features', (req, res) => {
|
2014-11-01 11:47:21 +01:00
|
|
|
req.checkBody('name', 'Name is required').notEmpty();
|
2016-08-22 15:16:50 +02:00
|
|
|
req.checkBody('name', 'Name must match format ^[0-9a-zA-Z\\.\\-]+$').matches(/^[0-9a-zA-Z\\.\\-]+$/i);
|
2014-11-01 11:47:21 +01:00
|
|
|
|
2014-12-10 19:11:52 +01:00
|
|
|
validateRequest(req)
|
2016-08-23 00:04:11 +02:00
|
|
|
.then(validateFormat)
|
2014-12-10 18:49:08 +01:00
|
|
|
.then(validateUniqueName)
|
2016-11-05 10:16:48 +01:00
|
|
|
.then(() => eventStore.store({
|
2016-06-18 21:55:46 +02:00
|
|
|
type: eventType.featureCreated,
|
|
|
|
createdBy: extractUser(req),
|
2016-07-02 16:25:30 +02:00
|
|
|
data: legacyFeatureMapper.toNewFormat(req.body),
|
2016-06-18 21:55:46 +02:00
|
|
|
}))
|
2016-07-02 16:25:30 +02:00
|
|
|
.then(() => res.status(201).end())
|
2016-11-05 15:08:00 +01:00
|
|
|
.catch(error => handleErrors(req, res, error));
|
2016-04-24 22:41:37 +02:00
|
|
|
});
|
2014-10-20 15:12:30 +02:00
|
|
|
|
2016-06-18 21:53:18 +02:00
|
|
|
app.put('/features/:featureName', (req, res) => {
|
|
|
|
const featureName = req.params.featureName;
|
|
|
|
const userName = extractUser(req);
|
2016-07-02 16:25:30 +02:00
|
|
|
const updatedFeature = legacyFeatureMapper.toNewFormat(req.body);
|
2014-11-14 12:56:23 +01:00
|
|
|
|
|
|
|
updatedFeature.name = featureName;
|
|
|
|
|
2016-11-05 10:16:48 +01:00
|
|
|
featureToggleStore.getFeature(featureName)
|
|
|
|
.then(() => eventStore.store({
|
2016-06-18 21:55:46 +02:00
|
|
|
type: eventType.featureUpdated,
|
|
|
|
createdBy: userName,
|
|
|
|
data: updatedFeature,
|
|
|
|
}))
|
2016-07-02 16:25:30 +02:00
|
|
|
.then(() => res.status(200).end())
|
2016-11-05 15:08:00 +01:00
|
|
|
.catch(error => handleErrors(req, res, error));
|
2014-10-21 15:28:10 +02:00
|
|
|
});
|
2014-12-10 18:49:08 +01:00
|
|
|
|
2016-06-18 21:53:18 +02:00
|
|
|
app.delete('/features/:featureName', (req, res) => {
|
|
|
|
const featureName = req.params.featureName;
|
|
|
|
const userName = extractUser(req);
|
2014-12-15 22:40:07 +01:00
|
|
|
|
2016-11-05 10:16:48 +01:00
|
|
|
featureToggleStore.getFeature(featureName)
|
|
|
|
.then(() => eventStore.store({
|
2016-06-18 21:55:46 +02:00
|
|
|
type: eventType.featureArchived,
|
|
|
|
createdBy: userName,
|
|
|
|
data: {
|
|
|
|
name: featureName,
|
|
|
|
},
|
|
|
|
}))
|
2016-07-02 16:25:30 +02:00
|
|
|
.then(() => res.status(200).end())
|
2016-11-05 15:08:00 +01:00
|
|
|
.catch(error => handleErrors(req, res, error));
|
2014-12-15 22:40:07 +01:00
|
|
|
});
|
|
|
|
|
2016-07-02 11:54:50 +02:00
|
|
|
function validateUniqueName (req) {
|
2016-11-05 15:08:00 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2016-11-05 10:16:48 +01:00
|
|
|
featureToggleStore.getFeature(req.body.name)
|
2016-11-05 15:08:00 +01:00
|
|
|
.then(() => reject(new NameExistsError('Feature name already exist')))
|
|
|
|
.catch(() => resolve(req));
|
2014-12-10 18:49:08 +01:00
|
|
|
});
|
|
|
|
}
|
2016-08-23 00:04:11 +02:00
|
|
|
|
|
|
|
function validateFormat (req) {
|
|
|
|
if (req.body.strategy && req.body.strategies) {
|
2016-11-05 15:08:00 +01:00
|
|
|
return Promise.reject(new ValidationError('Cannot use both "strategy" and "strategies".'));
|
2016-08-23 00:04:11 +02:00
|
|
|
}
|
|
|
|
|
2016-11-05 15:08:00 +01:00
|
|
|
return Promise.resolve(req);
|
2016-08-23 00:04:11 +02:00
|
|
|
}
|
2014-10-20 15:12:30 +02:00
|
|
|
};
|