Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 1 | /// <reference path="../typings/index.d.ts" /> |
| 2 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 3 | export function interceptorConfig($httpProvider: angular.IHttpProvider, $resourceProvider: angular.resource.IResourceServiceProvider) { |
| 4 | $httpProvider.interceptors.push('UserStatusInterceptor'); |
| 5 | $httpProvider.interceptors.push('CredentialsInterceptor'); |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 6 | $httpProvider.interceptors.push('NoHyperlinksInterceptor'); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 7 | } |
| 8 | |
Matteo Scandolo | ebe5a22 | 2017-02-27 11:09:26 -0800 | [diff] [blame] | 9 | export function userStatusInterceptor($state: angular.ui.IStateService, $cookies: ng.cookies.ICookiesService, $q: ng.IQService) { |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 10 | const checkLogin = (res) => { |
Matteo Scandolo | 042ea63 | 2017-03-01 19:02:34 -0800 | [diff] [blame] | 11 | switch (res.status) { |
| 12 | case -1: |
| 13 | case 401: |
Matteo Scandolo | 042ea63 | 2017-03-01 19:02:34 -0800 | [diff] [blame] | 14 | $cookies.remove('sessionid', {path: '/'}); |
| 15 | $state.go('login'); |
| 16 | return $q.reject(res); |
| 17 | default: |
Matteo Scandolo | 42c6692 | 2017-05-01 17:24:59 -0700 | [diff] [blame] | 18 | return $q.reject(res); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 19 | } |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 20 | }; |
| 21 | |
| 22 | return { |
Matteo Scandolo | 42c6692 | 2017-05-01 17:24:59 -0700 | [diff] [blame] | 23 | // response: checkLogin, |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 24 | responseError: checkLogin |
| 25 | }; |
| 26 | } |
| 27 | |
| 28 | export function CredentialsInterceptor($cookies: angular.cookies.ICookiesService) { |
| 29 | return { |
| 30 | request: (req) => { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 31 | if (!$cookies.get('sessionid')) { |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 32 | return req; |
| 33 | } |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 34 | req.headers['x-sessionid'] = $cookies.get('sessionid'); |
| 35 | req.headers['x-xossession'] = $cookies.get('sessionid'); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 36 | return req; |
| 37 | } |
| 38 | }; |
| 39 | } |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 40 | |
Matteo Scandolo | 42c6692 | 2017-05-01 17:24:59 -0700 | [diff] [blame] | 41 | export function NoHyperlinksInterceptor($q: ng.IQService) { |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 42 | return { |
| 43 | request: (req) => { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 44 | if (req.url.indexOf('.html') === -1) { |
| 45 | // NOTE force content type to be JSON |
| 46 | req.headers['Content-Type'] = 'application/json'; |
Matteo Scandolo | 520a8a1 | 2017-03-10 17:31:37 -0800 | [diff] [blame] | 47 | |
| 48 | if (req.method === 'PUT') { |
Matteo Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 49 | // XosModelStore.search add this value for visualization purpose, |
Matteo Scandolo | 520a8a1 | 2017-03-10 17:31:37 -0800 | [diff] [blame] | 50 | // no one should change models |
| 51 | delete req.data.modelName; |
| 52 | } |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 53 | } |
| 54 | return req; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 55 | }, |
| 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 Scandolo | 42c6692 | 2017-05-01 17:24:59 -0700 | [diff] [blame] | 70 | }, |
| 71 | responseError: (res) => { |
| 72 | return $q.reject(res.data); |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 73 | } |
| 74 | }; |
| 75 | } |