@rdlabo/ionic-angular-kit 21.4.0 → 21.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/README.md
CHANGED
|
@@ -330,6 +330,129 @@ A fleet-canonical HTTP interceptor with:
|
|
|
330
330
|
- Configurable bypass (CDN, S3, external URLs)
|
|
331
331
|
- **Safe retry**: only idempotent methods (`GET`/`HEAD`/`OPTIONS`, or a request with an `Idempotency-Key`) are retried, and only on a transient status `[0, 408, 429, 502, 503, 504]`, up to 2 times with a short jittered backoff (honoring `Retry-After`). **Writes are never auto-retried** (no duplicate saves).
|
|
332
332
|
- **Offline fast-fail**: when the device is offline the interceptor stops retrying immediately and hands off to `offlineFallback` instead of waiting out the backoff.
|
|
333
|
+
|
|
334
|
+
### Scoped local replica and outbox (`@rdlabo/ionic-angular-kit/offline`)
|
|
335
|
+
|
|
336
|
+
The optional `offline` entry point provides a user/group-scoped local replica, durable outbox, authenticated
|
|
337
|
+
session boundary, cursor-based delta pull, aggregate-ordered replay, optimistic updates, retry classification, and a
|
|
338
|
+
read-only request-policy interceptor. Applications provide URL/DTO read policies, a replica puller, and a command
|
|
339
|
+
executor through `provideOffline(...)`.
|
|
340
|
+
Mutations are queued explicitly with `OfflineSyncService.enqueue`, not through HTTP interceptor policy.
|
|
341
|
+
Web storage uses Ionic Storage; iOS and Android use encrypted Capawesome SQLite. Importing either the
|
|
342
|
+
primary entry point or `/offline` does not pull the private plugin into existing applications.
|
|
343
|
+
|
|
344
|
+
The offline interceptor observes real transport responses to update API reachability. For matched `GET`
|
|
345
|
+
requests only, a transport failure with `status=0` may return a local replica response tagged
|
|
346
|
+
`X-Offline-Response: local`. `POST` and other write methods always go to transport unchanged; outbox replay
|
|
347
|
+
requests bypass policy with `OFFLINE_BYPASS` while still using the same transport observation.
|
|
348
|
+
|
|
349
|
+
Native applications install the Insiders package in the application, then pass its `Sqlite` export
|
|
350
|
+
and an encryption key loaded from secure device storage to the kit:
|
|
351
|
+
|
|
352
|
+
```bash
|
|
353
|
+
npm install @capawesome-team/capacitor-sqlite
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
```ts
|
|
357
|
+
provideOffline({
|
|
358
|
+
// ...product policies, puller, and executor
|
|
359
|
+
sqlitePlugin: Sqlite,
|
|
360
|
+
encryptionKey: async () => {
|
|
361
|
+
const { value } = await securePreferences.get({ key: 'offline-database-key' });
|
|
362
|
+
if (!value) throw new Error('offline-database-key is missing');
|
|
363
|
+
return value;
|
|
364
|
+
},
|
|
365
|
+
});
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
SQLite entities use an immutable client-generated UUID as `localId` and keep the server's
|
|
369
|
+
`AUTO_INCREMENT` id separately as nullable `serverId`. The outbox references only `aggregateLocalId`.
|
|
370
|
+
Immediately before each send, the executor receives the latest `{ localId, serverId }` resolved from
|
|
371
|
+
SQLite; a successful create adds `serverId` without replacing `localId`. Entity projection and outbox
|
|
372
|
+
append/removal are committed in one local transaction.
|
|
373
|
+
|
|
374
|
+
Each synchronization cycle pulls authoritative server deltas before replaying the outbox. Every page carries the
|
|
375
|
+
replica schema version/hash and advances a durable user/group cursor in the same transaction as its rows. A schema
|
|
376
|
+
mismatch, malformed row, or non-advancing cursor rejects synchronization without advancing that cursor. If a remote
|
|
377
|
+
revision changed while a local command is pending, the optimistic row remains visible and both row and command move
|
|
378
|
+
to `conflict`; the new server value is retained as the confirmed baseline.
|
|
379
|
+
|
|
380
|
+
The command adapter must send `commandId` as the server-side idempotency key. The server persists that key with the
|
|
381
|
+
mutation and returns all keys represented by a delta row as `acknowledgedCommandIds`. This correlation is required:
|
|
382
|
+
if the server commits a create/update/delete but its HTTP acknowledgement is lost, the next pull reconciles the
|
|
383
|
+
server result into the original `localId`, removes the acknowledged outbox prefix, and rebases later commands without
|
|
384
|
+
creating a second local identity.
|
|
385
|
+
|
|
386
|
+
Versioned replica schemas lock web and native storage. Web metadata stores
|
|
387
|
+
`replicaSchemaVersion` and `replicaSchemaHash`; native stores the same pair in
|
|
388
|
+
`offline_replica_schema_metadata`. Bump `version` for every intentional shape change and supply a
|
|
389
|
+
complete one-step migration chain. Native runs each step's SQL `statements`; web runs
|
|
390
|
+
`migrateWebRow`, which receives only `{ sourceKey, values, confirmedValues }` and may return the same
|
|
391
|
+
shape or `null` to delete a row. Identity and sync metadata (`localId`, `serverId`, scope, revision,
|
|
392
|
+
`syncState`) stay outside the callback. The bundle fingerprint hashes `version`, entity layouts, and
|
|
393
|
+
migration `fromVersion`/`statements` — never function bodies.
|
|
394
|
+
|
|
395
|
+
```typescript
|
|
396
|
+
import {
|
|
397
|
+
defineOfflineReplicaSchema,
|
|
398
|
+
defineReplicaEntity,
|
|
399
|
+
provideOffline,
|
|
400
|
+
serverId,
|
|
401
|
+
text,
|
|
402
|
+
} from '@rdlabo/ionic-angular-kit/offline';
|
|
403
|
+
|
|
404
|
+
// This is the Hono package's existing `typeof items.$inferSelect` export.
|
|
405
|
+
import type { Items as ItemSelect } from '@product/hono/db/schema';
|
|
406
|
+
|
|
407
|
+
const itemEntityV2 = defineReplicaEntity<ItemSelect>()({
|
|
408
|
+
table: 'items',
|
|
409
|
+
sourceKey: 'items',
|
|
410
|
+
scope: 'group',
|
|
411
|
+
fields: {
|
|
412
|
+
id: serverId(),
|
|
413
|
+
title: text(),
|
|
414
|
+
subtitle: text(),
|
|
415
|
+
},
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
const replicaSchema = defineOfflineReplicaSchema({
|
|
419
|
+
version: 2,
|
|
420
|
+
entities: [itemEntityV2],
|
|
421
|
+
migrations: [
|
|
422
|
+
{
|
|
423
|
+
fromVersion: 1,
|
|
424
|
+
statements: ['ALTER TABLE items ADD COLUMN subtitle TEXT NOT NULL DEFAULT ""'],
|
|
425
|
+
migrateWebRow: (row) => ({
|
|
426
|
+
sourceKey: row.sourceKey,
|
|
427
|
+
values: { ...row.values, subtitle: '' },
|
|
428
|
+
confirmedValues:
|
|
429
|
+
row.confirmedValues === null ? null : { ...row.confirmedValues, subtitle: '' },
|
|
430
|
+
}),
|
|
431
|
+
},
|
|
432
|
+
],
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
provideOffline({
|
|
436
|
+
replicaSchema,
|
|
437
|
+
replicaPuller: ProductReplicaPuller,
|
|
438
|
+
commandExecutor: ProductCommandExecutor,
|
|
439
|
+
// ...request policies, sqlitePlugin, encryptionKey
|
|
440
|
+
});
|
|
441
|
+
```
|
|
442
|
+
|
|
443
|
+
The schema definition must map every `ItemSelect` key exactly once as a SQLite column, `serverId()`, or
|
|
444
|
+
`ignored(reason)`, with exactly one `serverId()` per replicated entity. Nullable Hono columns require
|
|
445
|
+
`nullable(...)`; non-null columns reject it. Therefore adding,
|
|
446
|
+
removing, or changing nullability of a Drizzle column breaks the app build until its replica mapping is updated.
|
|
447
|
+
At runtime, `values` contains only the mapped column projection; `localId` and `serverId` remain dedicated replica
|
|
448
|
+
fields and ignored server fields are never persisted.
|
|
449
|
+
|
|
450
|
+
Encrypted native builds also require the plugin's SQLCipher platform setup: enable
|
|
451
|
+
`capawesomeCapacitorSqliteIncludeSqlcipher = true` on Android; select the `SQLCipher` pod when using
|
|
452
|
+
CocoaPods, or enable the `SQLCipher` package trait when using Swift Package Manager on iOS. Follow
|
|
453
|
+
the [Capawesome SQLite installation guide](https://capawesome.io/docs/plugins/sqlite/#installation)
|
|
454
|
+
for the exact native configuration and export-compliance notes.
|
|
455
|
+
|
|
333
456
|
- **Status classification**: `0`→`onNetworkError` (connected only), `429`→`onRateLimited`, `502/503/504`→`onServerBusy`, `400/422/500`+message→`onServerError`, `401`→`onUnauthorized`, `403`→`onForbidden`. Other statuses (e.g. `404`) are left to the caller.
|
|
334
457
|
- **Universal 60s timeout** — every request fails with a synthetic (retryable) `408` if it hangs for 60s. Deliberately generous (catches a dead server without cutting off a large upload / AI generation; `timeout({ each })` resets per emission, so streaming is unaffected). Not configurable — one fleet-wide behavior.
|
|
335
458
|
- **Optional `treatAsError(response)`** — reject a 2xx (e.g. `204`/`206`) as an error when a backend uses it to signal a condition. The one genuinely app-specific bit (some apps receive a normal `204`), kept optional so class interceptors with a 2xx-as-error convention can migrate to `provideKitHttp`.
|