refactor: clean code

This commit is contained in:
Polianin Nikita 2024-08-23 23:21:12 +03:00
parent e0a2ba257c
commit 3cea5f7982
6 changed files with 8 additions and 18 deletions

View File

@ -1,4 +1,4 @@
import {catchError, filter, mergeMap, Observable, retryWhen, switchMap, take, tap, timer} from "rxjs";
import {catchError, filter, mergeMap, Observable, retryWhen, switchMap, tap, timer} from "rxjs";
import {HttpClient, HttpErrorResponse} from "@angular/common/http";
import {NotifyColor, OpenNotifyService} from "@service/open-notify.service";
import {environment} from "@environment";
@ -144,7 +144,7 @@ export default abstract class ApiService implements SetRequestBuilderAfterBuild
private handleError(error: HttpErrorResponse): void {
// todo: change to Retry-After condition
if (error.error.toString().includes("setup")) {
if (error.error && error.error.toString().includes("setup")) {
this.router.navigate(['/setup/']).then();
return;
}

View File

@ -13,7 +13,6 @@ export class FacultyService extends ApiService {
.setQueryParams({page: page, pageSize: pageSize})
.build<ApiService>()
.get<FacultyResponse[]>();
}
public getById(id: number) {

View File

@ -11,7 +11,7 @@ import {FocusNextDirective} from "@/directives/focus-next.directive";
import {DataSpinnerComponent} from "@component/common/data-spinner/data-spinner.component";
import AuthApiService from "@api/v1/authApiService";
import {Router} from "@angular/router";
import {catchError, of} from "rxjs";
import {catchError} from "rxjs";
@Component({
selector: 'app-login',
@ -43,7 +43,7 @@ export class LoginComponent {
constructor(private formBuilder: FormBuilder, private auth: AuthApiService, private route: Router) {
this.auth.getRole()
.subscribe(data => {
if (data != null)
if (data !== null)
route.navigate(['admin']).then();
});

View File

@ -1,6 +1,6 @@
import {EventEmitter, Injectable} from '@angular/core';
import {HttpClient, HttpHeaders} from "@angular/common/http";
import {catchError, Observable, of, tap} from "rxjs";
import {Observable, tap} from "rxjs";
import {TokenResponse} from "@api/v1/tokenResponse";
import ApiService from "@api/api.service";
@ -36,7 +36,6 @@ export class AuthToken {
})
export class AuthService {
public expireTokenChange = new EventEmitter<Date>();
public tokenChangeError = new EventEmitter();
constructor(private http: HttpClient) {
}
@ -60,8 +59,9 @@ export class AuthService {
public refreshToken(): Observable<TokenResponse> {
const token = localStorage.getItem(ApiService.tokenKey);
console.log(token);
if (!token)
return of({} as TokenResponse);
throw new Error("token is not found");
const authToken = JSON.parse(token) as AuthToken;
@ -69,10 +69,6 @@ export class AuthService {
case AvailableAuthenticationProvider.Bearer:
return this.http.get<TokenResponse>(authToken.endpoint, {withCredentials: true})
.pipe(
catchError(error => {
this.tokenChangeError.emit();
throw error;
}),
tap(response => {
const newExpireDate = new Date(response.expiresIn);
const oldExpireDate = new Date(authToken.expiresIn);

View File

@ -15,11 +15,6 @@ export class TokenRefreshService {
constructor(private authService: AuthService) {
this.setRefreshTokenExpireMs(AuthService.tokenExpiresIn.getTime() - 1000 - Date.now());
authService.tokenChangeError.subscribe(_ => {
console.debug('Token change error event received');
this.tokenRefreshing$.next(false);
this.stopTokenRefresh();
});
authService.expireTokenChange.subscribe(date => {
console.debug('Expire token change event received:', date);
this.setRefreshTokenExpireMs(date.getTime() - 1000 - Date.now());