blob: d1a23ad3db8e7d39c4646ba855ccf7f40624a502 [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
8 * @version 0.0.1
9 */
10
11window.angular && (function (angular) {
12 'use strict';
13
14 angular
15 .module('app.common.services')
16 .service('apiInterceptor', ['$q', '$rootScope', 'dataService', function($q, $rootScope, dataService){
17 return {
18 'request': function(config){
Iftekharul Islam99d199f2017-03-24 15:28:25 -050019 dataService.loading = true;
Iftekharul Islam1221c0c2017-07-27 10:23:49 -050020 config.timeout = 20000;
Iftekharul Islam99d199f2017-03-24 15:28:25 -050021 return config;
22 },
23 'response': function(response){
24 dataService.loading = false;
Iftekharul Islam99d199f2017-03-24 15:28:25 -050025
Iftekharul Islamcd789502017-04-19 14:37:55 -050026 //not interested in template requests
27 if(!/^https?\:/i.test(response.config.url)){
28 return response;
29 }
30
31 dataService.last_updated = new Date();
Iftekharul Islamc1535922017-06-19 12:49:04 -050032 if(!response){
Iftekharul Islam99d199f2017-03-24 15:28:25 -050033 dataService.server_unreachable = true;
Iftekharul Islamcd789502017-04-19 14:37:55 -050034 }else{
35 dataService.server_unreachable = false;
Iftekharul Islam99d199f2017-03-24 15:28:25 -050036 }
37
38 if(response && response.status == 'error' &&
39 dataService.path != '/login'){
40 $rootScope.$emit('timedout-user', {});
41 }
42
43 return response;
44 },
45 'responseError': function(rejection){
46 dataService.server_unreachable = true;
47 dataService.loading = false;
Iftekharul Islamf3f7a5f2017-03-27 13:53:24 -050048 if(dataService.path != '/login'){
49 $rootScope.$emit('timedout-user', {});
50 }
Iftekharul Islam99d199f2017-03-24 15:28:25 -050051 return $q.reject(rejection);
52 }
53 };
54 }]);
55
56})(window.angular);