|
|
|
var siteUrl, apiUrl, staticUrl;
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
siteUrl = 'http://spaceport.dns.t0.vc';
|
|
|
|
apiUrl = 'http://spaceport-api.dns.t0.vc';
|
|
|
|
staticUrl = 'http://spaceport-static.dns.t0.vc';
|
|
|
|
} else {
|
|
|
|
siteUrl = 'https://' + window.location.hostname;
|
|
|
|
apiUrl = 'https://api.' + window.location.hostname;
|
|
|
|
staticUrl = 'https://static.' + window.location.hostname;
|
|
|
|
}
|
|
|
|
|
|
|
|
export { siteUrl, apiUrl, staticUrl };
|
|
|
|
|
|
|
|
export const requester = (route, method, token, data) => {
|
|
|
|
let options = {headers: {}};
|
|
|
|
|
|
|
|
if (token) {
|
|
|
|
options.headers.Authorization = 'Token ' + token;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (method == 'GET') {
|
|
|
|
// pass
|
|
|
|
} else if (['POST', 'PUT', 'PATCH'].includes(method)) {
|
|
|
|
const formData = new FormData();
|
|
|
|
Object.keys(data).forEach(key =>
|
|
|
|
formData.append(key, data[key])
|
|
|
|
);
|
|
|
|
|
|
|
|
options = {
|
|
|
|
...options,
|
|
|
|
method: method,
|
|
|
|
body: formData,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
throw new Error('Method not supported');
|
|
|
|
}
|
|
|
|
|
|
|
|
const customError = (data) => {
|
|
|
|
const error = new Error(JSON.stringify(data));
|
|
|
|
error.data = data;
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fetch(apiUrl + route, options)
|
|
|
|
.then(response => {
|
|
|
|
if (!response.ok) {
|
|
|
|
throw customError(response);
|
|
|
|
}
|
|
|
|
return response.json();
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
const code = error.data.status;
|
|
|
|
if (code == 413) {
|
|
|
|
throw customError({non_field_errors: ['File too big']});
|
|
|
|
} else if (code >= 400 && code < 500) {
|
|
|
|
return error.data.json()
|
|
|
|
.then(result => {
|
|
|
|
throw customError(result);
|
|
|
|
});
|
|
|
|
} else if (code >= 500 && code < 600) {
|
|
|
|
throw customError({non_field_errors: ['Server Error']});
|
|
|
|
} else {
|
|
|
|
throw customError({non_field_errors: ['Network Error']});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|