@rdlabo/ionic-angular-kit 0.0.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.
|
@@ -0,0 +1,684 @@
|
|
|
1
|
+
import * as i0 from '@angular/core';
|
|
2
|
+
import { InjectionToken, EnvironmentProviders, OnInit } from '@angular/core';
|
|
3
|
+
import { ModalOptions, ToastOptions } from '@ionic/angular/standalone';
|
|
4
|
+
import { RouterStateSnapshot, UrlTree, CanActivateFn } from '@angular/router';
|
|
5
|
+
import { Observable } from 'rxjs';
|
|
6
|
+
import { HttpRequest, HttpResponse, HttpErrorResponse, HttpEvent, HttpInterceptorFn } from '@angular/common/http';
|
|
7
|
+
import { ImpactStyle } from '@capacitor/haptics';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Thin, typed wrapper around `@ionic/storage-angular`.
|
|
11
|
+
*
|
|
12
|
+
* Starts `create()` exactly once and stores the resulting ready Promise. Every operation awaits
|
|
13
|
+
* that Promise before touching the underlying store, so calls made before initialization completes
|
|
14
|
+
* are queued rather than dropped.
|
|
15
|
+
*
|
|
16
|
+
* @remarks
|
|
17
|
+
* A naive wrapper that reads the store synchronously would silently no-op (or throw) when invoked
|
|
18
|
+
* before `create()` resolves, losing early writes. Awaiting the one-time ready Promise on every
|
|
19
|
+
* operation removes that race without forcing callers to coordinate initialization themselves.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* constructor(private readonly storage: KitStorageService) {}
|
|
24
|
+
*
|
|
25
|
+
* async ngOnInit(): Promise<void> {
|
|
26
|
+
* await this.storage.set('token', 'abc123');
|
|
27
|
+
* const token = await this.storage.get<string>('token');
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
declare class KitStorageService {
|
|
32
|
+
#private;
|
|
33
|
+
/**
|
|
34
|
+
* Persist a value under the given key.
|
|
35
|
+
*
|
|
36
|
+
* @typeParam T - type of the value being stored
|
|
37
|
+
* @param key - key to store the value under
|
|
38
|
+
* @param value - value to persist; overwrites any existing value for the key
|
|
39
|
+
* @returns a Promise that resolves once the value has been written
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* await storage.set('user', { id: 1, name: 'Ada' });
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
set<T>(key: string, value: T): Promise<void>;
|
|
46
|
+
/**
|
|
47
|
+
* Read the value stored under the given key.
|
|
48
|
+
*
|
|
49
|
+
* @typeParam T - expected type of the stored value
|
|
50
|
+
* @param key - key to read
|
|
51
|
+
* @returns the stored value, or `null` when the key is absent
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* const user = await storage.get<{ id: number }>('user');
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
get<T>(key: string): Promise<T | null>;
|
|
58
|
+
/**
|
|
59
|
+
* Remove the value stored under the given key.
|
|
60
|
+
*
|
|
61
|
+
* @param key - key to remove; a no-op when the key is absent
|
|
62
|
+
* @returns a Promise that resolves once the key has been removed
|
|
63
|
+
*/
|
|
64
|
+
remove(key: string): Promise<void>;
|
|
65
|
+
/**
|
|
66
|
+
* Remove every key/value pair from the store.
|
|
67
|
+
*
|
|
68
|
+
* @returns a Promise that resolves once the store has been emptied
|
|
69
|
+
*/
|
|
70
|
+
clear(): Promise<void>;
|
|
71
|
+
/**
|
|
72
|
+
* List every key currently present in the store.
|
|
73
|
+
*
|
|
74
|
+
* @returns an array of all stored keys
|
|
75
|
+
*/
|
|
76
|
+
keys(): Promise<string[]>;
|
|
77
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<KitStorageService, never>;
|
|
78
|
+
static ɵprov: i0.ɵɵInjectableDeclaration<KitStorageService>;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* User-visible button labels consumed by `KitOverlayController`.
|
|
83
|
+
*
|
|
84
|
+
* @remarks
|
|
85
|
+
* The kit deliberately ships no i18n strings of its own and has no hard dependency on
|
|
86
|
+
* `@angular/localize`. The consuming application always provides these labels: multilingual apps
|
|
87
|
+
* pass `$localize`-resolved strings, single-language apps pass plain literals.
|
|
88
|
+
*/
|
|
89
|
+
interface KitLabels {
|
|
90
|
+
/** Text for the "close" button used by alerts and toasts. */
|
|
91
|
+
readonly close: string;
|
|
92
|
+
/** Text for the "cancel" button used by confirmation alerts. */
|
|
93
|
+
readonly cancel: string;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Overlay configuration injected via `provideKitOverlay()`.
|
|
97
|
+
*
|
|
98
|
+
* @remarks
|
|
99
|
+
* All fields are required; the consuming application must supply a complete configuration.
|
|
100
|
+
*/
|
|
101
|
+
interface KitOverlayConfig {
|
|
102
|
+
/** Button labels used across the overlays presented by `KitOverlayController`. */
|
|
103
|
+
readonly labels: KitLabels;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Injection token carrying the {@link KitOverlayConfig} for `KitOverlayController`.
|
|
107
|
+
*
|
|
108
|
+
* @remarks
|
|
109
|
+
* Provide it through {@link provideKitOverlay} rather than registering it directly.
|
|
110
|
+
*/
|
|
111
|
+
declare const KIT_OVERLAY_CONFIG: InjectionToken<KitOverlayConfig>;
|
|
112
|
+
/**
|
|
113
|
+
* Wire `KitOverlayController` into the application by providing its button labels.
|
|
114
|
+
*
|
|
115
|
+
* @param config - overlay configuration, including the button labels to inject
|
|
116
|
+
* @returns environment providers to add to the application's provider list
|
|
117
|
+
* @example
|
|
118
|
+
* ```ts
|
|
119
|
+
* bootstrapApplication(AppComponent, {
|
|
120
|
+
* providers: [
|
|
121
|
+
* provideKitOverlay({ labels: { close: $localize`Close`, cancel: $localize`Cancel` } }),
|
|
122
|
+
* ],
|
|
123
|
+
* });
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
declare const provideKitOverlay: (config: KitOverlayConfig) => EnvironmentProviders;
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Options for {@link KitOverlayController.presentModal}.
|
|
130
|
+
*
|
|
131
|
+
* @remarks
|
|
132
|
+
* Extends Ionic's `ModalOptions` but omits `component` and `componentProps`, which are passed as
|
|
133
|
+
* dedicated arguments instead.
|
|
134
|
+
*/
|
|
135
|
+
interface KitModalPresentOptions extends Omit<ModalOptions, 'component' | 'componentProps'> {
|
|
136
|
+
/**
|
|
137
|
+
* When `true`, expand the sheet to its maximum breakpoint while the native keyboard is shown.
|
|
138
|
+
*
|
|
139
|
+
* @remarks
|
|
140
|
+
* Only has an effect on native platforms; ignored on the web.
|
|
141
|
+
*/
|
|
142
|
+
watchKeyboard?: boolean;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Options for {@link KitOverlayController.alertClose}.
|
|
146
|
+
*/
|
|
147
|
+
interface KitAlertCloseOptions {
|
|
148
|
+
/** Alert header text. */
|
|
149
|
+
header: string;
|
|
150
|
+
/** Alert body message. */
|
|
151
|
+
message: string;
|
|
152
|
+
/** Optional alert sub-header text shown beneath the header. */
|
|
153
|
+
subHeader?: string;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Options for {@link KitOverlayController.alertConfirm}.
|
|
157
|
+
*
|
|
158
|
+
* @remarks
|
|
159
|
+
* Extends {@link KitAlertCloseOptions} with the confirm-button text.
|
|
160
|
+
*/
|
|
161
|
+
interface KitAlertConfirmOptions extends KitAlertCloseOptions {
|
|
162
|
+
/**
|
|
163
|
+
* Text for the OK (confirm) button.
|
|
164
|
+
*
|
|
165
|
+
* @remarks
|
|
166
|
+
* Action-specific, so it is supplied by the caller rather than taken from the shared labels.
|
|
167
|
+
*/
|
|
168
|
+
okText: string;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Ergonomic wrapper that consolidates Ionic's overlay controllers (Modal / Toast / Alert).
|
|
172
|
+
*
|
|
173
|
+
* @remarks
|
|
174
|
+
* Folds the repetitive create → present → onDidDismiss sequence into single calls and returns the
|
|
175
|
+
* relevant result directly. It holds no application-specific policy such as navigation; compose
|
|
176
|
+
* those concerns on the consuming side.
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* ```ts
|
|
180
|
+
* constructor(private readonly overlay: KitOverlayController) {}
|
|
181
|
+
*
|
|
182
|
+
* async edit(): Promise<void> {
|
|
183
|
+
* const result = await this.overlay.presentModal<EditResult>(EditPage, { id: 1 });
|
|
184
|
+
* if (result) {
|
|
185
|
+
* await this.overlay.presentToast({ message: 'Saved' });
|
|
186
|
+
* }
|
|
187
|
+
* }
|
|
188
|
+
* ```
|
|
189
|
+
*/
|
|
190
|
+
declare class KitOverlayController {
|
|
191
|
+
#private;
|
|
192
|
+
/**
|
|
193
|
+
* Present a modal and resolve with the data passed to its dismissal.
|
|
194
|
+
*
|
|
195
|
+
* @typeParam O - type of the data returned when the modal is dismissed
|
|
196
|
+
* @param component - the component to render inside the modal
|
|
197
|
+
* @param componentProps - props to pass to the modal component
|
|
198
|
+
* @param options - additional modal options, including {@link KitModalPresentOptions.watchKeyboard}
|
|
199
|
+
* @returns the dismiss data, or `undefined` when the modal is dismissed without data
|
|
200
|
+
* @example
|
|
201
|
+
* ```ts
|
|
202
|
+
* const data = await overlay.presentModal<{ saved: boolean }>(EditPage, { id: 1 }, { watchKeyboard: true });
|
|
203
|
+
* ```
|
|
204
|
+
*/
|
|
205
|
+
presentModal<O = unknown>(component: ModalOptions['component'], componentProps?: ModalOptions['componentProps'], options?: KitModalPresentOptions): Promise<O | undefined>;
|
|
206
|
+
/**
|
|
207
|
+
* Present a toast using kit defaults that the caller may override.
|
|
208
|
+
*
|
|
209
|
+
* @remarks
|
|
210
|
+
* Defaults to a top position, a 2000ms duration, a vertical swipe gesture, and a close button
|
|
211
|
+
* from the configured labels; any of these can be overridden via `options`. Presenting a toast
|
|
212
|
+
* also triggers light native haptic feedback as an intentional kit UX choice.
|
|
213
|
+
*
|
|
214
|
+
* @param options - Ionic toast options that override the kit defaults
|
|
215
|
+
* @returns the presented toast element
|
|
216
|
+
* @example
|
|
217
|
+
* ```ts
|
|
218
|
+
* await overlay.presentToast({ message: 'Copied to clipboard' });
|
|
219
|
+
* ```
|
|
220
|
+
*/
|
|
221
|
+
presentToast(options: ToastOptions): Promise<HTMLIonToastElement>;
|
|
222
|
+
/**
|
|
223
|
+
* Present a notification alert with a single "close" button and wait for it to be dismissed.
|
|
224
|
+
*
|
|
225
|
+
* @param options - alert content (header, message, optional sub-header)
|
|
226
|
+
* @returns a Promise that resolves once the alert has been dismissed
|
|
227
|
+
* @example
|
|
228
|
+
* ```ts
|
|
229
|
+
* await overlay.alertClose({ header: 'Done', message: 'Your changes were saved.' });
|
|
230
|
+
* ```
|
|
231
|
+
*/
|
|
232
|
+
alertClose(options: KitAlertCloseOptions): Promise<void>;
|
|
233
|
+
/**
|
|
234
|
+
* Present a confirmation alert with cancel and OK buttons.
|
|
235
|
+
*
|
|
236
|
+
* @param options - alert content plus the OK button text via {@link KitAlertConfirmOptions.okText}
|
|
237
|
+
* @returns `true` when the user presses OK, `false` otherwise (cancel or backdrop dismissal)
|
|
238
|
+
* @example
|
|
239
|
+
* ```ts
|
|
240
|
+
* const ok = await overlay.alertConfirm({
|
|
241
|
+
* header: 'Delete item?',
|
|
242
|
+
* message: 'This cannot be undone.',
|
|
243
|
+
* okText: 'Delete',
|
|
244
|
+
* });
|
|
245
|
+
* if (ok) {
|
|
246
|
+
* await remove();
|
|
247
|
+
* }
|
|
248
|
+
* ```
|
|
249
|
+
*/
|
|
250
|
+
alertConfirm(options: KitAlertConfirmOptions): Promise<boolean>;
|
|
251
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<KitOverlayController, never>;
|
|
252
|
+
static ɵprov: i0.ɵɵInjectableDeclaration<KitOverlayController>;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Work around iOS `ion-input` autofill values not propagating to the Angular form model.
|
|
257
|
+
*
|
|
258
|
+
* On iOS, when the browser autofills an `ion-input` (for example a saved password), the value
|
|
259
|
+
* is written to the underlying native `<input>` element but the corresponding `change` event is
|
|
260
|
+
* not forwarded to the host `ion-input`, so the Angular form control (and `ngModel`) never sees
|
|
261
|
+
* the autofilled value. This directive listens for the first `change` event on the inner input
|
|
262
|
+
* element and mirrors its value back onto the host element, restoring two-way binding.
|
|
263
|
+
*
|
|
264
|
+
* Apply it to any `ion-input` that participates in a form and may be autofilled by attaching the
|
|
265
|
+
* `rdlaboAutofill` attribute.
|
|
266
|
+
*
|
|
267
|
+
* @remarks
|
|
268
|
+
* The directive is a no-op on every platform other than iOS, where the value already propagates
|
|
269
|
+
* correctly. A short timeout is used because `ion-input` creates its inner `<input>` element
|
|
270
|
+
* asynchronously after the host element is initialized.
|
|
271
|
+
*
|
|
272
|
+
* @example
|
|
273
|
+
* ```html
|
|
274
|
+
* <ion-input rdlaboAutofill type="password" [(ngModel)]="password"></ion-input>
|
|
275
|
+
* ```
|
|
276
|
+
*/
|
|
277
|
+
declare class KitAutofillDirective implements OnInit {
|
|
278
|
+
#private;
|
|
279
|
+
constructor();
|
|
280
|
+
/**
|
|
281
|
+
* Register the iOS autofill workaround once the directive is initialized.
|
|
282
|
+
*
|
|
283
|
+
* Returns immediately on non-iOS platforms. On iOS, after a short delay it attaches a one-shot,
|
|
284
|
+
* passive `change` listener to the inner `<input>` element that `ion-input` renders, copying the
|
|
285
|
+
* autofilled value back onto the host element so the Angular form model stays in sync. Any error
|
|
286
|
+
* while locating the inner input (for example if the element is not yet present) is swallowed.
|
|
287
|
+
*
|
|
288
|
+
* @returns Nothing.
|
|
289
|
+
*/
|
|
290
|
+
ngOnInit(): void;
|
|
291
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<KitAutofillDirective, never>;
|
|
292
|
+
static ɵdir: i0.ɵɵDirectiveDeclaration<KitAutofillDirective, "[rdlaboAutofill]", never, {}, {}, never, never, true, never>;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Discriminated set of authentication states the guards react to.
|
|
297
|
+
*
|
|
298
|
+
* @remarks
|
|
299
|
+
* The application is responsible for emitting these values through {@link KitAuthConfig.authState}.
|
|
300
|
+
* An application that does not use a value (for example email confirmation) simply never emits it.
|
|
301
|
+
*
|
|
302
|
+
* - `user` — fully authenticated and verified.
|
|
303
|
+
* - `confirm` — awaiting email confirmation.
|
|
304
|
+
* - `required` — not authenticated.
|
|
305
|
+
* - `anonymous` — signed in anonymously; the user can still be guided toward full registration.
|
|
306
|
+
*/
|
|
307
|
+
type KitAuthState = 'user' | 'confirm' | 'required' | 'anonymous';
|
|
308
|
+
/**
|
|
309
|
+
* Redirect targets (route paths) used by the guards when access is denied.
|
|
310
|
+
*
|
|
311
|
+
* @remarks
|
|
312
|
+
* Every field is required and must be provided per application, because the guards have no
|
|
313
|
+
* knowledge of the host application's route layout.
|
|
314
|
+
*/
|
|
315
|
+
interface KitAuthRedirects {
|
|
316
|
+
/** Used by {@link kitRequiredUnauthorizedGuard}: where to navigate when the user is already authenticated (`user`). */
|
|
317
|
+
readonly whenAuthorized: string;
|
|
318
|
+
/** Used by {@link kitRequiredUnauthorizedGuard}: where to navigate when the user is awaiting email confirmation (`confirm`). */
|
|
319
|
+
readonly whenConfirming: string;
|
|
320
|
+
/** Used by {@link kitRequireConfirmingGuard}: where to navigate when the state is not `confirm`. */
|
|
321
|
+
readonly whenNotConfirming: string;
|
|
322
|
+
/** Used by {@link kitRequireAuthorizedGuard}: where to navigate when the state is not `user` and the fallback is not allowed. */
|
|
323
|
+
readonly whenUnauthorized: string;
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Configuration consumed by the authentication guards, injected through {@link provideKitAuth}.
|
|
327
|
+
*
|
|
328
|
+
* @remarks
|
|
329
|
+
* All members are required. The hooks let the host application plug its own auth service and
|
|
330
|
+
* navigation policy into the otherwise fixed guard control flow.
|
|
331
|
+
*/
|
|
332
|
+
interface KitAuthConfig {
|
|
333
|
+
/**
|
|
334
|
+
* Source of the current authentication state.
|
|
335
|
+
*
|
|
336
|
+
* @remarks
|
|
337
|
+
* Typically backed by the application's own auth service (for example `AuthService.isAuth()`).
|
|
338
|
+
*
|
|
339
|
+
* @returns A stream of {@link KitAuthState} values.
|
|
340
|
+
*/
|
|
341
|
+
authState(): Observable<KitAuthState>;
|
|
342
|
+
/**
|
|
343
|
+
* Application-specific work that runs in {@link kitRequireAuthorizedGuard} after the state is confirmed to be `user`.
|
|
344
|
+
*
|
|
345
|
+
* @remarks
|
|
346
|
+
* Typical responsibilities include token login, permission checks, terms-of-service acceptance,
|
|
347
|
+
* or restoring a previously requested redirect.
|
|
348
|
+
*
|
|
349
|
+
* @param state - The router state snapshot of the route being activated.
|
|
350
|
+
* @returns `true` to allow activation, or a `UrlTree` to perform a custom redirect.
|
|
351
|
+
*/
|
|
352
|
+
onAuthorized(state: RouterStateSnapshot): Promise<boolean | UrlTree>;
|
|
353
|
+
/**
|
|
354
|
+
* Fallback that runs in {@link kitRequireAuthorizedGuard} when the state is `required` (not authenticated).
|
|
355
|
+
*
|
|
356
|
+
* @remarks
|
|
357
|
+
* For example, attempt an anonymous sign-in and allow the route. Applications that do not need
|
|
358
|
+
* this should pass `async () => false` to fall through to the default redirect.
|
|
359
|
+
*
|
|
360
|
+
* @param state - The router state snapshot of the route being activated.
|
|
361
|
+
* @returns `true` to allow activation, a `UrlTree` for a custom redirect, or `false` to use the default redirect.
|
|
362
|
+
*/
|
|
363
|
+
onUnauthenticated(state: RouterStateSnapshot): Promise<boolean | UrlTree>;
|
|
364
|
+
/** Redirect targets used by the guards. */
|
|
365
|
+
redirects: KitAuthRedirects;
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* Injection token that carries the {@link KitAuthConfig} to the authentication guards.
|
|
369
|
+
*/
|
|
370
|
+
declare const KIT_AUTH_CONFIG: InjectionToken<KitAuthConfig>;
|
|
371
|
+
/**
|
|
372
|
+
* Wire the authentication guard configuration into the application's dependency injection.
|
|
373
|
+
*
|
|
374
|
+
* @remarks
|
|
375
|
+
* The factory runs inside an injection context, so it may call `inject()` (for example
|
|
376
|
+
* `inject(AuthService)`) to build the configuration.
|
|
377
|
+
*
|
|
378
|
+
* @param configFactory - Factory that returns the {@link KitAuthConfig} for the application.
|
|
379
|
+
* @returns Environment providers to add to the application bootstrap.
|
|
380
|
+
*
|
|
381
|
+
* @example
|
|
382
|
+
* ```ts
|
|
383
|
+
* provideKitAuth(() => {
|
|
384
|
+
* const auth = inject(AuthService);
|
|
385
|
+
* return {
|
|
386
|
+
* authState: () => auth.isAuth(),
|
|
387
|
+
* onAuthorized: async () => true,
|
|
388
|
+
* onUnauthenticated: async () => false,
|
|
389
|
+
* redirects: {
|
|
390
|
+
* whenAuthorized: '/',
|
|
391
|
+
* whenConfirming: '/auth/confirm',
|
|
392
|
+
* whenNotConfirming: '/auth/signin',
|
|
393
|
+
* whenUnauthorized: 'auth',
|
|
394
|
+
* },
|
|
395
|
+
* };
|
|
396
|
+
* });
|
|
397
|
+
* ```
|
|
398
|
+
*/
|
|
399
|
+
declare const provideKitAuth: (configFactory: () => KitAuthConfig) => EnvironmentProviders;
|
|
400
|
+
/**
|
|
401
|
+
* Guard that requires the user to be unauthenticated (for example sign-in or sign-up pages).
|
|
402
|
+
*
|
|
403
|
+
* @remarks
|
|
404
|
+
* Allows the `required` and `anonymous` states (an anonymous user is permitted to proceed to a
|
|
405
|
+
* registration page). An authenticated user (`user`) is sent to `whenAuthorized`, and a user
|
|
406
|
+
* awaiting confirmation (`confirm`) is sent to `whenConfirming`.
|
|
407
|
+
*
|
|
408
|
+
* @returns A stream emitting `true` to allow activation, or `false` after triggering a redirect.
|
|
409
|
+
*
|
|
410
|
+
* @example
|
|
411
|
+
* ```ts
|
|
412
|
+
* const routes: Routes = [{ path: 'signin', component: SigninPage, canActivate: [kitRequiredUnauthorizedGuard] }];
|
|
413
|
+
* ```
|
|
414
|
+
*/
|
|
415
|
+
declare const kitRequiredUnauthorizedGuard: CanActivateFn;
|
|
416
|
+
/**
|
|
417
|
+
* Guard that requires the user to be awaiting email confirmation (`confirm`).
|
|
418
|
+
*
|
|
419
|
+
* @remarks
|
|
420
|
+
* Any other state triggers a redirect: an `anonymous` user is sent to the authenticated area
|
|
421
|
+
* (`whenAuthorized`), and every remaining state is sent to `whenNotConfirming`.
|
|
422
|
+
*
|
|
423
|
+
* @returns A stream emitting `true` to allow activation, or `false` after triggering a redirect.
|
|
424
|
+
*
|
|
425
|
+
* @example
|
|
426
|
+
* ```ts
|
|
427
|
+
* const routes: Routes = [{ path: 'confirm', component: ConfirmPage, canActivate: [kitRequireConfirmingGuard] }];
|
|
428
|
+
* ```
|
|
429
|
+
*/
|
|
430
|
+
declare const kitRequireConfirmingGuard: CanActivateFn;
|
|
431
|
+
/**
|
|
432
|
+
* Guard that requires the user to be fully authenticated (`user`).
|
|
433
|
+
*
|
|
434
|
+
* @remarks
|
|
435
|
+
* - `user` — runs {@link KitAuthConfig.onAuthorized} (token login, permission checks, and so on).
|
|
436
|
+
* - `anonymous` — allowed as-is, for applications that permit anonymous browsing.
|
|
437
|
+
* - `required` / `confirm` — runs {@link KitAuthConfig.onUnauthenticated}; if it resolves to `false`,
|
|
438
|
+
* the user is redirected to `whenUnauthorized`.
|
|
439
|
+
*
|
|
440
|
+
* @param _route - The activated route snapshot (unused).
|
|
441
|
+
* @param state - The router state snapshot, forwarded to the configuration hooks.
|
|
442
|
+
* @returns A stream emitting the activation result: `true`, a `UrlTree`, or `false` after a redirect.
|
|
443
|
+
*
|
|
444
|
+
* @example
|
|
445
|
+
* ```ts
|
|
446
|
+
* const routes: Routes = [{ path: 'home', component: HomePage, canActivate: [kitRequireAuthorizedGuard] }];
|
|
447
|
+
* ```
|
|
448
|
+
*/
|
|
449
|
+
declare const kitRequireAuthorizedGuard: CanActivateFn;
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* Configuration that customizes the behavior of {@link kitAuthInterceptor}, injected through {@link provideKitHttp}.
|
|
453
|
+
*
|
|
454
|
+
* @remarks
|
|
455
|
+
* The interceptor fixes the retry policy (up to 2 retries with a linearly increasing backoff, plus immediate
|
|
456
|
+
* throw on `401` and on every {@link NON_RETRYABLE_STATUSES | non-retryable status}) and the overall
|
|
457
|
+
* control flow. Only the hooks below are application-specific.
|
|
458
|
+
*/
|
|
459
|
+
interface KitHttpConfig {
|
|
460
|
+
/**
|
|
461
|
+
* Produce authentication and metadata headers for the outgoing request.
|
|
462
|
+
*
|
|
463
|
+
* @param request - The outgoing request about to be sent.
|
|
464
|
+
* @returns A map of header names to values, resolved asynchronously.
|
|
465
|
+
*/
|
|
466
|
+
getAuthHeaders(request: HttpRequest<unknown>): Promise<Record<string, string>>;
|
|
467
|
+
/**
|
|
468
|
+
* Produce additional headers for the outgoing request.
|
|
469
|
+
*
|
|
470
|
+
* @param request - The outgoing request about to be sent.
|
|
471
|
+
* @returns A map of header names to values; return `{}` when none are needed.
|
|
472
|
+
*/
|
|
473
|
+
buildExtraHeaders(request: HttpRequest<unknown>): Record<string, string>;
|
|
474
|
+
/**
|
|
475
|
+
* Called for every successful response that completed an actual network round trip.
|
|
476
|
+
*
|
|
477
|
+
* @remarks
|
|
478
|
+
* Responses synthesized by {@link KitHttpConfig.offlineFallback} are produced after `catchError`
|
|
479
|
+
* and therefore never reach this hook, so it observes genuine successes only. A typical use is to
|
|
480
|
+
* reset an "offline" flag once connectivity is restored. Implement as a no-op when not needed.
|
|
481
|
+
*
|
|
482
|
+
* @param event - The successful `HttpResponse`.
|
|
483
|
+
*/
|
|
484
|
+
onResponse(event: HttpResponse<unknown>): void;
|
|
485
|
+
/**
|
|
486
|
+
* Decide whether to pass the request straight through, skipping auth, retry, and error handling.
|
|
487
|
+
*
|
|
488
|
+
* @remarks
|
|
489
|
+
* Useful for external URLs such as S3 or a CDN. Return `false` to always apply the standard pipeline.
|
|
490
|
+
*
|
|
491
|
+
* @param request - The outgoing request.
|
|
492
|
+
* @returns `true` to bypass the interceptor pipeline.
|
|
493
|
+
*/
|
|
494
|
+
bypass(request: HttpRequest<unknown>): boolean;
|
|
495
|
+
/**
|
|
496
|
+
* Provide an offline short-circuit when a request fails.
|
|
497
|
+
*
|
|
498
|
+
* @remarks
|
|
499
|
+
* Returning a non-null observable replaces the error with that response (for example a queued
|
|
500
|
+
* offline result). Return `null` to let normal error handling proceed.
|
|
501
|
+
*
|
|
502
|
+
* @param request - The request that failed (after headers were applied).
|
|
503
|
+
* @param error - The error response that triggered the fallback.
|
|
504
|
+
* @returns A replacement event stream, or `null` for no fallback.
|
|
505
|
+
*/
|
|
506
|
+
offlineFallback(request: HttpRequest<unknown>, error: HttpErrorResponse): Observable<HttpEvent<unknown>> | null;
|
|
507
|
+
/**
|
|
508
|
+
* Side effect to run on a `401` response (for example an expired token).
|
|
509
|
+
*
|
|
510
|
+
* @param request - The request that received the `401`.
|
|
511
|
+
*/
|
|
512
|
+
onUnauthorized(request: HttpRequest<unknown>): void;
|
|
513
|
+
/**
|
|
514
|
+
* Side effect to run on a `403` response (a permission error).
|
|
515
|
+
*
|
|
516
|
+
* @remarks
|
|
517
|
+
* Implement as a no-op when not needed.
|
|
518
|
+
*
|
|
519
|
+
* @param request - The request that received the `403`.
|
|
520
|
+
*/
|
|
521
|
+
onForbidden(request: HttpRequest<unknown>): void;
|
|
522
|
+
/**
|
|
523
|
+
* UX hook for network-originated errors while the device is connected.
|
|
524
|
+
*
|
|
525
|
+
* @param status - The HTTP status code, or a string descriptor for non-HTTP failures.
|
|
526
|
+
* @returns Optionally a promise to await before continuing.
|
|
527
|
+
*/
|
|
528
|
+
onNetworkError(status: number | string): Promise<void> | void;
|
|
529
|
+
/**
|
|
530
|
+
* UX hook for `400` / `500` responses that carry a server-provided message.
|
|
531
|
+
*
|
|
532
|
+
* @param message - The message extracted from the error body.
|
|
533
|
+
*/
|
|
534
|
+
onServerError(message: string): void;
|
|
535
|
+
}
|
|
536
|
+
/**
|
|
537
|
+
* Injection token that carries the {@link KitHttpConfig} to {@link kitAuthInterceptor}.
|
|
538
|
+
*/
|
|
539
|
+
declare const KIT_HTTP_CONFIG: InjectionToken<KitHttpConfig>;
|
|
540
|
+
/**
|
|
541
|
+
* Wire the {@link kitAuthInterceptor} configuration into the application's dependency injection.
|
|
542
|
+
*
|
|
543
|
+
* @remarks
|
|
544
|
+
* Register the interceptor itself separately via `provideHttpClient(withInterceptors([kitAuthInterceptor]))`.
|
|
545
|
+
* The factory runs inside an injection context, so it may call `inject()`.
|
|
546
|
+
*
|
|
547
|
+
* @param configFactory - Factory that returns the {@link KitHttpConfig} for the application.
|
|
548
|
+
* @returns Environment providers to add to the application bootstrap.
|
|
549
|
+
*
|
|
550
|
+
* @example
|
|
551
|
+
* ```ts
|
|
552
|
+
* bootstrapApplication(AppComponent, {
|
|
553
|
+
* providers: [
|
|
554
|
+
* provideHttpClient(withInterceptors([kitAuthInterceptor])),
|
|
555
|
+
* provideKitHttp(() => {
|
|
556
|
+
* const auth = inject(AuthService);
|
|
557
|
+
* return {
|
|
558
|
+
* getAuthHeaders: async () => ({ Authorization: `Bearer ${await auth.token()}` }),
|
|
559
|
+
* buildExtraHeaders: () => ({}),
|
|
560
|
+
* onResponse: () => {},
|
|
561
|
+
* bypass: (request) => request.url.startsWith('https://cdn.example.com'),
|
|
562
|
+
* offlineFallback: () => null,
|
|
563
|
+
* onUnauthorized: () => auth.signOut(),
|
|
564
|
+
* onForbidden: () => {},
|
|
565
|
+
* onNetworkError: () => {},
|
|
566
|
+
* onServerError: (message) => console.error(message),
|
|
567
|
+
* };
|
|
568
|
+
* }),
|
|
569
|
+
* ],
|
|
570
|
+
* });
|
|
571
|
+
* ```
|
|
572
|
+
*/
|
|
573
|
+
declare const provideKitHttp: (configFactory: () => KitHttpConfig) => EnvironmentProviders;
|
|
574
|
+
/**
|
|
575
|
+
* Canonical functional HTTP interceptor that applies authentication, retries, and error handling.
|
|
576
|
+
*
|
|
577
|
+
* @remarks
|
|
578
|
+
* Behavior, driven by the injected {@link KitHttpConfig}:
|
|
579
|
+
*
|
|
580
|
+
* 1. Requests for which `bypass` returns `true` are forwarded untouched.
|
|
581
|
+
* 2. Otherwise the headers from `getAuthHeaders` and `buildExtraHeaders` are merged onto a cloned request.
|
|
582
|
+
* 3. Failed requests are retried up to 2 times with a linearly increasing backoff of `500ms * (retryCount + 5)`,
|
|
583
|
+
* except that `401` and any {@link NON_RETRYABLE_STATUSES | non-retryable status}
|
|
584
|
+
* (`400`, `403`, `404`, `418`, `500`, `502`) are thrown immediately without retrying.
|
|
585
|
+
* 4. On error, `offlineFallback` is consulted first; otherwise `401` calls `onUnauthorized`, `403`
|
|
586
|
+
* calls `onForbidden`, network-class failures (anything other than `400`/`500`) call
|
|
587
|
+
* `onNetworkError` when the device is connected, and `400`/`500` responses carrying a body
|
|
588
|
+
* message call `onServerError`.
|
|
589
|
+
*
|
|
590
|
+
* @param request - The outgoing request.
|
|
591
|
+
* @param next - The next handler in the interceptor chain.
|
|
592
|
+
* @returns A stream of HTTP events for the (possibly modified, retried, or replaced) request.
|
|
593
|
+
*
|
|
594
|
+
* @example
|
|
595
|
+
* ```ts
|
|
596
|
+
* provideHttpClient(withInterceptors([kitAuthInterceptor]));
|
|
597
|
+
* ```
|
|
598
|
+
*/
|
|
599
|
+
declare const kitAuthInterceptor: HttpInterceptorFn;
|
|
600
|
+
|
|
601
|
+
/**
|
|
602
|
+
* Trigger native haptic impact feedback.
|
|
603
|
+
*
|
|
604
|
+
* Delegates to `@capacitor/haptics` `Haptics.impact()` when running on a native platform. On the
|
|
605
|
+
* web (or any non-native platform) it is a no-op and resolves without doing anything.
|
|
606
|
+
*
|
|
607
|
+
* @remarks
|
|
608
|
+
* This haptic side effect was previously bundled implicitly into toast/modal presentation. It has
|
|
609
|
+
* been decoupled into this explicit function so callers opt in to the feedback deliberately, rather
|
|
610
|
+
* than receiving it as a hidden side effect of presenting UI.
|
|
611
|
+
*
|
|
612
|
+
* @param style - The impact intensity to play. Defaults to {@link ImpactStyle.Light}.
|
|
613
|
+
* @returns A promise that resolves once the feedback has been requested (immediately on the web).
|
|
614
|
+
* @example
|
|
615
|
+
* ```ts
|
|
616
|
+
* import { ImpactStyle } from '@capacitor/haptics';
|
|
617
|
+
*
|
|
618
|
+
* // Light tap (default)
|
|
619
|
+
* await kitImpact();
|
|
620
|
+
*
|
|
621
|
+
* // Stronger feedback, e.g. on a confirming action
|
|
622
|
+
* await kitImpact(ImpactStyle.Heavy);
|
|
623
|
+
* ```
|
|
624
|
+
*/
|
|
625
|
+
declare const kitImpact: (style?: ImpactStyle) => Promise<void>;
|
|
626
|
+
|
|
627
|
+
/**
|
|
628
|
+
* Merge a newly fetched page of items into an existing list by id, keeping the result sorted.
|
|
629
|
+
*
|
|
630
|
+
* Items are merged by the numeric `key` field. On a duplicate `key` the item from `arrayNew` wins
|
|
631
|
+
* and replaces the one in `arrayOld`. Old items whose `key` falls *inside* the window spanned by
|
|
632
|
+
* `arrayNew` (between its first and last `key`) are dropped, since the freshly fetched page is the
|
|
633
|
+
* authoritative copy of that window; old items *outside* the window are kept. The merged items are
|
|
634
|
+
* finally sorted by `key`, ascending or descending according to `order`.
|
|
635
|
+
*
|
|
636
|
+
* The algorithm is:
|
|
637
|
+
* 1. If both inputs are empty, return an empty array.
|
|
638
|
+
* 2. Read the first (`lead`) and last (`last`) `key` values of `arrayNew` as the bounds of the
|
|
639
|
+
* page's window. Keep the old items whose `key` lies *outside* that window (at or beyond the
|
|
640
|
+
* extremes) and drop those strictly inside it, handling both a high-to-low page (`lead > last`)
|
|
641
|
+
* and a low-to-high page (`lead < last`). A single-value page (`lead === last`) keeps all old items.
|
|
642
|
+
* 3. Remove old items whose `key` already exists in `arrayNew` (new wins on duplicates).
|
|
643
|
+
* 4. Concatenate `arrayNew` with the surviving old items and sort by `key` in the requested
|
|
644
|
+
* direction.
|
|
645
|
+
*
|
|
646
|
+
* @remarks
|
|
647
|
+
* Designed for infinite-scroll / paginated list merging, where each fetched page may overlap the
|
|
648
|
+
* previously held items and the server returns a contiguous, ordered window of records. The `key`
|
|
649
|
+
* field is treated as a number for range comparison and sorting.
|
|
650
|
+
*
|
|
651
|
+
* @typeParam T - Element type of both arrays. Its `key` property must be a numeric value.
|
|
652
|
+
* @param arrayOld - The previously accumulated list. May be empty or nullish-safe (empty when falsy).
|
|
653
|
+
* @param arrayNew - The newly fetched page of items; its `key` range defines the window that its own
|
|
654
|
+
* items replace, and its items take precedence on duplicates.
|
|
655
|
+
* @param key - The property of `T` used as the unique, numeric id for matching, range filtering, and sorting.
|
|
656
|
+
* @param order - Sort direction by `key`: `'ASC'` for ascending or `'DESC'` for descending. Defaults to `'DESC'`.
|
|
657
|
+
* @returns A new array containing `arrayNew` merged with the out-of-window, non-duplicate old items, sorted by `key`.
|
|
658
|
+
* @example
|
|
659
|
+
* ```ts
|
|
660
|
+
* interface Post {
|
|
661
|
+
* id: number;
|
|
662
|
+
* title: string;
|
|
663
|
+
* }
|
|
664
|
+
*
|
|
665
|
+
* const loaded: Post[] = [
|
|
666
|
+
* { id: 30, title: 'c' },
|
|
667
|
+
* { id: 20, title: 'b' },
|
|
668
|
+
* { id: 10, title: 'a' },
|
|
669
|
+
* ];
|
|
670
|
+
* const nextPage: Post[] = [
|
|
671
|
+
* { id: 20, title: 'b (updated)' },
|
|
672
|
+
* { id: 15, title: 'a.5' },
|
|
673
|
+
* ];
|
|
674
|
+
*
|
|
675
|
+
* // Descending merge: id 30 lies above the new page's [15, 20] window so it is kept; id 20 is
|
|
676
|
+
* // inside the window and is replaced by the new value; id 10 lies below the window and is kept.
|
|
677
|
+
* const merged = arrayConcatById(loaded, nextPage, 'id', 'DESC');
|
|
678
|
+
* // => [{ id: 30, title: 'c' }, { id: 20, title: 'b (updated)' }, { id: 15, title: 'a.5' }, { id: 10, title: 'a' }]
|
|
679
|
+
* ```
|
|
680
|
+
*/
|
|
681
|
+
declare const arrayConcatById: <T>(arrayOld: T[], arrayNew: T[], key: keyof T, order?: "ASC" | "DESC") => T[];
|
|
682
|
+
|
|
683
|
+
export { KIT_AUTH_CONFIG, KIT_HTTP_CONFIG, KIT_OVERLAY_CONFIG, KitAutofillDirective, KitOverlayController, KitStorageService, arrayConcatById, kitAuthInterceptor, kitImpact, kitRequireAuthorizedGuard, kitRequireConfirmingGuard, kitRequiredUnauthorizedGuard, provideKitAuth, provideKitHttp, provideKitOverlay };
|
|
684
|
+
export type { KitAlertCloseOptions, KitAlertConfirmOptions, KitAuthConfig, KitAuthRedirects, KitAuthState, KitHttpConfig, KitLabels, KitModalPresentOptions, KitOverlayConfig };
|