2016-08-01 16:03:17 +00:00
|
|
|
import _ from 'lodash';
|
2016-08-03 12:36:50 +00:00
|
|
|
import { browserHistory } from 'react-router';
|
2017-04-28 05:18:46 +00:00
|
|
|
import stores from 'stores';
|
2016-02-27 21:53:11 +00:00
|
|
|
|
|
|
|
class ApiClient {
|
|
|
|
constructor(options = {}) {
|
2017-04-29 21:20:41 +00:00
|
|
|
this.baseUrl = options.baseUrl || '/api';
|
|
|
|
this.userAgent = 'AtlasFrontend';
|
2016-02-27 21:53:11 +00:00
|
|
|
}
|
|
|
|
|
2016-08-01 16:03:17 +00:00
|
|
|
fetch = (path, method, data, options = {}) => {
|
2016-02-27 21:53:11 +00:00
|
|
|
let body;
|
|
|
|
let modifiedPath;
|
|
|
|
|
|
|
|
if (method === 'GET') {
|
2016-07-22 07:11:54 +00:00
|
|
|
modifiedPath = `${path}?${this.constructQueryString(data)}`;
|
2016-02-27 21:53:11 +00:00
|
|
|
} else if (method === 'POST' || method === 'PUT') {
|
|
|
|
body = JSON.stringify(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct headers
|
|
|
|
const headers = new Headers({
|
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
});
|
2016-06-26 06:31:22 +00:00
|
|
|
if (stores.user.authenticated) {
|
|
|
|
headers.set('Authorization', `Bearer ${stores.user.token}`);
|
2016-02-27 21:53:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Construct request
|
|
|
|
const request = fetch(this.baseUrl + (modifiedPath || path), {
|
|
|
|
method,
|
|
|
|
body,
|
|
|
|
headers,
|
|
|
|
redirect: 'follow',
|
|
|
|
});
|
|
|
|
|
|
|
|
// Handle request promises and return a new promise
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
request
|
2017-04-27 04:47:03 +00:00
|
|
|
.then(response => {
|
|
|
|
// Handle successful responses
|
|
|
|
if (response.status >= 200 && response.status < 300) {
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle 404
|
|
|
|
if (response.status === 404) {
|
|
|
|
return browserHistory.push('/404');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle 401, log out user
|
|
|
|
if (response.status === 401) {
|
|
|
|
return stores.user.logout();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle failed responses
|
|
|
|
const error = {};
|
|
|
|
error.statusCode = response.status;
|
|
|
|
error.response = response;
|
|
|
|
throw error;
|
|
|
|
})
|
|
|
|
.then(response => {
|
|
|
|
return response.json();
|
|
|
|
})
|
2016-09-17 21:53:30 +00:00
|
|
|
.then(json => {
|
2017-04-27 04:47:03 +00:00
|
|
|
resolve(json);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
error.response.json().then(json => {
|
|
|
|
error.data = json;
|
|
|
|
reject(error);
|
|
|
|
});
|
2016-09-17 21:53:30 +00:00
|
|
|
});
|
2016-02-27 21:53:11 +00:00
|
|
|
});
|
2017-04-27 04:47:03 +00:00
|
|
|
};
|
2016-02-27 21:53:11 +00:00
|
|
|
|
2016-08-01 16:03:17 +00:00
|
|
|
get = (path, data, options) => {
|
|
|
|
return this.fetch(path, 'GET', data, options);
|
2017-04-27 04:47:03 +00:00
|
|
|
};
|
2016-02-27 21:53:11 +00:00
|
|
|
|
2016-08-01 16:03:17 +00:00
|
|
|
post = (path, data, options) => {
|
|
|
|
return this.fetch(path, 'POST', data, options);
|
2017-04-27 04:47:03 +00:00
|
|
|
};
|
2016-07-23 19:09:50 +00:00
|
|
|
|
2016-02-27 21:53:11 +00:00
|
|
|
// Helpers
|
|
|
|
|
2017-04-27 04:47:03 +00:00
|
|
|
constructQueryString = data => {
|
2016-08-01 16:03:17 +00:00
|
|
|
return _.map(data, (v, k) => {
|
2016-02-27 21:53:11 +00:00
|
|
|
return `${encodeURIComponent(k)}=${encodeURIComponent(v)}`;
|
|
|
|
}).join('&');
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ApiClient;
|
|
|
|
|
|
|
|
// In case you don't want to always initiate, just import with `import { client } ...`
|
|
|
|
const client = new ApiClient();
|
2017-04-29 21:08:15 +00:00
|
|
|
export { client };
|