2017-07-21 16:23:34 +02:00
|
|
|
import 'whatwg-fetch';
|
2017-09-26 16:36:28 +02:00
|
|
|
import { startsWith } from 'lodash';
|
2017-07-21 16:23:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {object|undefined} Returns either the response, or throws an error
|
|
|
|
*/
|
|
|
|
function checkStatus(response) {
|
2017-07-24 11:11:28 +02:00
|
|
|
|
2017-07-21 16:23:34 +02:00
|
|
|
if (response.status >= 200 && response.status < 300) {
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parseJSON(response).then(responseFormatted => {
|
|
|
|
const error = new Error(response.statusText);
|
|
|
|
error.response = response;
|
|
|
|
error.response.payload = responseFormatted;
|
|
|
|
throw error;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Format query params
|
|
|
|
*
|
|
|
|
* @param params
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
function formatQueryParams(params) {
|
|
|
|
return Object.keys(params)
|
|
|
|
.map(k => `${encodeURIComponent(k)}=${encodeURIComponent(params[k])}`)
|
|
|
|
.join('&');
|
|
|
|
}
|
|
|
|
|
2017-09-30 13:49:49 +02:00
|
|
|
function serverRestartWatcher(response) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
setTimeout(() => {
|
|
|
|
resolve(response);
|
|
|
|
}, 5000);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-21 16:23:34 +02:00
|
|
|
/**
|
|
|
|
* 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} The response data
|
|
|
|
*/
|
2017-09-30 13:49:49 +02:00
|
|
|
export default function request(url, options, shouldWatchServerRestart = false) {
|
2017-07-21 16:23:34 +02:00
|
|
|
const optionsObj = options || {};
|
|
|
|
|
|
|
|
// Set headers
|
|
|
|
optionsObj.headers = {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
};
|
|
|
|
|
|
|
|
// Add parameters to url
|
2017-09-26 16:36:28 +02:00
|
|
|
let urlFormatted = startsWith(url, '/')
|
2017-08-22 10:51:53 +02:00
|
|
|
? `${Strapi.apiUrl}${url}`
|
|
|
|
: url;
|
|
|
|
|
2017-07-21 16:23:34 +02:00
|
|
|
if (optionsObj && optionsObj.params) {
|
|
|
|
const params = formatQueryParams(optionsObj.params);
|
|
|
|
urlFormatted = `${url}?${params}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stringify body object
|
|
|
|
if (optionsObj && optionsObj.body) {
|
|
|
|
optionsObj.body = JSON.stringify(optionsObj.body);
|
|
|
|
}
|
|
|
|
|
2017-09-30 13:49:49 +02:00
|
|
|
return fetch(urlFormatted, optionsObj)
|
|
|
|
.then(checkStatus)
|
|
|
|
.then(parseJSON)
|
|
|
|
.then((response) => {
|
|
|
|
if (shouldWatchServerRestart) {
|
|
|
|
return serverRestartWatcher(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
});
|
2017-07-21 16:23:34 +02:00
|
|
|
}
|