Skip to content

Commit

Permalink
add api fix
Browse files Browse the repository at this point in the history
  • Loading branch information
izica committed Apr 10, 2018
1 parent c84f45f commit 260f61e
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
81 changes: 81 additions & 0 deletions src/shared/utils/Api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import superagent from 'superagent';

import { action, toJS, history } from '!/app';
import { UserService } from '!/services';
import { UserStore } from '!/stores';

export default new class Api {
base_url = document.body.getAttribute('api');

catchError = (err) => {
if (err.response.status === 401) {
UserService.logout();
return Promise.reject(err.response.status);
}

if (err.response.status === 404) {
history.push('/error404');
return Promise.reject(err.response.status);
}
return err.response;
};

@action
post = (url, data) => {
let request = superagent
.post(this.base_url + url)
.set('Accept', 'application/json')
.send(data);

if (UserStore.token) {
request.set('Authorization', 'Bearer ' + UserStore.token);
}
return request.catch(this.catchError);
};

@action
get = (url, data = {}) => {
let request = superagent
.get(this.base_url + url)
.set('Accept', 'application/json')
.query(toJS(data));

if (UserStore.token) {
request.set('Authorization', 'Bearer ' + UserStore.token);
}

return request.catch(this.catchError);
};

@action
put = (url, data) => {
console.log(data);
let request = superagent
.put(this.base_url + url)
.set('Accept', 'application/json')
.send(data);

if (UserStore.token) {
request.set('Authorization', 'Bearer ' + UserStore.token);
}
return request.catch(this.catchError);
};

@action
file = (url, file, data = false, onProgress = false) => {
let request = superagent.post(this.base_url + url).attach('file', file);

if (data !== false) {
request.field(data);
}

if (onProgress !== false) {
request.on('progress', onProgress);
}

if (UserStore.token) {
request.set('Authorization', 'Bearer ' + UserStore.token);
}
return request.catch(this.catchError);
};
}();
1 change: 1 addition & 0 deletions src/shared/utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export Api from './Api';

0 comments on commit 260f61e

Please sign in to comment.