Update to Angular v19 and fixing (#3)
Reviewed-on: #3 Co-authored-by: sandre <perso@sebastienandre.com> Co-committed-by: sandre <perso@sebastienandre.com>
This commit was merged in pull request #3.
This commit is contained in:
@@ -1,26 +1,25 @@
|
||||
import { HttpHeaders } from '@angular/common/http';
|
||||
import { HttpHeaders } from "@angular/common/http";
|
||||
|
||||
import { ConfigurationHelper } from './configuration-helper';
|
||||
import { ServiceCacheApi } from './service-cache-api.service';
|
||||
import { ConfigurationHelper } from "./configuration-helper";
|
||||
import { ServiceCacheApi } from "./service-cache-api.service";
|
||||
|
||||
export class BaseService {
|
||||
protected headers: HttpHeaders;
|
||||
protected apiUrl: string;
|
||||
protected serviceCacheApi : ServiceCacheApi;
|
||||
protected headers: HttpHeaders;
|
||||
protected apiUrl: string;
|
||||
protected serviceCacheApi: ServiceCacheApi;
|
||||
|
||||
constructor() {
|
||||
ConfigurationHelper.settings.subscribe(settings =>
|
||||
{
|
||||
if (settings != null) {
|
||||
let tmpApiUrl : string = settings.apiUrl;
|
||||
|
||||
this.headers = new HttpHeaders({
|
||||
'Access-Control-Allow-Origin': tmpApiUrl
|
||||
});
|
||||
this.apiUrl = tmpApiUrl + '/api';
|
||||
}
|
||||
constructor() {
|
||||
ConfigurationHelper.settings.subscribe((settings) => {
|
||||
if (settings != null) {
|
||||
let tmpApiUrl: string = settings.apiUrl;
|
||||
|
||||
this.headers = new HttpHeaders({
|
||||
"Access-Control-Allow-Origin": tmpApiUrl,
|
||||
});
|
||||
this.apiUrl = tmpApiUrl + "/api";
|
||||
}
|
||||
});
|
||||
|
||||
this.serviceCacheApi = new ServiceCacheApi();
|
||||
}
|
||||
this.serviceCacheApi = new ServiceCacheApi();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,31 +1,33 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { Injectable } from "@angular/core";
|
||||
import { HttpClient } from "@angular/common/http";
|
||||
|
||||
import { BehaviorSubject } from 'rxjs';
|
||||
|
||||
import { IAppSettings } from '../models/app-settings';
|
||||
import { BehaviorSubject } from "rxjs";
|
||||
|
||||
import { IAppSettings } from "../models/app-settings";
|
||||
|
||||
@Injectable({ providedIn: "root" })
|
||||
export class ConfigurationHelper {
|
||||
private static settingsSource = new BehaviorSubject<IAppSettings>(null);
|
||||
public static settings = ConfigurationHelper.settingsSource.asObservable();
|
||||
|
||||
constructor(private http: HttpClient) {}
|
||||
private static settingsSource = new BehaviorSubject<IAppSettings>(null);
|
||||
public static settings = ConfigurationHelper.settingsSource.asObservable();
|
||||
|
||||
load(env: string) {
|
||||
const jsonFile = `/config/config.${env}.json`;
|
||||
constructor(private http: HttpClient) {}
|
||||
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
this.http.get(jsonFile)
|
||||
.toPromise()
|
||||
.then((response : IAppSettings) => {
|
||||
ConfigurationHelper.settingsSource.next(<IAppSettings>response);
|
||||
resolve();
|
||||
})
|
||||
.catch((response: any) => {
|
||||
reject(`Could not load file '${jsonFile}': ${JSON.stringify(response)}`);
|
||||
});
|
||||
load(env: string) {
|
||||
const jsonFile = `/config/config.${env}.json`;
|
||||
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
this.http
|
||||
.get(jsonFile)
|
||||
.toPromise()
|
||||
.then((response: IAppSettings) => {
|
||||
ConfigurationHelper.settingsSource.next(<IAppSettings>response);
|
||||
resolve();
|
||||
})
|
||||
.catch((response: any) => {
|
||||
reject(
|
||||
`Could not load file '${jsonFile}': ${JSON.stringify(response)}`
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { HttpRequest, HttpResponse } from '@angular/common/http';
|
||||
import { Injectable } from "@angular/core";
|
||||
import { HttpRequest, HttpResponse } from "@angular/common/http";
|
||||
|
||||
@Injectable()
|
||||
export class RequestCache {
|
||||
@@ -10,8 +10,8 @@ export class RequestCache {
|
||||
}
|
||||
|
||||
get(req: HttpRequest<any>): HttpResponse<any> | undefined {
|
||||
const splittedUrl = req.urlWithParams.split('/');
|
||||
const url = splittedUrl[splittedUrl.length - 1] + '_' + req.method;
|
||||
const splittedUrl = req.urlWithParams.split("/");
|
||||
const url = splittedUrl[splittedUrl.length - 1] + "_" + req.method;
|
||||
this.clearExpiredEntries();
|
||||
const cached = this.cache.get(url);
|
||||
|
||||
@@ -24,8 +24,8 @@ export class RequestCache {
|
||||
}
|
||||
|
||||
put(req: HttpRequest<any>, response: HttpResponse<any>): void {
|
||||
const splittedUrl = req.urlWithParams.split('/');
|
||||
const url = splittedUrl[splittedUrl.length - 1] + '_' + req.method;
|
||||
const splittedUrl = req.urlWithParams.split("/");
|
||||
const url = splittedUrl[splittedUrl.length - 1] + "_" + req.method;
|
||||
|
||||
const entry = { url, response, lastRead: Date.now() };
|
||||
this.cache.set(url, entry);
|
||||
@@ -36,7 +36,7 @@ export class RequestCache {
|
||||
const maxAge = 30000;
|
||||
const expired = Date.now() - maxAge;
|
||||
|
||||
this.cache.forEach(expiredEntry => {
|
||||
this.cache.forEach((expiredEntry) => {
|
||||
if (expiredEntry.lastRead < expired) {
|
||||
this.cache.delete(expiredEntry.url);
|
||||
}
|
||||
|
||||
@@ -1,29 +1,28 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Observable } from 'rxjs';
|
||||
import { tap } from 'rxjs/operators';
|
||||
import { CacheApiKey } from '../models/cache-api-key.enum';
|
||||
import { of } from 'rxjs';
|
||||
import { Injectable } from "@angular/core";
|
||||
import { Observable } from "rxjs";
|
||||
import { tap } from "rxjs/operators";
|
||||
import { CacheApiKey } from "../models/cache-api-key.enum";
|
||||
import { of } from "rxjs";
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
@Injectable({ providedIn: "root" })
|
||||
export class ServiceCacheApi {
|
||||
private cache: Map<CacheApiKey, Observable<any>>;
|
||||
|
||||
constructor() {
|
||||
constructor() {
|
||||
this.cache = new Map<CacheApiKey, Observable<any>>();
|
||||
}
|
||||
|
||||
public get<T>(key: CacheApiKey, callToApi: Observable<T>) : Observable<T> {
|
||||
public get<T>(key: CacheApiKey, callToApi: Observable<T>): Observable<T> {
|
||||
const cached = this.cache.get(key);
|
||||
|
||||
if (cached) {
|
||||
return cached;
|
||||
} else {
|
||||
return callToApi.pipe(
|
||||
tap(event => {
|
||||
this.cache.set(key, of(event));
|
||||
}));
|
||||
tap((event) => {
|
||||
this.cache.set(key, of(event));
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +30,7 @@ export class ServiceCacheApi {
|
||||
this.cache.delete(key);
|
||||
}
|
||||
|
||||
public getByKey<T>(key: CacheApiKey) : Observable<T> {
|
||||
public getByKey<T>(key: CacheApiKey): Observable<T> {
|
||||
return this.cache.get(key);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user