@rdlabo/ionic-angular-kit 21.2.0-3 → 21.2.0-5

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.
@@ -702,6 +702,157 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.17", ngImpo
702
702
  }]
703
703
  }] });
704
704
 
705
+ /**
706
+ * Singleton "maintenance lock" overlay: backdrop-locked, no dismiss buttons, auto-clears via SSE.
707
+ *
708
+ * @remarks
709
+ * Mirrors {@link KitReloadAlertController} as a de-duped stateful controller, but the user cannot
710
+ * cancel or reload out of it — only the server `event: ended` on {@link KitMaintenanceOptions.waitUrl}
711
+ * (or an explicit {@link dismiss}) clears it. Uses Ionic's `AlertController` (not a feature modal)
712
+ * so the kit stays free of page components / i18n; all copy is supplied by the caller.
713
+ *
714
+ * Wire from {@link KitHttpConfig.onMaintenance} when the API returns `503` with
715
+ * `error.error?.code === 'MAINTENANCE'`.
716
+ *
717
+ * @example
718
+ * ```ts
719
+ * provideKitHttp(() => {
720
+ * const maintenance = inject(KitMaintenanceController);
721
+ * return {
722
+ * getAuthHeaders: async () => ({}),
723
+ * onMaintenance: () =>
724
+ * void maintenance.present({
725
+ * header: 'メンテナンス中です',
726
+ * message: 'しばらくお待ちください。終了次第、自動で閉じます。',
727
+ * waitUrl: environment.api + 'public/maintenance/wait',
728
+ * }),
729
+ * };
730
+ * });
731
+ * ```
732
+ */
733
+ class KitMaintenanceController {
734
+ #alertCtrl = inject(AlertController);
735
+ #alert = null;
736
+ #source = null;
737
+ #reconnectTimer = null;
738
+ #reconnectAttempt = 0;
739
+ #waitUrl = null;
740
+ /** When true, EventSource errors must not schedule reconnect (ended / dismiss). */
741
+ #closed = false;
742
+ /**
743
+ * Present the lock overlay and start waiting on the maintenance SSE, unless one is already showing.
744
+ *
745
+ * @param options - overlay copy plus the wait SSE URL
746
+ */
747
+ async present(options) {
748
+ if (this.#alert || document.querySelector('ion-alert.kit-maintenance-alert')) {
749
+ return;
750
+ }
751
+ this.#closed = false;
752
+ this.#waitUrl = options.waitUrl;
753
+ const alert = await this.#alertCtrl.create({
754
+ header: options.header,
755
+ message: options.message,
756
+ backdropDismiss: false,
757
+ // No buttons — dismissal is server-driven via SSE (or an explicit dismiss()).
758
+ buttons: [],
759
+ cssClass: 'kit-maintenance-alert',
760
+ });
761
+ this.#alert = alert;
762
+ void alert.onDidDismiss().then(() => {
763
+ if (this.#alert === alert) {
764
+ this.#alert = null;
765
+ }
766
+ this.#stopWait();
767
+ });
768
+ await alert.present();
769
+ this.#startWait(options.waitUrl);
770
+ }
771
+ /**
772
+ * Dismiss the tracked overlay and tear down the wait SSE.
773
+ *
774
+ * @remarks
775
+ * Normally called when the wait stream emits `ended`. A no-op when nothing is showing.
776
+ */
777
+ async dismiss() {
778
+ this.#stopWait();
779
+ const alert = this.#alert;
780
+ this.#alert = null;
781
+ await alert?.dismiss();
782
+ }
783
+ #startWait(waitUrl) {
784
+ this.#stopSourceOnly();
785
+ if (this.#closed) {
786
+ return;
787
+ }
788
+ const source = new EventSource(waitUrl);
789
+ this.#source = source;
790
+ source.addEventListener('ended', () => {
791
+ void this.dismiss();
792
+ });
793
+ source.addEventListener('ping', () => {
794
+ // Keepalive — resets reconnect backoff so a lively stream does not treat the next pause as failure.
795
+ this.#reconnectAttempt = 0;
796
+ });
797
+ source.onerror = () => {
798
+ // EventSource auto-reconnects, but after a deploy the old isolate may hang; force a clean
799
+ // reconnect with exponential backoff once the browser surfaces an error.
800
+ if (this.#closed || this.#waitUrl !== waitUrl) {
801
+ return;
802
+ }
803
+ this.#stopSourceOnly();
804
+ this.#scheduleReconnect();
805
+ };
806
+ source.onopen = () => {
807
+ this.#reconnectAttempt = 0;
808
+ };
809
+ }
810
+ #scheduleReconnect() {
811
+ if (this.#closed || !this.#waitUrl) {
812
+ return;
813
+ }
814
+ if (this.#reconnectTimer) {
815
+ clearTimeout(this.#reconnectTimer);
816
+ }
817
+ const delay = Math.min(30_000, 500 * 2 ** this.#reconnectAttempt);
818
+ this.#reconnectAttempt += 1;
819
+ const waitUrl = this.#waitUrl;
820
+ this.#reconnectTimer = setTimeout(() => {
821
+ this.#reconnectTimer = null;
822
+ if (!this.#closed && this.#waitUrl === waitUrl) {
823
+ this.#startWait(waitUrl);
824
+ }
825
+ }, delay);
826
+ }
827
+ #stopWait() {
828
+ this.#closed = true;
829
+ this.#waitUrl = null;
830
+ this.#reconnectAttempt = 0;
831
+ if (this.#reconnectTimer) {
832
+ clearTimeout(this.#reconnectTimer);
833
+ this.#reconnectTimer = null;
834
+ }
835
+ this.#stopSourceOnly();
836
+ }
837
+ #stopSourceOnly() {
838
+ const source = this.#source;
839
+ this.#source = null;
840
+ if (source) {
841
+ source.onerror = null;
842
+ source.onopen = null;
843
+ source.close();
844
+ }
845
+ }
846
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.17", ngImport: i0, type: KitMaintenanceController, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
847
+ static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.2.17", ngImport: i0, type: KitMaintenanceController, providedIn: 'root' }); }
848
+ }
849
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.17", ngImport: i0, type: KitMaintenanceController, decorators: [{
850
+ type: Injectable,
851
+ args: [{
852
+ providedIn: 'root',
853
+ }]
854
+ }] });
855
+
705
856
  /**
706
857
  * Present the fleet's canonical "sign-in / token exchange failed" alert.
707
858
  *
@@ -1229,6 +1380,17 @@ const MAX_RETRIES = 2;
1229
1380
  * @internal
1230
1381
  */
