blob: 3e72cc694f1b972e782bde8e18ddcb88950345f1 [file] [log] [blame]
Iftekharul Islam99d199f2017-03-24 15:28:25 -05001/**
2 * api Interceptor
3 *
4 * @module app/common/services/apiInterceptor
5 * @exports apiInterceptor
6 * @name apiInterceptor
7
Iftekharul Islam99d199f2017-03-24 15:28:25 -05008 */
9
10window.angular && (function (angular) {
11 'use strict';
12
13 angular
14 .module('app.common.services')
15 .service('apiInterceptor', ['$q', '$rootScope', 'dataService', function($q, $rootScope, dataService){
16 return {
17 'request': function(config){
Iftekharul Islam99d199f2017-03-24 15:28:25 -050018 dataService.loading = true;
Iftekharul Islam1221c0c2017-07-27 10:23:49 -050019 config.timeout = 20000;
Iftekharul Islam99d199f2017-03-24 15:28:25 -050020 return config;
21 },
22 'response': function(response){
23 dataService.loading = false;
Iftekharul Islam99d199f2017-03-24 15:28:25 -050024
Iftekharul Islamcd789502017-04-19 14:37:55 -050025 //not interested in template requests
26 if(!/^https?\:/i.test(response.config.url)){
27 return response;
28 }
29
30 dataService.last_updated = new Date();
Iftekharul Islamc1535922017-06-19 12:49:04 -050031 if(!response){
Iftekharul Islam99d199f2017-03-24 15:28:25 -050032 dataService.server_unreachable = true;
Iftekharul Islamcd789502017-04-19 14:37:55 -050033 }else{
34 dataService.server_unreachable = false;
Iftekharul Islam99d199f2017-03-24 15:28:25 -050035 }
36
37 if(response && response.status == 'error' &&
38 dataService.path != '/login'){
39 $rootScope.$emit('timedout-user', {});
40 }
41
42 return response;
43 },
44 'responseError': function(rejection){
Gunnar Mills32581cf2018-03-16 15:52:54 -050045 if (dataService.ignoreHttpError === false)
46 {
47 // If unauthorized, log out
48 if (rejection.status == 401){
49 if (dataService.path != '/login'){
50 $rootScope.$emit('timedout-user', {});
51 }
52 } else if (rejection.status == -1){
53 dataService.server_unreachable = true;
Ed Tanousbbcf6702017-10-06 13:53:06 -070054 }
Ed Tanousbbcf6702017-10-06 13:53:06 -070055
Gunnar Mills32581cf2018-03-16 15:52:54 -050056 dataService.loading = false;
57 }
Iftekharul Islam99d199f2017-03-24 15:28:25 -050058 return $q.reject(rejection);
59 }
60 };
61 }]);
62
Ed Tanousbbcf6702017-10-06 13:53:06 -070063})(window.angular);