-1

Under my angular 5 app , I'm writing an interceptor service :

@Injectable()
export class myInterceptor implements HttpInterceptor {

  constructor() { }

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const tokenInHeader = req.clone({setHeaders: {'version': '1LF'}});
    return next.handle(tokenInHeader);
  }
}

This is results on attaching the header "version" to all my http requests , which is not what i want

My purpose is how may i filter my https request by url with RegExp , and attach the "version" header to only the matching urls

Something like this :

request(new RegExp('^api/monitoring|^api/guiTrackers|^/api/monitoring|^/api/guiTrackers'))

Suggestions??

firasKoubaa
  • 6,439
  • 25
  • 79
  • 148

1 Answers1

3

simply use an if condition and pass through the original request when false:

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

    if (new RegExp('^api/monitoring|^api/guiTrackers|^/api/monitoring|^/api/guiTrackers').test(req.url)) {
      const tokenInHeader = req.clone({ setHeaders: { 'version': '1LF' } });
      return next.handle(tokenInHeader);
    } else {
      return next.handle(req);
    }
  }
A.Winnen
  • 1,680
  • 1
  • 7
  • 13