blob: de344cd1aff2a2b61593767dba5d72035f83ce0a [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;
Andrew Geissler18f72662018-05-15 13:56:48 -050019 // If caller has not defined a timeout, set to default of 20s
20 if (config.timeout == null){
21 config.timeout = 20000;
22 }
Iftekharul Islam99d199f2017-03-24 15:28:25 -050023 return config;
24 },
25 'response': function(response){
26 dataService.loading = false;
Iftekharul Islam99d199f2017-03-24 15:28:25 -050027
Iftekharul Islamcd789502017-04-19 14:37:55 -050028 //not interested in template requests
29 if(!/^https?\:/i.test(response.config.url)){
30 return response;
31 }
32
33 dataService.last_updated = new Date();
Iftekharul Islamc1535922017-06-19 12:49:04 -050034 if(!response){
Iftekharul Islam99d199f2017-03-24 15:28:25 -050035 dataService.server_unreachable = true;
Iftekharul Islamcd789502017-04-19 14:37:55 -050036 }else{
37 dataService.server_unreachable = false;
Iftekharul Islam99d199f2017-03-24 15:28:25 -050038 }
39
40 if(response && response.status == 'error' &&
41 dataService.path != '/login'){
42 $rootScope.$emit('timedout-user', {});
43 }
44
45 return response;
46 },
47 'responseError': function(rejection){
Gunnar Mills32581cf2018-03-16 15:52:54 -050048 if (dataService.ignoreHttpError === false)
49 {
50 // If unauthorized, log out
51 if (rejection.status == 401){
52 if (dataService.path != '/login'){
53 $rootScope.$emit('timedout-user', {});
54 }
55 } else if (rejection.status == -1){
56 dataService.server_unreachable = true;
Ed Tanousbbcf6702017-10-06 13:53:06 -070057 }
Ed Tanousbbcf6702017-10-06 13:53:06 -070058
Gunnar Mills32581cf2018-03-16 15:52:54 -050059 dataService.loading = false;
60 }
Iftekharul Islam99d199f2017-03-24 15:28:25 -050061 return $q.reject(rejection);
62 }
63 };
64 }]);
65
Ed Tanousbbcf6702017-10-06 13:53:06 -070066})(window.angular);