Skip to content

Instantly share code, notes, and snippets.

@KhodeN
Created February 15, 2018 11:03
Show Gist options
  • Save KhodeN/84e928c799450477c25b30217741f05f to your computer and use it in GitHub Desktop.
Save KhodeN/84e928c799450477c25b30217741f05f to your computer and use it in GitHub Desktop.
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