Created
December 28, 2015 23:34
-
-
Save kitze/1c888cd87dd7d1e174c9 to your computer and use it in GitHub Desktop.
lightweight wrapper around axios with a cache layer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//external libs | |
import Q from 'q'; | |
import axios from 'axios'; | |
import _ from 'lodash'; | |
//modules | |
import CONSTANTS from '../helpers/constants'; | |
import Notifications from '../helpers/notifications-service'; | |
import ApiCache from '../helpers/cache'; | |
import {setLocation} from '../helpers/location-changer'; | |
class ApiService { | |
constructor() { | |
this.errorMessages = { | |
500: `The requested resource doesn't exist!`, | |
502: `Server error, please try again.`, | |
401: `You aren't authorized to access this resource.` | |
}; | |
this.$http = axios.create(); | |
//interceptors | |
this.$http.interceptors.request.use(config => { | |
config.url = `${CONSTANTS.API_URL}/${config.url}` | |
return config; | |
}, this.catchInterceptorError); | |
this.$http.interceptors.response.use(response => { | |
ApiCache.set(response.config.url, response.data); | |
return response; | |
}, this.catchInterceptorError); | |
} | |
resolveAndDelay(deferred, data) { | |
setTimeout(() => deferred.resolve(data), CONSTANTS.DEBUG_MODE ? 500 : 0); | |
} | |
apiCall(path, params = {}) { | |
const deferred = Q.defer(); | |
const cachedObject = ApiCache.get(`${CONSTANTS.API_URL}/${path}`); | |
//return data from cache | |
if (!_.isEmpty(cachedObject)) { | |
deferred.resolve(cachedObject); | |
} | |
else { | |
this.$http.get(path, params) | |
.then(({data}) => this.resolveAndDelay(deferred, data)) | |
.catch(this.catchError); | |
} | |
return deferred.promise; | |
} | |
spreadData(result, spreadMembers) { | |
const data = {}; | |
spreadMembers.forEach((member, i) => { | |
data[member] = result[i] | |
}); | |
return data; | |
} | |
get(path) { | |
return this.apiCall(path); | |
} | |
post(path, params) { | |
return this.$http.post(path, params); | |
} | |
all(path, spreadMembers) { | |
const deferred = Q.defer(); | |
const apiCalls = path.map(p => this.apiCall(p)); | |
axios.all(apiCalls) | |
.then(result => deferred.resolve(this.spreadData(result, spreadMembers))) | |
.catch(this.catchError); | |
return deferred.promise; | |
} | |
catchInterceptorError(error) { | |
return Promise.reject(error); | |
} | |
catchError(response, deferred) { | |
let error = { | |
code: response.status, | |
message: response.message | |
}; | |
const errorMessage = this.errorMessages[response.status]; | |
if (errorMessage) { | |
error = { | |
code: 500, | |
message: errorMessage | |
}; | |
setLocation('/'); | |
Notifications.error(error.message); | |
} | |
deferred.reject(error); | |
} | |
} | |
let apiService = new ApiService(); | |
export default apiService; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment