Last active
March 28, 2019 11:39
-
-
Save nicowernli/c097a37a6f05552cc5a9a48b4e945e07 to your computer and use it in GitHub Desktop.
Simple HttpInterceptor that adds a header to a request
This file contains hidden or 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 { Injectable } from '@angular/core'; | |
import {HttpHandler, HttpRequest, HttpInterceptor} from '@angular/common/http'; | |
import {environment} from '../../environments/environment'; | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class BaseURLInterceptor implements HttpInterceptor { | |
intercept(req: HttpRequest<any>, next: HttpHandler) { | |
if (!req.url.match(/^http(s)?:\/\/(.*)$/)) { | |
const url = `${environment.baseURL}${req.url}`.replace(/([^:]\/)\/+/g, '$1'); | |
req = req.clone({ url }); | |
} | |
return next.handle(req); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment