@esfaenza/httpservice 13.3.1 → 13.4.1

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.
@@ -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
- let myGuid = this.defaultStartCall(url);
233
- let cachedInfos = this.tryGetCacheInformations(url, params);
234
- if (cachedInfos.cache && cachedInfos.cacheItem) {
235
- this.log(this.loc("Result for") + " " + url + " " + this.loc("retrieved from cache"));
236
- return of(cachedInfos.cacheItem).pipe(delay(0), finalize(() => { this.tryFinalizeByGuid(myGuid); }));
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
- return responseItem;
246
- }), catchError((err, caught) => {
247
- if (errorCallback)
248
- errorCallback();
249
- return !succesfulResultMandatory ? throwError(err) : this.registerErrorAndReturnEmptyObservable(err, myGuid);
250
- }), finalize(() => { this.tryFinalizeByGuid(myGuid, cachedInfos); }));
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
- let myGuid = this.defaultStartCall(url);
269
- let cachedInfos = this.tryGetCacheInformations(url, params);
270
- if (cachedInfos.cache && cachedInfos.cacheItem) {
271
- this.log(this.loc("Result for") + " " + url + " " + this.loc("retrieved from cache"));
272
- return of(cachedInfos.cacheItem).pipe(delay(0), finalize(() => { this.tryFinalizeByGuid(myGuid); }));
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
- return blob;
281
- }), catchError((err, caught) => {
282
- if (errorCallback)
283
- errorCallback();
284
- return !succesfulResultMandatory ? throwError(err) : this.registerErrorAndReturnEmptyObservable(err, myGuid);
285
- }), finalize(() => { this.tryFinalizeByGuid(myGuid, cachedInfos); }));
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
- let myGuid = this.defaultStartCall(url);
301
- var httpCall = this.http.post(url, bodyData, { headers: this.getHeaders(transaction), params: params });
302
- return httpCall.pipe(map((res) => {
303
- let responseItem = res;
304
- return responseItem;
305
- }), catchError((err, caught) => {
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
- let myGuid = this.defaultStartCall(url);
322
- var httpCall = this.http.delete(url, { headers: this.getHeaders(transaction), params: params });
323
- return httpCall.pipe(map((res) => {
324
- let responseItem = res;
325
- return responseItem;
326
- }), catchError((err, caught) => {
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
- let myGuid = this.defaultStartCall(url);
344
- var httpCall = this.http.put(url, bodyData, { headers: this.getHeaders(transaction), params: params });
345
- return httpCall.pipe(map((res) => {
346
- let responseItem = res;
347
- return responseItem;
348
- }), catchError((err, caught) => {
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
@@ -360,12 +359,13 @@ class HTTPService {
360
359
  *
361
360
  * @returns {Observable<T>} Observable che contiene il risultato della chiamata
362
361
  */
363
- postwithFiles(url, bodyData, fileArrayOrFile) {
364
- let myGuid = this.defaultStartCall(url);
365
- var formData = this.getFilesFormData(fileArrayOrFile);
366
- formData.append("body", JSON.stringify(bodyData));
367
- var httpCall = this.http.post(url, formData, { headers: this.getHeaders(null, true, null, null) });
368
- return httpCall.pipe(catchError((err, caught) => { return this.registerErrorAndReturnEmptyObservable(err, myGuid); }), finalize(() => { this.tryFinalizeByGuid(myGuid); }));
362
+ postwithFiles(url, bodyData, fileArrayOrFile, params = null) {
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, { params: params, 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
@@ -428,7 +428,12 @@ class HTTPService {
428
428
  * @param {HttpParams} params Parametri della chiamata
429
429
  */
430
430
  getFileNative(url, params = null) {
431
- let finalUrl = url + (params ? "?" + params.toString() : "");
431
+ // Encodo tutti i parametri per evitare casi particolari
432
+ // var encodedParams = new HttpParams();
433
+ // let parKeys = params.keys();
434
+ // for (let i = 0; i < parKeys.length; i++)
435
+ // encodedParams = encodedParams.set(parKeys[i], encodeURIComponent(params.get(parKeys[i])));
436
+ let finalUrl = url + (params ? "?" + params.toString().replace(/\+/g, '%2b') : "");
432
437
  window.location.href = finalUrl;
433
438
  }
434
439
  /**
@@ -526,17 +531,20 @@ class HTTPService {
526
531
  * che mi cambi sotto l'oggetto di cache mentre sto salvando le informazioni
527
532
  */
528
533
  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
- // Alla prima chiamata buona, significa che almeno il login ce l'ho. Posso mettermi in modalità redirectInProgress false
531
- // e riiniziare a valutare errori di javashit
532
- if (this.callStatus[guid].success && this.ExtensionsImpl.readRedirectionStatus())
533
- this.ExtensionsImpl.writeRedirectionStatus(false);
534
- // Aggiungo la chiamata alla lista delle ultime 3 completate
535
- this.LastThreeCompletedCalls.unshift(this.callStatus[guid].url);
536
- if (this.LastThreeCompletedCalls.length > 3)
537
- this.LastThreeCompletedCalls.pop();
538
- // In ogni caso questa posso rimuoverla
539
- delete this.callStatus[guid];
534
+ 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')));
535
+ // TODO: Perdere 3-4 ore per capire perché ogni tanto arriva null in casi pesanti di Observable spammati a random
536
+ if (this.callStatus[guid]) {
537
+ // Alla prima chiamata buona, significa che almeno il login ce l'ho. Posso mettermi in modalità redirectInProgress false
538
+ // e riiniziare a valutare errori di javashit
539
+ if (this.callStatus[guid].success && this.ExtensionsImpl.readRedirectionStatus())
540
+ this.ExtensionsImpl.writeRedirectionStatus(false);
541
+ // Aggiungo la chiamata alla lista delle ultime 3 completate
542
+ this.LastThreeCompletedCalls.unshift(this.callStatus[guid].url);
543
+ if (this.LastThreeCompletedCalls.length > 3)
544
+ this.LastThreeCompletedCalls.pop();
545
+ // In ogni caso questa posso rimuoverla
546
+ delete this.callStatus[guid];
547
+ }
540
548
  // Se vengo da una chiamata che ha aggiornato la cache a timer o globale marco il fatto che quella cache dev'essere aggiornata
541
549
  if (cacheInfos && cacheInfos.cache) {
542
550
  this.PersistTimedCacheASAP = this.PersistTimedCacheASAP || !!cacheInfos.cacheTime;
@@ -751,7 +759,7 @@ class HTTPService {
751
759
  * @returns {CacheInfos} Oggetto contenente le informazioni sulla cache per l'oggetto specificato
752
760
  */
753
761
  tryGetCacheInformations(url, params) {
754
- let cachingKey = null, cacheTime = null, cache = null, item = null;
762
+ let cachingKey = null, cacheTime = null, item = null;
755
763
  let useCaching = !!(/#cache/gi.exec(url));
756
764
  if (useCaching) {
757
765
  let tmp = /#cache(:?-(\d+))/gi.exec(url);
@@ -771,7 +779,8 @@ class HTTPService {
771
779
  item = cache[cachingKey];
772
780
  }
773
781
  }
774
- return new CacheInfos(url, useCaching, cachingKey, cacheTime, item);
782
+ // Per assicurarmi che il rifeirmento degli oggetti sia diverso utilizzo il deepClone
783
+ return new CacheInfos(url, useCaching, cachingKey, cacheTime, this.utisExts.deepClone(item));
775
784
  }
776
785
  /**
777
786
  * Inserisce un oggetto nella cache appropriata in base alle informazioni contenute nel parametro **cacheInfos**
@@ -868,9 +877,9 @@ class HTTPService {
868
877
  return Loc[message][this.LOCALE];
869
878
  }
870
879
  }
871
- HTTPService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.9", 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.PreferencesService }], target: i0.ɵɵFactoryTarget.Injectable });
872
- HTTPService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "13.3.9", ngImport: i0, type: HTTPService });
873
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.9", ngImport: i0, type: HTTPService, decorators: [{
880
+ HTTPService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.4.0", 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 });
881
+ HTTPService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "13.4.0", ngImport: i0, type: HTTPService });
882
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.4.0", ngImport: i0, type: HTTPService, decorators: [{
874
883
  type: Injectable
875
884
  }], ctorParameters: function () { return [{ type: undefined, decorators: [{
876
885
  type: Inject,
@@ -905,7 +914,7 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.9", ngImpor
905
914
  }, {
906
915
  type: Inject,
907
916
  args: [DEBUG_MODE]
908
- }] }, { type: i2.HttpClient }, { type: i3.PreferencesService }]; } });
917
+ }] }, { type: i2.HttpClient }, { type: i3.CacheService }, { type: i4.UtilityService }]; } });
909
918
 
910
919
  class HttpserviceModule {
911
920
  static forRoot(config) {
@@ -925,10 +934,10 @@ class HttpserviceModule {
925
934
  };
926
935
  }
927
936
  }
928
- HttpserviceModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.9", ngImport: i0, type: HttpserviceModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
929
- HttpserviceModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "13.3.9", ngImport: i0, type: HttpserviceModule });
930
- HttpserviceModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "13.3.9", ngImport: i0, type: HttpserviceModule });
931
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.9", ngImport: i0, type: HttpserviceModule, decorators: [{
937
+ HttpserviceModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.4.0", ngImport: i0, type: HttpserviceModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
938
+ HttpserviceModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "13.4.0", ngImport: i0, type: HttpserviceModule });
939
+ HttpserviceModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "13.4.0", ngImport: i0, type: HttpserviceModule });
940
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.4.0", ngImport: i0, type: HttpserviceModule, decorators: [{
932
941
  type: NgModule
933
942
  }] });
934
943