blob: a1b5df8fb9152944ab5ef8b9c333cb57789935ba [file] [log] [blame]
Matteo Scandolof6acdbe2016-12-13 10:29:37 -08001/// <reference path="../typings/index.d.ts" />
2
Matteo Scandolof6acdbe2016-12-13 10:29:37 -08003export function interceptorConfig($httpProvider: angular.IHttpProvider, $resourceProvider: angular.resource.IResourceServiceProvider) {
4 $httpProvider.interceptors.push('UserStatusInterceptor');
5 $httpProvider.interceptors.push('CredentialsInterceptor');
Matteo Scandolof2c3ed62016-12-15 14:32:50 -08006 $httpProvider.interceptors.push('NoHyperlinksInterceptor');
Matteo Scandolof6acdbe2016-12-13 10:29:37 -08007}
8
Matteo Scandoloebe5a222017-02-27 11:09:26 -08009export function userStatusInterceptor($state: angular.ui.IStateService, $cookies: ng.cookies.ICookiesService, $q: ng.IQService) {
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080010 const checkLogin = (res) => {
Matteo Scandolo042ea632017-03-01 19:02:34 -080011 switch (res.status) {
12 case -1:
13 case 401:
Matteo Scandolo042ea632017-03-01 19:02:34 -080014 $cookies.remove('sessionid', {path: '/'});
15 $state.go('login');
16 return $q.reject(res);
17 default:
Matteo Scandolo42c66922017-05-01 17:24:59 -070018 return $q.reject(res);
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080019 }
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080020 };
21
22 return {
Matteo Scandolo42c66922017-05-01 17:24:59 -070023 // response: checkLogin,
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080024 responseError: checkLogin
25 };
26}
27
28export function CredentialsInterceptor($cookies: angular.cookies.ICookiesService) {
29 return {
30 request: (req) => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080031 if (!$cookies.get('sessionid')) {
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080032 return req;
33 }
Matteo Scandolo1aee1982017-02-17 08:33:23 -080034 req.headers['x-sessionid'] = $cookies.get('sessionid');
35 req.headers['x-xossession'] = $cookies.get('sessionid');
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080036 return req;
37 }
38 };
39}
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080040
Matteo Scandolo42c66922017-05-01 17:24:59 -070041export function NoHyperlinksInterceptor($q: ng.IQService) {
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080042 return {
43 request: (req) => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080044 if (req.url.indexOf('.html') === -1) {
45 // NOTE force content type to be JSON
46 req.headers['Content-Type'] = 'application/json';
Matteo Scandolo520a8a12017-03-10 17:31:37 -080047
48 if (req.method === 'PUT') {
Matteo Scandolo02229382017-04-18 11:52:23 -070049 // XosModelStore.search add this value for visualization purpose,
Matteo Scandolo520a8a12017-03-10 17:31:37 -080050 // no one should change models
51 delete req.data.modelName;
52 }
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080053 }
54 return req;
Matteo Scandolo1aee1982017-02-17 08:33:23 -080055 },
56 response: (res) => {
57 try {
58 // NOTE convert res.data from string to JSON
59 res.data = JSON.parse(res.data);
60 try {
61 // NOTE chameleon return everything inside an "items" field
62 res.data = res.data.items;
63 } catch (_e) {
64 res.data = res.data;
65 }
66 } catch (e) {
67 res.data = res.data;
68 }
69 return res;
Matteo Scandolo42c66922017-05-01 17:24:59 -070070 },
71 responseError: (res) => {
72 return $q.reject(res.data);
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080073 }
74 };
75}