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 | |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 8 | */ |
| 9 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 10 | window.angular && (function(angular) { |
| 11 | 'use strict'; |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 12 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 13 | angular.module('app.common.services').service('apiInterceptor', [ |
| 14 | '$q', '$rootScope', 'dataService', |
| 15 | function($q, $rootScope, dataService) { |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 16 | return { |
| 17 | 'request': function(config) { |
| 18 | dataService.loading = true; |
| 19 | // If caller has not defined a timeout, set to default of 20s |
| 20 | if (config.timeout == null) { |
| 21 | config.timeout = 20000; |
| 22 | } |
| 23 | return config; |
| 24 | }, |
| 25 | 'response': function(response) { |
| 26 | dataService.loading = false; |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 27 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 28 | // not interested in template requests |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 29 | if (!/^https?\:/i.test(response.config.url)) { |
| 30 | return response; |
| 31 | } |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 32 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 33 | dataService.last_updated = new Date(); |
| 34 | if (!response) { |
| 35 | dataService.server_unreachable = true; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 36 | } else { |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 37 | dataService.server_unreachable = false; |
| 38 | } |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 39 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 40 | if (response && response.status == 'error' && |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 41 | dataService.path != '/login') { |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 42 | $rootScope.$emit('timedout-user', {}); |
| 43 | } |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 44 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 45 | return response; |
| 46 | }, |
| 47 | 'responseError': function(rejection) { |
| 48 | if (dataService.ignoreHttpError === false) { |
| 49 | // If unauthorized, log out |
| 50 | if (rejection.status == 401) { |
| 51 | if (dataService.path != '/login') { |
| 52 | $rootScope.$emit('timedout-user', {}); |
| 53 | } |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 54 | } else if (rejection.status == -1) { |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 55 | dataService.server_unreachable = true; |
| 56 | } |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 57 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 58 | dataService.loading = false; |
| 59 | } |
| 60 | return $q.reject(rejection); |
| 61 | } |
| 62 | }; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 63 | } |
| 64 | ]); |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 65 | })(window.angular); |