Aurélien Georget bd91a2ef93 Add 'packages/strapi-plugin-settings-manager/' from commit 'cd241c14c6a6239bca279e7accd709ba58e87cc8'
git-subtree-dir: packages/strapi-plugin-settings-manager
git-subtree-mainline: 80aa83d8460c95366547e143c74bf79ea6ae69f8
git-subtree-split: cd241c14c6a6239bca279e7accd709ba58e87cc8
2017-01-15 20:14:40 +01:00

60 lines
1.5 KiB
JavaScript

import 'whatwg-fetch';
/**
* Parses the JSON returned by a network request
*
* @param {object} response A response from a network request
*
* @return {object} The parsed JSON from the request
*/
function parseJSON(response) {
return response.json();
}
/**
* Checks if a network request came back fine, and throws an error if not
*
* @param {object} response A response from a network request
*
* @return {Promise} Returns either the response, or throws an error
*/
function checkStatus(response) {
return new Promise((resolve) => {
if (response.status >= 200 && response.status < 300) {
return resolve(response);
}
return parseJSON(response)
.then((data) => {
const error = new Error(data.message || response.statusText);
error.data = data;
error.response = response;
throw error;
});
});
}
/**
* Requests a URL, returning a promise
*
* @param {string} url The URL we want to request
* @param {object} [options] The options we want to pass to "fetch"
*
* @return {object} An object containing either "data" or "err"
*/
export default function request(url, options) {
// Default headers
const params = options || { };
const defaultHeaders = {
Accept: 'application/json',
'Content-Type': 'application/json',
};
params.headers = params && params.headers ? params.headers : defaultHeaders;
return fetch(url, params)
.then(checkStatus)
.then(parseJSON)
.then((data) => ({ data }))
.catch((err) => ({ err }));
}