Browse Source

changed interceptor service to interceptor utils

pull/11365/head
mpetrov 2 years ago
parent
commit
9395390d7a
  1. 11
      ui-ngx/src/app/core/interceptors/entity-conflict.interceptor.ts
  2. 7
      ui-ngx/src/app/core/interceptors/global-http-interceptor.ts
  3. 13
      ui-ngx/src/app/core/interceptors/interceptor.util.ts

11
ui-ngx/src/app/core/interceptors/entity-conflict.interceptor.ts

@ -29,16 +29,15 @@ import { MatDialog } from '@angular/material/dialog';
import {
EntityConflictDialogComponent
} from '@shared/components/dialog/entity-conflict-dialog/entity-conflict-dialog.component';
import { InterceptorConfigService } from '@core/services/interceptor-config.service';
import { HasId } from '@shared/models/base-data';
import { HasVersion } from '@shared/models/entity.models';
import { InterceptorUtil } from '@core/utils/interceptor.util';
@Injectable()
export class EntityConflictInterceptor implements HttpInterceptor {
constructor(
private dialog: MatDialog,
private interceptorConfigService: InterceptorConfigService
) {}
intercept(request: HttpRequest<unknown & HasId & HasVersion>, next: HttpHandler): Observable<HttpEvent<unknown>> {
@ -62,11 +61,11 @@ export class EntityConflictInterceptor implements HttpInterceptor {
next: HttpHandler,
error: HttpErrorResponse
): Observable<HttpEvent<unknown>> {
if (this.interceptorConfigService.getInterceptorConfig(request).ignoreVersionConflict) {
if (InterceptorUtil.getConfig(request).ignoreVersionConflict) {
return next.handle(this.updateRequestVersion(request));
}
return this.openConflictDialog(request, error.error.message).pipe(
return this.openConflictDialog(request.body, error.error.message).pipe(
switchMap(result => {
if (result) {
return next.handle(this.updateRequestVersion(request));
@ -81,9 +80,9 @@ export class EntityConflictInterceptor implements HttpInterceptor {
return request.clone({ body });
}
private openConflictDialog(request: HttpRequest<unknown & HasId & HasVersion>, message: string): Observable<boolean> {
private openConflictDialog(entity: unknown & HasId & HasVersion, message: string): Observable<boolean> {
const dialogRef = this.dialog.open(EntityConflictDialogComponent, {
data: { message, entity: request.body }
data: { message, entity }
});
return dialogRef.afterClosed();

7
ui-ngx/src/app/core/interceptors/global-http-interceptor.ts

@ -29,7 +29,7 @@ import { ActionNotificationShow } from '@app/core/notification/notification.acti
import { DialogService } from '@core/services/dialog.service';
import { TranslateService } from '@ngx-translate/core';
import { parseHttpErrorMessage } from '@core/utils';
import { InterceptorConfigService } from '@core/services/interceptor-config.service';
import { InterceptorUtil } from '@core/interceptors/interceptor.util';
const tmpHeaders = {};
@ -46,12 +46,11 @@ export class GlobalHttpInterceptor implements HttpInterceptor {
private dialogService: DialogService,
private translate: TranslateService,
private authService: AuthService,
private interceptorConfigService: InterceptorConfigService
) {}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (req.url.startsWith('/api/')) {
const config = this.interceptorConfigService.getInterceptorConfig(req);
const config = InterceptorUtil.getConfig(req);
this.updateLoadingState(config, true);
let observable$: Observable<HttpEvent<any>>;
if (this.isTokenBasedAuthEntryPoint(req.url)) {
@ -95,7 +94,7 @@ export class GlobalHttpInterceptor implements HttpInterceptor {
}
private handleResponseError(req: HttpRequest<any>, next: HttpHandler, errorResponse: HttpErrorResponse): Observable<HttpEvent<any>> {
const config = this.interceptorConfigService.getInterceptorConfig(req);
const config = InterceptorUtil.getConfig(req);
let unhandled = false;
const ignoreErrors = config.ignoreErrors;
const resendRequest = config.resendRequest;

13
ui-ngx/src/app/core/services/interceptor-config.service.ts → ui-ngx/src/app/core/interceptors/interceptor.util.ts

@ -14,22 +14,17 @@
/// limitations under the License.
///
import { Injectable } from '@angular/core';
import { HttpRequest } from '@angular/common/http';
import { InterceptorConfig } from '@core/interceptors/interceptor-config';
import { InterceptorHttpParams } from '@core/interceptors/interceptor-http-params';
@Injectable({
providedIn: 'root'
})
export class InterceptorConfigService {
private readonly internalUrlPrefixes = [
export class InterceptorUtil {
private static readonly internalUrlPrefixes = [
'/api/auth/token',
'/api/rpc'
];
getInterceptorConfig(req: HttpRequest<unknown>): InterceptorConfig {
static getConfig(req: HttpRequest<unknown>): InterceptorConfig {
let config: InterceptorConfig;
if (req.params && req.params instanceof InterceptorHttpParams) {
config = (req.params as InterceptorHttpParams).interceptorConfig;
@ -42,7 +37,7 @@ export class InterceptorConfigService {
return config;
}
private isInternalUrlPrefix(url: string): boolean {
private static isInternalUrlPrefix(url: string): boolean {
for (const prefix of this.internalUrlPrefixes) {
if (url.startsWith(prefix)) {
return true;
Loading…
Cancel
Save