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

63 lines
1.5 KiB
JavaScript
Raw Normal View History

2016-11-10 14:26:24 +01:00
import { throwIfNotSuccess, headers } from './helper';
2016-11-10 16:09:48 +01:00
const URI = '/api/features';
const URI_VALIDATE = '/api/features-validate';
2016-11-10 14:26:24 +01:00
function validateToggle (featureToggle) {
return new Promise((resolve, reject) => {
if (!featureToggle.strategies || featureToggle.strategies.length === 0) {
reject(new Error('You must add at least one activation strategy'));
} else {
resolve(featureToggle);
}
});
}
2016-11-10 14:26:24 +01:00
function fetchAll () {
return fetch(URI)
.then(throwIfNotSuccess)
.then(response => response.json());
}
function create (featureToggle) {
return validateToggle(featureToggle)
.then(() => fetch(URI, {
method: 'POST',
headers,
body: JSON.stringify(featureToggle),
}))
.then(throwIfNotSuccess);
2016-11-10 14:26:24 +01:00
}
function validate (featureToggle) {
return fetch(URI_VALIDATE, {
method: 'POST',
headers,
body: JSON.stringify(featureToggle),
}).then(throwIfNotSuccess);
}
function update (featureToggle) {
return validateToggle(featureToggle)
.then(() => fetch(`${URI}/${featureToggle.name}`, {
method: 'PUT',
headers,
body: JSON.stringify(featureToggle),
}))
.then(throwIfNotSuccess);
2016-11-10 14:26:24 +01:00
}
function remove (featureToggleName) {
return fetch(`${URI}/${featureToggleName}`, {
method: 'DELETE',
}).then(throwIfNotSuccess);
}
module.exports = {
fetchAll,
create,
validate,
update,
remove,
};