@classic-homes/offline-core 0.1.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,179 @@
1
+ /**
2
+ * Types for form draft persistence
3
+ */
4
+ /**
5
+ * Stored draft data structure (legacy single-draft format)
6
+ */
7
+ interface StoredDraft<T = Record<string, unknown>> {
8
+ /** Unique form identifier */
9
+ formKey: string;
10
+ /** Partial form data */
11
+ data: Partial<T>;
12
+ /** Map of field names to file IDs */
13
+ fileRefs: Record<string, string[]>;
14
+ /** Timestamp when draft was saved */
15
+ timestamp: number;
16
+ /** Timestamp when draft will expire */
17
+ expiresAt: number;
18
+ }
19
+ /**
20
+ * Sync status for a draft
21
+ */
22
+ type DraftSyncStatus = 'local-only' | 'synced' | 'pending-upload' | 'pending-download' | 'conflict' | 'error';
23
+ /**
24
+ * Draft metadata for listing and management
25
+ */
26
+ interface DraftMetadata {
27
+ /** Unique draft identifier (UUID) */
28
+ id: string;
29
+ /** Form type identifier */
30
+ formKey: string;
31
+ /** User-defined name for the draft */
32
+ name: string;
33
+ /** Timestamp when draft was created */
34
+ createdAt: number;
35
+ /** Timestamp when draft was last modified */
36
+ updatedAt: number;
37
+ /** Timestamp when draft will expire */
38
+ expiresAt: number;
39
+ /** Current sync status */
40
+ syncStatus: DraftSyncStatus;
41
+ /** Server-side ID (null if never synced) */
42
+ remoteId: string | null;
43
+ /** Version number for conflict detection */
44
+ version: number;
45
+ /** Timestamp of last successful sync */
46
+ lastSyncedAt: number | null;
47
+ /** Device ID where draft was created/modified */
48
+ deviceId: string;
49
+ /** Number of files attached to this draft */
50
+ fileCount: number;
51
+ }
52
+ /**
53
+ * Stored draft with full metadata (V2 format)
54
+ */
55
+ interface StoredDraftV2<T = Record<string, unknown>> {
56
+ /** Draft metadata */
57
+ metadata: DraftMetadata;
58
+ /** Form data */
59
+ data: Partial<T>;
60
+ /** Map of field names to file IDs */
61
+ fileRefs: Record<string, string[]>;
62
+ }
63
+ /**
64
+ * Options for creating a new draft
65
+ */
66
+ interface CreateDraftOptions {
67
+ /** Optional name for the draft (default: auto-generated) */
68
+ name?: string;
69
+ /** Custom expiration in milliseconds */
70
+ expirationMs?: number;
71
+ /** Initial data for the draft */
72
+ initialData?: Record<string, unknown>;
73
+ /** Initial file references */
74
+ initialFileRefs?: Record<string, string[]>;
75
+ }
76
+ /**
77
+ * Options for listing drafts
78
+ */
79
+ interface ListDraftsOptions {
80
+ /** Filter by form key */
81
+ formKey?: string;
82
+ /** Filter by sync status */
83
+ syncStatus?: DraftSyncStatus | DraftSyncStatus[];
84
+ /** Sort field */
85
+ sortBy?: 'createdAt' | 'updatedAt' | 'name';
86
+ /** Sort direction */
87
+ sortOrder?: 'asc' | 'desc';
88
+ /** Limit number of results */
89
+ limit?: number;
90
+ /** Offset for pagination */
91
+ offset?: number;
92
+ }
93
+ /**
94
+ * Options for saving a draft
95
+ */
96
+ interface SaveDraftOptions {
97
+ /** Fields to exclude from persistence */
98
+ excludeFields?: string[];
99
+ /** Custom expiration in milliseconds */
100
+ expirationMs?: number;
101
+ /**
102
+ * Whether to validate that file references exist.
103
+ * If true, will log warnings for non-existent file references.
104
+ * @default false
105
+ */
106
+ validateFileRefs?: boolean;
107
+ }
108
+ /**
109
+ * Options for loading a draft
110
+ */
111
+ interface LoadDraftOptions {
112
+ /** Whether to load associated files */
113
+ loadFiles?: boolean;
114
+ }
115
+ /**
116
+ * Result of loading a draft
117
+ */
118
+ interface LoadDraftResult<T = Record<string, unknown>> {
119
+ /** The draft data (null if no draft exists) */
120
+ draft: StoredDraft<T> | null;
121
+ /** Whether the draft was restored */
122
+ restored: boolean;
123
+ /** Whether the draft was expired and deleted */
124
+ wasExpired: boolean;
125
+ }
126
+ /**
127
+ * Draft key prefix for organizing drafts in storage
128
+ */
129
+ declare const DRAFT_PREFIX = "draft:";
130
+ /**
131
+ * Generate a draft storage key from form key
132
+ */
133
+ declare function getDraftKey(formKey: string): string;
134
+ /**
135
+ * Extract form key from draft storage key (legacy format)
136
+ */
137
+ declare function getFormKeyFromDraftKey(key: string): string | null;
138
+ /**
139
+ * Prefix for multi-draft storage keys
140
+ */
141
+ declare const DRAFT_V2_PREFIX = "draft-v2:";
142
+ /**
143
+ * Prefix for tracking the current draft per form
144
+ */
145
+ declare const CURRENT_DRAFT_PREFIX = "current-draft:";
146
+ /**
147
+ * Generate a V2 draft storage key
148
+ * Format: draft-v2:{formKey}:{draftId}
149
+ */
150
+ declare function getDraftV2Key(formKey: string, draftId: string): string;
151
+ /**
152
+ * Parse a V2 draft storage key
153
+ */
154
+ declare function parseDraftV2Key(key: string): {
155
+ formKey: string;
156
+ draftId: string;
157
+ } | null;
158
+ /**
159
+ * Get the current draft key for a form
160
+ */
161
+ declare function getCurrentDraftKey(formKey: string): string;
162
+ /**
163
+ * Generate a unique draft ID
164
+ */
165
+ declare function generateDraftId(): string;
166
+ /**
167
+ * Generate a device ID (persistent per browser)
168
+ */
169
+ declare function getOrCreateDeviceId(): string;
170
+ /**
171
+ * Check if a storage key is a legacy draft key
172
+ */
173
+ declare function isLegacyDraftKey(key: string): boolean;
174
+ /**
175
+ * Check if a storage key is a V2 draft key
176
+ */
177
+ declare function isDraftV2Key(key: string): boolean;
178
+
179
+ export { type CreateDraftOptions as C, DRAFT_PREFIX as D, type LoadDraftOptions as L, type StoredDraft as S, type SaveDraftOptions as a, type LoadDraftResult as b, type DraftSyncStatus as c, type DraftMetadata as d, type StoredDraftV2 as e, type ListDraftsOptions as f, DRAFT_V2_PREFIX as g, generateDraftId as h, getOrCreateDeviceId as i, CURRENT_DRAFT_PREFIX as j, getDraftKey as k, getFormKeyFromDraftKey as l, getDraftV2Key as m, getCurrentDraftKey as n, isLegacyDraftKey as o, parseDraftV2Key as p, isDraftV2Key as q };
@@ -0,0 +1,204 @@
1
+ /**
2
+ * Core types for the offline persistence package
3
+ */
4
+ /**
5
+ * Storage adapter names
6
+ */
7
+ type StorageAdapterName = 'opfs' | 'indexeddb' | 'memory';
8
+ /**
9
+ * Quota information returned by storage adapters
10
+ */
11
+ interface StorageQuota {
12
+ /** Bytes currently used */
13
+ used: number;
14
+ /** Bytes available (may be estimated) */
15
+ available: number;
16
+ }
17
+ /**
18
+ * Cleanup result information
19
+ */
20
+ interface CleanupResult {
21
+ /** Number of items deleted */
22
+ deletedCount: number;
23
+ /** Approximate bytes freed */
24
+ freedBytes: number;
25
+ }
26
+ /**
27
+ * Initialization options for the offline storage system
28
+ */
29
+ interface OfflineStorageOptions {
30
+ /**
31
+ * Preferred storage adapter order. Falls back to next if unavailable.
32
+ * @default ['opfs', 'indexeddb', 'memory']
33
+ */
34
+ preferredAdapters?: StorageAdapterName[];
35
+ /**
36
+ * Database name for IndexedDB
37
+ * @default 'classic-offline'
38
+ */
39
+ dbName?: string;
40
+ /**
41
+ * Whether to run automatic cleanup on initialization
42
+ * @default true
43
+ */
44
+ autoCleanup?: boolean;
45
+ /**
46
+ * Cleanup interval in milliseconds (0 to disable periodic cleanup)
47
+ * @default 60000 (1 minute)
48
+ */
49
+ cleanupIntervalMs?: number;
50
+ }
51
+ /**
52
+ * Configuration for the offline storage singleton
53
+ */
54
+ interface OfflineStorageConfig {
55
+ /** The active storage adapter name */
56
+ adapterName: StorageAdapterName;
57
+ /** Database name (for IndexedDB) */
58
+ dbName: string;
59
+ /** Whether auto cleanup is enabled */
60
+ autoCleanup: boolean;
61
+ /** Cleanup interval in ms */
62
+ cleanupIntervalMs: number;
63
+ }
64
+ /**
65
+ * Metadata stored alongside blobs
66
+ */
67
+ interface BlobMetadata {
68
+ /** Content type (MIME) */
69
+ contentType?: string;
70
+ /** Original filename */
71
+ filename?: string;
72
+ /** Size in bytes */
73
+ size?: number;
74
+ /** Creation timestamp */
75
+ createdAt?: number;
76
+ /** Expiration timestamp */
77
+ expiresAt?: number;
78
+ /** Associated form key */
79
+ formKey?: string;
80
+ /** Associated field name */
81
+ fieldName?: string;
82
+ /** Additional custom metadata */
83
+ [key: string]: unknown;
84
+ }
85
+ /**
86
+ * Result of getting a blob with its metadata
87
+ */
88
+ interface BlobWithMetadata {
89
+ blob: Blob;
90
+ metadata: BlobMetadata;
91
+ }
92
+ /**
93
+ * Options for storing JSON data
94
+ */
95
+ interface StoreJSONOptions {
96
+ /** Time to live in milliseconds */
97
+ ttl?: number;
98
+ /** Tags for bulk invalidation */
99
+ tags?: string[];
100
+ }
101
+ /**
102
+ * Internal structure for stored JSON entries
103
+ */
104
+ interface JSONEntry<T = unknown> {
105
+ data: T;
106
+ createdAt: number;
107
+ expiresAt: number | null;
108
+ tags?: string[];
109
+ }
110
+
111
+ /**
112
+ * Storage adapter interface and types
113
+ */
114
+
115
+ /**
116
+ * Storage adapter interface
117
+ *
118
+ * All storage adapters (OPFS, IndexedDB, Memory) implement this interface
119
+ * to provide a consistent API for the higher-level services.
120
+ */
121
+ interface StorageAdapter {
122
+ /**
123
+ * Adapter name identifier
124
+ */
125
+ readonly name: StorageAdapterName;
126
+ /**
127
+ * Store a blob with optional metadata
128
+ */
129
+ storeBlob(key: string, blob: Blob, metadata?: BlobMetadata): Promise<void>;
130
+ /**
131
+ * Retrieve a blob with its metadata
132
+ * Returns null if not found or expired
133
+ */
134
+ getBlob(key: string): Promise<BlobWithMetadata | null>;
135
+ /**
136
+ * Delete a blob
137
+ */
138
+ deleteBlob(key: string): Promise<void>;
139
+ /**
140
+ * List blob keys, optionally filtered by prefix
141
+ */
142
+ listBlobs(prefix?: string): Promise<string[]>;
143
+ /**
144
+ * List blobs filtered by formKey (using index when available)
145
+ * Falls back to scanning if adapter doesn't support indexed queries
146
+ */
147
+ listBlobsByFormKey?(formKey: string, fieldName?: string): Promise<{
148
+ key: string;
149
+ metadata: BlobMetadata;
150
+ }[]>;
151
+ /**
152
+ * Store a JSON-serializable value
153
+ */
154
+ storeJSON<T>(key: string, data: T, options?: StoreJSONOptions): Promise<void>;
155
+ /**
156
+ * Retrieve a JSON value
157
+ * Returns null if not found or expired
158
+ */
159
+ getJSON<T>(key: string): Promise<JSONEntry<T> | null>;
160
+ /**
161
+ * Delete a JSON entry
162
+ */
163
+ deleteJSON(key: string): Promise<void>;
164
+ /**
165
+ * List JSON keys, optionally filtered by prefix
166
+ */
167
+ listJSON(prefix?: string): Promise<string[]>;
168
+ /**
169
+ * Delete JSON entries by tag
170
+ */
171
+ deleteJSONByTag(tag: string): Promise<number>;
172
+ /**
173
+ * Clean up expired entries
174
+ */
175
+ cleanup(): Promise<CleanupResult>;
176
+ /**
177
+ * Get storage quota information
178
+ */
179
+ getQuota(): Promise<StorageQuota>;
180
+ /**
181
+ * Check if this adapter is available in the current environment
182
+ */
183
+ isAvailable(): Promise<boolean>;
184
+ /**
185
+ * Initialize the adapter (create directories, open databases, etc.)
186
+ */
187
+ initialize(): Promise<void>;
188
+ /**
189
+ * Dispose of resources (close connections, terminate workers, etc.)
190
+ */
191
+ dispose(): Promise<void>;
192
+ }
193
+ /**
194
+ * Options for creating a storage adapter
195
+ */
196
+ interface StorageAdapterOptions {
197
+ /**
198
+ * Database name (for IndexedDB)
199
+ * @default 'classic-offline'
200
+ */
201
+ dbName?: string;
202
+ }
203
+
204
+ export type { BlobMetadata as B, CleanupResult as C, JSONEntry as J, OfflineStorageConfig as O, StorageAdapter as S, OfflineStorageOptions as a, StorageAdapterName as b, StorageQuota as c, BlobWithMetadata as d, StoreJSONOptions as e, StorageAdapterOptions as f };
package/package.json ADDED
@@ -0,0 +1,77 @@
1
+ {
2
+ "name": "@classic-homes/offline-core",
3
+ "version": "0.1.1",
4
+ "description": "Framework-agnostic offline persistence and caching for Classic Theme",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "module": "dist/index.js",
8
+ "types": "dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
13
+ },
14
+ "./storage": {
15
+ "types": "./dist/storage/index.d.ts",
16
+ "import": "./dist/storage/index.js"
17
+ },
18
+ "./files": {
19
+ "types": "./dist/files/index.d.ts",
20
+ "import": "./dist/files/index.js"
21
+ },
22
+ "./drafts": {
23
+ "types": "./dist/drafts/index.d.ts",
24
+ "import": "./dist/drafts/index.js"
25
+ },
26
+ "./cache": {
27
+ "types": "./dist/cache/index.d.ts",
28
+ "import": "./dist/cache/index.js"
29
+ },
30
+ "./sync": {
31
+ "types": "./dist/sync/index.d.ts",
32
+ "import": "./dist/sync/index.js"
33
+ }
34
+ },
35
+ "sideEffects": false,
36
+ "files": [
37
+ "dist"
38
+ ],
39
+ "scripts": {
40
+ "build": "tsup",
41
+ "dev": "tsup --watch",
42
+ "clean": "rm -rf dist",
43
+ "typecheck": "tsc --noEmit",
44
+ "test": "vitest run",
45
+ "test:watch": "vitest",
46
+ "test:coverage": "vitest run --coverage"
47
+ },
48
+ "keywords": [
49
+ "offline",
50
+ "persistence",
51
+ "opfs",
52
+ "indexeddb",
53
+ "storage",
54
+ "cache",
55
+ "classic-theme"
56
+ ],
57
+ "license": "MIT",
58
+ "repository": {
59
+ "type": "git",
60
+ "url": "https://github.com/Classic-Homes/classic-theme.git",
61
+ "directory": "packages/offline-core"
62
+ },
63
+ "publishConfig": {
64
+ "access": "public"
65
+ },
66
+ "dependencies": {
67
+ "idb": "^8.0.0"
68
+ },
69
+ "devDependencies": {
70
+ "@vitest/coverage-v8": "^2.0.0",
71
+ "fake-indexeddb": "^6.0.0",
72
+ "jsdom": "^25.0.0",
73
+ "tsup": "^8.0.0",
74
+ "typescript": "^5.3.0",
75
+ "vitest": "^2.0.0"
76
+ }
77
+ }