1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/packages/unleash-api/lib/routes/feature.js

119 lines
4.0 KiB
JavaScript
Raw Normal View History

2016-06-18 21:53:18 +02:00
'use strict';
2016-06-18 21:55:46 +02:00
const Promise = require('bluebird');
2016-06-18 21:53:18 +02:00
const logger = require('../logger');
const eventType = require('../eventType');
const NameExistsError = require('../error/NameExistsError');
const NotFoundError = require('../error/NotFoundError');
const ValidationError = require('../error/ValidationError');
const validateRequest = require('../error/validateRequest');
const extractUser = require('../extractUser');
2014-10-20 15:12:30 +02:00
module.exports = function (app, config) {
2016-06-18 21:53:18 +02:00
const featureDb = config.featureDb;
const eventStore = config.eventStore;
2016-06-18 21:53:18 +02:00
app.get('/features', (req, res) => {
featureDb.getFeatures().then(features => {
res.json({ features });
});
2014-10-20 15:12:30 +02:00
});
2016-06-18 21:53:18 +02:00
app.get('/features/:featureName', (req, res) => {
featureDb.getFeature(req.params.featureName)
2016-06-18 21:53:18 +02:00
.then(feature => {
res.json(feature);
})
2016-06-18 21:53:18 +02:00
.catch(() => {
res.status(404).json({ error: 'Could not find feature' });
});
2014-10-20 15:12:30 +02:00
});
2016-06-18 21:53:18 +02:00
app.post('/features', (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)
2016-06-18 21:53:18 +02:00
.then(() => eventStore.create({
2016-06-18 21:55:46 +02:00
type: eventType.featureCreated,
createdBy: extractUser(req),
data: req.body,
}))
2016-06-18 21:53:18 +02:00
.then(() => {
res.status(201).end();
})
2016-06-18 21:53:18 +02:00
.catch(NameExistsError, () => {
2014-12-17 21:56:27 +01:00
res.status(403).json([{
2016-06-18 21:55:46 +02:00
msg: `A feature named '${req.body.name}' already exists. It could be archived.`,
2014-12-17 21:56:27 +01:00
}]).end();
})
2016-06-18 21:53:18 +02:00
.catch(ValidationError, () => {
res.status(400).json(req.validationErrors());
})
2016-06-18 21:53:18 +02:00
.catch(err => {
2016-06-18 21:55:46 +02:00
logger.error('Could not create feature toggle', err);
res.status(500).end();
});
});
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);
const updatedFeature = req.body;
updatedFeature.name = featureName;
featureDb.getFeature(featureName)
2016-06-18 21:53:18 +02:00
.then(() => eventStore.create({
2016-06-18 21:55:46 +02:00
type: eventType.featureUpdated,
createdBy: userName,
data: updatedFeature,
}))
2016-06-18 21:53:18 +02:00
.then(() => {
res.status(200).end();
})
2016-06-18 21:53:18 +02:00
.catch(NotFoundError, () => {
res.status(404).end();
})
2016-06-18 21:53:18 +02:00
.catch(err => {
logger.error(`Could not update feature toggle=${featureName}`, err);
res.status(500).end();
});
2014-10-21 15:28:10 +02: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
featureDb.getFeature(featureName)
2016-06-18 21:53:18 +02:00
.then(() => eventStore.create({
2016-06-18 21:55:46 +02:00
type: eventType.featureArchived,
createdBy: userName,
data: {
name: featureName,
},
}))
2016-06-18 21:53:18 +02:00
.then(() => {
2014-12-15 22:40:07 +01:00
res.status(200).end();
})
2016-06-18 21:53:18 +02:00
.catch(NotFoundError, () => {
2014-12-15 22:40:07 +01:00
res.status(404).end();
})
2016-06-18 21:53:18 +02:00
.catch(err => {
logger.error(`Could not archive feature=${featureName}`, err);
2014-12-15 22:40:07 +01:00
res.status(500).end();
});
});
function validateUniqueName(req) {
2016-06-18 21:53:18 +02:00
return new Promise((resolve, reject) => {
featureDb.getFeature(req.body.name)
2016-06-18 21:53:18 +02:00
.then(() => {
2016-06-18 21:55:46 +02:00
reject(new NameExistsError('Feature name already exist'));
2016-06-18 21:53:18 +02:00
}, () => {
resolve(req);
});
});
}
2014-10-20 15:12:30 +02:00
};