blob: b3f2ff409c9233ca572aada9f71da026f50d5a74 [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
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070010window.angular && (function(angular) {
11 'use strict';
Iftekharul Islam99d199f2017-03-24 15:28:25 -050012
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070013 angular
14 .module('app.common.services')
15 .service('apiInterceptor', ['$q', '$rootScope', 'dataService', function($q, $rootScope, dataService) {
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 Islam99d199f2017-03-24 15:28:25 -050027
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070028 //not interested in template requests
29 if (!/^https?\:/i.test(response.config.url)) {
30 return response;
31 }
Iftekharul Islamcd789502017-04-19 14:37:55 -050032
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070033 dataService.last_updated = new Date();
34 if (!response) {
35 dataService.server_unreachable = true;
36 }
37 else {
38 dataService.server_unreachable = false;
39 }
Iftekharul Islam99d199f2017-03-24 15:28:25 -050040
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070041 if (response && response.status == 'error' &&
42 dataService.path != '/login') {
43 $rootScope.$emit('timedout-user', {});
44 }
Iftekharul Islam99d199f2017-03-24 15:28:25 -050045
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070046 return response;
47 },
48 'responseError': function(rejection) {
49 if (dataService.ignoreHttpError === false) {
50 // If unauthorized, log out
51 if (rejection.status == 401) {
52 if (dataService.path != '/login') {
53 $rootScope.$emit('timedout-user', {});
54 }
55 }
56 else if (rejection.status == -1) {
57 dataService.server_unreachable = true;
58 }
Ed Tanousbbcf6702017-10-06 13:53:06 -070059
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070060 dataService.loading = false;
61 }
62 return $q.reject(rejection);
63 }
64 };
65 }]);
Iftekharul Islam99d199f2017-03-24 15:28:25 -050066
Ed Tanousbbcf6702017-10-06 13:53:06 -070067})(window.angular);