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('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 };