1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00
unleash.unleash/lib/routes/feature.js

124 lines
4.3 KiB
JavaScript
Raw Normal View History

2014-12-10 18:51:27 +01:00
var Promise = require("bluebird");
2016-04-28 16:58:59 +02:00
var logger = require('../logger');
var eventType = require('../eventType');
var NameExistsError = require('../error/NameExistsError');
var NotFoundError = require('../error/NotFoundError');
var ValidationError = require('../error/ValidationError');
var validateRequest = require('../error/validateRequest');
var extractUser = require('../extractUser');
2014-10-20 15:12:30 +02:00
module.exports = function (app, config) {
var featureDb = config.featureDb;
var eventStore = config.eventStore;
app.get('/features', function (req, res) {
featureDb.getFeatures().then(function (features) {
res.json({ features: features });
});
2014-10-20 15:12:30 +02:00
});
2014-10-23 16:00:51 +02:00
app.get('/features/:featureName', function (req, res) {
featureDb.getFeature(req.params.featureName)
.then(function (feature) {
res.json(feature);
})
.catch(function () {
res.status(404).json({ error: 'Could not find feature' });
});
2014-10-20 15:12:30 +02:00
});
app.post('/features', function (req, res) {
req.checkBody('name', 'Name is required').notEmpty();
req.checkBody('name', 'Name must match format ^[a-zA-Z\\.\\-]+$').matches(/^[a-zA-Z\\.\\-]+$/i);
validateRequest(req)
.then(validateUniqueName)
.then(function() {
return eventStore.create({
type: eventType.featureCreated,
createdBy: extractUser(req),
data: req.body
});
})
.then(function () {
res.status(201).end();
})
.catch(NameExistsError, function() {
2014-12-17 21:56:27 +01:00
res.status(403).json([{
msg: "A feature named '" + req.body.name + "' already exists. It could be archived."
}]).end();
})
.catch(ValidationError, function() {
res.status(400).json(req.validationErrors());
})
.catch(function(err) {
logger.error("Could not create feature toggle", err);
res.status(500).end();
});
});
2014-10-20 15:12:30 +02:00
app.put('/features/:featureName', function (req, res) {
var featureName = req.params.featureName;
var userName = extractUser(req);
var updatedFeature = req.body;
updatedFeature.name = featureName;
featureDb.getFeature(featureName)
.then(function () {
return eventStore.create({
type: eventType.featureUpdated,
createdBy: userName,
data: updatedFeature
});
})
.then(function () {
res.status(200).end();
})
.catch(NotFoundError, function () {
res.status(404).end();
})
.catch(function (err) {
logger.error("Could not update feature toggle="+featureName, err);
res.status(500).end();
});
2014-10-21 15:28:10 +02:00
});
2014-12-15 22:40:07 +01:00
app.delete('/features/:featureName', function (req, res) {
var featureName = req.params.featureName;
var userName = extractUser(req);
2014-12-15 22:40:07 +01:00
featureDb.getFeature(featureName)
.then(function () {
return eventStore.create({
2015-02-09 18:28:51 +01:00
type: eventType.featureArchived,
2014-12-15 22:40:07 +01:00
createdBy: userName,
data: {
name: featureName
}
});
})
.then(function () {
res.status(200).end();
})
.catch(NotFoundError, function () {
res.status(404).end();
})
.catch(function (err) {
logger.error("Could not archive feature="+featureName, err);
res.status(500).end();
});
});
function validateUniqueName(req) {
return new Promise(function(resolve, reject) {
featureDb.getFeature(req.body.name)
.then(function() {
reject(new NameExistsError("Feature name already exist"));
}, function() {
resolve(req);
});
});
}
2014-10-20 15:12:30 +02:00
};