1231
1382
  const DEFAULT_TIMEOUT_MS = 60_000;
1383
+ /**
1384
+ * True when the error is the fleet maintenance short-circuit (`503` + body `code: 'MAINTENANCE'`).
1385
+ *
1386
+ * @remarks
1387
+ * Matches Angular `HttpErrorResponse` as `error.error?.code === 'MAINTENANCE'` (body top-level
1388
+ * `code`). Must not be retried and must not fall through to {@link KitHttpConfig.onServerBusy}.
1389
+ *
1390
+ * @param error - The failed response.
1391
+ * @returns Whether this is a maintenance response.
1392
+ */
1393
+ const isMaintenanceError = (error) => error.status === 503 && error.error?.code === 'MAINTENANCE';
1232
1394
  /**
1233
1395
  * Parse a `Retry-After` header (delta-seconds or an HTTP-date) into milliseconds, or `null` when it
1234
1396
  * is absent or unparseable.
@@ -1310,6 +1472,9 @@ const dispatchError = (config, req, error) => {
1310
1472
  else if (status === 429) {
1311
1473
  config.onRateLimited?.(retryAfterSeconds);
1312
1474
  }
1475
+ else if (isMaintenanceError(error)) {
1476
+ config.onMaintenance?.();
1477
+ }
1313
1478
  else if ([502, 503, 504].includes(status)) {
1314
1479
  config.onServerBusy?.(status, retryAfterSeconds);
1315
1480
  }
@@ -1334,8 +1499,8 @@ const dispatchError = (config, req, error) => {
1334
1499
  * When the device is offline it stops retrying immediately.
1335
1500
  * 5. On the final error, `offlineFallback` is consulted first; otherwise the error is classified by
1336
1501
  * status (see {@link dispatchError}): `401`→`onUnauthorized`, `403`→`onForbidden`, `0`→
1337
- * `onNetworkError` (when connected), `429`→`onRateLimited`, `502`/`503`/`504`→`onServerBusy`, and
1338
- * `400`/`422`/`500` with a body message→`onServerError`.
1502
+ * `onNetworkError` (when connected), `429`→`onRateLimited`, maintenance `503`→`onMaintenance`,
1503
+ * other `502`/`503`/`504`→`onServerBusy`, and `400`/`422`/`500` with a body message→`onServerError`.
1339
1504
  *
1340
1505
  * @param request - The outgoing request.
1341
1506
  * @param next - The next handler in the interceptor chain.
@@ -1376,7 +1541,8 @@ const kitAuthInterceptor = (request, next) => {
1376
1541
  return throwError(() => error);
1377
1542
  }
1378
1543
  // Only replay idempotent requests, and only on a transient status.
1379
- if (!retryable || !RETRYABLE_STATUSES.includes(error.status)) {
1544
+ // Maintenance 503 is intentional — never retry; go straight to onMaintenance.
1545
+ if (!retryable || !RETRYABLE_STATUSES.includes(error.status) || isMaintenanceError(error)) {
1380
1546
  return throwError(() => error);
1381
1547
  }
1382
1548
  // Short linear backoff (500ms, 1000ms, …) plus jitter to de-correlate a fleet of
@@ -1616,5 +1782,5 @@ const kitCreateDidEnter = (el) => {
1616
1782
  * Generated bundle index. Do not edit.
1617
1783
  */
