Joaquín Reñé
2026-03-27 4ee50e257b32f6ec0f72907305d1f2b1212808a4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { Locker } from 'angular-safeguard';
import { Injectable } from '@angular/core';
import {
    BaseRequestOptions,
    BrowserXhr,
    Request,
    RequestOptions,
    ResponseOptions,
    XHRBackend,
    XHRConnection,
    XSRFStrategy 
} from '@angular/http';
import { IS_DEV_MODE } from './session';
// TODO: Chnage this to use Covalent Http helper service
// https://www.npmjs.com/package/@covalent/http 
@Injectable()
export class DefaultRequestOptions extends BaseRequestOptions {
  constructor(private store: Locker) {
    super();
    // Set the default 'Content-Type' header
    this.headers.set('Content-Type', 'application/json');
    
  }
}
 
@Injectable()
export class ApiXHRBackend extends XHRBackend {
    
    static DEV_TOMCAT_URL : String = 'http://localhost:8080/securis/';
    constructor(_browserXHR: BrowserXhr,  _baseResponseOptions: ResponseOptions, _xsrfStrategy: XSRFStrategy, private store: Locker) {
          super(_browserXHR, _baseResponseOptions, _xsrfStrategy);
    }
    createConnection(request: Request): XHRConnection {
        let token = this.store.get('token');
        if (token) {
            request.headers.set('X-SECURIS-TOKEN', token);
        }    
        if (IS_DEV_MODE && !request.url.endsWith('.js') && !request.url.endsWith('.json') && !request.url.endsWith('.html') && !request.url.endsWith('.svg')){
           request.url = ApiXHRBackend.DEV_TOMCAT_URL + request.url;     // prefix base url
        }
        return super.createConnection(request);
    }
}
export const requestOptionsProvider = { provide: RequestOptions, useClass: DefaultRequestOptions };
export const requestBackendProvider = { provide: XHRBackend, useClass: ApiXHRBackend };