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

Add ability to create custom stratgies. Closes #11.

This commit is contained in:
Jari Bakken 2014-11-25 15:28:08 +01:00 committed by Ivar Conradi Østhus
parent 479ea0772a
commit a169ec1352
9 changed files with 117 additions and 56 deletions

View File

@ -1,6 +1,6 @@
var eventStore = require('./eventStore'),
eventType = require('./eventType'),
featureDb = require('./featureDb');
var eventStore = require('./eventStore');
var eventType = require('./eventType');
var featureDb = require('./featureDb');
module.exports = function (app) {
@ -44,11 +44,9 @@ module.exports = function (app) {
type: eventType.featureCreated,
createdBy: req.connection.remoteAddress,
data: newFeature
}).then(function () {
res.status(201).end();
}, function () {
res.status(500).end();
});
})
.then(function () { res.status(201).end(); })
.catch(function () { res.status(500).end(); });
};
featureDb.getFeature(newFeature.name)

View File

@ -1,25 +1,7 @@
var eventStore = require('./eventStore');
var eventType = require('./eventType');
var strategyDb = require('./strategyDb');
var strategies = [
{
name: "default",
description: "Default on or off Strategy."
},
{
name: "usersWithEmail",
description: "Active for users defined in the comma-separated emails-parameter.",
parametersTemplate: {
emails: "String"
}
}
];
function byName(name) {
return strategies.filter(function(s) {
return s.name === name;
})[0];
}
module.exports = function (app) {
app.get('/strategies', function (req, res) {
@ -29,16 +11,42 @@ module.exports = function (app) {
});
app.get('/strategies/:name', function (req, res) {
var strategy = byName(req.params.name);
if (strategy) {
res.json(strategy);
} else {
res.json(404, {error: 'Could not find strategy'});
}
strategyDb.getStrategy(req.params.name)
.then(function (strategy) { res.json(strategy); })
.catch(function () { res.json(404, {error: 'Could not find strategy'}); });
});
app.post('/strategies', function (req, res) {
res.json(500, {error: 'Not implemented yet'});
req.checkBody('name', 'Name is required').notEmpty();
req.checkBody('name', 'Name must match format ^[a-zA-Z\\.\\-]+$').matches(/^[a-zA-Z\\.\\-]+$/i);
var errors = req.validationErrors();
if (errors) {
res.status(400).json(errors);
return;
}
var newStrategy = req.body;
var handleStrategyExists = function() {
var errors = [{msg: "A strategy named " + newStrategy.name + " already exists."}];
res.status(403).json(errors);
};
var handleCreateStrategy = function() {
eventStore.create({
type: eventType.strategyCreated,
createdBy: req.connection.remoteAddress,
data: newStrategy
})
.then(function () { res.status(201).end(); })
.catch(function () { res.status(500).end(); });
};
strategyDb.getStrategy(newStrategy.name)
.then(handleStrategyExists)
.catch(handleCreateStrategy);
});
};

View File

@ -41,12 +41,20 @@ var StrategiesComponent = React.createClass({
},
onSave: function(strategy) {
var strategies = this.state.strategies.concat([strategy]);
this.setState({
createView: false,
strategies: strategies
});
console.log("Saved strategy: ", strategy);
function handleSuccess() {
var strategies = this.state.strategies.concat([strategy]);
this.setState({
createView: false,
strategies: strategies
});
console.log("Saved strategy: ", strategy);
}
strategyStore.createStrategy(strategy)
.then(handleSuccess.bind(this))
.catch(this.onError);
},
render: function() {

View File

@ -1,7 +1,7 @@
var reqwest = require('reqwest');
TYPE = 'json';
CONTENT_TYPE = 'application/json';
var TYPE = 'json';
var CONTENT_TYPE = 'application/json';
var EventStore = {
getEvents: function () {

View File

@ -3,16 +3,16 @@ var reqwest = require('reqwest');
var FeatureStore = function () {
};
FeatureStore.TYPE = 'json';
FeatureStore.CONTENT_TYPE = 'application/json';
var TYPE = 'json';
var CONTENT_TYPE = 'application/json';
FeatureStore.prototype = {
updateFeature: function (feature) {
return reqwest({
url: 'features/' + feature.name,
method: 'put',
type: FeatureStore.TYPE,
contentType: FeatureStore.CONTENT_TYPE,
type: TYPE,
contentType: CONTENT_TYPE,
data: JSON.stringify(feature)
});
},
@ -21,8 +21,8 @@ FeatureStore.prototype = {
return reqwest({
url: 'features',
method: 'post',
type: FeatureStore.TYPE,
contentType: FeatureStore.CONTENT_TYPE,
type: TYPE,
contentType: CONTENT_TYPE,
data: JSON.stringify(feature)
});
},
@ -31,7 +31,7 @@ FeatureStore.prototype = {
return reqwest({
url: 'features',
method: 'get',
type: FeatureStore.TYPE
type: TYPE
});
}
};

View File

@ -1,7 +1,7 @@
var reqwest = require('reqwest');
TYPE = 'json';
CONTENT_TYPE = 'application/json';
var TYPE = 'json';
var CONTENT_TYPE = 'application/json';
var StrategyStore = {
createStrategy: function (strategy) {

View File

@ -13,6 +13,13 @@ describe('The features api', function () {
.expect(200, done);
});
it('gets a feature by name', function (done) {
request
.get('/features/featureX')
.expect('Content-Type', /json/)
.expect(200, done);
});
it('creates new feature toggle', function (done) {
request
.post('/features')

40
test/strategyApiSpec.js Normal file
View File

@ -0,0 +1,40 @@
var specHelper = require('./specHelper');
describe('The strategy api', function () {
var request;
before(function () { request = specHelper.setupMockServer(); });
after(specHelper.tearDownMockServer);
it('gets all strategies', function (done) {
request
.get('/strategies')
.expect('Content-Type', /json/)
.expect(200, done);
});
it('gets a strategy by name', function (done) {
request
.get('/strategies/default')
.expect('Content-Type', /json/)
.expect(200, done);
});
it('creates a new strategy', function (done) {
request
.post('/strategies')
.send({name: 'myCustomStrategy', description: 'Best strategy ever.'})
.set('Content-Type', 'application/json')
.expect(201, done);
});
it('requires new strategies to have a name', function (done) {
request
.post('/strategies')
.send({name: ''})
.set('Content-Type', 'application/json')
.expect(400, done);
});
});

View File

@ -26,10 +26,10 @@ module.exports = {
resolve(strategies);
});
},
getFeature: function(name) {
var feature = byName(name);
if(feature) {
return Promise.resolve(feature);
getStrategy: function(name) {
var strategy = byName(name);
if(strategy) {
return Promise.resolve(strategy);
} else {
return Promise.reject("strategy not found");
}