@supersoniks/concorde 1.1.43 → 1.1.45

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.
Files changed (100) hide show
  1. package/README.md +9 -7
  2. package/concorde-core.bundle.js +20 -20
  3. package/concorde-core.es.js +987 -804
  4. package/core/_types/types.d.ts +13 -8
  5. package/core/_types/types.js +1 -0
  6. package/core/components/functional/date/date.d.ts +2 -2
  7. package/core/components/functional/date/date.js +10 -10
  8. package/core/components/functional/example/example.d.ts +1 -1
  9. package/core/components/functional/fetch/fetch.d.ts +8 -6
  10. package/core/components/functional/list/list.d.ts +16 -8
  11. package/core/components/functional/list/list.js +29 -14
  12. package/core/components/functional/mix/mix.d.ts +5 -5
  13. package/core/components/functional/mix/mix.js +11 -8
  14. package/core/components/functional/queue/queue.d.ts +13 -4
  15. package/core/components/functional/queue/queue.js +33 -27
  16. package/core/components/functional/router/redirect.d.ts +2 -2
  17. package/core/components/functional/router/redirect.js +2 -6
  18. package/core/components/functional/router/router.d.ts +1 -1
  19. package/core/components/functional/router/router.js +22 -13
  20. package/core/components/functional/sdui/SDUIDescriptorTransformer.d.ts +0 -1
  21. package/core/components/functional/sdui/SDUIDescriptorTransformer.js +35 -36
  22. package/core/components/functional/sdui/sdui-utils.js +11 -11
  23. package/core/components/functional/sdui/sdui.d.ts +11 -10
  24. package/core/components/functional/sdui/sdui.js +33 -34
  25. package/core/components/functional/sdui/types.d.ts +8 -8
  26. package/core/components/functional/states/states.d.ts +3 -3
  27. package/core/components/functional/states/states.js +9 -8
  28. package/core/components/functional/submit/submit.d.ts +1 -1
  29. package/core/components/functional/submit/submit.js +16 -16
  30. package/core/components/functional/subscriber/subscriber.d.ts +1 -1
  31. package/core/components/functional/value/value.d.ts +1 -1
  32. package/core/components/functional/value/value.js +3 -2
  33. package/core/components/ui/_css/size.d.ts +1 -1
  34. package/core/components/ui/button/button.d.ts +6 -4
  35. package/core/components/ui/button/button.js +34 -30
  36. package/core/components/ui/captcha/captcha.d.ts +2 -2
  37. package/core/components/ui/captcha/captcha.js +2 -2
  38. package/core/components/ui/card/card-header.js +5 -1
  39. package/core/components/ui/card/card.js +7 -5
  40. package/core/components/ui/form/checkbox/checkbox.d.ts +36 -12
  41. package/core/components/ui/form/fieldset/fieldset.d.ts +1 -1
  42. package/core/components/ui/form/form-layout/form-layout.d.ts +1 -1
  43. package/core/components/ui/form/input/input.d.ts +14 -9
  44. package/core/components/ui/form/input/input.js +2 -2
  45. package/core/components/ui/form/input/password-helper.d.ts +1 -1
  46. package/core/components/ui/form/input/password-helper.js +1 -1
  47. package/core/components/ui/form/input/same-value-helper.d.ts +1 -1
  48. package/core/components/ui/form/input/same-value-helper.js +2 -2
  49. package/core/components/ui/form/input-autocomplete/input-autocomplete.d.ts +1 -1
  50. package/core/components/ui/form/input-autocomplete/input-autocomplete.js +1 -1
  51. package/core/components/ui/form/select/select.d.ts +5 -4
  52. package/core/components/ui/form/select/select.js +24 -12
  53. package/core/components/ui/form/textarea/textarea.d.ts +11 -9
  54. package/core/components/ui/form/textarea/textarea.js +1 -1
  55. package/core/components/ui/group/group.js +8 -2
  56. package/core/components/ui/icon/icons.js +15 -13
  57. package/core/components/ui/loader/loader.d.ts +1 -1
  58. package/core/components/ui/loader/loader.js +1 -1
  59. package/core/components/ui/menu/menu.js +15 -16
  60. package/core/components/ui/modal/modal.d.ts +1 -1
  61. package/core/components/ui/modal/modal.js +1 -1
  62. package/core/components/ui/pop/pop.d.ts +6 -5
  63. package/core/components/ui/pop/pop.js +76 -46
  64. package/core/components/ui/table/table-tbody.js +1 -1
  65. package/core/components/ui/theme/theme.js +15 -13
  66. package/core/components/ui/toast/message-subscriber.d.ts +12 -9
  67. package/core/components/ui/toast/message-subscriber.js +10 -8
  68. package/core/components/ui/toast/toast.d.ts +1 -1
  69. package/core/components/ui/toast/toast.js +7 -8
  70. package/core/components/ui/toast/types.d.ts +3 -2
  71. package/core/components/ui/tooltip/tooltip.js +4 -4
  72. package/core/components/ui/ui.d.ts +1 -1
  73. package/core/components/ui/ui.js +1 -1
  74. package/core/mixins/Fetcher.d.ts +15 -17
  75. package/core/mixins/Fetcher.js +19 -10
  76. package/core/mixins/FormCheckable.d.ts +8 -5
  77. package/core/mixins/FormCheckable.js +21 -19
  78. package/core/mixins/FormElement.d.ts +10 -7
  79. package/core/mixins/FormElement.js +24 -23
  80. package/core/mixins/FormInput.d.ts +16 -10
  81. package/core/mixins/FormInput.js +1 -1
  82. package/core/mixins/Subscriber.d.ts +8 -6
  83. package/core/mixins/Subscriber.js +24 -23
  84. package/core/mixins/TemplatesContainer.d.ts +2 -1
  85. package/core/mixins/TemplatesContainer.js +2 -2
  86. package/core/utils/Arrays.js +12 -9
  87. package/core/utils/DataBindObserver.d.ts +4 -4
  88. package/core/utils/DataBindObserver.js +26 -28
  89. package/core/utils/HTML.d.ts +1 -1
  90. package/core/utils/HTML.js +2 -2
  91. package/core/utils/LocationHandler.js +10 -10
  92. package/core/utils/Objects.js +10 -4
  93. package/core/utils/PublisherProxy.d.ts +21 -15
  94. package/core/utils/PublisherProxy.js +67 -63
  95. package/core/utils/api.d.ts +40 -32
  96. package/core/utils/api.js +127 -32
  97. package/mixins.d.ts +25 -19
  98. package/package.json +11 -6
  99. package/core/components/ui/taxonomy/taxonomy.d.ts +0 -44
  100. package/core/components/ui/taxonomy/taxonomy.js +0 -115
