Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 1 | /** |
| 2 | * api Interceptor |
| 3 | * |
| 4 | * @module app/common/services/apiInterceptor |
| 5 | * @exports apiInterceptor |
| 6 | * @name apiInterceptor |
| 7 | |
| 8 | * @version 0.0.1 |
| 9 | */ |
| 10 | |
| 11 | window.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 Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 19 | dataService.loading = true; |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 20 | config.timeout = 10000; |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 21 | return config; |
| 22 | }, |
| 23 | 'response': function(response){ |
| 24 | dataService.loading = false; |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 25 | |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 26 | //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 Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 32 | if(response == null){ |
| 33 | dataService.server_unreachable = true; |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 34 | }else{ |
| 35 | dataService.server_unreachable = false; |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 36 | } |
| 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 Islam | f3f7a5f | 2017-03-27 13:53:24 -0500 | [diff] [blame] | 48 | if(dataService.path != '/login'){ |
| 49 | $rootScope.$emit('timedout-user', {}); |
| 50 | } |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 51 | return $q.reject(rejection); |
| 52 | } |
| 53 | }; |
| 54 | }]); |
| 55 | |
| 56 | })(window.angular); |