@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.
- package/esm2020/lib/httpservice.module.mjs +4 -4
- package/esm2020/lib/httpservice.service.mjs +96 -87
- package/fesm2015/esfaenza-httpservice.mjs +99 -90
- package/fesm2015/esfaenza-httpservice.mjs.map +1 -1
- package/fesm2020/esfaenza-httpservice.mjs +99 -90
- package/fesm2020/esfaenza-httpservice.mjs.map +1 -1
- package/lib/httpservice.service.d.ts +6 -4
- package/package.json +8 -7
|
@@ -3,9 +3,10 @@ import * as i2 from '@angular/common/http';
|
|
|
3
3
|
import { HttpEventType, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
|
|
4
4
|
import * as i0 from '@angular/core';
|
|
5
5
|
import { InjectionToken, Injectable, Inject, Optional, NgModule } from '@angular/core';
|
|
6
|
-
import { delay, finalize, map, catchError, scan } from 'rxjs/operators';
|
|
7
|
-
import { forkJoin, of, throwError, Observable } from 'rxjs';
|
|
6
|
+
import { concatMap, delay, finalize, map, catchError, scan } from 'rxjs/operators';
|
|
7
|
+
import { forkJoin, from, of, throwError, Observable } from 'rxjs';
|
|
8
8
|
import * as i3 from '@esfaenza/preferences';
|
|
9
|
+
import * as i4 from '@esfaenza/extensions';
|
|
9
10
|
|
|
10
11
|
/**
|
|
11
12
|
* 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
|
var _a, _b;
|
|
166
167
|
this.BACKEND_URL = BACKEND_URL;
|
|
167
168
|
this.EXTENSION_CLASS = EXTENSION_CLASS;
|
|
@@ -173,6 +174,7 @@ class HTTPService {
|
|
|
173
174
|
this.DEBUG_MODE = DEBUG_MODE;
|
|
174
175
|
this.http = http;
|
|
175
176
|
this.localStorage = localStorage;
|
|
177
|
+
this.utisExts = utisExts;
|
|
176
178
|
/** Cache dello stato delle chiamate HTTP attualmente effettuate. Ogni chiamata è rappresentata da un GUID e lo status assume valore **false** solo sugli errori */
|
|
177
179
|
this.callStatus = {};
|
|
178
180
|
/** Nome della cache in cui vengono storicizzate le informazioni sulle risposte delle chiamate sotto forma di dictionary */
|
|
@@ -232,25 +234,27 @@ class HTTPService {
|
|
|
232
234
|
* @returns {Observable<T>} Observable che contiene il risultato della chiamata
|
|
233
235
|
*/
|
|
234
236
|
get(url, params = null, transaction = "", succesfulResultMandatory = true, errorCallback = null) {
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
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(responseItem, cachedInfos);
|
|
246
|
-
this.log(this.loc("Cached result for") + " " + url);
|
|
237
|
+
// TODO provare e testare
|
|
238
|
+
return from([this.defaultStartCall(url)]).pipe(concatMap((myGuid) => {
|
|
239
|
+
let cachedInfos = this.tryGetCacheInformations(url, params);
|
|
240
|
+
if (cachedInfos.cache && cachedInfos.cacheItem) {
|
|
241
|
+
this.log(this.loc("Result for") + " " + url + " " + this.loc("retrieved from cache"));
|
|
242
|
+
return of(cachedInfos.cacheItem).pipe(delay(0), finalize(() => { this.tryFinalizeByGuid(myGuid); }));
|
|
247
243
|
}
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
244
|
+
var httpCall = this.http.get(cachedInfos.finalUrl, { headers: this.getHeaders(transaction), params: params });
|
|
245
|
+
return httpCall.pipe(map((res) => {
|
|
246
|
+
let responseItem = res;
|
|
247
|
+
if (cachedInfos.cache) {
|
|
248
|
+
this.cacheResponse(this.utisExts.deepClone(responseItem), cachedInfos);
|
|
249
|
+
this.log(this.loc("Cached result for") + " " + url);
|
|
250
|
+
}
|
|
251
|
+
return responseItem;
|
|
252
|
+
}), catchError((err, caught) => {
|
|
253
|
+
if (errorCallback)
|
|
254
|
+
errorCallback();
|
|
255
|
+
return !succesfulResultMandatory ? throwError(err) : this.registerErrorAndReturnEmptyObservable(err, myGuid);
|
|
256
|
+
}), finalize(() => { this.tryFinalizeByGuid(myGuid, cachedInfos); }));
|
|
257
|
+
}));
|
|
254
258
|
}
|
|
255
259
|
/**
|
|
256
260
|
* Esecuzione di una GET HTTP con eventualmente la gestione della cache qualora la chiamata termini con la convenzione "#cache"
|
|
@@ -268,24 +272,25 @@ class HTTPService {
|
|
|
268
272
|
* @returns {Observable<Blob | ArrayBuffer>} Observable che contiene il risultato della chiamata
|
|
269
273
|
*/
|
|
270
274
|
getRaw(url, params = null, type = "arraybuffer", transaction = "", succesfulResultMandatory = true, errorCallback = null) {
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
}
|
|
277
|
-
var httpCall = this.http.get(cachedInfos.finalUrl, { responseType: type, headers: this.getHeaders(transaction), params: params });
|
|
278
|
-
return httpCall.pipe(map((blob) => {
|
|
279
|
-
if (cachedInfos.cache) {
|
|
280
|
-
this.cacheResponse(blob, cachedInfos);
|
|
281
|
-
this.log(this.loc("Cached result for") + " " + url);
|
|
275
|
+
return from([this.defaultStartCall(url)]).pipe(concatMap((myGuid) => {
|
|
276
|
+
let cachedInfos = this.tryGetCacheInformations(url, params);
|
|
277
|
+
if (cachedInfos.cache && cachedInfos.cacheItem) {
|
|
278
|
+
this.log(this.loc("Result for") + " " + url + " " + this.loc("retrieved from cache"));
|
|
279
|
+
return of(cachedInfos.cacheItem).pipe(delay(0), finalize(() => { this.tryFinalizeByGuid(myGuid); }));
|
|
282
280
|
}
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
281
|
+
var httpCall = this.http.get(cachedInfos.finalUrl, { responseType: type, headers: this.getHeaders(transaction), params: params });
|
|
282
|
+
return httpCall.pipe(map((blob) => {
|
|
283
|
+
if (cachedInfos.cache) {
|
|
284
|
+
this.cacheResponse(blob, cachedInfos);
|
|
285
|
+
this.log(this.loc("Cached result for") + " " + url);
|
|
286
|
+
}
|
|
287
|
+
return blob;
|
|
288
|
+
}), catchError((err, caught) => {
|
|
289
|
+
if (errorCallback)
|
|
290
|
+
errorCallback();
|
|
291
|
+
return !succesfulResultMandatory ? throwError(err) : this.registerErrorAndReturnEmptyObservable(err, myGuid);
|
|
292
|
+
}), finalize(() => { this.tryFinalizeByGuid(myGuid, cachedInfos); }));
|
|
293
|
+
}));
|
|
289
294
|
}
|
|
290
295
|
/**
|
|
291
296
|
* Esecuzione di una POST HTTP
|
|
@@ -300,14 +305,12 @@ class HTTPService {
|
|
|
300
305
|
* @returns {Observable<T>} Observable che contiene il risultato della chiamata
|
|
301
306
|
*/
|
|
302
307
|
post(url, bodyData, params = null, transaction = "", succesfulResultMandatory = true) {
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
})
|
|
309
|
-
return !succesfulResultMandatory ? throwError(err) : this.registerErrorAndReturnEmptyObservable(err, myGuid);
|
|
310
|
-
}), finalize(() => { this.tryFinalizeByGuid(myGuid); }));
|
|
308
|
+
return from([this.defaultStartCall(url)]).pipe(concatMap((myGuid) => {
|
|
309
|
+
var httpCall = this.http.post(url, bodyData, { headers: this.getHeaders(transaction), params: params });
|
|
310
|
+
return httpCall.pipe(catchError((err, caught) => {
|
|
311
|
+
return !succesfulResultMandatory ? throwError(err) : this.registerErrorAndReturnEmptyObservable(err, myGuid);
|
|
312
|
+
}), finalize(() => { this.tryFinalizeByGuid(myGuid); }));
|
|
313
|
+
}));
|
|
311
314
|
}
|
|
312
315
|
/**
|
|
313
316
|
* Esecuzione di una DELETE HTTP
|
|
@@ -321,14 +324,12 @@ class HTTPService {
|
|
|
321
324
|
* @returns {Observable<T>} Observable che contiene il risultato della chiamata
|
|
322
325
|
*/
|
|
323
326
|
delete(url, params = null, transaction = "", succesfulResultMandatory = true) {
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
})
|
|
330
|
-
return !succesfulResultMandatory ? throwError(err) : this.registerErrorAndReturnEmptyObservable(err, myGuid);
|
|
331
|
-
}), finalize(() => { this.tryFinalizeByGuid(myGuid); }));
|
|
327
|
+
return from([this.defaultStartCall(url)]).pipe(concatMap((myGuid) => {
|
|
328
|
+
var httpCall = this.http.delete(url, { headers: this.getHeaders(transaction), params: params });
|
|
329
|
+
return httpCall.pipe(catchError((err, caught) => {
|
|
330
|
+
return !succesfulResultMandatory ? throwError(err) : this.registerErrorAndReturnEmptyObservable(err, myGuid);
|
|
331
|
+
}), finalize(() => { this.tryFinalizeByGuid(myGuid); }));
|
|
332
|
+
}));
|
|
332
333
|
}
|
|
333
334
|
/**
|
|
334
335
|
* Esecuzione di una PUT HTTP
|
|
@@ -343,14 +344,12 @@ class HTTPService {
|
|
|
343
344
|
* @returns {Observable<T>} Observable che contiene il risultato della chiamata
|
|
344
345
|
*/
|
|
345
346
|
put(url, bodyData, params = null, transaction = "", succesfulResultMandatory = true) {
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
})
|
|
352
|
-
return !succesfulResultMandatory ? throwError(err) : this.registerErrorAndReturnEmptyObservable(err, myGuid);
|
|
353
|
-
}), finalize(() => { this.tryFinalizeByGuid(myGuid); }));
|
|
347
|
+
return from([this.defaultStartCall(url)]).pipe(concatMap((myGuid) => {
|
|
348
|
+
var httpCall = this.http.put(url, bodyData, { headers: this.getHeaders(transaction), params: params });
|
|
349
|
+
return httpCall.pipe(catchError((err, caught) => {
|
|
350
|
+
return !succesfulResultMandatory ? throwError(err) : this.registerErrorAndReturnEmptyObservable(err, myGuid);
|
|
351
|
+
}), finalize(() => { this.tryFinalizeByGuid(myGuid); }));
|
|
352
|
+
}));
|
|
354
353
|
}
|
|
355
354
|
/**
|
|
356
355
|
* Esecuzione di una POST HTTP
|
|
@@ -363,12 +362,13 @@ class HTTPService {
|
|
|
363
362
|
*
|
|
364
363
|
* @returns {Observable<T>} Observable che contiene il risultato della chiamata
|
|
365
364
|
*/
|
|
366
|
-
postwithFiles(url, bodyData, fileArrayOrFile) {
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
365
|
+
postwithFiles(url, bodyData, fileArrayOrFile, params = null) {
|
|
366
|
+
return from([this.defaultStartCall(url)]).pipe(concatMap((myGuid) => {
|
|
367
|
+
var formData = this.getFilesFormData(fileArrayOrFile);
|
|
368
|
+
formData.append("body", JSON.stringify(bodyData));
|
|
369
|
+
var httpCall = this.http.post(url, formData, { params: params, headers: this.getHeaders(null, true, null, null) });
|
|
370
|
+
return httpCall.pipe(catchError((err, caught) => { return this.registerErrorAndReturnEmptyObservable(err, myGuid); }), finalize(() => { this.tryFinalizeByGuid(myGuid); }));
|
|
371
|
+
}));
|
|
372
372
|
}
|
|
373
373
|
/**
|
|
374
374
|
* Esecuzione di una POST HTTP
|
|
@@ -433,7 +433,12 @@ class HTTPService {
|
|
|
433
433
|
* @param {HttpParams} params Parametri della chiamata
|
|
434
434
|
*/
|
|
435
435
|
getFileNative(url, params = null) {
|
|
436
|
-
|
|
436
|
+
// Encodo tutti i parametri per evitare casi particolari
|
|
437
|
+
// var encodedParams = new HttpParams();
|
|
438
|
+
// let parKeys = params.keys();
|
|
439
|
+
// for (let i = 0; i < parKeys.length; i++)
|
|
440
|
+
// encodedParams = encodedParams.set(parKeys[i], encodeURIComponent(params.get(parKeys[i])));
|
|
441
|
+
let finalUrl = url + (params ? "?" + params.toString().replace(/\+/g, '%2b') : "");
|
|
437
442
|
window.location.href = finalUrl;
|
|
438
443
|
}
|
|
439
444
|
/**
|
|
@@ -532,17 +537,20 @@ class HTTPService {
|
|
|
532
537
|
*/
|
|
533
538
|
tryFinalizeByGuid(guid, cacheInfos) {
|
|
534
539
|
return __awaiter(this, void 0, void 0, function* () {
|
|
535
|
-
this.log(this.loc("Finalizing call") + " " + guid + " " + this.loc("with status") + ": " + (this.callStatus[guid].success ? this.loc('Success') : this.loc('Failure')));
|
|
536
|
-
//
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
this.LastThreeCompletedCalls.
|
|
544
|
-
|
|
545
|
-
|
|
540
|
+
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')));
|
|
541
|
+
// TODO: Perdere 3-4 ore per capire perché ogni tanto arriva null in casi pesanti di Observable spammati a random
|
|
542
|
+
if (this.callStatus[guid]) {
|
|
543
|
+
// Alla prima chiamata buona, significa che almeno il login ce l'ho. Posso mettermi in modalità redirectInProgress false
|
|
544
|
+
// e riiniziare a valutare errori di javashit
|
|
545
|
+
if (this.callStatus[guid].success && this.ExtensionsImpl.readRedirectionStatus())
|
|
546
|
+
this.ExtensionsImpl.writeRedirectionStatus(false);
|
|
547
|
+
// Aggiungo la chiamata alla lista delle ultime 3 completate
|
|
548
|
+
this.LastThreeCompletedCalls.unshift(this.callStatus[guid].url);
|
|
549
|
+
if (this.LastThreeCompletedCalls.length > 3)
|
|
550
|
+
this.LastThreeCompletedCalls.pop();
|
|
551
|
+
// In ogni caso questa posso rimuoverla
|
|
552
|
+
delete this.callStatus[guid];
|
|
553
|
+
}
|
|
546
554
|
// Se vengo da una chiamata che ha aggiornato la cache a timer o globale marco il fatto che quella cache dev'essere aggiornata
|
|
547
555
|
if (cacheInfos && cacheInfos.cache) {
|
|
548
556
|
this.PersistTimedCacheASAP = this.PersistTimedCacheASAP || !!cacheInfos.cacheTime;
|
|
@@ -762,7 +770,7 @@ class HTTPService {
|
|
|
762
770
|
* @returns {CacheInfos} Oggetto contenente le informazioni sulla cache per l'oggetto specificato
|
|
763
771
|
*/
|
|
764
772
|
tryGetCacheInformations(url, params) {
|
|
765
|
-
let cachingKey = null, cacheTime = null,
|
|
773
|
+
let cachingKey = null, cacheTime = null, item = null;
|
|
766
774
|
let useCaching = !!(/#cache/gi.exec(url));
|
|
767
775
|
if (useCaching) {
|
|
768
776
|
let tmp = /#cache(:?-(\d+))/gi.exec(url);
|
|
@@ -782,7 +790,8 @@ class HTTPService {
|
|
|
782
790
|
item = cache[cachingKey];
|
|
783
791
|
}
|
|
784
792
|
}
|
|
785
|
-
|
|
793
|
+
// Per assicurarmi che il rifeirmento degli oggetti sia diverso utilizzo il deepClone
|
|
794
|
+
return new CacheInfos(url, useCaching, cachingKey, cacheTime, this.utisExts.deepClone(item));
|
|
786
795
|
}
|
|
787
796
|
/**
|
|
788
797
|
* Inserisce un oggetto nella cache appropriata in base alle informazioni contenute nel parametro **cacheInfos**
|
|
@@ -884,9 +893,9 @@ class HTTPService {
|
|
|
884
893
|
return Loc[message][this.LOCALE];
|
|
885
894
|
}
|
|
886
895
|
}
|
|
887
|
-
HTTPService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.
|
|
888
|
-
HTTPService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "13.
|
|
889
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.
|
|
896
|
+
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 });
|
|
897
|
+
HTTPService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "13.4.0", ngImport: i0, type: HTTPService });
|
|
898
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.4.0", ngImport: i0, type: HTTPService, decorators: [{
|
|
890
899
|
type: Injectable
|
|
891
900
|
}], ctorParameters: function () {
|
|
892
901
|
return [{ type: undefined, decorators: [{
|
|
@@ -922,7 +931,7 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.9", ngImpor
|
|
|
922
931
|
}, {
|
|
923
932
|
type: Inject,
|
|
924
933
|
args: [DEBUG_MODE]
|
|
925
|
-
}] }, { type: i2.HttpClient }, { type: i3.
|
|
934
|
+
}] }, { type: i2.HttpClient }, { type: i3.CacheService }, { type: i4.UtilityService }];
|
|
926
935
|
} });
|
|
927
936
|
|
|
928
937
|
class HttpserviceModule {
|
|
@@ -943,10 +952,10 @@ class HttpserviceModule {
|
|
|
943
952
|
};
|
|
944
953
|
}
|
|
945
954
|
}
|
|
946
|
-
HttpserviceModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.
|
|
947
|
-
HttpserviceModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "13.
|
|
948
|
-
HttpserviceModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "13.
|
|
949
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.
|
|
955
|
+
HttpserviceModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.4.0", ngImport: i0, type: HttpserviceModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
|
|
956
|
+
HttpserviceModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "13.4.0", ngImport: i0, type: HttpserviceModule });
|
|
957
|
+
HttpserviceModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "13.4.0", ngImport: i0, type: HttpserviceModule });
|
|
958
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.4.0", ngImport: i0, type: HttpserviceModule, decorators: [{
|
|
950
959
|
type: NgModule
|
|
951
960
|
}] });
|
|
952
961
|
|