2016-11-10 14:26:24 +01:00
|
|
|
import { throwIfNotSuccess, headers } from './helper';
|
|
|
|
|
2017-06-23 08:48:45 +02:00
|
|
|
const URI = 'api/admin/features';
|
2016-11-10 14:26:24 +01:00
|
|
|
|
2016-11-13 22:25:14 +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 () {
|
2017-06-23 08:48:45 +02:00
|
|
|
return fetch(URI, { credentials: 'include' })
|
2016-11-10 14:26:24 +01:00
|
|
|
.then(throwIfNotSuccess)
|
|
|
|
.then(response => response.json());
|
|
|
|
}
|
|
|
|
|
|
|
|
function create (featureToggle) {
|
2016-11-13 22:25:14 +01:00
|
|
|
return validateToggle(featureToggle)
|
|
|
|
.then(() => fetch(URI, {
|
|
|
|
method: 'POST',
|
|
|
|
headers,
|
2016-11-24 21:58:42 +01:00
|
|
|
credentials: 'include',
|
2016-11-13 22:25:14 +01:00
|
|
|
body: JSON.stringify(featureToggle),
|
|
|
|
}))
|
|
|
|
.then(throwIfNotSuccess);
|
2016-11-10 14:26:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function validate (featureToggle) {
|
2017-06-23 08:48:45 +02:00
|
|
|
return fetch(`${URI}/validate`, {
|
2016-11-10 14:26:24 +01:00
|
|
|
method: 'POST',
|
|
|
|
headers,
|
2016-11-24 21:58:42 +01:00
|
|
|
credentials: 'include',
|
2016-11-10 14:26:24 +01:00
|
|
|
body: JSON.stringify(featureToggle),
|
|
|
|
}).then(throwIfNotSuccess);
|
|
|
|
}
|
|
|
|
|
|
|
|
function update (featureToggle) {
|
2016-11-13 22:25:14 +01:00
|
|
|
return validateToggle(featureToggle)
|
|
|
|
.then(() => fetch(`${URI}/${featureToggle.name}`, {
|
|
|
|
method: 'PUT',
|
|
|
|
headers,
|
2016-11-24 21:58:42 +01:00
|
|
|
credentials: 'include',
|
2016-11-13 22:25:14 +01:00
|
|
|
body: JSON.stringify(featureToggle),
|
|
|
|
}))
|
|
|
|
.then(throwIfNotSuccess);
|
2016-11-10 14:26:24 +01:00
|
|
|
}
|
|
|
|
|
2017-01-09 11:08:04 +01:00
|
|
|
function toggle (name) {
|
|
|
|
return fetch(`${URI}/${name}/toggle`, {
|
|
|
|
method: 'POST',
|
|
|
|
headers,
|
|
|
|
credentials: 'include',
|
|
|
|
})
|
2017-06-29 08:36:10 +02:00
|
|
|
.then(throwIfNotSuccess);
|
2017-01-09 11:08:04 +01:00
|
|
|
}
|
|
|
|
|
2016-11-10 14:26:24 +01:00
|
|
|
function remove (featureToggleName) {
|
|
|
|
return fetch(`${URI}/${featureToggleName}`, {
|
|
|
|
method: 'DELETE',
|
2016-11-24 21:58:42 +01:00
|
|
|
credentials: 'include',
|
2016-11-10 14:26:24 +01:00
|
|
|
}).then(throwIfNotSuccess);
|
|
|
|
}
|
|
|
|
|
2017-07-10 23:30:38 +02:00
|
|
|
export default {
|
2016-11-10 14:26:24 +01:00
|
|
|
fetchAll,
|
|
|
|
create,
|
|
|
|
validate,
|
|
|
|
update,
|
2017-01-09 11:08:04 +01:00
|
|
|
toggle,
|
2016-11-10 14:26:24 +01:00
|
|
|
remove,
|
|
|
|
};
|