1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-28 00:17:12 +01:00
unleash.unleash/frontend/src/data/feature-api.js

80 lines
1.9 KiB
JavaScript
Raw Normal View History

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
function validateToggle(featureToggle) {
return new Promise((resolve, reject) => {
2017-08-28 21:40:44 +02:00
if (!featureToggle.strategies || featureToggle.strategies.length === 0) {
reject(new Error('You must add at least one activation strategy'));
} else {
resolve(featureToggle);
}
});
}
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) {
return validateToggle(featureToggle)
.then(() =>
fetch(URI, {
method: 'POST',
headers,
credentials: 'include',
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,
credentials: 'include',
2016-11-10 14:26:24 +01:00
body: JSON.stringify(featureToggle),
}).then(throwIfNotSuccess);
}
function update(featureToggle) {
return validateToggle(featureToggle)
.then(() =>
fetch(`${URI}/${featureToggle.name}`, {
method: 'PUT',
headers,
credentials: 'include',
body: JSON.stringify(featureToggle),
})
)
.then(throwIfNotSuccess);
2016-11-10 14:26:24 +01:00
}
function toggle(enable, name) {
const action = enable ? 'on' : 'off';
return fetch(`${URI}/${name}/toggle/${action}`, {
method: 'POST',
headers,
credentials: 'include',
}).then(throwIfNotSuccess);
}
function remove(featureToggleName) {
2016-11-10 14:26:24 +01:00
return fetch(`${URI}/${featureToggleName}`, {
method: 'DELETE',
credentials: 'include',
2016-11-10 14:26:24 +01:00
}).then(throwIfNotSuccess);
}
export default {
2016-11-10 14:26:24 +01:00
fetchAll,
create,
validate,
update,
toggle,
2016-11-10 14:26:24 +01:00
remove,
};