@@ -1,3 +1,4 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any*/
1
2
  function isComplex(value) {
2
3
  return typeof value === "object" && value != null;
3
4
  }
@@ -14,9 +15,9 @@ export class PublisherProxy {
14
15
  this._fillListeners_ = new Set();
15
16
  this._templateFillListeners_ = new Set();
16
17
  this._lockInternalMutationPublishing_ = false;
17
- this.root = this;
18
18
  this._value_ = target;
19
19
  this.parent = parentProxPub || null;
20
+ this.root = this;
20
21
  while (this.root.parent) {
21
22
  this.root = this.root.parent;
22
23
  }
@@ -26,7 +27,7 @@ export class PublisherProxy {
26
27
  * Supprime les écouteurs associés
27
28
  */
28
29
  delete() {
29
- for (let proxy of this._proxies_.values()) {
30
+ for (const proxy of this._proxies_.values()) {
30
31
  proxy.delete();
31
32
  }
32
33
  this._invalidateListeners_.clear();
@@ -70,7 +71,7 @@ export class PublisherProxy {
70
71
  }
71
72
  _publishTemplateFilling_(key, value) {
72
73
  this._templateFillListeners_.forEach((handler) => {
73
- let desc = Object.getOwnPropertyDescriptor(handler, key);
74
+ const desc = Object.getOwnPropertyDescriptor(handler, key);
74
75
  if (desc && !desc.set && !desc.writable)
75
76
  return;
76
77
  if (handler.propertyMap && handler.propertyMap[key]) {
@@ -143,13 +144,16 @@ export class PublisherProxy {
143
144
  */
144
145
  startTemplateFilling(handler) {
145
146
  this._templateFillListeners_.add(handler);
146
- for (var z in this._value_) {
147
+ if (typeof this._value_ != "object")
148
+ return;
149
+ for (const z in this._value_) {
150
+ let valueKey = z;
147
151
  const value = this._value_[z];
148
152
  if (handler.propertyMap && handler.propertyMap[z]) {
149
- z = handler.propertyMap[z];
153
+ valueKey = handler.propertyMap[z];
150
154
  }
151
155
  if (typeof handler[z] != "undefined" && handler[z] !== value) {
152
- handler[z] = value;
156
+ handler[valueKey] = value;
153
157
  }
154
158
  }
155
159
  }
@@ -165,7 +169,7 @@ export class PublisherProxy {
165
169
  */
166
170
  startDynamicFilling(handler) {
167
171
  this._fillListeners_.add(handler);
168
- for (var z in this._value_) {
172
+ for (const z in this._value_) {
169
173
  const value = this._value_[z];
170
174
  if (handler[z] !== value)
171
175
  handler[z] = value;
@@ -187,9 +191,9 @@ export class PublisherProxy {
187
191
  */
188
192
  if (this._value_ === newValue)
189
193
  return true;
190
- if ("hasOwnProperty" in this._value_ &&
191
- this._value_.hasOwnProperty("__value") &&
192
- newValue.hasOwnProperty("__value") &&
194
+ if (this._value_ &&
195
+ Object.prototype.hasOwnProperty.call(this._value_, "__value") &&
196
+ Object.prototype.hasOwnProperty.call(newValue, "__value") &&
193
197
  this._value_.__value === newValue.__value) {
194
198
  return true;
195
199
  }
@@ -202,7 +206,7 @@ export class PublisherProxy {
202
206
  * Si il s'agit d'une valeur primitive (un entier, une chaine ) la valeure en renseignée par un objet contenant la vaeur {__value}
203
207
  * On publie juste et on sen va.
204
208
  */
205
- const isPrimitiveValue = this._value_.hasOwnProperty("__value");
209
+ const isPrimitiveValue = Object.prototype.hasOwnProperty.call(this._value_, "__value");
206
210
  if (isPrimitiveValue) {
207
211
  this._publishAssignement_(lockInternalMutationsTransmission);
208
212
  return true;
@@ -213,7 +217,7 @@ export class PublisherProxy {
213
217
  * On met à jour leur valeur interne
214
218
  * On publie les maj au fur et a mesure de modifications
215
219
  */
216
- for (let key in this._value_) {
220
+ for (const key in this._value_) {
217
221
  if (typeof this._value_[key] === "undefined")
218
222
  delete this._value_[key];
219
223
  }
@@ -244,12 +248,12 @@ export class PublisherProxy {
244
248
  * On renseigne les nouvelles valeurs internes des proxies
245
249
  */
246
250
  if (isComplex(this._value_)) {
247
- for (let key in this._value_) {
248
- let v = newValue[key];
249
- let isVComplex = isComplex(v);
250
- let valueV = isVComplex ? v : { __value: v };
251
+ for (const key in this._value_) {
252
+ const v = newValue[key];
253
+ const isVComplex = isComplex(v);
254
+ const valueV = isVComplex ? v : { __value: v };
251
255
  if (!this._proxies_.has(key)) {
252
- let newPublisher = new Publisher({}, this);
256
+ const newPublisher = new Publisher({}, this);
253
257
  this._proxies_.set(key, newPublisher);
254
258
  newPublisher._proxies_.set("_parent_", this);
255
259
  }
@@ -263,9 +267,9 @@ export class PublisherProxy {
263
267
  * Extraire la valeur actuelle du proxy
264
268
  */
265
269
  get() {
266
- if (this._value_.hasOwnProperty("__value")) {
267
- let v = this._value_.__value;
268
- return v != undefined ? v : null;
270
+ if (Object.prototype.hasOwnProperty.call(this._value_, "__value")) {
271
+ const v = this._value_.__value;
272
+ return (v != undefined ? v : null);
269
273
  }
270
274
  return this._value_;
271
275
  }
@@ -336,15 +340,13 @@ PublisherManager.instance = null;
336
340
  export default class Publisher extends PublisherProxy {
337
341
  constructor(target, parentProxPub = null) {
338
342
  super(target, parentProxPub);
339
- let that = this;
340
- let thisProxy = new Proxy(this, {
343
+ const thisProxy = new Proxy(this, {
341
344
  /**
342
345
  * Lorsque l'on écrit monConteneur = publisher.maClef ou monConteneur = publisher["maClef"] monConteneur contient :
343
346
  * Les methodes de PublisherProxy (onAssign... : voir liste dans kle tableaus si dessous), si la clef est une méthode de PublisherProxy,,
344
347
  * Sinon un autre proxy qui a comme valeur interne la valeur corespondante à la clef dans l'objet.
345
348
  */
346
- get: function (oTarget, sKey) {
347
- oTarget;
349
+ get: function (publisherInstance, sKey) {
348
350
  if ([
349
351
  "invalidate",
350
352
  "onInvalidate",
@@ -376,79 +378,81 @@ export default class Publisher extends PublisherProxy {
376
378
  "_value_",
377
379
  "_lockInternalMutationPublishing_",
378
380
  ].includes(sKey))
379
- return that[sKey];
380
- if (!that._proxies_.has(sKey)) {
381
- let vValue = that._value_[sKey];
382
- let newPublisher = new Publisher(isComplex(vValue) ? vValue : { __value: vValue }, that);
381
+ return publisherInstance[sKey];
382
+ if (!publisherInstance._proxies_.has(sKey)) {
383
+ const vValue = publisherInstance._value_[sKey];
384
+ const newPublisher = new Publisher(isComplex(vValue) ? vValue : { __value: vValue }, publisherInstance);
383
385
  newPublisher._proxies_.set("_parent_", thisProxy);
384
- that._proxies_.set(sKey, newPublisher);
386
+ publisherInstance._proxies_.set(sKey, newPublisher);
385
387
  }
386
- return that._proxies_.get(sKey);
388
+ return publisherInstance._proxies_.get(sKey);
387
389
  },
388
390
  /**
389
391
  * Lorsque l'on écrit publisher.maClef = value ou publisher["maClef"] = value, on assigne la valeur à la clef dans l'objet interne.
390
392
  * Les gestionnairs associés sopnt déclenchés en conséquence de manière profonde et remontante si nécessaire.
391
393
  */
392
- set: function (oTarget, sKey, vValue) {
394
+ set: function (publisherInstance, sKey, vValue) {
393
395
  var _a;
394
396
  //Fonctionnement pour la donnée interne pas de dispatch;
395
397
  if (sKey == "_value_") {
396
- oTarget._value_ = vValue;
397
- return oTarget._value_;
398
+ publisherInstance._value_ = vValue;
399
+ // return publisherInstance._value_;
400
+ return true;
398
401
  }
399
402
  //Création du publisher si il n'existe pas
400
- if (!that._proxies_.has(sKey)) {
401
- let newPublisher = new Publisher({}, that);
403
+ if (!publisherInstance._proxies_.has(sKey)) {
404
+ const newPublisher = new Publisher({}, publisherInstance);
402
405
  newPublisher._proxies_.set("_parent_", thisProxy);
403
- that._proxies_.set(sKey, newPublisher);
406
+ publisherInstance._proxies_.set(sKey, newPublisher);
404
407
  }
405
408
  //mis à jour et publication de la donnée si elle a changé
406
- const prevValue = that._value_[sKey];
409
+ const prevValue = publisherInstance._value_[sKey];
407
410
  if (prevValue !== vValue) {
408
- that._value_[sKey] = vValue;
409
- that._publishDynamicFilling_(sKey, vValue);
410
- (_a = that._proxies_.get(sKey)) === null || _a === void 0 ? void 0 : _a.set(isComplex(vValue) ? vValue : { __value: vValue });
411
+ publisherInstance._value_[sKey] = vValue;
412
+ publisherInstance._publishDynamicFilling_(sKey, vValue);
413
+ (_a = publisherInstance._proxies_.get(sKey)) === null || _a === void 0 ? void 0 : _a.set(isComplex(vValue) ? vValue : { __value: vValue });
411
414
  }
412
415
  //on retourne le proxy pour pouvoir chainer les assignements
413
- return that._proxies_.get(sKey);
416
+ // return publisherInstance._proxies_.get(sKey);
417
+ return true;
414
418
  },
415
419
  /**
416
420
  * Autres propriétés classiques d'un objet implémentées par le proxy
417
421
  */
418
- deleteProperty: function (oTarget, sKey) {
419
- oTarget;
420
- // that._proxies_.get(sKey).set(null);
421
- that._publishDynamicFilling_(sKey, null);
422
- that._proxies_.delete(sKey);
423
- return delete that._value_[sKey];
422
+ deleteProperty: function (publisherInstance, sKey) {
423
+ publisherInstance;
424
+ // publisherInstance._proxies_.get(sKey).set(null);
425
+ publisherInstance._publishDynamicFilling_(sKey, null);
426
+ publisherInstance._proxies_.delete(sKey);
427
+ return delete publisherInstance._value_[sKey];
424
428
  },
425
- // enumerate: function (oTarget, sKey): any {
426
- // return that._value_.keys();
429
+ // enumerate: function (publisherInstance, sKey): CoreJSType {
430
+ // return publisherInstance._value_.keys();
427
431
  // },
428
- has: function (oTarget, sKey) {
429
- oTarget;
430
- return sKey in that._value_ && sKey != "_lockInternalMutationPublishing_";
432
+ has: function (publisherInstance, sKey) {
433
+ publisherInstance;
434
+ return sKey in publisherInstance._value_ && sKey != "_lockInternalMutationPublishing_";
431
435
  },
432
- defineProperty: function (oTarget, sKey, oDesc) {
433
- oTarget;
436
+ defineProperty: function (publisherInstance, sKey, oDesc) {
437
+ publisherInstance;
434
438
  if (oDesc && "value" in oDesc) {
435
- that._value_[sKey] = oDesc.value;
439
+ publisherInstance._value_[sKey] = oDesc.value;
436
440
  }
437
- return that._value_;
441
+ return true;
442
+ // return publisherInstance._value_;
438
443
  },
439
- getOwnPropertyDescriptor: function (oTarget, sKey) {
440
- oTarget;
444
+ getOwnPropertyDescriptor: function (publisherInstance, sKey) {
445
+ publisherInstance;
441
446
  sKey;
442
447
  return {
443
448
  enumerable: true,
444
449
  configurable: true,
445
450
  };
446
451
  },
447
- ownKeys: function (target) {
448
- target;
449
- if (that._value_.__value)
450
- return Object.keys(that._value_.__value);
451
- return Object.keys(that._value_);
452
+ ownKeys: function (publisherInstance) {
453
+ if (publisherInstance._value_.__value)
454
+ return Object.keys(publisherInstance._value_.__value);
455
+ return Object.keys(publisherInstance._value_);
452
456
  },
453
457
  });
454
458
  return thisProxy;
@@ -1,19 +1,30 @@
1
- export declare type APIConfiguration = {
1
+ import { CoreJSType } from "@supersoniks/concorde/core/_types/types";
2
+ export type APIConfiguration = {
2
3
  serviceURL: string | null;
3
4
  token: string | null;
4
5
  userName: string | null;
5
6
  password: string | null;
7
+ authToken: string | null;
6
8
  tokenProvider: string | null;
7
9
  addHTTPResponse?: boolean;
8
10
  credentials?: RequestCredentials;
9
11
  };
10
- declare class API<ResultType = any & {
12
+ export type ResultTypeInterface = CoreJSType & {
11
13
  _sonic_http_response_?: Response;
12
- }, SendType = any> {
14
+ text?: string;
15
+ };
16
+ export type APICall = {
17
+ apiMethod: "get" | "send" | "submitFormData";
18
+ path: string;
19
+ additionalHeaders: HeadersInit | undefined;
20
+ method?: string | undefined;
21
+ data?: unknown;
22
+ };
23
+ declare class API {
13
24
  /**
14
25
  * Ce tableau static permet de ne pas appeler plusieurs fois le même service lors d'appel concurrents en GET.
15
26
  */
16
- static loadingGetPromises: Map<string, Promise<any>>;
27
+ static loadingGetPromises: Map<string, Promise<unknown>>;
17
28
  /**
18
29
  * L'url de base du service sans endpoint
19
30
  */
@@ -29,11 +40,17 @@ declare class API<ResultType = any & {
29
40
  /**
30
41
  * le bearer token a passer pour les appels REST
31
42
  */
32
- token: string | null | undefined;
43
+ private _token;
44
+ set token(token: string | null | undefined);
45
+ get token(): string | null | undefined;
33
46
  /**
34
47
  * Le endPoint pour obtenir le bearer token qui sera concaténé à l'url du service
35
48
  */
36
49
  tokenProvider: string | null;
50
+ /**
51
+ * le bearer token à passer pour un éventuel renouvellement de token automatique
52
+ */
53
+ authToken: string | null;
37
54
  /**
38
55
  * credentials
39
56
  */
@@ -42,21 +59,27 @@ declare class API<ResultType = any & {
42
59
  * Tableau static des tokens stokés en memoire vive (comportement à revoir à l'occasion)
43
60
  */
44
61
  static tokens: Map<string | null, string | null | undefined>;
62
+ /**
63
+ * Tableau stockant l'ensemble des tokens invalides
64
+ */
65
+ static invalidTokens: (string | null | undefined)[];
66
+ handleInvalidToken(token: string | null | undefined): void;
67
+ /**
68
+ * Tableau static des tentatives échouées de récupération auto du token
69
+ */
70
+ static failledTokenUpdates: Map<string | null, true>;
45
71
  /**
46
72
  * Le endPoint pour obtenir le bearer token qui sera concaténé à l'url du service
47
73
  */
48
- addHTTPResponse: Boolean;
74
+ addHTTPResponse: boolean;
49
75
  lastResult?: Response;
50
76
  constructor(config: APIConfiguration);
51
- handleResult(fetchResult: Response): Promise<ResultType & {
52
- _sonic_http_response_?: Response | undefined;
53
- text?: string | undefined;
54
- }>;
77
+ handleResult(fetchResult: Response, lastCall: APICall): Promise<ResultTypeInterface>;
55
78
  /**
56
79
  * Basic auth
57
80
  */
58
81
  auth(): Promise<void>;
59
- get(path: String, additionalHeaders?: HeadersInit): Promise<ResultType>;
82
+ get<T>(path: string, additionalHeaders?: HeadersInit): Promise<T & ResultTypeInterface>;
60
83
  /**
61
84
  * Création du header, avec authentification si besoin
62
85
  * ajout du language via le header accept-language qui contient le langue du navigateur
@@ -65,38 +88,23 @@ declare class API<ResultType = any & {
65
88
  /**
66
89
  * Concatène le serviceURL et le endpoint donné en paramètre
67
90
  */
68
- computeURL(path: String): string;
69
- send(path: String, data: SendType, method?: string, additionalHeaders?: HeadersInit): Promise<ResultType & {
70
- _sonic_http_response_?: Response | undefined;
71
- text?: string | undefined;
72
- }>;
91
+ computeURL(path: string, query?: Record<string, string>): string;
92
+ send<T, SendType = CoreJSType>(path: string, data: SendType, method?: string, additionalHeaders?: HeadersInit): Promise<T & ResultTypeInterface>;
73
93
  /**
74
94
  * Agit comme une soumission de formulaire, mais attends un json en réponse
75
95
  */
76
- submitFormData(path: String, data: SendType | Record<string, string | Blob>, method?: string, additionalHeaders?: HeadersInit): Promise<ResultType & {
77
- _sonic_http_response_?: Response | undefined;
78
- text?: string | undefined;
79
- }>;
96
+ submitFormData<T, SendType = CoreJSType>(path: string, data: SendType, method?: string, additionalHeaders?: HeadersInit): Promise<T & ResultTypeInterface>;
80
97
  /**
81
98
  * Appel send en utilisant le méthode PUT
82
99
  */
83
- put(path: String, data: SendType, additionalHeaders?: HeadersInit): Promise<ResultType & {
84
- _sonic_http_response_?: Response | undefined;
85
- text?: string | undefined;
86
- }>;
100
+ put<T, SendType = CoreJSType>(path: string, data: SendType, additionalHeaders?: HeadersInit): Promise<T & ResultTypeInterface>;
87
101
  /**
88
102
  * Appel send en utilisant le méthode POST
89
103
  */
90
- post(path: String, data: SendType, additionalHeaders?: HeadersInit): Promise<ResultType & {
91
- _sonic_http_response_?: Response | undefined;
92
- text?: string | undefined;
93
- }>;
104
+ post<T, SendType = CoreJSType>(path: string, data: SendType, additionalHeaders?: HeadersInit): Promise<T & ResultTypeInterface>;
94
105
  /**
95
106
  * Appel send en utilisant le méthode delete
96
107
  */
97
- delete(path: String, data: SendType, additionalHeaders?: HeadersInit): Promise<ResultType & {
98
- _sonic_http_response_?: Response | undefined;
99
- text?: string | undefined;
100
- }>;
108
+ delete<T, SendType = CoreJSType>(path: string, data: SendType, additionalHeaders?: HeadersInit): Promise<T & ResultTypeInterface>;
101
109
  }
102
110
  export default API;
package/core/utils/api.js CHANGED
@@ -7,9 +7,33 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
+ // on désactive une regle eslint pour ce fichier
11
+ /* eslint no-async-promise-executor: 0 */ // --> OFF
10
12
  import HTML from "@supersoniks/concorde/core/utils/HTML";
11
13
  import Objects from "@supersoniks/concorde/core/utils/Objects";
12
14
  class API {
15
+ set token(token) {
16
+ this._token = token;
17
+ if (!token) {
18
+ API.tokens.delete(this.serviceURL);
19
+ return;
20
+ }
21
+ if (API.invalidTokens.includes(token))
22
+ return;
23
+ API.tokens.set(this.serviceURL, token);
24
+ }
25
+ get token() {
26
+ // si le token est marqué invalide, on utilise utilise la dernière version valide connue du token pour ce serviceURL
27
+ return API.invalidTokens.includes(this._token) ? API.tokens.get(this.serviceURL) : this._token;
28
+ }
29
+ handleInvalidToken(token) {
30
+ if (!token)
31
+ return;
32
+ if (API.invalidTokens.includes(token))
33
+ return;
34
+ API.invalidTokens.push(token);
35
+ this.token = null;
36
+ }
13
37
  constructor(config) {
14
38
  /**
15
39
  * Le endPoint pour obtenir le bearer token qui sera concaténé à l'url du service
@@ -20,19 +44,22 @@ class API {
20
44
  this.serviceURL = document.location.origin;
21
45
  this.userName = config.userName;
22
46
  this.password = config.password;
23
- this.token = config.token;
47
+ if (config.token)
48
+ this.token = config.token;
24
49
  this.tokenProvider = config.tokenProvider;
50
+ this.authToken = config.authToken;
25
51
  this.addHTTPResponse = config.addHTTPResponse || false;
26
52
  this.credentials = config.credentials;
27
53
  }
28
- handleResult(fetchResult) {
54
+ handleResult(fetchResult, lastCall) {
29
55
  var _a;
30
56
  return __awaiter(this, void 0, void 0, function* () {
31
57
  this.lastResult = fetchResult;
32
58
  const contentType = (_a = fetchResult.headers.get("content-type")) === null || _a === void 0 ? void 0 : _a.toLowerCase();
59
+ const httpCode = fetchResult.status;
33
60
  let result = {};
34
- if (contentType && contentType.indexOf("text/") == 0) {
35
- let str = yield fetchResult.text();
61
+ if (!contentType || contentType.indexOf("text/") == 0) {
62
+ const str = yield fetchResult.text();
36
63
  result = { text: str };
37
64
  }
38
65
  else {
@@ -46,6 +73,15 @@ class API {
46
73
  if (this.addHTTPResponse && Objects.isObject(result)) {
47
74
  result._sonic_http_response_ = fetchResult;
48
75
  }
76
+ if (httpCode === 498 && !API.failledTokenUpdates.has(this.serviceURL)) {
77
+ this.handleInvalidToken(this.token);
78
+ if (lastCall.apiMethod === "get") {
79
+ result = yield this[lastCall.apiMethod](lastCall.path, lastCall.additionalHeaders);
80
+ }
81
+ else {
82
+ result = yield this[lastCall.apiMethod](lastCall.path, lastCall.data, lastCall.method, lastCall.additionalHeaders);
83
+ }
84
+ }
49
85
  return result;
50
86
  });
51
87
  }
@@ -60,37 +96,63 @@ class API {
60
96
  this.token = API.tokens.get(this.serviceURL);
61
97
  return;
62
98
  }
63
- if (!this.userName || !this.password || !this.tokenProvider)
99
+ if (!this.tokenProvider)
64
100
  return;
65
- let headers = {
66
- Authorization: "Basic " + window.btoa(unescape(encodeURIComponent(this.userName + ":" + this.password))),
67
- };
68
- let result = yield fetch(this.computeURL(this.tokenProvider), {
101
+ let headers = {};
102
+ if (this.userName && this.password) {
103
+ headers = {
104
+ Authorization: "Basic " + window.btoa(unescape(encodeURIComponent(this.userName + ":" + this.password))),
105
+ };
106
+ }
107
+ else if (this.authToken) {
108
+ headers = {
109
+ Authorization: "Bearer " + this.authToken,
110
+ };
111
+ }
112
+ const serviceURL = new URL(this.serviceURL);
113
+ const serviceHost = serviceURL.protocol + "//" + serviceURL.host;
114
+ const result = yield fetch(this.computeURL(this.tokenProvider, { serviceHost: serviceHost }), {
69
115
  headers: headers,
70
116
  credentials: this.credentials,
71
117
  });
72
118
  try {
73
- let json = yield result.json();
74
- this.token = json.token;
75
- API.tokens.set(this.serviceURL, this.token);
119
+ const json = yield result.json();
120
+ const newToken = json.token;
121
+ if (newToken) {
122
+ this.token = json.token;
123
+ }
124
+ else {
125
+ API.failledTokenUpdates.set(this.serviceURL, true);
126
+ }
127
+ }
128
+ catch (e) {
129
+ API.failledTokenUpdates.set(this.serviceURL, true);
76
130
  }
77
- catch (e) { }
78
131
  });
79
132
  }
80
133
  get(path, additionalHeaders) {
81
134
  return __awaiter(this, void 0, void 0, function* () {
82
- let headers = yield this.createHeaders(additionalHeaders);
135
+ const lastCall = {
136
+ apiMethod: "get",
137
+ path: path,
138
+ additionalHeaders: additionalHeaders,
139
+ };
140
+ const headers = yield this.createHeaders(additionalHeaders);
83
141
  const url = this.computeURL(path);
84
- if (!API.loadingGetPromises.has(url)) {
85
- let promise = new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
86
- let result = yield fetch(url, { headers: headers, credentials: this.credentials });
87
- let handledResult = yield this.handleResult(result);
142
+ const mapKey = JSON.stringify({
143
+ url: url,
144
+ headers: headers,
145
+ });
146
+ if (!API.loadingGetPromises.has(mapKey)) {
147
+ const promise = new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
148
+ const result = yield fetch(url, { headers: headers, credentials: this.credentials });
149
+ const handledResult = yield this.handleResult(result, lastCall);
88
150
  resolve(handledResult);
89
151
  }));
90
- API.loadingGetPromises.set(url, promise);
152
+ API.loadingGetPromises.set(mapKey, promise);
91
153
  }
92
- let result = yield API.loadingGetPromises.get(url);
93
- API.loadingGetPromises.delete(url);
154
+ const result = (yield API.loadingGetPromises.get(mapKey));
155
+ API.loadingGetPromises.delete(mapKey);
94
156
  return result;
95
157
  });
96
158
  }
@@ -101,7 +163,7 @@ class API {
101
163
  createHeaders(additionalHeaders) {
102
164
  return __awaiter(this, void 0, void 0, function* () {
103
165
  yield this.auth();
104
- let headers = {};
166
+ const headers = {};
105
167
  if (this.token)
106
168
  headers.Authorization = "Bearer " + this.token;
107
169
  headers["Accept-Language"] = HTML.getLanguage();
@@ -114,24 +176,42 @@ class API {
114
176
  /**
115
177
  * Concatène le serviceURL et le endpoint donné en paramètre
116
178
  */
117
- computeURL(path) {
118
- return (this.serviceURL + "/" + path).replace(/([^(https?\:)])\/{2,}/g, "$1/");
179
+ computeURL(path, query = {}) {
180
+ let url = "";
181
+ if (path.startsWith("http"))
182
+ url = path;
183
+ else
184
+ url = this.serviceURL + "/" + path;
185
+ if (!url.startsWith("http"))
186
+ url = window.location.origin + url;
187
+ const computedUrl = new URL(url);
188
+ for (const key in query) {
189
+ computedUrl.searchParams.set(key, query[key]);
190
+ }
191
+ return computedUrl.toString().replace(/([^(https?:)])\/{2,}/g, "$1/");
119
192
  }
120
193
  /*
121
194
  * Envoie des données au endPoint passé en paramètre. par défaut en POST
122
195
  */
123
196
  send(path, data, method = "POST", additionalHeaders) {
124
197
  return __awaiter(this, void 0, void 0, function* () {
125
- let headers = yield this.createHeaders(additionalHeaders);
198
+ const lastCall = {
199
+ apiMethod: "send",
200
+ path: path,
201
+ additionalHeaders: additionalHeaders,
202
+ method: method,
203
+ data: data,
204
+ };
205
+ const headers = yield this.createHeaders(additionalHeaders);
126
206
  headers["Accept"] = "application/json";
127
207
  headers["Content-Type"] = "application/json";
128
- let result = yield fetch(this.computeURL(path), {
208
+ const result = yield fetch(this.computeURL(path), {
129
209
  headers: headers,
130
210
  credentials: this.credentials,
131
211
  method: method,
132
212
  body: JSON.stringify(data),
133
213
  });
134
- return yield this.handleResult(result);
214
+ return (yield this.handleResult(result, lastCall));
135
215
  });
136
216
  }
137
217
  /**
@@ -139,19 +219,26 @@ class API {
139
219
  */
140
220
  submitFormData(path, data, method = "POST", additionalHeaders) {
141
221
  return __awaiter(this, void 0, void 0, function* () {
142
- let headers = yield this.createHeaders(additionalHeaders);
222
+ const lastCall = {
223
+ apiMethod: "submitFormData",
224
+ path: path,
225
+ additionalHeaders: additionalHeaders,
226
+ method: method,
227
+ data: data,
228
+ };
229
+ const headers = yield this.createHeaders(additionalHeaders);
143
230
  headers["Accept"] = "application/json";
144
- let formData = new FormData();
231
+ const formData = new FormData();
145
232
  const dynamicData = data;
146
- for (let z in dynamicData)
233
+ for (const z in dynamicData)
147
234
  formData.set(z, dynamicData[z]);
148
- let result = yield fetch(this.computeURL(path), {
235
+ const result = yield fetch(this.computeURL(path), {
149
236
  headers: headers,
150
237
  credentials: this.credentials,
151
238
  method: method,
152
239
  body: formData,
153
240
  });
154
- return yield this.handleResult(result);
241
+ return (yield this.handleResult(result, lastCall));
155
242
  });
156
243
  }
157
244
  /**
@@ -187,4 +274,12 @@ API.loadingGetPromises = new Map();
187
274
  * Tableau static des tokens stokés en memoire vive (comportement à revoir à l'occasion)
188
275
  */
189
276
  API.tokens = new Map();
277
+ /**
278
+ * Tableau stockant l'ensemble des tokens invalides
279
+ */
280
+ API.invalidTokens = [];
281
+ /**
282
+ * Tableau static des tentatives échouées de récupération auto du token
283
+ */
284
+ API.failledTokenUpdates = new Map();
190
285
  export default API;