2016-03-29 04:38:48 +02:00
|
|
|
import fetch from 'isomorphic-fetch';
|
|
|
|
import es6promise from 'es6-promise';
|
|
|
|
es6promise.polyfill();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see https://github.com/github/fetch#handling-http-error-statuses
|
|
|
|
*/
|
|
|
|
function checkStatus(response) {
|
2016-03-30 23:45:54 +02:00
|
|
|
let ret;
|
|
|
|
let error;
|
2016-03-29 04:38:48 +02:00
|
|
|
if (response.status >= 200 && response.status < 300) {
|
2016-03-30 23:45:54 +02:00
|
|
|
ret = response;
|
2016-03-29 04:38:48 +02:00
|
|
|
} else {
|
2016-03-30 23:45:54 +02:00
|
|
|
error = new Error(response.statusText);
|
|
|
|
error.response = response;
|
|
|
|
throw error;
|
2016-03-29 04:38:48 +02:00
|
|
|
}
|
2016-03-30 23:45:54 +02:00
|
|
|
|
|
|
|
return ret;
|
2016-03-29 04:38:48 +02:00
|
|
|
}
|
2016-03-16 01:30:39 +01:00
|
|
|
|
|
|
|
class SilverStripeBackend {
|
|
|
|
|
2016-03-30 23:45:54 +02:00
|
|
|
constructor() {
|
|
|
|
// Allow mocking
|
|
|
|
this.fetch = fetch;
|
|
|
|
}
|
2016-03-29 04:38:48 +02:00
|
|
|
|
2016-03-30 23:45:54 +02:00
|
|
|
/**
|
|
|
|
* Makes a network request using the GET HTTP verb.
|
|
|
|
*
|
|
|
|
* @param string url - Endpoint URL.
|
|
|
|
* @return object - Promise
|
|
|
|
*/
|
|
|
|
get(url) {
|
|
|
|
return this.fetch(url, { method: 'get', credentials: 'same-origin' })
|
|
|
|
.then(checkStatus);
|
|
|
|
}
|
2016-03-16 01:30:39 +01:00
|
|
|
|
2016-03-30 23:45:54 +02:00
|
|
|
/**
|
|
|
|
* Makes a network request using the POST HTTP verb.
|
|
|
|
*
|
|
|
|
* @param string url - Endpoint URL.
|
|
|
|
* @param object data - Data to send with the request.
|
|
|
|
* @return object - Promise
|
|
|
|
*/
|
|
|
|
post(url, data) {
|
2016-04-03 11:04:59 +02:00
|
|
|
return this.fetch(url, {
|
|
|
|
method: 'post',
|
|
|
|
headers: new Headers({
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
}),
|
|
|
|
credentials: 'same-origin',
|
|
|
|
body: data,
|
|
|
|
})
|
|
|
|
.then(checkStatus);
|
2016-03-30 23:45:54 +02:00
|
|
|
}
|
2016-03-16 01:30:39 +01:00
|
|
|
|
2016-03-30 23:45:54 +02:00
|
|
|
/**
|
|
|
|
* Makes a newtwork request using the PUT HTTP verb.
|
|
|
|
*
|
|
|
|
* @param string url - Endpoint URL.
|
|
|
|
* @param object data - Data to send with the request.
|
|
|
|
* @return object - Promise
|
|
|
|
*/
|
|
|
|
put(url, data) {
|
|
|
|
return this.fetch(url, { method: 'put', credentials: 'same-origin', body: data })
|
|
|
|
.then(checkStatus);
|
|
|
|
}
|
2016-03-16 01:30:39 +01:00
|
|
|
|
2016-03-30 23:45:54 +02:00
|
|
|
/**
|
|
|
|
* Makes a newtwork request using the DELETE HTTP verb.
|
|
|
|
*
|
|
|
|
* @param string url - Endpoint URL.
|
|
|
|
* @param object data - Data to send with the request.
|
|
|
|
* @return object - Promise
|
|
|
|
*/
|
|
|
|
delete(url, data) {
|
|
|
|
return this.fetch(url, { method: 'delete', credentials: 'same-origin', body: data })
|
|
|
|
.then(checkStatus);
|
|
|
|
}
|
2016-03-16 01:30:39 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exported as a singleton so we can implement things like
|
|
|
|
// global caching and request batching at some stage.
|
2016-03-30 23:45:54 +02:00
|
|
|
const backend = new SilverStripeBackend();
|
2016-03-16 01:30:39 +01:00
|
|
|
|
|
|
|
export default backend;
|