199d199f3SIftekharul Islam/** 299d199f3SIftekharul Islam * api Interceptor 399d199f3SIftekharul Islam * 499d199f3SIftekharul Islam * @module app/common/services/apiInterceptor 599d199f3SIftekharul Islam * @exports apiInterceptor 699d199f3SIftekharul Islam * @name apiInterceptor 799d199f3SIftekharul Islam 899d199f3SIftekharul Islam */ 999d199f3SIftekharul Islam 1099d199f3SIftekharul Islamwindow.angular && (function(angular) { 1199d199f3SIftekharul Islam 'use strict'; 1299d199f3SIftekharul Islam 13d27bb135SAndrew Geissler angular.module('app.common.services').service('apiInterceptor', [ 14*eaa40dd3SYoshie Muranaka '$q', '$rootScope', 'dataService', '$location', 15*eaa40dd3SYoshie Muranaka function($q, $rootScope, dataService, $location) { 1699d199f3SIftekharul Islam return { 1799d199f3SIftekharul Islam 'request': function(config) { 1899d199f3SIftekharul Islam dataService.loading = true; 1918f72665SAndrew Geissler // If caller has not defined a timeout, set to default of 20s 2018f72665SAndrew Geissler if (config.timeout == null) { 211221c0caSIftekharul Islam config.timeout = 20000; 2218f72665SAndrew Geissler } 2399d199f3SIftekharul Islam return config; 2499d199f3SIftekharul Islam }, 2599d199f3SIftekharul Islam 'response': function(response) { 2699d199f3SIftekharul Islam dataService.loading = false; 2799d199f3SIftekharul Islam 28cd789508SIftekharul Islam // not interested in template requests 29cd789508SIftekharul Islam if (!/^https?\:/i.test(response.config.url)) { 30cd789508SIftekharul Islam return response; 31cd789508SIftekharul Islam } 32cd789508SIftekharul Islam 33cd789508SIftekharul Islam dataService.last_updated = new Date(); 34c1535926SIftekharul Islam if (!response) { 3599d199f3SIftekharul Islam dataService.server_unreachable = true; 36d27bb135SAndrew Geissler } else { 37cd789508SIftekharul Islam dataService.server_unreachable = false; 3899d199f3SIftekharul Islam } 3999d199f3SIftekharul Islam 4099d199f3SIftekharul Islam if (response && response.status == 'error' && 4199d199f3SIftekharul Islam dataService.path != '/login') { 4299d199f3SIftekharul Islam $rootScope.$emit('timedout-user', {}); 4399d199f3SIftekharul Islam } 4499d199f3SIftekharul Islam 4599d199f3SIftekharul Islam return response; 4699d199f3SIftekharul Islam }, 4799d199f3SIftekharul Islam 'responseError': function(rejection) { 48ba5e3f34SAndrew Geissler if (dataService.ignoreHttpError === false) { 49bbcf670aSEd Tanous // If unauthorized, log out 50bbcf670aSEd Tanous if (rejection.status == 401) { 51f3f7a5f0SIftekharul Islam if (dataService.path != '/login') { 52f3f7a5f0SIftekharul Islam $rootScope.$emit('timedout-user', {}); 53f3f7a5f0SIftekharul Islam } 54*eaa40dd3SYoshie Muranaka } else if (rejection.status == 403) { 55*eaa40dd3SYoshie Muranaka // TODO: when permission role mapping ready, remove 56*eaa40dd3SYoshie Muranaka // this global redirect and handle forbidden 57*eaa40dd3SYoshie Muranaka // requests in context of user action 58*eaa40dd3SYoshie Muranaka if (dataService.path != '/login') { 59*eaa40dd3SYoshie Muranaka $location.url('/unauthorized'); 60*eaa40dd3SYoshie Muranaka } 61d27bb135SAndrew Geissler } else if (rejection.status == -1) { 62bbcf670aSEd Tanous dataService.server_unreachable = true; 63bbcf670aSEd Tanous } 64bbcf670aSEd Tanous 65bbcf670aSEd Tanous dataService.loading = false; 6632581cf4SGunnar Mills } 6799d199f3SIftekharul Islam return $q.reject(rejection); 6899d199f3SIftekharul Islam } 6999d199f3SIftekharul Islam }; 70d27bb135SAndrew Geissler } 71d27bb135SAndrew Geissler ]); 7299d199f3SIftekharul Islam})(window.angular); 73