31 lines
1.3 KiB
TypeScript
31 lines
1.3 KiB
TypeScript
|
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
|
||
|
import { Observable } from 'rxjs';
|
||
|
import { BaseHttpParams } from '../base-http-params';
|
||
|
import { InterceptorType } from './interceptor-type';
|
||
|
import { environment } from '../../../../environments/environment';
|
||
|
|
||
|
export abstract class BaseInterceptor implements HttpInterceptor {
|
||
|
|
||
|
constructor() { }
|
||
|
|
||
|
abstract type: InterceptorType;
|
||
|
abstract interceptRequest(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>>;
|
||
|
|
||
|
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
||
|
if (this.isApplied(req)) {
|
||
|
return this.interceptRequest(req, next);
|
||
|
}
|
||
|
return next.handle(req);
|
||
|
}
|
||
|
|
||
|
isApplied(req: HttpRequest<any>): boolean {
|
||
|
|
||
|
if (req.params instanceof BaseHttpParams && req.params.interceptorContext && Array.isArray(req.params.interceptorContext.excludedInterceptors) && req.params.interceptorContext.excludedInterceptors.includes(this.type)) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
return (req.params instanceof BaseHttpParams && req.params.interceptorContext && Array.isArray(req.params.interceptorContext.interceptAllRequests) && req.params.interceptorContext.interceptAllRequests.includes(this.type))
|
||
|
|| req.url.startsWith(environment.Server);
|
||
|
}
|
||
|
}
|