1618
1784
 
1619
- export { KIT_AUTH_CONFIG, KIT_HTTP_CONFIG, KIT_LAST_AUTH_EMAIL_KEY, KIT_OVERLAY_CONFIG, KIT_THEME_STORAGE_KEY, KitAuthInputDirective, KitLoadingController, KitOverlayController, KitReloadAlertController, KitStorageService, arrayConcatById, disableHandler, kitAuthInterceptor, kitChangeEventDisabled, kitClearStoragePreservingKeys, kitCreateDidEnter, kitForgetEmail, kitImpact, kitIsValidEmail, kitKeyboardInit, kitPresentAuthFailedAlert, kitPresentLanguageActionSheet, kitRecallEmail, kitRememberEmail, kitRequireAuthorizedGuard, kitRequireConfirmingGuard, kitRequiredUnauthorizedGuard, objectEqual, provideKitAuth, provideKitHttp, provideKitOverlay };
1785
+ export { KIT_AUTH_CONFIG, KIT_HTTP_CONFIG, KIT_LAST_AUTH_EMAIL_KEY, KIT_OVERLAY_CONFIG, KIT_THEME_STORAGE_KEY, KitAuthInputDirective, KitLoadingController, KitMaintenanceController, KitOverlayController, KitReloadAlertController, KitStorageService, arrayConcatById, disableHandler, isMaintenanceError, kitAuthInterceptor, kitChangeEventDisabled, kitClearStoragePreservingKeys, kitCreateDidEnter, kitForgetEmail, kitImpact, kitIsValidEmail, kitKeyboardInit, kitPresentAuthFailedAlert, kitPresentLanguageActionSheet, kitRecallEmail, kitRememberEmail, kitRequireAuthorizedGuard, kitRequireConfirmingGuard, kitRequiredUnauthorizedGuard, objectEqual, provideKitAuth, provideKitHttp, provideKitOverlay };
1620
1786
  //# sourceMappingURL=rdlabo-ionic-angular-kit.mjs.map