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 | 0f3692e | 2017-07-10 14:06:41 -0700 | [diff] [blame] | 11 | // NOTE this interceptor may never be called as the request is not rejected byt the "model-discoverer" service |
Matteo Scandolo | 042ea63 | 2017-03-01 19:02:34 -0800 | [diff] [blame] | 12 | switch (res.status) { |
| 13 | case -1: |
| 14 | case 401: |
Matteo Scandolo | 0f3692e | 2017-07-10 14:06:41 -0700 | [diff] [blame] | 15 | case 403: |
Matteo Scandolo | 042ea63 | 2017-03-01 19:02:34 -0800 | [diff] [blame] | 16 | $cookies.remove('sessionid', {path: '/'}); |
| 17 | $state.go('login'); |
| 18 | return $q.reject(res); |
| 19 | default: |
Matteo Scandolo | 42c6692 | 2017-05-01 17:24:59 -0700 | [diff] [blame] | 20 | return $q.reject(res); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 21 | } |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 22 | }; |
| 23 | |
| 24 | return { |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 25 | responseError: checkLogin |
| 26 | }; |
| 27 | } |
| 28 | |
| 29 | export function CredentialsInterceptor($cookies: angular.cookies.ICookiesService) { |
| 30 | return { |
| 31 | request: (req) => { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 32 | if (!$cookies.get('sessionid')) { |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 33 | return req; |
| 34 | } |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 35 | req.headers['x-sessionid'] = $cookies.get('sessionid'); |
| 36 | req.headers['x-xossession'] = $cookies.get('sessionid'); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 37 | return req; |
| 38 | } |
| 39 | }; |
| 40 | } |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 41 | |
Matteo Scandolo | 42c6692 | 2017-05-01 17:24:59 -0700 | [diff] [blame] | 42 | export function NoHyperlinksInterceptor($q: ng.IQService) { |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 43 | return { |
| 44 | request: (req) => { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 45 | if (req.url.indexOf('.html') === -1) { |
| 46 | // NOTE force content type to be JSON |
| 47 | req.headers['Content-Type'] = 'application/json'; |
Matteo Scandolo | 520a8a1 | 2017-03-10 17:31:37 -0800 | [diff] [blame] | 48 | |
| 49 | if (req.method === 'PUT') { |
Matteo Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 50 | // XosModelStore.search add this value for visualization purpose, |
Matteo Scandolo | 520a8a1 | 2017-03-10 17:31:37 -0800 | [diff] [blame] | 51 | // no one should change models |
| 52 | delete req.data.modelName; |
| 53 | } |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 54 | } |
| 55 | return req; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 56 | }, |
| 57 | response: (res) => { |
| 58 | try { |
| 59 | // NOTE convert res.data from string to JSON |
| 60 | res.data = JSON.parse(res.data); |
| 61 | try { |
| 62 | // NOTE chameleon return everything inside an "items" field |
| 63 | res.data = res.data.items; |
| 64 | } catch (_e) { |
| 65 | res.data = res.data; |
| 66 | } |
| 67 | } catch (e) { |
| 68 | res.data = res.data; |
| 69 | } |
| 70 | return res; |
Matteo Scandolo | 42c6692 | 2017-05-01 17:24:59 -0700 | [diff] [blame] | 71 | }, |
| 72 | responseError: (res) => { |
| 73 | return $q.reject(res.data); |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 74 | } |
| 75 | }; |
| 76 | } |