2017-08-22 10:51:53 +02:00
|
|
|
import 'whatwg-fetch';
|
2017-11-29 11:43:38 +01:00
|
|
|
import auth from 'utils/auth';
|
2018-01-17 12:58:14 +01:00
|
|
|
|
2017-08-22 10:51:53 +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) {
|
2017-09-26 13:46:22 +02:00
|
|
|
return response.json ? response.json() : response;
|
|
|
|
}
|
2017-08-22 10:51:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2018-01-11 15:59:00 +01:00
|
|
|
function checkStatus(response, checkToken = true) {
|
2019-02-22 15:25:11 +01:00
|
|
|
if ((response.status >= 200 && response.status < 300) || response.status === 0) {
|
2017-08-22 10:51:53 +02:00
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2018-01-11 15:59:00 +01:00
|
|
|
if (response.status === 401 && auth.getToken() && checkToken) {
|
|
|
|
return checkTokenValidity(response);
|
|
|
|
}
|
|
|
|
|
2017-08-22 10:51:53 +02:00
|
|
|
return parseJSON(response).then(responseFormatted => {
|
|
|
|
const error = new Error(response.statusText);
|
|
|
|
error.response = response;
|
|
|
|
error.response.payload = responseFormatted;
|
|
|
|
throw error;
|
|
|
|
});
|
|
|
|
}
|
2018-01-11 15:59:00 +01:00
|
|
|
|
|
|
|
function checkTokenValidity(response) {
|
|
|
|
const options = {
|
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
2019-02-14 10:36:20 +01:00
|
|
|
Authorization: `Bearer ${auth.getToken()}`,
|
2018-01-11 15:59:00 +01:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
if (auth.getToken()) {
|
2019-02-14 10:36:20 +01:00
|
|
|
return fetch(`${strapi.backendURL}/user/me`, options).then(() => {
|
|
|
|
if (response.status === 401) {
|
|
|
|
window.location = `${
|
|
|
|
strapi.remoteURL
|
|
|
|
}/plugins/users-permissions/auth/login`;
|
2018-01-11 15:59:00 +01:00
|
|
|
|
2019-02-14 10:36:20 +01:00
|
|
|
auth.clearAppStorage();
|
|
|
|
}
|
2018-01-11 15:59:00 +01:00
|
|
|
|
2019-02-14 10:36:20 +01:00
|
|
|
return checkStatus(response, false);
|
|
|
|
});
|
2018-01-11 15:59:00 +01:00
|
|
|
}
|
|
|
|
}
|
2017-08-22 10:51:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 15:10:12 +02:00
|
|
|
/**
|
2019-02-14 10:36:20 +01:00
|
|
|
* Server restart watcher
|
|
|
|
* @param response
|
|
|
|
* @returns {object} the response data
|
|
|
|
*/
|
2017-09-30 15:10:12 +02:00
|
|
|
function serverRestartWatcher(response) {
|
2019-02-14 10:36:20 +01:00
|
|
|
return new Promise(resolve => {
|
2017-11-08 17:40:47 +01:00
|
|
|
fetch(`${strapi.backendURL}/_health`, {
|
2017-10-02 14:17:44 +02:00
|
|
|
method: 'HEAD',
|
|
|
|
mode: 'no-cors',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
2018-03-09 16:41:24 +01:00
|
|
|
'Keep-Alive': false,
|
|
|
|
},
|
2017-10-02 14:17:44 +02:00
|
|
|
})
|
|
|
|
.then(() => {
|
2018-01-17 12:58:14 +01:00
|
|
|
// Hide the global OverlayBlocker
|
|
|
|
strapi.unlockApp();
|
2017-10-02 14:17:44 +02:00
|
|
|
resolve(response);
|
|
|
|
})
|
2018-03-09 16:41:24 +01:00
|
|
|
.catch(() => {
|
2017-10-02 14:17:44 +02:00
|
|
|
setTimeout(() => {
|
2019-02-14 10:36:20 +01:00
|
|
|
return serverRestartWatcher(response).then(resolve);
|
2017-10-02 14:17:44 +02:00
|
|
|
}, 100);
|
|
|
|
});
|
2017-09-30 15:10:12 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-22 10:51:53 +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
|
|
|
|
*/
|
2019-02-15 18:55:28 +01:00
|
|
|
export default function request(...args) {
|
|
|
|
let [url, options = {}, shouldWatchServerRestart, stringify = true, ...rest] = args;
|
|
|
|
let noAuth;
|
|
|
|
|
|
|
|
try {
|
|
|
|
[{ noAuth }] = rest;
|
|
|
|
} catch(err) {
|
|
|
|
noAuth = false;
|
|
|
|
}
|
|
|
|
|
2018-03-09 16:41:24 +01:00
|
|
|
// Set headers
|
2017-12-20 11:23:50 +01:00
|
|
|
if (!options.headers) {
|
2019-02-14 10:36:20 +01:00
|
|
|
options.headers = Object.assign(
|
|
|
|
{
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
options.headers,
|
|
|
|
{
|
|
|
|
'X-Forwarded-Host': 'strapi',
|
|
|
|
},
|
|
|
|
);
|
2017-12-20 11:23:50 +01:00
|
|
|
}
|
2017-08-22 10:51:53 +02:00
|
|
|
|
2017-12-20 11:23:50 +01:00
|
|
|
const token = auth.getToken();
|
2017-11-29 12:48:58 +01:00
|
|
|
|
2019-02-15 18:55:28 +01:00
|
|
|
if (token && !noAuth) {
|
2019-02-14 10:36:20 +01:00
|
|
|
options.headers = Object.assign(
|
|
|
|
{
|
|
|
|
Authorization: `Bearer ${token}`,
|
|
|
|
},
|
|
|
|
options.headers,
|
|
|
|
);
|
2017-12-20 11:23:50 +01:00
|
|
|
}
|
2017-11-29 12:48:58 +01:00
|
|
|
|
2017-12-20 11:23:50 +01:00
|
|
|
// Add parameters to url
|
|
|
|
url = _.startsWith(url, '/') ? `${strapi.backendURL}${url}` : url;
|
2017-08-22 10:51:53 +02:00
|
|
|
|
2017-12-20 11:23:50 +01:00
|
|
|
if (options && options.params) {
|
|
|
|
const params = formatQueryParams(options.params);
|
|
|
|
url = `${url}?${params}`;
|
|
|
|
}
|
2017-08-22 10:51:53 +02:00
|
|
|
|
2017-12-20 11:23:50 +01:00
|
|
|
// Stringify body object
|
2018-02-22 18:24:17 +01:00
|
|
|
if (options && options.body && stringify) {
|
2017-12-20 11:23:50 +01:00
|
|
|
options.body = JSON.stringify(options.body);
|
|
|
|
}
|
2019-02-22 15:25:11 +01:00
|
|
|
|
2017-12-20 11:23:50 +01:00
|
|
|
return fetch(url, options)
|
2017-09-30 13:49:49 +02:00
|
|
|
.then(checkStatus)
|
|
|
|
.then(parseJSON)
|
2019-02-14 10:36:20 +01:00
|
|
|
.then(response => {
|
2017-09-30 13:49:49 +02:00
|
|
|
if (shouldWatchServerRestart) {
|
2018-01-17 12:58:14 +01:00
|
|
|
// Display the global OverlayBlocker
|
2019-01-24 18:24:24 +01:00
|
|
|
strapi.lockApp(shouldWatchServerRestart);
|
2017-09-30 13:49:49 +02:00
|
|
|
return serverRestartWatcher(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
2018-01-25 18:12:29 +01:00
|
|
|
});
|
2017-12-20 11:23:50 +01:00
|
|
|
}
|