@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.
- package/README.md +272 -0
- package/dist/cache/index.d.ts +198 -0
- package/dist/cache/index.js +2 -0
- package/dist/chunk-4QWZX3LM.js +498 -0
- package/dist/chunk-IIG4QLLN.js +178 -0
- package/dist/chunk-U27TKOYZ.js +312 -0
- package/dist/chunk-UQUCUBV2.js +297 -0
- package/dist/chunk-UXUGWW74.js +1432 -0
- package/dist/chunk-XHMX3BYT.js +1040 -0
- package/dist/chunk-ZC7F2HDV.js +100 -0
- package/dist/drafts/index.d.ts +203 -0
- package/dist/drafts/index.js +3 -0
- package/dist/files/index.d.ts +181 -0
- package/dist/files/index.js +2 -0
- package/dist/index.d.ts +249 -0
- package/dist/index.js +107 -0
- package/dist/storage/index.d.ts +198 -0
- package/dist/storage/index.js +2 -0
- package/dist/sync/index.d.ts +621 -0
- package/dist/sync/index.js +3 -0
- package/dist/types-B2FJpQuN.d.ts +179 -0
- package/dist/types-CWuauHAr.d.ts +204 -0
- package/package.json +77 -0
|
@@ -0,0 +1,498 @@
|
|
|
1
|
+
import { DEFAULT_EXPIRATIONS } from './chunk-ZC7F2HDV.js';
|
|
2
|
+
|
|
3
|
+
// src/drafts/types.ts
|
|
4
|
+
var DRAFT_PREFIX = "draft:";
|
|
5
|
+
function getDraftKey(formKey) {
|
|
6
|
+
return `${DRAFT_PREFIX}${formKey}`;
|
|
7
|
+
}
|
|
8
|
+
function getFormKeyFromDraftKey(key) {
|
|
9
|
+
if (!key.startsWith(DRAFT_PREFIX)) {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
return key.slice(DRAFT_PREFIX.length);
|
|
13
|
+
}
|
|
14
|
+
var DRAFT_V2_PREFIX = "draft-v2:";
|
|
15
|
+
var CURRENT_DRAFT_PREFIX = "current-draft:";
|
|
16
|
+
function getDraftV2Key(formKey, draftId) {
|
|
17
|
+
return `${DRAFT_V2_PREFIX}${formKey}:${draftId}`;
|
|
18
|
+
}
|
|
19
|
+
function parseDraftV2Key(key) {
|
|
20
|
+
if (!key.startsWith(DRAFT_V2_PREFIX)) {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
const rest = key.slice(DRAFT_V2_PREFIX.length);
|
|
24
|
+
const colonIndex = rest.indexOf(":");
|
|
25
|
+
if (colonIndex === -1) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
return {
|
|
29
|
+
formKey: rest.slice(0, colonIndex),
|
|
30
|
+
draftId: rest.slice(colonIndex + 1)
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function getCurrentDraftKey(formKey) {
|
|
34
|
+
return `${CURRENT_DRAFT_PREFIX}${formKey}`;
|
|
35
|
+
}
|
|
36
|
+
function generateDraftId() {
|
|
37
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
|
|
38
|
+
const r = Math.random() * 16 | 0;
|
|
39
|
+
const v = c === "x" ? r : r & 3 | 8;
|
|
40
|
+
return v.toString(16);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
function getOrCreateDeviceId() {
|
|
44
|
+
const DEVICE_ID_KEY = "classic-offline-device-id";
|
|
45
|
+
if (typeof localStorage === "undefined") {
|
|
46
|
+
return generateDraftId();
|
|
47
|
+
}
|
|
48
|
+
let deviceId = localStorage.getItem(DEVICE_ID_KEY);
|
|
49
|
+
if (!deviceId) {
|
|
50
|
+
deviceId = generateDraftId();
|
|
51
|
+
localStorage.setItem(DEVICE_ID_KEY, deviceId);
|
|
52
|
+
}
|
|
53
|
+
return deviceId;
|
|
54
|
+
}
|
|
55
|
+
function isLegacyDraftKey(key) {
|
|
56
|
+
return key.startsWith(DRAFT_PREFIX) && !key.startsWith(DRAFT_V2_PREFIX);
|
|
57
|
+
}
|
|
58
|
+
function isDraftV2Key(key) {
|
|
59
|
+
return key.startsWith(DRAFT_V2_PREFIX);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// src/drafts/manager.ts
|
|
63
|
+
var DraftManagerService = class {
|
|
64
|
+
constructor(adapter) {
|
|
65
|
+
this.adapter = adapter;
|
|
66
|
+
this.deviceId = getOrCreateDeviceId();
|
|
67
|
+
}
|
|
68
|
+
// ============================================
|
|
69
|
+
// Create Operations
|
|
70
|
+
// ============================================
|
|
71
|
+
/**
|
|
72
|
+
* Create a new draft
|
|
73
|
+
*/
|
|
74
|
+
async createDraft(formKey, options = {}) {
|
|
75
|
+
const {
|
|
76
|
+
name,
|
|
77
|
+
expirationMs = DEFAULT_EXPIRATIONS.DRAFT,
|
|
78
|
+
initialData = {},
|
|
79
|
+
initialFileRefs = {}
|
|
80
|
+
} = options;
|
|
81
|
+
const now = Date.now();
|
|
82
|
+
const draftId = generateDraftId();
|
|
83
|
+
const fileCount = Object.values(initialFileRefs).flat().length;
|
|
84
|
+
const metadata = {
|
|
85
|
+
id: draftId,
|
|
86
|
+
formKey,
|
|
87
|
+
name: name || this.generateDefaultDraftName(formKey),
|
|
88
|
+
createdAt: now,
|
|
89
|
+
updatedAt: now,
|
|
90
|
+
expiresAt: now + expirationMs,
|
|
91
|
+
syncStatus: "local-only",
|
|
92
|
+
remoteId: null,
|
|
93
|
+
version: 1,
|
|
94
|
+
lastSyncedAt: null,
|
|
95
|
+
deviceId: this.deviceId,
|
|
96
|
+
fileCount
|
|
97
|
+
};
|
|
98
|
+
const draft = {
|
|
99
|
+
metadata,
|
|
100
|
+
data: initialData,
|
|
101
|
+
fileRefs: initialFileRefs
|
|
102
|
+
};
|
|
103
|
+
const key = getDraftV2Key(formKey, draftId);
|
|
104
|
+
await this.adapter.storeJSON(key, draft, {
|
|
105
|
+
ttl: expirationMs,
|
|
106
|
+
tags: ["draft", "draft-v2", `draft:${formKey}`, `draft-v2:${formKey}`]
|
|
107
|
+
});
|
|
108
|
+
return metadata;
|
|
109
|
+
}
|
|
110
|
+
// ============================================
|
|
111
|
+
// Read Operations
|
|
112
|
+
// ============================================
|
|
113
|
+
/**
|
|
114
|
+
* Get a draft by ID
|
|
115
|
+
*/
|
|
116
|
+
async getDraft(draftId) {
|
|
117
|
+
const keys = await this.adapter.listJSON(DRAFT_V2_PREFIX);
|
|
118
|
+
for (const key of keys) {
|
|
119
|
+
const parsed = parseDraftV2Key(key);
|
|
120
|
+
if (parsed?.draftId === draftId) {
|
|
121
|
+
const entry = await this.adapter.getJSON(key);
|
|
122
|
+
return entry?.data ?? null;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Get a draft by form key and draft ID (more efficient)
|
|
129
|
+
*/
|
|
130
|
+
async getDraftByFormKey(formKey, draftId) {
|
|
131
|
+
const key = getDraftV2Key(formKey, draftId);
|
|
132
|
+
const entry = await this.adapter.getJSON(key);
|
|
133
|
+
return entry?.data ?? null;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Get draft metadata only
|
|
137
|
+
*/
|
|
138
|
+
async getDraftMetadata(draftId) {
|
|
139
|
+
const draft = await this.getDraft(draftId);
|
|
140
|
+
return draft?.metadata ?? null;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* List all drafts with optional filtering
|
|
144
|
+
*/
|
|
145
|
+
async listDrafts(options = {}) {
|
|
146
|
+
const {
|
|
147
|
+
formKey,
|
|
148
|
+
syncStatus,
|
|
149
|
+
sortBy = "updatedAt",
|
|
150
|
+
sortOrder = "desc",
|
|
151
|
+
limit,
|
|
152
|
+
offset = 0
|
|
153
|
+
} = options;
|
|
154
|
+
const prefix = formKey ? `${DRAFT_V2_PREFIX}${formKey}:` : DRAFT_V2_PREFIX;
|
|
155
|
+
const keys = await this.adapter.listJSON(prefix);
|
|
156
|
+
const metadataList = [];
|
|
157
|
+
for (const key of keys) {
|
|
158
|
+
const entry = await this.adapter.getJSON(key);
|
|
159
|
+
if (entry?.data?.metadata) {
|
|
160
|
+
if (syncStatus) {
|
|
161
|
+
const statuses = Array.isArray(syncStatus) ? syncStatus : [syncStatus];
|
|
162
|
+
if (!statuses.includes(entry.data.metadata.syncStatus)) {
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
metadataList.push(entry.data.metadata);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
metadataList.sort((a, b) => {
|
|
170
|
+
const aValue = a[sortBy];
|
|
171
|
+
const bValue = b[sortBy];
|
|
172
|
+
if (typeof aValue === "string" && typeof bValue === "string") {
|
|
173
|
+
return sortOrder === "asc" ? aValue.localeCompare(bValue) : bValue.localeCompare(aValue);
|
|
174
|
+
}
|
|
175
|
+
const numA = typeof aValue === "number" ? aValue : 0;
|
|
176
|
+
const numB = typeof bValue === "number" ? bValue : 0;
|
|
177
|
+
return sortOrder === "asc" ? numA - numB : numB - numA;
|
|
178
|
+
});
|
|
179
|
+
const start = offset;
|
|
180
|
+
const end = limit ? offset + limit : void 0;
|
|
181
|
+
return metadataList.slice(start, end);
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* List drafts by form key (convenience method)
|
|
185
|
+
*/
|
|
186
|
+
async listDraftsByFormKey(formKey) {
|
|
187
|
+
return this.listDrafts({ formKey });
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Get count of drafts
|
|
191
|
+
*/
|
|
192
|
+
async getDraftCount(formKey) {
|
|
193
|
+
const prefix = formKey ? `${DRAFT_V2_PREFIX}${formKey}:` : DRAFT_V2_PREFIX;
|
|
194
|
+
const keys = await this.adapter.listJSON(prefix);
|
|
195
|
+
return keys.length;
|
|
196
|
+
}
|
|
197
|
+
// ============================================
|
|
198
|
+
// Update Operations
|
|
199
|
+
// ============================================
|
|
200
|
+
/**
|
|
201
|
+
* Update draft data
|
|
202
|
+
*/
|
|
203
|
+
async updateDraft(draftId, data, fileRefs) {
|
|
204
|
+
const draft = await this.getDraft(draftId);
|
|
205
|
+
if (!draft) {
|
|
206
|
+
return false;
|
|
207
|
+
}
|
|
208
|
+
const now = Date.now();
|
|
209
|
+
const updatedFileRefs = fileRefs ?? draft.fileRefs;
|
|
210
|
+
const fileCount = Object.values(updatedFileRefs).flat().length;
|
|
211
|
+
const updatedDraft = {
|
|
212
|
+
metadata: {
|
|
213
|
+
...draft.metadata,
|
|
214
|
+
updatedAt: now,
|
|
215
|
+
version: draft.metadata.version + 1,
|
|
216
|
+
deviceId: this.deviceId,
|
|
217
|
+
fileCount,
|
|
218
|
+
// Mark as pending upload if was synced
|
|
219
|
+
syncStatus: draft.metadata.syncStatus === "synced" ? "pending-upload" : draft.metadata.syncStatus
|
|
220
|
+
},
|
|
221
|
+
data: { ...draft.data, ...data },
|
|
222
|
+
fileRefs: updatedFileRefs
|
|
223
|
+
};
|
|
224
|
+
const key = getDraftV2Key(draft.metadata.formKey, draftId);
|
|
225
|
+
const ttl = updatedDraft.metadata.expiresAt - now;
|
|
226
|
+
await this.adapter.storeJSON(key, updatedDraft, {
|
|
227
|
+
ttl: ttl > 0 ? ttl : DEFAULT_EXPIRATIONS.DRAFT,
|
|
228
|
+
tags: [
|
|
229
|
+
"draft",
|
|
230
|
+
"draft-v2",
|
|
231
|
+
`draft:${draft.metadata.formKey}`,
|
|
232
|
+
`draft-v2:${draft.metadata.formKey}`
|
|
233
|
+
]
|
|
234
|
+
});
|
|
235
|
+
return true;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Update draft metadata only
|
|
239
|
+
*/
|
|
240
|
+
async updateDraftMetadata(draftId, updates) {
|
|
241
|
+
const draft = await this.getDraft(draftId);
|
|
242
|
+
if (!draft) {
|
|
243
|
+
return false;
|
|
244
|
+
}
|
|
245
|
+
const now = Date.now();
|
|
246
|
+
const updatedDraft = {
|
|
247
|
+
...draft,
|
|
248
|
+
metadata: {
|
|
249
|
+
...draft.metadata,
|
|
250
|
+
...updates,
|
|
251
|
+
updatedAt: now,
|
|
252
|
+
version: draft.metadata.version + 1
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
const key = getDraftV2Key(draft.metadata.formKey, draftId);
|
|
256
|
+
const ttl = updatedDraft.metadata.expiresAt - now;
|
|
257
|
+
await this.adapter.storeJSON(key, updatedDraft, {
|
|
258
|
+
ttl: ttl > 0 ? ttl : DEFAULT_EXPIRATIONS.DRAFT,
|
|
259
|
+
tags: [
|
|
260
|
+
"draft",
|
|
261
|
+
"draft-v2",
|
|
262
|
+
`draft:${draft.metadata.formKey}`,
|
|
263
|
+
`draft-v2:${draft.metadata.formKey}`
|
|
264
|
+
]
|
|
265
|
+
});
|
|
266
|
+
return true;
|
|
267
|
+
}
|
|
268
|
+
// ============================================
|
|
269
|
+
// Delete Operations
|
|
270
|
+
// ============================================
|
|
271
|
+
/**
|
|
272
|
+
* Delete a draft
|
|
273
|
+
*/
|
|
274
|
+
async deleteDraft(draftId, deleteFiles = false) {
|
|
275
|
+
const draft = await this.getDraft(draftId);
|
|
276
|
+
if (!draft) {
|
|
277
|
+
return false;
|
|
278
|
+
}
|
|
279
|
+
const key = getDraftV2Key(draft.metadata.formKey, draftId);
|
|
280
|
+
await this.adapter.deleteJSON(key);
|
|
281
|
+
if (deleteFiles) {
|
|
282
|
+
const fileIds = Object.values(draft.fileRefs).flat();
|
|
283
|
+
for (const fileId of fileIds) {
|
|
284
|
+
try {
|
|
285
|
+
await this.adapter.deleteBlob(fileId);
|
|
286
|
+
} catch {
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
return true;
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Delete all drafts for a form key
|
|
294
|
+
*/
|
|
295
|
+
async deleteDraftsByFormKey(formKey) {
|
|
296
|
+
const prefix = `${DRAFT_V2_PREFIX}${formKey}:`;
|
|
297
|
+
const keys = await this.adapter.listJSON(prefix);
|
|
298
|
+
let count = 0;
|
|
299
|
+
for (const key of keys) {
|
|
300
|
+
await this.adapter.deleteJSON(key);
|
|
301
|
+
count++;
|
|
302
|
+
}
|
|
303
|
+
return count;
|
|
304
|
+
}
|
|
305
|
+
// ============================================
|
|
306
|
+
// Duplicate Operations
|
|
307
|
+
// ============================================
|
|
308
|
+
/**
|
|
309
|
+
* Duplicate a draft
|
|
310
|
+
*/
|
|
311
|
+
async duplicateDraft(draftId, newName) {
|
|
312
|
+
const draft = await this.getDraft(draftId);
|
|
313
|
+
if (!draft) {
|
|
314
|
+
return null;
|
|
315
|
+
}
|
|
316
|
+
return this.createDraft(draft.metadata.formKey, {
|
|
317
|
+
name: newName || `${draft.metadata.name} (copy)`,
|
|
318
|
+
initialData: draft.data,
|
|
319
|
+
initialFileRefs: draft.fileRefs
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
// ============================================
|
|
323
|
+
// Current Draft Management (for backward compatibility)
|
|
324
|
+
// ============================================
|
|
325
|
+
/**
|
|
326
|
+
* Get the current draft for a form (auto-migrates legacy drafts)
|
|
327
|
+
*/
|
|
328
|
+
async getCurrentDraft(formKey) {
|
|
329
|
+
const currentKey = getCurrentDraftKey(formKey);
|
|
330
|
+
const currentEntry = await this.adapter.getJSON(currentKey);
|
|
331
|
+
if (currentEntry?.data?.draftId) {
|
|
332
|
+
const draft = await this.getDraftByFormKey(formKey, currentEntry.data.draftId);
|
|
333
|
+
if (draft) {
|
|
334
|
+
return draft;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
const legacyDraft = await this.migrateLegacyDraft(formKey);
|
|
338
|
+
if (legacyDraft) {
|
|
339
|
+
return legacyDraft;
|
|
340
|
+
}
|
|
341
|
+
const drafts = await this.listDrafts({
|
|
342
|
+
formKey,
|
|
343
|
+
sortBy: "updatedAt",
|
|
344
|
+
sortOrder: "desc",
|
|
345
|
+
limit: 1
|
|
346
|
+
});
|
|
347
|
+
if (drafts.length > 0) {
|
|
348
|
+
return this.getDraftByFormKey(formKey, drafts[0].id);
|
|
349
|
+
}
|
|
350
|
+
return null;
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* Set the current draft for a form
|
|
354
|
+
*/
|
|
355
|
+
async setCurrentDraft(formKey, draftId) {
|
|
356
|
+
const currentKey = getCurrentDraftKey(formKey);
|
|
357
|
+
await this.adapter.storeJSON(currentKey, { draftId }, { tags: ["current-draft"] });
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Clear the current draft for a form
|
|
361
|
+
*/
|
|
362
|
+
async clearCurrentDraft(formKey) {
|
|
363
|
+
const currentKey = getCurrentDraftKey(formKey);
|
|
364
|
+
await this.adapter.deleteJSON(currentKey);
|
|
365
|
+
}
|
|
366
|
+
// ============================================
|
|
367
|
+
// Sync Status Helpers
|
|
368
|
+
// ============================================
|
|
369
|
+
/**
|
|
370
|
+
* Get drafts by sync status
|
|
371
|
+
*/
|
|
372
|
+
async getDraftsBySyncStatus(status) {
|
|
373
|
+
return this.listDrafts({ syncStatus: status });
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* Get all drafts pending sync
|
|
377
|
+
*/
|
|
378
|
+
async getPendingSyncDrafts() {
|
|
379
|
+
return this.listDrafts({
|
|
380
|
+
syncStatus: ["pending-upload", "pending-download", "conflict"]
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* Mark a draft as synced
|
|
385
|
+
*/
|
|
386
|
+
async markAsSynced(draftId, remoteId) {
|
|
387
|
+
return this.updateDraftMetadata(draftId, {
|
|
388
|
+
syncStatus: "synced",
|
|
389
|
+
remoteId,
|
|
390
|
+
lastSyncedAt: Date.now()
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* Mark a draft as having a conflict
|
|
395
|
+
*/
|
|
396
|
+
async markAsConflict(draftId) {
|
|
397
|
+
return this.updateDraftMetadata(draftId, {
|
|
398
|
+
syncStatus: "conflict"
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
// ============================================
|
|
402
|
+
// Legacy Migration
|
|
403
|
+
// ============================================
|
|
404
|
+
/**
|
|
405
|
+
* Migrate a legacy draft to V2 format
|
|
406
|
+
*/
|
|
407
|
+
async migrateLegacyDraft(formKey) {
|
|
408
|
+
const legacyKey = getDraftKey(formKey);
|
|
409
|
+
const legacyEntry = await this.adapter.getJSON(legacyKey);
|
|
410
|
+
if (!legacyEntry?.data) {
|
|
411
|
+
return null;
|
|
412
|
+
}
|
|
413
|
+
const legacy = legacyEntry.data;
|
|
414
|
+
const now = Date.now();
|
|
415
|
+
const draftId = generateDraftId();
|
|
416
|
+
const fileCount = Object.values(legacy.fileRefs).flat().length;
|
|
417
|
+
const metadata = {
|
|
418
|
+
id: draftId,
|
|
419
|
+
formKey: legacy.formKey,
|
|
420
|
+
name: `Migrated Draft (${new Date(legacy.timestamp).toLocaleDateString()})`,
|
|
421
|
+
createdAt: legacy.timestamp,
|
|
422
|
+
updatedAt: legacy.timestamp,
|
|
423
|
+
expiresAt: legacy.expiresAt,
|
|
424
|
+
syncStatus: "local-only",
|
|
425
|
+
remoteId: null,
|
|
426
|
+
version: 1,
|
|
427
|
+
lastSyncedAt: null,
|
|
428
|
+
deviceId: this.deviceId,
|
|
429
|
+
fileCount
|
|
430
|
+
};
|
|
431
|
+
const draft = {
|
|
432
|
+
metadata,
|
|
433
|
+
data: legacy.data,
|
|
434
|
+
fileRefs: legacy.fileRefs
|
|
435
|
+
};
|
|
436
|
+
const key = getDraftV2Key(formKey, draftId);
|
|
437
|
+
const ttl = legacy.expiresAt - now;
|
|
438
|
+
await this.adapter.storeJSON(key, draft, {
|
|
439
|
+
ttl: ttl > 0 ? ttl : DEFAULT_EXPIRATIONS.DRAFT,
|
|
440
|
+
tags: ["draft", "draft-v2", `draft:${formKey}`, `draft-v2:${formKey}`]
|
|
441
|
+
});
|
|
442
|
+
await this.setCurrentDraft(formKey, draftId);
|
|
443
|
+
await this.adapter.deleteJSON(legacyKey);
|
|
444
|
+
console.info(`[DraftManager] Migrated legacy draft for form "${formKey}" to V2 format`);
|
|
445
|
+
return draft;
|
|
446
|
+
}
|
|
447
|
+
/**
|
|
448
|
+
* Check if there are any legacy drafts
|
|
449
|
+
*/
|
|
450
|
+
async hasLegacyDrafts() {
|
|
451
|
+
const keys = await this.adapter.listJSON(DRAFT_PREFIX);
|
|
452
|
+
return keys.some((key) => isLegacyDraftKey(key));
|
|
453
|
+
}
|
|
454
|
+
/**
|
|
455
|
+
* Migrate all legacy drafts
|
|
456
|
+
*/
|
|
457
|
+
async migrateAllLegacyDrafts() {
|
|
458
|
+
const keys = await this.adapter.listJSON(DRAFT_PREFIX);
|
|
459
|
+
let count = 0;
|
|
460
|
+
for (const key of keys) {
|
|
461
|
+
if (isLegacyDraftKey(key)) {
|
|
462
|
+
const formKey = key.slice(DRAFT_PREFIX.length);
|
|
463
|
+
const result = await this.migrateLegacyDraft(formKey);
|
|
464
|
+
if (result) {
|
|
465
|
+
count++;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
return count;
|
|
470
|
+
}
|
|
471
|
+
// ============================================
|
|
472
|
+
// Utilities
|
|
473
|
+
// ============================================
|
|
474
|
+
/**
|
|
475
|
+
* Generate a default draft name
|
|
476
|
+
*/
|
|
477
|
+
generateDefaultDraftName(formKey) {
|
|
478
|
+
const date = /* @__PURE__ */ new Date();
|
|
479
|
+
const dateStr = date.toLocaleDateString("en-US", {
|
|
480
|
+
month: "short",
|
|
481
|
+
day: "numeric",
|
|
482
|
+
hour: "numeric",
|
|
483
|
+
minute: "2-digit"
|
|
484
|
+
});
|
|
485
|
+
return `${this.formatFormKey(formKey)} - ${dateStr}`;
|
|
486
|
+
}
|
|
487
|
+
/**
|
|
488
|
+
* Format a form key for display
|
|
489
|
+
*/
|
|
490
|
+
formatFormKey(formKey) {
|
|
491
|
+
return formKey.split(/[-_]/).map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join(" ");
|
|
492
|
+
}
|
|
493
|
+
};
|
|
494
|
+
function createDraftManagerService(adapter) {
|
|
495
|
+
return new DraftManagerService(adapter);
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
export { CURRENT_DRAFT_PREFIX, DRAFT_PREFIX, DRAFT_V2_PREFIX, DraftManagerService, createDraftManagerService, generateDraftId, getCurrentDraftKey, getDraftKey, getDraftV2Key, getFormKeyFromDraftKey, getOrCreateDeviceId, isDraftV2Key, isLegacyDraftKey, parseDraftV2Key };
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { getDraftKey, DRAFT_PREFIX, getFormKeyFromDraftKey } from './chunk-4QWZX3LM.js';
|
|
2
|
+
import { DEFAULT_EXPIRATIONS } from './chunk-ZC7F2HDV.js';
|
|
3
|
+
|
|
4
|
+
// src/drafts/service.ts
|
|
5
|
+
var DraftStorageService = class {
|
|
6
|
+
constructor(adapter) {
|
|
7
|
+
this.adapter = adapter;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Set a function to validate file existence
|
|
11
|
+
* Used when validateFileRefs option is enabled
|
|
12
|
+
*/
|
|
13
|
+
setFileValidator(validator) {
|
|
14
|
+
this.fileExistsValidator = validator;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Validate file references and log warnings for missing files
|
|
18
|
+
*/
|
|
19
|
+
async validateFileReferences(fileRefs) {
|
|
20
|
+
if (!this.fileExistsValidator) {
|
|
21
|
+
return { valid: true, missingFiles: [] };
|
|
22
|
+
}
|
|
23
|
+
const missingFiles = [];
|
|
24
|
+
for (const [fieldName, fileIds] of Object.entries(fileRefs)) {
|
|
25
|
+
for (const fileId of fileIds) {
|
|
26
|
+
const exists = await this.fileExistsValidator(fileId);
|
|
27
|
+
if (!exists) {
|
|
28
|
+
missingFiles.push(fileId);
|
|
29
|
+
console.warn(
|
|
30
|
+
`[DraftStorageService] File reference "${fileId}" in field "${fieldName}" does not exist`
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
valid: missingFiles.length === 0,
|
|
37
|
+
missingFiles
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Save a form draft
|
|
42
|
+
*/
|
|
43
|
+
async saveDraft(formKey, data, fileRefs = {}, options = {}) {
|
|
44
|
+
const {
|
|
45
|
+
excludeFields = [],
|
|
46
|
+
expirationMs = DEFAULT_EXPIRATIONS.DRAFT,
|
|
47
|
+
validateFileRefs = false
|
|
48
|
+
} = options;
|
|
49
|
+
if (validateFileRefs) {
|
|
50
|
+
await this.validateFileReferences(fileRefs);
|
|
51
|
+
}
|
|
52
|
+
const filteredData = { ...data };
|
|
53
|
+
for (const field of excludeFields) {
|
|
54
|
+
delete filteredData[field];
|
|
55
|
+
}
|
|
56
|
+
const now = Date.now();
|
|
57
|
+
const draft = {
|
|
58
|
+
formKey,
|
|
59
|
+
data: filteredData,
|
|
60
|
+
fileRefs,
|
|
61
|
+
timestamp: now,
|
|
62
|
+
expiresAt: now + expirationMs
|
|
63
|
+
};
|
|
64
|
+
const key = getDraftKey(formKey);
|
|
65
|
+
await this.adapter.storeJSON(key, draft, {
|
|
66
|
+
ttl: expirationMs,
|
|
67
|
+
tags: ["draft", `draft:${formKey}`]
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Load a form draft
|
|
72
|
+
*/
|
|
73
|
+
async loadDraft(formKey, _options = {}) {
|
|
74
|
+
const key = getDraftKey(formKey);
|
|
75
|
+
const entry = await this.adapter.getJSON(key);
|
|
76
|
+
if (!entry) {
|
|
77
|
+
return {
|
|
78
|
+
draft: null,
|
|
79
|
+
restored: false,
|
|
80
|
+
wasExpired: false
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
return {
|
|
84
|
+
draft: entry.data,
|
|
85
|
+
restored: true,
|
|
86
|
+
wasExpired: false
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Clear a form draft
|
|
91
|
+
*/
|
|
92
|
+
async clearDraft(formKey) {
|
|
93
|
+
const key = getDraftKey(formKey);
|
|
94
|
+
await this.adapter.deleteJSON(key);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Check if a draft exists
|
|
98
|
+
*/
|
|
99
|
+
async hasDraft(formKey) {
|
|
100
|
+
const key = getDraftKey(formKey);
|
|
101
|
+
const entry = await this.adapter.getJSON(key);
|
|
102
|
+
return entry !== null;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* List all draft form keys
|
|
106
|
+
*/
|
|
107
|
+
async listDraftFormKeys() {
|
|
108
|
+
const keys = await this.adapter.listJSON(DRAFT_PREFIX);
|
|
109
|
+
return keys.map(getFormKeyFromDraftKey).filter((key) => key !== null);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Clear all drafts
|
|
113
|
+
*/
|
|
114
|
+
async clearAllDrafts() {
|
|
115
|
+
return this.adapter.deleteJSONByTag("draft");
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Get draft metadata without full data (for listing)
|
|
119
|
+
*/
|
|
120
|
+
async getDraftInfo(formKey) {
|
|
121
|
+
const key = getDraftKey(formKey);
|
|
122
|
+
const entry = await this.adapter.getJSON(key);
|
|
123
|
+
if (!entry) {
|
|
124
|
+
return {
|
|
125
|
+
exists: false,
|
|
126
|
+
timestamp: null,
|
|
127
|
+
expiresAt: null,
|
|
128
|
+
hasFiles: false
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
const draft = entry.data;
|
|
132
|
+
const hasFiles = Object.values(draft.fileRefs).some((refs) => refs.length > 0);
|
|
133
|
+
return {
|
|
134
|
+
exists: true,
|
|
135
|
+
timestamp: draft.timestamp,
|
|
136
|
+
expiresAt: draft.expiresAt,
|
|
137
|
+
hasFiles
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Update file references in a draft
|
|
142
|
+
* Useful when files are added/removed without full draft save
|
|
143
|
+
*
|
|
144
|
+
* @param formKey - The form key
|
|
145
|
+
* @param fieldName - The field name to update
|
|
146
|
+
* @param fileIds - Array of file IDs to set
|
|
147
|
+
* @param options - Optional settings including validateFileRefs
|
|
148
|
+
*/
|
|
149
|
+
async updateFileRefs(formKey, fieldName, fileIds, options) {
|
|
150
|
+
const result = await this.loadDraft(formKey);
|
|
151
|
+
if (!result.draft) {
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
if (options?.validateFileRefs) {
|
|
155
|
+
await this.validateFileReferences({ [fieldName]: fileIds });
|
|
156
|
+
}
|
|
157
|
+
result.draft.fileRefs[fieldName] = fileIds;
|
|
158
|
+
await this.saveDraft(formKey, result.draft.data, result.draft.fileRefs);
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Merge data into an existing draft
|
|
163
|
+
*/
|
|
164
|
+
async mergeDraft(formKey, partialData, options = {}) {
|
|
165
|
+
const existing = await this.loadDraft(formKey);
|
|
166
|
+
const mergedData = {
|
|
167
|
+
...existing.draft?.data ?? {},
|
|
168
|
+
...partialData
|
|
169
|
+
};
|
|
170
|
+
const fileRefs = existing.draft?.fileRefs ?? {};
|
|
171
|
+
await this.saveDraft(formKey, mergedData, fileRefs, options);
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
function createDraftStorageService(adapter) {
|
|
175
|
+
return new DraftStorageService(adapter);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export { DraftStorageService, createDraftStorageService };
|