changed interceptor util to non class

This commit is contained in:
mpetrov 2024-08-07 17:22:14 +03:00
parent bfd9a94650
commit f1737cc206
3 changed files with 29 additions and 32 deletions

View File

@ -31,7 +31,7 @@ import {
} from '@shared/components/dialog/entity-conflict-dialog/entity-conflict-dialog.component'; } from '@shared/components/dialog/entity-conflict-dialog/entity-conflict-dialog.component';
import { HasId } from '@shared/models/base-data'; import { HasId } from '@shared/models/base-data';
import { HasVersion } from '@shared/models/entity.models'; import { HasVersion } from '@shared/models/entity.models';
import { InterceptorUtil } from './interceptor.util'; import { getInterceptorConfig } from './interceptor.util';
@Injectable() @Injectable()
export class EntityConflictInterceptor implements HttpInterceptor { export class EntityConflictInterceptor implements HttpInterceptor {
@ -61,8 +61,8 @@ export class EntityConflictInterceptor implements HttpInterceptor {
next: HttpHandler, next: HttpHandler,
error: HttpErrorResponse error: HttpErrorResponse
): Observable<HttpEvent<unknown>> { ): Observable<HttpEvent<unknown>> {
if (InterceptorUtil.getConfig(request).ignoreVersionConflict) { if (getInterceptorConfig(request).ignoreVersionConflict) {
return next.handle(this.updateRequestVersion(request)); return throwError(() => error);
} }
return this.openConflictDialog(request.body, error.error.message).pipe( return this.openConflictDialog(request.body, error.error.message).pipe(

View File

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

View File

@ -18,32 +18,29 @@ import { HttpRequest } from '@angular/common/http';
import { InterceptorConfig } from '@core/interceptors/interceptor-config'; import { InterceptorConfig } from '@core/interceptors/interceptor-config';
import { InterceptorHttpParams } from '@core/interceptors/interceptor-http-params'; import { InterceptorHttpParams } from '@core/interceptors/interceptor-http-params';
export class InterceptorUtil { const internalUrlPrefixes = [
private static readonly internalUrlPrefixes = [
'/api/auth/token', '/api/auth/token',
'/api/rpc' '/api/rpc'
]; ];
static getConfig(req: HttpRequest<unknown>): InterceptorConfig { export const getInterceptorConfig = (req: HttpRequest<unknown>): InterceptorConfig => {
let config: InterceptorConfig; let config: InterceptorConfig;
if (req.params && req.params instanceof InterceptorHttpParams) { if (req.params && req.params instanceof InterceptorHttpParams) {
config = (req.params as InterceptorHttpParams).interceptorConfig; config = (req.params as InterceptorHttpParams).interceptorConfig;
} else { } else {
config = new InterceptorConfig(); config = new InterceptorConfig();
} }
if (this.isInternalUrlPrefix(req.url)) { if (isInternalUrlPrefix(req.url)) {
config.ignoreLoading = true; config.ignoreLoading = true;
} }
return config; return config;
} };
private static isInternalUrlPrefix(url: string): boolean { const isInternalUrlPrefix = (url: string): boolean => {
for (const prefix of this.internalUrlPrefixes) { for (const prefix of internalUrlPrefixes) {
if (url.startsWith(prefix)) { if (url.startsWith(prefix)) {
return true; return true;
} }
} }
return false; return false;
} };
}