Created
July 14, 2017 16:52
-
-
Save cristoferdomingues/b2c999562011bd26e4529e50434a79a3 to your computer and use it in GitHub Desktop.
Angular HttpInterceptor defaut URL prefix
This file contains hidden or 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
'use strict'; | |
let self; | |
const API_URL = 'http://localhost:8084/'; | |
function messagesHandler(result) { | |
if (result.data.messages) { | |
result.data.messages.forEach(function(message) { | |
if (!message.show) { | |
return; | |
} | |
self.JtechBoxService[message.tipo]('JtBigBox', message.msg); | |
}); | |
} | |
} | |
class JtechHttpInterceptor { | |
constructor($q, $window, JtechBoxService, JtechLoading) { | |
this.$q = $q; | |
this.$window = $window; | |
this.JtechBoxService = JtechBoxService; | |
this.jtechLoading = JtechLoading; | |
self = this; | |
} | |
request(config) { | |
var url = config.url; | |
self.jtechLoading.show(); | |
// ignore template requests | |
if (url.substr(url.length - 5) == '.html') { | |
return config || $q.when(config); | |
} | |
config.url = API_URL + config.url; | |
return config || $q.when(config); | |
} | |
response(result) { | |
var deferred = self.$q.defer(); | |
self.jtechLoading.hide(); | |
messagesHandler(result); | |
if (result.data.result && result.data.result === 'error') { | |
result.message = result.data.messages[0].msg; | |
deferred.reject(result); | |
} else { | |
deferred.resolve(result); | |
} | |
return deferred.promise; | |
} | |
responseError(rejection) { | |
// console.info('response error', rejection); | |
} | |
} | |
JtechHttpInterceptor.$inject = [ | |
'$q', | |
'$window', | |
'JtechBoxService', | |
'JtechLoading' | |
]; | |
export default angular | |
.module('jtechHttpInterceptor', []) | |
.service('JtechHttpInterceptor', JtechHttpInterceptor).name; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment