Created
February 15, 2018 11:03
-
-
Save KhodeN/84e928c799450477c25b30217741f05f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http'; | |
import { Inject, Injectable } from '@angular/core'; | |
import { Observable } from 'rxjs/Observable'; | |
import { API_URL } from 'st/constants'; | |
@Injectable() | |
export class ApiUrlInterceptor implements HttpInterceptor { | |
constructor(@Inject(API_URL) private _apiUrl: string) { | |
} | |
public intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | |
// Обращение к статичным вещам, а не к API | |
if ( /^\/assets\//.test(req.url) ) { | |
return next.handle(req); | |
} | |
// Обращение к API | |
const url = this._apiUrl + req.url; | |
return next.handle(req.clone({url})); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment