refactor: make compliance with the new api
This commit is contained in:
parent
5d265e4b48
commit
7c66f31bac
@ -1,7 +1,6 @@
|
|||||||
import {Injectable} from "@angular/core";
|
import {Injectable} from "@angular/core";
|
||||||
import ApiService, {AvailableVersion} from "@api/api.service";
|
import ApiService, {AvailableVersion} from "@api/api.service";
|
||||||
import {FacultyResponse} from "@api/v1/facultyResponse";
|
import {FacultyResponse} from "@api/v1/facultyResponse";
|
||||||
import {FacultyDetailsResponse} from "@api/v1/facultyDetailsResponse";
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class FacultyService extends ApiService {
|
export class FacultyService extends ApiService {
|
||||||
@ -15,8 +14,4 @@ export class FacultyService extends ApiService {
|
|||||||
|
|
||||||
return this.get<FacultyResponse[]>(request);
|
return this.get<FacultyResponse[]>(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
public getById(id: number) {
|
|
||||||
return this.get<FacultyDetailsResponse>(id.toString());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,82 +0,0 @@
|
|||||||
import {BehaviorSubject, catchError, of, Subject} from "rxjs";
|
|
||||||
import {Injectable} from "@angular/core";
|
|
||||||
import {AuthService} from "@service/auth.service";
|
|
||||||
import {environment} from "@environment";
|
|
||||||
import ApiService from "@api/api.service";
|
|
||||||
|
|
||||||
@Injectable({
|
|
||||||
providedIn: 'root',
|
|
||||||
})
|
|
||||||
export class TokenRefreshService {
|
|
||||||
private tokenRefreshing$ = new BehaviorSubject<boolean>(false);
|
|
||||||
private refreshTokenTimeout: any;
|
|
||||||
private refreshTokenExpireMs: number = environment.retryDelay;
|
|
||||||
|
|
||||||
constructor(private authService: AuthService) {
|
|
||||||
this.setRefreshTokenExpireMs(AuthService.tokenExpiresIn);
|
|
||||||
}
|
|
||||||
|
|
||||||
private startTokenRefresh(): void {
|
|
||||||
this.refreshTokenTimeout = setTimeout(() => {
|
|
||||||
this.refreshToken();
|
|
||||||
}, this.refreshTokenExpireMs);
|
|
||||||
}
|
|
||||||
|
|
||||||
private refreshToken(): void {
|
|
||||||
if (this.tokenRefreshing$.value)
|
|
||||||
return;
|
|
||||||
|
|
||||||
this.tokenRefreshing$.next(true);
|
|
||||||
|
|
||||||
this.authService.refreshToken()
|
|
||||||
.pipe(
|
|
||||||
catchError(error => {
|
|
||||||
if (error.status === 403 || error.status === 401 || !localStorage.getItem(ApiService.tokenKey)) {
|
|
||||||
localStorage.removeItem(ApiService.tokenKey);
|
|
||||||
return of(undefined);
|
|
||||||
}
|
|
||||||
|
|
||||||
let retryTime = this.refreshTokenExpireMs;
|
|
||||||
|
|
||||||
if (retryTime < environment.retryDelay)
|
|
||||||
retryTime = environment.retryDelay;
|
|
||||||
|
|
||||||
// 15 minutes
|
|
||||||
if (retryTime * 2 <= 900_000)
|
|
||||||
retryTime *= 2;
|
|
||||||
else
|
|
||||||
retryTime = 900_000;
|
|
||||||
|
|
||||||
return of(retryTime);
|
|
||||||
}))
|
|
||||||
.subscribe(data => {
|
|
||||||
if (data)
|
|
||||||
this.setRefreshTokenExpireMs(data);
|
|
||||||
|
|
||||||
this.tokenRefreshing$.next(false);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public getTokenRefreshing$(): Subject<boolean> {
|
|
||||||
return this.tokenRefreshing$;
|
|
||||||
}
|
|
||||||
|
|
||||||
public setRefreshTokenExpireMs(expireMs: number | string | Date | null = null): void {
|
|
||||||
let expireMsNumber: number;
|
|
||||||
if (expireMs === null)
|
|
||||||
expireMsNumber = -1;
|
|
||||||
else if (expireMs instanceof Date || typeof expireMs === 'string')
|
|
||||||
expireMsNumber = new Date(expireMs).getTime() - 1000 - Date.now();
|
|
||||||
else
|
|
||||||
expireMsNumber = expireMs;
|
|
||||||
|
|
||||||
if (expireMsNumber < environment.retryDelay)
|
|
||||||
expireMsNumber = environment.retryDelay;
|
|
||||||
|
|
||||||
this.refreshTokenExpireMs = expireMsNumber;
|
|
||||||
console.log('New refresh token interval:', this.refreshTokenExpireMs);
|
|
||||||
|
|
||||||
clearTimeout(this.refreshTokenTimeout);
|
|
||||||
this.startTokenRefresh();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,37 +0,0 @@
|
|||||||
/**
|
|
||||||
* MIREA Schedule Web API
|
|
||||||
* This API provides a convenient interface for retrieving data stored in the database. Special attention was paid to the lightweight and easy transfer of all necessary data. Made by the Winsomnia team.
|
|
||||||
*
|
|
||||||
* OpenAPI spec version: 1.0
|
|
||||||
* Contact: support@winsomnia.net
|
|
||||||
*
|
|
||||||
* NOTE: This class is auto generated by the swagger code generator program.
|
|
||||||
* https://github.com/swagger-api/swagger-codegen.git
|
|
||||||
* Do not edit the class manually.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents detailed information about a faculty.
|
|
||||||
*/
|
|
||||||
export interface FacultyDetailsResponse {
|
|
||||||
/**
|
|
||||||
* Gets or sets the unique identifier of the faculty.
|
|
||||||
*/
|
|
||||||
id: number;
|
|
||||||
/**
|
|
||||||
* Gets or sets the name of the faculty.
|
|
||||||
*/
|
|
||||||
name: string;
|
|
||||||
/**
|
|
||||||
* Gets or sets the unique identifier of the campus to which the faculty belongs (optional).
|
|
||||||
*/
|
|
||||||
campusId?: number;
|
|
||||||
/**
|
|
||||||
* Gets or sets the name of the campus to which the faculty belongs (optional).
|
|
||||||
*/
|
|
||||||
campusName?: string;
|
|
||||||
/**
|
|
||||||
* Gets or sets the code name of the campus to which the faculty belongs (optional).
|
|
||||||
*/
|
|
||||||
campusCode?: string;
|
|
||||||
}
|
|
@ -13,7 +13,7 @@
|
|||||||
/**
|
/**
|
||||||
* Represents basic information about a faculty.
|
* Represents basic information about a faculty.
|
||||||
*/
|
*/
|
||||||
export interface FacultyResponse {
|
export interface FacultyResponse {
|
||||||
/**
|
/**
|
||||||
* Gets or sets the unique identifier of the faculty.
|
* Gets or sets the unique identifier of the faculty.
|
||||||
*/
|
*/
|
||||||
@ -22,8 +22,4 @@ export interface FacultyResponse {
|
|||||||
* Gets or sets the name of the faculty.
|
* Gets or sets the name of the faculty.
|
||||||
*/
|
*/
|
||||||
name: string;
|
name: string;
|
||||||
/**
|
}
|
||||||
* Gets or sets the unique identifier of the campus to which the faculty belongs (optional).
|
|
||||||
*/
|
|
||||||
campusId?: number;
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user