@esfaenza/httpservice 13.3.0 → 14.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm2020/lib/httpservice.module.mjs +5 -5
- package/esm2020/lib/httpservice.service.mjs +89 -85
- package/fesm2015/esfaenza-httpservice.mjs +92 -88
- package/fesm2015/esfaenza-httpservice.mjs.map +1 -1
- package/fesm2020/esfaenza-httpservice.mjs +92 -88
- package/fesm2020/esfaenza-httpservice.mjs.map +1 -1
- package/{esfaenza-httpservice.d.ts → index.d.ts} +0 -0
- package/lib/httpservice.service.d.ts +5 -3
- package/package.json +7 -6
|
@@ -2,9 +2,10 @@ import * as i2 from '@angular/common/http';
|
|
|
2
2
|
import { HttpEventType, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
|
|
3
3
|
import * as i0 from '@angular/core';
|
|
4
4
|
import { InjectionToken, Injectable, Inject, Optional, NgModule } from '@angular/core';
|
|
5
|
-
import { delay, finalize, map, catchError, scan } from 'rxjs/operators';
|
|
6
|
-
import { forkJoin, of, throwError, Observable } from 'rxjs';
|
|
5
|
+
import { concatMap, delay, finalize, map, catchError, scan } from 'rxjs/operators';
|
|
6
|
+
import { forkJoin, from, of, throwError, Observable } from 'rxjs';
|
|
7
7
|
import * as i3 from '@esfaenza/preferences';
|
|
8
|
+
import * as i4 from '@esfaenza/extensions';
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
11
|
* Dizionario di invalidazioni delle chiamate HTTP
|
|
@@ -161,7 +162,7 @@ class HTTPService {
|
|
|
161
162
|
/**
|
|
162
163
|
* @ignore
|
|
163
164
|
*/
|
|
164
|
-
constructor(BACKEND_URL, EXTENSION_CLASS, HTTP_INVALIDATION, LOCALE, APP_NAME, CLIENT_CACHE_DURATION_S, CACHE_BY_USER, DEBUG_MODE, http, localStorage) {
|
|
165
|
+
constructor(BACKEND_URL, EXTENSION_CLASS, HTTP_INVALIDATION, LOCALE, APP_NAME, CLIENT_CACHE_DURATION_S, CACHE_BY_USER, DEBUG_MODE, http, localStorage, utisExts) {
|
|
165
166
|
this.BACKEND_URL = BACKEND_URL;
|
|
166
167
|
this.EXTENSION_CLASS = EXTENSION_CLASS;
|
|
167
168
|
this.HTTP_INVALIDATION = HTTP_INVALIDATION;
|
|
@@ -172,6 +173,7 @@ class HTTPService {
|
|
|
172
173
|
this.DEBUG_MODE = DEBUG_MODE;
|
|
173
174
|
this.http = http;
|
|
174
175
|
this.localStorage = localStorage;
|
|
176
|
+
this.utisExts = utisExts;
|
|
175
177
|
/** Cache dello stato delle chiamate HTTP attualmente effettuate. Ogni chiamata è rappresentata da un GUID e lo status assume valore **false** solo sugli errori */
|
|
176
178
|
this.callStatus = {};
|
|
177
179
|
/** Nome della cache in cui vengono storicizzate le informazioni sulle risposte delle chiamate sotto forma di dictionary */
|
|
@@ -229,25 +231,27 @@ class HTTPService {
|
|
|
229
231
|
* @returns {Observable<T>} Observable che contiene il risultato della chiamata
|
|
230
232
|
*/
|
|
231
233
|
get(url, params = null, transaction = "", succesfulResultMandatory = true, errorCallback = null) {
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
var httpCall = this.http.get(cachedInfos.finalUrl, { headers: this.getHeaders(transaction), params: params });
|
|
239
|
-
return httpCall.pipe(map((res) => {
|
|
240
|
-
let responseItem = res;
|
|
241
|
-
if (cachedInfos.cache) {
|
|
242
|
-
this.cacheResponse(responseItem, cachedInfos);
|
|
243
|
-
this.log(this.loc("Cached result for") + " " + url);
|
|
234
|
+
// TODO provare e testare
|
|
235
|
+
return from([this.defaultStartCall(url)]).pipe(concatMap((myGuid) => {
|
|
236
|
+
let cachedInfos = this.tryGetCacheInformations(url, params);
|
|
237
|
+
if (cachedInfos.cache && cachedInfos.cacheItem) {
|
|
238
|
+
this.log(this.loc("Result for") + " " + url + " " + this.loc("retrieved from cache"));
|
|
239
|
+
return of(cachedInfos.cacheItem).pipe(delay(0), finalize(() => { this.tryFinalizeByGuid(myGuid); }));
|
|
244
240
|
}
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
241
|
+
var httpCall = this.http.get(cachedInfos.finalUrl, { headers: this.getHeaders(transaction), params: params });
|
|
242
|
+
return httpCall.pipe(map((res) => {
|
|
243
|
+
let responseItem = res;
|
|
244
|
+
if (cachedInfos.cache) {
|
|
245
|
+
this.cacheResponse(this.utisExts.deepClone(responseItem), cachedInfos);
|
|
246
|
+
this.log(this.loc("Cached result for") + " " + url);
|
|
247
|
+
}
|
|
248
|
+
return responseItem;
|
|
249
|
+
}), catchError((err, caught) => {
|
|
250
|
+
if (errorCallback)
|
|
251
|
+
errorCallback();
|
|
252
|
+
return !succesfulResultMandatory ? throwError(err) : this.registerErrorAndReturnEmptyObservable(err, myGuid);
|
|
253
|
+
}), finalize(() => { this.tryFinalizeByGuid(myGuid, cachedInfos); }));
|
|
254
|
+
}));
|
|
251
255
|
}
|
|
252
256
|
/**
|
|
253
257
|
* Esecuzione di una GET HTTP con eventualmente la gestione della cache qualora la chiamata termini con la convenzione "#cache"
|
|
@@ -265,24 +269,25 @@ class HTTPService {
|
|
|
265
269
|
* @returns {Observable<Blob | ArrayBuffer>} Observable che contiene il risultato della chiamata
|
|
266
270
|
*/
|
|
267
271
|
getRaw(url, params = null, type = "arraybuffer", transaction = "", succesfulResultMandatory = true, errorCallback = null) {
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
}
|
|
274
|
-
var httpCall = this.http.get(cachedInfos.finalUrl, { responseType: type, headers: this.getHeaders(transaction), params: params });
|
|
275
|
-
return httpCall.pipe(map((blob) => {
|
|
276
|
-
if (cachedInfos.cache) {
|
|
277
|
-
this.cacheResponse(blob, cachedInfos);
|
|
278
|
-
this.log(this.loc("Cached result for") + " " + url);
|
|
272
|
+
return from([this.defaultStartCall(url)]).pipe(concatMap((myGuid) => {
|
|
273
|
+
let cachedInfos = this.tryGetCacheInformations(url, params);
|
|
274
|
+
if (cachedInfos.cache && cachedInfos.cacheItem) {
|
|
275
|
+
this.log(this.loc("Result for") + " " + url + " " + this.loc("retrieved from cache"));
|
|
276
|
+
return of(cachedInfos.cacheItem).pipe(delay(0), finalize(() => { this.tryFinalizeByGuid(myGuid); }));
|
|
279
277
|
}
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
278
|
+
var httpCall = this.http.get(cachedInfos.finalUrl, { responseType: type, headers: this.getHeaders(transaction), params: params });
|
|
279
|
+
return httpCall.pipe(map((blob) => {
|
|
280
|
+
if (cachedInfos.cache) {
|
|
281
|
+
this.cacheResponse(blob, cachedInfos);
|
|
282
|
+
this.log(this.loc("Cached result for") + " " + url);
|
|
283
|
+
}
|
|
284
|
+
return blob;
|
|
285
|
+
}), catchError((err, caught) => {
|
|
286
|
+
if (errorCallback)
|
|
287
|
+
errorCallback();
|
|
288
|
+
return !succesfulResultMandatory ? throwError(err) : this.registerErrorAndReturnEmptyObservable(err, myGuid);
|
|
289
|
+
}), finalize(() => { this.tryFinalizeByGuid(myGuid, cachedInfos); }));
|
|
290
|
+
}));
|
|
286
291
|
}
|
|
287
292
|
/**
|
|
288
293
|
* Esecuzione di una POST HTTP
|
|
@@ -297,14 +302,12 @@ class HTTPService {
|
|
|
297
302
|
* @returns {Observable<T>} Observable che contiene il risultato della chiamata
|
|
298
303
|
*/
|
|
299
304
|
post(url, bodyData, params = null, transaction = "", succesfulResultMandatory = true) {
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
})
|
|
306
|
-
return !succesfulResultMandatory ? throwError(err) : this.registerErrorAndReturnEmptyObservable(err, myGuid);
|
|
307
|
-
}), finalize(() => { this.tryFinalizeByGuid(myGuid); }));
|
|
305
|
+
return from([this.defaultStartCall(url)]).pipe(concatMap((myGuid) => {
|
|
306
|
+
var httpCall = this.http.post(url, bodyData, { headers: this.getHeaders(transaction), params: params });
|
|
307
|
+
return httpCall.pipe(catchError((err, caught) => {
|
|
308
|
+
return !succesfulResultMandatory ? throwError(err) : this.registerErrorAndReturnEmptyObservable(err, myGuid);
|
|
309
|
+
}), finalize(() => { this.tryFinalizeByGuid(myGuid); }));
|
|
310
|
+
}));
|
|
308
311
|
}
|
|
309
312
|
/**
|
|
310
313
|
* Esecuzione di una DELETE HTTP
|
|
@@ -318,14 +321,12 @@ class HTTPService {
|
|
|
318
321
|
* @returns {Observable<T>} Observable che contiene il risultato della chiamata
|
|
319
322
|
*/
|
|
320
323
|
delete(url, params = null, transaction = "", succesfulResultMandatory = true) {
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
})
|
|
327
|
-
return !succesfulResultMandatory ? throwError(err) : this.registerErrorAndReturnEmptyObservable(err, myGuid);
|
|
328
|
-
}), finalize(() => { this.tryFinalizeByGuid(myGuid); }));
|
|
324
|
+
return from([this.defaultStartCall(url)]).pipe(concatMap((myGuid) => {
|
|
325
|
+
var httpCall = this.http.delete(url, { headers: this.getHeaders(transaction), params: params });
|
|
326
|
+
return httpCall.pipe(catchError((err, caught) => {
|
|
327
|
+
return !succesfulResultMandatory ? throwError(err) : this.registerErrorAndReturnEmptyObservable(err, myGuid);
|
|
328
|
+
}), finalize(() => { this.tryFinalizeByGuid(myGuid); }));
|
|
329
|
+
}));
|
|
329
330
|
}
|
|
330
331
|
/**
|
|
331
332
|
* Esecuzione di una PUT HTTP
|
|
@@ -340,14 +341,12 @@ class HTTPService {
|
|
|
340
341
|
* @returns {Observable<T>} Observable che contiene il risultato della chiamata
|
|
341
342
|
*/
|
|
342
343
|
put(url, bodyData, params = null, transaction = "", succesfulResultMandatory = true) {
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
})
|
|
349
|
-
return !succesfulResultMandatory ? throwError(err) : this.registerErrorAndReturnEmptyObservable(err, myGuid);
|
|
350
|
-
}), finalize(() => { this.tryFinalizeByGuid(myGuid); }));
|
|
344
|
+
return from([this.defaultStartCall(url)]).pipe(concatMap((myGuid) => {
|
|
345
|
+
var httpCall = this.http.put(url, bodyData, { headers: this.getHeaders(transaction), params: params });
|
|
346
|
+
return httpCall.pipe(catchError((err, caught) => {
|
|
347
|
+
return !succesfulResultMandatory ? throwError(err) : this.registerErrorAndReturnEmptyObservable(err, myGuid);
|
|
348
|
+
}), finalize(() => { this.tryFinalizeByGuid(myGuid); }));
|
|
349
|
+
}));
|
|
351
350
|
}
|
|
352
351
|
/**
|
|
353
352
|
* Esecuzione di una POST HTTP
|
|
@@ -361,11 +360,12 @@ class HTTPService {
|
|
|
361
360
|
* @returns {Observable<T>} Observable che contiene il risultato della chiamata
|
|
362
361
|
*/
|
|
363
362
|
postwithFiles(url, bodyData, fileArrayOrFile) {
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
363
|
+
return from([this.defaultStartCall(url)]).pipe(concatMap((myGuid) => {
|
|
364
|
+
var formData = this.getFilesFormData(fileArrayOrFile);
|
|
365
|
+
formData.append("body", JSON.stringify(bodyData));
|
|
366
|
+
var httpCall = this.http.post(url, formData, { headers: this.getHeaders(null, true, null, null) });
|
|
367
|
+
return httpCall.pipe(catchError((err, caught) => { return this.registerErrorAndReturnEmptyObservable(err, myGuid); }), finalize(() => { this.tryFinalizeByGuid(myGuid); }));
|
|
368
|
+
}));
|
|
369
369
|
}
|
|
370
370
|
/**
|
|
371
371
|
* Esecuzione di una POST HTTP
|
|
@@ -526,17 +526,20 @@ class HTTPService {
|
|
|
526
526
|
* che mi cambi sotto l'oggetto di cache mentre sto salvando le informazioni
|
|
527
527
|
*/
|
|
528
528
|
async tryFinalizeByGuid(guid, cacheInfos) {
|
|
529
|
-
this.log(this.loc("Finalizing call") + " " + guid + " " + this.loc("with status") + ": " + (this.callStatus[guid].success ? this.loc('Success') : this.loc('Failure')));
|
|
530
|
-
//
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
this.LastThreeCompletedCalls.
|
|
538
|
-
|
|
539
|
-
|
|
529
|
+
this.log(this.loc("Finalizing call") + " " + guid + " " + this.loc("with status") + ": " + (!this.callStatus[guid] ? 'Unknown' : this.callStatus[guid].success ? this.loc('Success') : this.loc('Failure')));
|
|
530
|
+
// TODO: Perdere 3-4 ore per capire perché ogni tanto arriva null in casi pesanti di Observable spammati a random
|
|
531
|
+
if (this.callStatus[guid]) {
|
|
532
|
+
// Alla prima chiamata buona, significa che almeno il login ce l'ho. Posso mettermi in modalità redirectInProgress false
|
|
533
|
+
// e riiniziare a valutare errori di javashit
|
|
534
|
+
if (this.callStatus[guid].success && this.ExtensionsImpl.readRedirectionStatus())
|
|
535
|
+
this.ExtensionsImpl.writeRedirectionStatus(false);
|
|
536
|
+
// Aggiungo la chiamata alla lista delle ultime 3 completate
|
|
537
|
+
this.LastThreeCompletedCalls.unshift(this.callStatus[guid].url);
|
|
538
|
+
if (this.LastThreeCompletedCalls.length > 3)
|
|
539
|
+
this.LastThreeCompletedCalls.pop();
|
|
540
|
+
// In ogni caso questa posso rimuoverla
|
|
541
|
+
delete this.callStatus[guid];
|
|
542
|
+
}
|
|
540
543
|
// Se vengo da una chiamata che ha aggiornato la cache a timer o globale marco il fatto che quella cache dev'essere aggiornata
|
|
541
544
|
if (cacheInfos && cacheInfos.cache) {
|
|
542
545
|
this.PersistTimedCacheASAP = this.PersistTimedCacheASAP || !!cacheInfos.cacheTime;
|
|
@@ -751,7 +754,7 @@ class HTTPService {
|
|
|
751
754
|
* @returns {CacheInfos} Oggetto contenente le informazioni sulla cache per l'oggetto specificato
|
|
752
755
|
*/
|
|
753
756
|
tryGetCacheInformations(url, params) {
|
|
754
|
-
let cachingKey = null, cacheTime = null,
|
|
757
|
+
let cachingKey = null, cacheTime = null, item = null;
|
|
755
758
|
let useCaching = !!(/#cache/gi.exec(url));
|
|
756
759
|
if (useCaching) {
|
|
757
760
|
let tmp = /#cache(:?-(\d+))/gi.exec(url);
|
|
@@ -771,7 +774,8 @@ class HTTPService {
|
|
|
771
774
|
item = cache[cachingKey];
|
|
772
775
|
}
|
|
773
776
|
}
|
|
774
|
-
|
|
777
|
+
// Per assicurarmi che il rifeirmento degli oggetti sia diverso utilizzo il deepClone
|
|
778
|
+
return new CacheInfos(url, useCaching, cachingKey, cacheTime, this.utisExts.deepClone(item));
|
|
775
779
|
}
|
|
776
780
|
/**
|
|
777
781
|
* Inserisce un oggetto nella cache appropriata in base alle informazioni contenute nel parametro **cacheInfos**
|
|
@@ -868,9 +872,9 @@ class HTTPService {
|
|
|
868
872
|
return Loc[message][this.LOCALE];
|
|
869
873
|
}
|
|
870
874
|
}
|
|
871
|
-
HTTPService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "
|
|
872
|
-
HTTPService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "
|
|
873
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "
|
|
875
|
+
HTTPService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.12", ngImport: i0, type: HTTPService, deps: [{ token: BACKEND_URL }, { token: HttpServiceExtensions }, { token: HTTP_INVALIDATION, optional: true }, { token: HTTP_LOCALE, optional: true }, { token: APP_NAME, optional: true }, { token: CLIENT_CACHE_DURATION_S, optional: true }, { token: CACHE_BY_USER, optional: true }, { token: DEBUG_MODE, optional: true }, { token: i2.HttpClient }, { token: i3.CacheService }, { token: i4.UtilityService }], target: i0.ɵɵFactoryTarget.Injectable });
|
|
876
|
+
HTTPService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.2.12", ngImport: i0, type: HTTPService });
|
|
877
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.12", ngImport: i0, type: HTTPService, decorators: [{
|
|
874
878
|
type: Injectable
|
|
875
879
|
}], ctorParameters: function () { return [{ type: undefined, decorators: [{
|
|
876
880
|
type: Inject,
|
|
@@ -905,7 +909,7 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.9", ngImpor
|
|
|
905
909
|
}, {
|
|
906
910
|
type: Inject,
|
|
907
911
|
args: [DEBUG_MODE]
|
|
908
|
-
}] }, { type: i2.HttpClient }, { type: i3.
|
|
912
|
+
}] }, { type: i2.HttpClient }, { type: i3.CacheService }, { type: i4.UtilityService }]; } });
|
|
909
913
|
|
|
910
914
|
class HttpserviceModule {
|
|
911
915
|
static forRoot(config) {
|
|
@@ -925,10 +929,10 @@ class HttpserviceModule {
|
|
|
925
929
|
};
|
|
926
930
|
}
|
|
927
931
|
}
|
|
928
|
-
HttpserviceModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "
|
|
929
|
-
HttpserviceModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "
|
|
930
|
-
HttpserviceModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "
|
|
931
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "
|
|
932
|
+
HttpserviceModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.12", ngImport: i0, type: HttpserviceModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
|
|
933
|
+
HttpserviceModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "14.2.12", ngImport: i0, type: HttpserviceModule });
|
|
934
|
+
HttpserviceModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "14.2.12", ngImport: i0, type: HttpserviceModule });
|
|
935
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.12", ngImport: i0, type: HttpserviceModule, decorators: [{
|
|
932
936
|
type: NgModule
|
|
933
937
|
}] });
|
|
934
938
|
|