rsanchez
2017-03-10 2762277c60db7df5ad3214b10a0dd93d4f2f1128
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
import { LocalStorageService } from 'angular-2-local-storage';
import { Injectable } from '@angular/core';
import {
    BaseRequestOptions,
    BrowserXhr,
    Request,
    RequestOptions,
    ResponseOptions,
    XHRBackend,
    XHRConnection,
    XSRFStrategy
} from '@angular/http';
// 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: LocalStorageService) {
    super();
    // Set the default 'Content-Type' header
    this.headers.set('Content-Type', 'application/json');
    let token = this.store.get<string>('token');
    if (token) {
        this.headers.set('X-SECURIS-TOKEN', token);
        
    }    
    
  }
}
@Injectable()
export class ApiXHRBackend extends XHRBackend {
    
    constructor(_browserXHR: BrowserXhr,  _baseResponseOptions: ResponseOptions, _xsrfStrategy: XSRFStrategy) {
          super(_browserXHR, _baseResponseOptions, _xsrfStrategy);
    }
    createConnection(request: Request): XHRConnection {
        if (!request.url.endsWith('.js') && !request.url.endsWith('.html') && !request.url.endsWith('.svg')){
           request.url = 'http://localhost:8080/securis/' + request.url;     // prefix base url
        }
        return super.createConnection(request);
    }
}
export const requestOptionsProvider = { provide: RequestOptions, useClass: DefaultRequestOptions };
export const requestBackendProvider = { provide: XHRBackend, useClass: ApiXHRBackend };