@jsb188/graphql 1.1.8 → 1.1.11
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/cache/config.ts +66 -42
- package/cache/index.test.ts +881 -0
- package/cache/index.ts +1815 -979
- package/cache/persist.ts +721 -0
- package/cache/store.ts +437 -0
- package/client/data-table-realtime-subscriptions.test.ts +740 -0
- package/client/data-table-realtime-subscriptions.ts +955 -0
- package/client/data-table-realtime.test.ts +792 -0
- package/client/data-table-realtime.ts +1218 -0
- package/client/fragment-realtime-sequence.ts +52 -0
- package/client/index.ts +4 -2
- package/client/open-sheet.ts +137 -0
- package/client/request-managed-session.test.ts +574 -0
- package/client/request-no-store.test.ts +157 -0
- package/client/request.ts +973 -143
- package/client/sse-ticket.test.ts +26 -0
- package/deno.lock +170 -0
- package/deno.test.json +10 -0
- package/drafts/index.ts +149 -0
- package/drafts/store.ts +397 -0
- package/gql/eagerQueryMap.ts +12 -0
- package/gql/fragments/accountAuthFragments.ts +5 -0
- package/gql/fragments/accountFragments.ts +1 -3
- package/gql/fragments/accountSessionFragments.ts +16 -0
- package/gql/fragments/agentDocFragments.ts +46 -0
- package/gql/fragments/agentMemoryFragments.ts +19 -0
- package/gql/fragments/aiChatFragments.ts +17 -0
- package/gql/fragments/billingFragments.ts +0 -11
- package/gql/fragments/campaignFragments.ts +277 -0
- package/gql/fragments/dataTableFragments.ts +102 -0
- package/gql/fragments/emailFragments.ts +36 -0
- package/gql/fragments/fieldDocFragments.ts +82 -0
- package/gql/fragments/inboundContactFragments.ts +15 -0
- package/gql/fragments/integrationFragments.ts +22 -0
- package/gql/fragments/logFragments.ts +33 -64
- package/gql/fragments/organizationFragments.ts +94 -21
- package/gql/fragments/scheduleFragments.ts +155 -0
- package/gql/fragments/sheetFragments.ts +675 -0
- package/gql/fragments/storageFragments.ts +21 -1
- package/gql/fragments/workflowFragments.ts +67 -0
- package/gql/mutations/accountMutations.ts +24 -4
- package/gql/mutations/agentDocMutations.ts +177 -0
- package/gql/mutations/agentMemoryMutations.ts +36 -0
- package/gql/mutations/aiChatMutations.ts +43 -3
- package/gql/mutations/authMutations.ts +160 -19
- package/gql/mutations/campaignMutations.ts +215 -0
- package/gql/mutations/dataTableMutations.ts +106 -0
- package/gql/mutations/fieldDocMutations.ts +24 -0
- package/gql/mutations/inboundContactMutations.ts +24 -0
- package/gql/mutations/index.ts +17 -8
- package/gql/mutations/integrationMutations.ts +25 -0
- package/gql/mutations/logMutations.ts +10 -19
- package/gql/mutations/organizationMutations.ts +160 -2
- package/gql/mutations/scheduleMutations.ts +423 -0
- package/gql/mutations/sheetMutations.ts +385 -0
- package/gql/mutations/storageMutations.ts +21 -2
- package/gql/mutations/workflowMutations.ts +42 -0
- package/gql/queries/agentDocQueries.ts +120 -0
- package/gql/queries/agentMemoryQueries.ts +22 -0
- package/gql/queries/aiChatQueries.ts +18 -2
- package/gql/queries/authQueries.ts +41 -0
- package/gql/queries/billingQueries.ts +24 -1
- package/gql/queries/campaignQueries.ts +127 -0
- package/gql/queries/dataTableQueries.ts +134 -0
- package/gql/queries/emailQueries.ts +96 -0
- package/gql/queries/fieldDocQueries.ts +158 -0
- package/gql/queries/inboundContactQueries.ts +40 -0
- package/gql/queries/index.ts +17 -6
- package/gql/queries/logQueries.ts +42 -32
- package/gql/queries/organizationQueries.ts +113 -8
- package/gql/queries/scheduleQueries.ts +315 -0
- package/gql/queries/sheetQueries.ts +383 -0
- package/gql/queries/storageQueries.ts +38 -0
- package/gql/queries/workflowQueries.ts +99 -0
- package/hooks/index.ts +2104 -860
- package/hooks/use-account-mtn.ts +91 -48
- package/hooks/use-agentDoc-mtn.ts +285 -0
- package/hooks/use-agentDoc-qry.ts +208 -0
- package/hooks/use-agentMemory-mtn.ts +73 -0
- package/hooks/use-agentMemory-qry.ts +44 -0
- package/hooks/use-aiChat-mtn.ts +248 -97
- package/hooks/use-aiChat-qry.ts +501 -191
- package/hooks/use-app-qry.ts +14 -11
- package/hooks/use-auth-mtn.ts +410 -256
- package/hooks/use-auth-qry.ts +51 -0
- package/hooks/use-campaign-mtn.ts +377 -0
- package/hooks/use-campaign-qry.ts +257 -0
- package/hooks/use-dataTable-mtn.ts +170 -0
- package/hooks/use-dataTable-qry.ts +996 -0
- package/hooks/use-email-qry.ts +227 -0
- package/hooks/use-fieldDoc-mtn.ts +50 -0
- package/hooks/use-fieldDoc-qry.ts +180 -0
- package/hooks/use-inboundContact-mtn.ts +34 -0
- package/hooks/use-inboundContact-qry.ts +96 -0
- package/hooks/use-integration-mtn.ts +77 -0
- package/hooks/use-integration-qry.ts +30 -0
- package/hooks/use-log-mtn.ts +77 -61
- package/hooks/use-log-qry.ts +99 -72
- package/hooks/use-organization-mtn.ts +409 -80
- package/hooks/use-organization-qry.ts +211 -92
- package/hooks/use-schedule-mtn.ts +416 -0
- package/hooks/use-schedule-qry.ts +157 -0
- package/hooks/use-sheet-mtn.ts +367 -0
- package/hooks/use-sheet-qry.ts +440 -0
- package/hooks/use-storage-mtn.ts +83 -42
- package/hooks/use-storage-qry.ts +104 -13
- package/hooks/use-workflow-mtn.ts +55 -0
- package/hooks/use-workflow-qry.ts +230 -0
- package/loaders/campaign.ts +137 -0
- package/package.json +94 -52
- package/sse.ts +158 -105
- package/tsconfig.json +13 -0
- package/types.d.ts +60 -29
- package/utils.ts +77 -50
- package/gql/fragments/actionFragments.ts +0 -16
- package/gql/fragments/productFragments.ts +0 -63
- package/gql/fragments/reportFragments.ts +0 -48
- package/gql/mutations/billingMutations.ts +0 -34
- package/gql/queries/productQueries.ts +0 -70
- package/gql/queries/reportQueries.ts +0 -66
- package/hooks/use-product-qry.ts +0 -109
- package/hooks/use-report-qry.ts +0 -69
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Assert one client-side SSE credential invariant with a useful failure.
|
|
3
|
+
*/
|
|
4
|
+
function expect(value: unknown, message: string) {
|
|
5
|
+
if (!value) {
|
|
6
|
+
throw new Error(message);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
Deno.test('AI chat EventSource URLs contain only the single-use SSE ticket', async () => {
|
|
11
|
+
const runtime = await Deno.readTextFile(
|
|
12
|
+
new URL('../hooks/use-aiChat-qry.ts', import.meta.url),
|
|
13
|
+
);
|
|
14
|
+
expect(
|
|
15
|
+
runtime.includes('session_key: sessionKey'),
|
|
16
|
+
'AI chat SSE must send its one-time ticket',
|
|
17
|
+
);
|
|
18
|
+
expect(
|
|
19
|
+
!runtime.includes('auth_token'),
|
|
20
|
+
'AI chat SSE URLs must never contain the reusable access JWT',
|
|
21
|
+
);
|
|
22
|
+
expect(
|
|
23
|
+
!runtime.includes('getAuthToken'),
|
|
24
|
+
'AI chat SSE must not read a reusable bearer for EventSource query parameters',
|
|
25
|
+
);
|
|
26
|
+
});
|
package/deno.lock
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": "5",
|
|
3
|
+
"specifiers": {
|
|
4
|
+
"npm:luxon@3.7.2": "3.7.2",
|
|
5
|
+
"npm:node-polyglot@^2.6.0": "2.6.0"
|
|
6
|
+
},
|
|
7
|
+
"npm": {
|
|
8
|
+
"call-bind-apply-helpers@1.0.2": {
|
|
9
|
+
"integrity": "sha512-Sp1ablJ0ivDkSzjcaJdxEunN5/XvksFJ2sMBFfq6x0ryhQV/2b/KwFe21cMpmHtPOSij8K99/wSfoEuTObmuMQ==",
|
|
10
|
+
"dependencies": [
|
|
11
|
+
"es-errors",
|
|
12
|
+
"function-bind"
|
|
13
|
+
]
|
|
14
|
+
},
|
|
15
|
+
"call-bind@1.0.9": {
|
|
16
|
+
"integrity": "sha512-a/hy+pNsFUTR+Iz8TCJvXudKVLAnz/DyeSUo10I5yvFDQJBFU2s9uqQpoSrJlroHUKoKqzg+epxyP9lqFdzfBQ==",
|
|
17
|
+
"dependencies": [
|
|
18
|
+
"call-bind-apply-helpers",
|
|
19
|
+
"es-define-property",
|
|
20
|
+
"get-intrinsic",
|
|
21
|
+
"set-function-length"
|
|
22
|
+
]
|
|
23
|
+
},
|
|
24
|
+
"call-bound@1.0.4": {
|
|
25
|
+
"integrity": "sha512-+ys997U96po4Kx/ABpBCqhA9EuxJaQWDQg7295H4hBphv3IZg0boBKuwYpt4YXp6MZ5AmZQnU/tyMTlRpaSejg==",
|
|
26
|
+
"dependencies": [
|
|
27
|
+
"call-bind-apply-helpers",
|
|
28
|
+
"get-intrinsic"
|
|
29
|
+
]
|
|
30
|
+
},
|
|
31
|
+
"define-data-property@1.1.4": {
|
|
32
|
+
"integrity": "sha512-rBMvIzlpA8v6E+SJZoo++HAYqsLrkg7MSfIinMPFhmkorw7X+dOXVJQs+QT69zGkzMyfDnIMN2Wid1+NbL3T+A==",
|
|
33
|
+
"dependencies": [
|
|
34
|
+
"es-define-property",
|
|
35
|
+
"es-errors",
|
|
36
|
+
"gopd"
|
|
37
|
+
]
|
|
38
|
+
},
|
|
39
|
+
"define-properties@1.2.1": {
|
|
40
|
+
"integrity": "sha512-8QmQKqEASLd5nx0U1B1okLElbUuuttJ/AnYmRXbbbGDWh6uS208EjD4Xqq/I9wK7u0v6O08XhTWnt5XtEbR6Dg==",
|
|
41
|
+
"dependencies": [
|
|
42
|
+
"define-data-property",
|
|
43
|
+
"has-property-descriptors",
|
|
44
|
+
"object-keys"
|
|
45
|
+
]
|
|
46
|
+
},
|
|
47
|
+
"dunder-proto@1.0.1": {
|
|
48
|
+
"integrity": "sha512-KIN/nDJBQRcXw0MLVhZE9iQHmG68qAVIBg9CqmUYjmQIhgij9U5MFvrqkUL5FbtyyzZuOeOt0zdeRe4UY7ct+A==",
|
|
49
|
+
"dependencies": [
|
|
50
|
+
"call-bind-apply-helpers",
|
|
51
|
+
"es-errors",
|
|
52
|
+
"gopd"
|
|
53
|
+
]
|
|
54
|
+
},
|
|
55
|
+
"es-define-property@1.0.1": {
|
|
56
|
+
"integrity": "sha512-e3nRfgfUZ4rNGL232gUgX06QNyyez04KdjFrF+LTRoOXmrOgFKDg4BCdsjW8EnT69eqdYGmRpJwiPVYNrCaW3g=="
|
|
57
|
+
},
|
|
58
|
+
"es-errors@1.3.0": {
|
|
59
|
+
"integrity": "sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw=="
|
|
60
|
+
},
|
|
61
|
+
"es-object-atoms@1.1.2": {
|
|
62
|
+
"integrity": "sha512-HWcBoN6NileqtSydK2FqHbS/LoDd2pqrnQHLyJzBj4kOp/ky2MWMN694xOfkK8/SnUsW2DH7EfyVlydKCsm1Zw==",
|
|
63
|
+
"dependencies": [
|
|
64
|
+
"es-errors"
|
|
65
|
+
]
|
|
66
|
+
},
|
|
67
|
+
"function-bind@1.1.2": {
|
|
68
|
+
"integrity": "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA=="
|
|
69
|
+
},
|
|
70
|
+
"get-intrinsic@1.3.0": {
|
|
71
|
+
"integrity": "sha512-9fSjSaos/fRIVIp+xSJlE6lfwhES7LNtKaCBIamHsjr2na1BiABJPo0mOjjz8GJDURarmCPGqaiVg5mfjb98CQ==",
|
|
72
|
+
"dependencies": [
|
|
73
|
+
"call-bind-apply-helpers",
|
|
74
|
+
"es-define-property",
|
|
75
|
+
"es-errors",
|
|
76
|
+
"es-object-atoms",
|
|
77
|
+
"function-bind",
|
|
78
|
+
"get-proto",
|
|
79
|
+
"gopd",
|
|
80
|
+
"has-symbols",
|
|
81
|
+
"hasown",
|
|
82
|
+
"math-intrinsics"
|
|
83
|
+
]
|
|
84
|
+
},
|
|
85
|
+
"get-proto@1.0.1": {
|
|
86
|
+
"integrity": "sha512-sTSfBjoXBp89JvIKIefqw7U2CCebsc74kiY6awiGogKtoSGbgjYE/G/+l9sF3MWFPNc9IcoOC4ODfKHfxFmp0g==",
|
|
87
|
+
"dependencies": [
|
|
88
|
+
"dunder-proto",
|
|
89
|
+
"es-object-atoms"
|
|
90
|
+
]
|
|
91
|
+
},
|
|
92
|
+
"gopd@1.2.0": {
|
|
93
|
+
"integrity": "sha512-ZUKRh6/kUFoAiTAtTYPZJ3hw9wNxx+BIBOijnlG9PnrJsCcSjs1wyyD6vJpaYtgnzDrKYRSqf3OO6Rfa93xsRg=="
|
|
94
|
+
},
|
|
95
|
+
"has-property-descriptors@1.0.2": {
|
|
96
|
+
"integrity": "sha512-55JNKuIW+vq4Ke1BjOTjM2YctQIvCT7GFzHwmfZPGo5wnrgkid0YQtnAleFSqumZm4az3n2BS+erby5ipJdgrg==",
|
|
97
|
+
"dependencies": [
|
|
98
|
+
"es-define-property"
|
|
99
|
+
]
|
|
100
|
+
},
|
|
101
|
+
"has-symbols@1.1.0": {
|
|
102
|
+
"integrity": "sha512-1cDNdwJ2Jaohmb3sg4OmKaMBwuC48sYni5HUw2DvsC8LjGTLK9h+eb1X6RyuOHe4hT0ULCW68iomhjUoKUqlPQ=="
|
|
103
|
+
},
|
|
104
|
+
"hasown@2.0.4": {
|
|
105
|
+
"integrity": "sha512-T2UbfbBEF32wiepXIsMlTW9+dDYC6wMh/t/vYA4tuOMKqWz/n3vr1NFSxQiyP+zk2mXsoMA/i/7qV6LKut1t1A==",
|
|
106
|
+
"dependencies": [
|
|
107
|
+
"function-bind"
|
|
108
|
+
]
|
|
109
|
+
},
|
|
110
|
+
"js-tokens@4.0.0": {
|
|
111
|
+
"integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ=="
|
|
112
|
+
},
|
|
113
|
+
"loose-envify@1.4.0": {
|
|
114
|
+
"integrity": "sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==",
|
|
115
|
+
"dependencies": [
|
|
116
|
+
"js-tokens"
|
|
117
|
+
],
|
|
118
|
+
"bin": true
|
|
119
|
+
},
|
|
120
|
+
"luxon@3.7.2": {
|
|
121
|
+
"integrity": "sha512-vtEhXh/gNjI9Yg1u4jX/0YVPMvxzHuGgCm6tC5kZyb08yjGWGnqAjGJvcXbqQR2P3MyMEFnRbpcdFS6PBcLqew=="
|
|
122
|
+
},
|
|
123
|
+
"math-intrinsics@1.1.0": {
|
|
124
|
+
"integrity": "sha512-/IXtbwEk5HTPyEwyKX6hGkYXxM9nbj64B+ilVJnC/R6B0pH5G4V3b0pVbL7DBj4tkhBAppbQUlf6F6Xl9LHu1g=="
|
|
125
|
+
},
|
|
126
|
+
"node-polyglot@2.6.0": {
|
|
127
|
+
"integrity": "sha512-ZZFkaYzIfGfBvSM6QhA9dM8EEaUJOVewzGSRcXWbJELXDj0lajAtKaENCYxvF5yE+TgHg6NQb0CmgYMsMdcNJQ==",
|
|
128
|
+
"dependencies": [
|
|
129
|
+
"hasown",
|
|
130
|
+
"object.entries",
|
|
131
|
+
"warning"
|
|
132
|
+
]
|
|
133
|
+
},
|
|
134
|
+
"object-keys@1.1.1": {
|
|
135
|
+
"integrity": "sha512-NuAESUOUMrlIXOfHKzD6bpPu3tYt3xvjNdRIQ+FeT0lNb4K8WR70CaDxhuNguS2XG+GjkyMwOzsN5ZktImfhLA=="
|
|
136
|
+
},
|
|
137
|
+
"object.entries@1.1.9": {
|
|
138
|
+
"integrity": "sha512-8u/hfXFRBD1O0hPUjioLhoWFHRmt6tKA4/vZPyckBr18l1KE9uHrFaFaUi8MDRTpi4uak2goyPTSNJLXX2k2Hw==",
|
|
139
|
+
"dependencies": [
|
|
140
|
+
"call-bind",
|
|
141
|
+
"call-bound",
|
|
142
|
+
"define-properties",
|
|
143
|
+
"es-object-atoms"
|
|
144
|
+
]
|
|
145
|
+
},
|
|
146
|
+
"set-function-length@1.2.2": {
|
|
147
|
+
"integrity": "sha512-pgRc4hJ4/sNjWCSS9AmnS40x3bNMDTknHgL5UaMBTMyJnU90EgWh1Rz+MC9eFu4BuN/UwZjKQuY/1v3rM7HMfg==",
|
|
148
|
+
"dependencies": [
|
|
149
|
+
"define-data-property",
|
|
150
|
+
"es-errors",
|
|
151
|
+
"function-bind",
|
|
152
|
+
"get-intrinsic",
|
|
153
|
+
"gopd",
|
|
154
|
+
"has-property-descriptors"
|
|
155
|
+
]
|
|
156
|
+
},
|
|
157
|
+
"warning@4.0.3": {
|
|
158
|
+
"integrity": "sha512-rpJyN222KWIvHJ/F53XSZv0Zl/accqHR8et1kpaMTD/fLCRxtV8iX8czMzY7sVZupTI3zcUTg8eycS2kNF9l6w==",
|
|
159
|
+
"dependencies": [
|
|
160
|
+
"loose-envify"
|
|
161
|
+
]
|
|
162
|
+
}
|
|
163
|
+
},
|
|
164
|
+
"workspace": {
|
|
165
|
+
"dependencies": [
|
|
166
|
+
"npm:luxon@3.7.2",
|
|
167
|
+
"npm:node-polyglot@^2.6.0"
|
|
168
|
+
]
|
|
169
|
+
}
|
|
170
|
+
}
|
package/deno.test.json
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"imports": {
|
|
3
|
+
"@jsb188/app": "../@jsb188:app/src/main.ts",
|
|
4
|
+
"@jsb188/app/": "../@jsb188:app/src/",
|
|
5
|
+
"@jsb188/mday/": "../@jsb188:mday/src/",
|
|
6
|
+
"@jsb188/react/": "../@jsb188:react/src/",
|
|
7
|
+
"luxon": "npm:luxon@3.7.2",
|
|
8
|
+
"node-polyglot": "npm:node-polyglot@^2.6.0"
|
|
9
|
+
}
|
|
10
|
+
}
|
package/drafts/index.ts
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type DraftSchema,
|
|
3
|
+
mergeSchemaDefaults,
|
|
4
|
+
pruneToSchema,
|
|
5
|
+
} from '@jsb188/app/utils/schema.ts';
|
|
6
|
+
import {
|
|
7
|
+
deleteAllDraftRecords,
|
|
8
|
+
deleteDraftRecord,
|
|
9
|
+
peekDraftRecord,
|
|
10
|
+
readDraftRecord,
|
|
11
|
+
writeDraftRecord,
|
|
12
|
+
} from './store.ts';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Expiring local objects ("drafts").
|
|
16
|
+
*
|
|
17
|
+
* A draft is one plain object persisted in IndexedDB under a caller-chosen
|
|
18
|
+
* key, readable until its TTL elapses. It exists for editing state that must
|
|
19
|
+
* survive a modal closing, a navigation, or a tab reload without becoming a
|
|
20
|
+
* server-side entity: composer text, selected uploads, form-in-progress.
|
|
21
|
+
*
|
|
22
|
+
* Keys are namespaced by the caller. The convention is
|
|
23
|
+
* "feature:version:scopeId", e.g. `createCampaignTemplate:v1:${orgId}` — the
|
|
24
|
+
* scope segment keeps one organization's draft out of another's, and the
|
|
25
|
+
* version segment is the escape hatch for a shape change the schema cannot
|
|
26
|
+
* catch (a field that keeps its type but changes its meaning).
|
|
27
|
+
*
|
|
28
|
+
* When a schema is passed, it prunes rather than rejects: values that do not
|
|
29
|
+
* match are dropped and everything else is kept, on read and on write alike.
|
|
30
|
+
* A draft written by an older build therefore degrades into a partial draft
|
|
31
|
+
* merged over defaultValue, never into an old shape reaching the UI.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
export type { DraftSchema, DraftSchemaNode } from '@jsb188/app/utils/schema.ts';
|
|
35
|
+
|
|
36
|
+
export interface DraftOptions<T> {
|
|
37
|
+
/* Returned on a miss; a hit is merged over it, so pruned fields fall back */
|
|
38
|
+
defaultValue?: T | null;
|
|
39
|
+
/* Values that fail the schema are dropped, never rejected wholesale */
|
|
40
|
+
schema?: DraftSchema;
|
|
41
|
+
/* Read: how old a record may be. Write: the sweep hint stored with it */
|
|
42
|
+
ttlHours?: number;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export const DRAFT_TTL_HOURS_DEFAULT = 24;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Apply the schema (when given) and merge the result over the default value.
|
|
49
|
+
*/
|
|
50
|
+
|
|
51
|
+
function resolveDraftValue<T>(
|
|
52
|
+
value: unknown,
|
|
53
|
+
options?: DraftOptions<T>,
|
|
54
|
+
): T | null {
|
|
55
|
+
const defaultValue = options?.defaultValue ?? null;
|
|
56
|
+
const prunedValue = options?.schema
|
|
57
|
+
? pruneToSchema(value, options.schema)
|
|
58
|
+
: value;
|
|
59
|
+
|
|
60
|
+
if (prunedValue === null || prunedValue === undefined) {
|
|
61
|
+
return defaultValue;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (defaultValue === null) {
|
|
65
|
+
return prunedValue as T;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return mergeSchemaDefaults(prunedValue, defaultValue);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Read one persisted object.
|
|
73
|
+
*
|
|
74
|
+
* Returns defaultValue (or null) when the key is missing, when its record has
|
|
75
|
+
* outlived ttlHours, or when IndexedDB is unavailable. An expired record is
|
|
76
|
+
* removed as part of the read.
|
|
77
|
+
*/
|
|
78
|
+
|
|
79
|
+
export async function getDraft<T>(
|
|
80
|
+
key: string,
|
|
81
|
+
options?: DraftOptions<T>,
|
|
82
|
+
): Promise<T | null> {
|
|
83
|
+
const record = await readDraftRecord(
|
|
84
|
+
key,
|
|
85
|
+
options?.ttlHours ?? DRAFT_TTL_HOURS_DEFAULT,
|
|
86
|
+
);
|
|
87
|
+
return resolveDraftValue<T>(record?.d, options);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Read one persisted object from the session mirror only.
|
|
92
|
+
*
|
|
93
|
+
* Synchronous, so it can seed React state on the first render, but it only
|
|
94
|
+
* sees keys already read or written in this session; a cold load must still go
|
|
95
|
+
* through getDraft().
|
|
96
|
+
*/
|
|
97
|
+
|
|
98
|
+
export function peekDraft<T>(key: string, options?: DraftOptions<T>): T | null {
|
|
99
|
+
const record = peekDraftRecord(
|
|
100
|
+
key,
|
|
101
|
+
options?.ttlHours ?? DRAFT_TTL_HOURS_DEFAULT,
|
|
102
|
+
);
|
|
103
|
+
return resolveDraftValue<T>(record?.d, options);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Write one persisted object, keeping only the values that pass the schema.
|
|
108
|
+
*
|
|
109
|
+
* defaultValue is ignored here; defaults are a read-time concern. Writes are
|
|
110
|
+
* immediate, so a caller writing on every keystroke should debounce.
|
|
111
|
+
*/
|
|
112
|
+
|
|
113
|
+
export async function setDraft<T>(
|
|
114
|
+
key: string,
|
|
115
|
+
value: T,
|
|
116
|
+
options?: DraftOptions<T>,
|
|
117
|
+
): Promise<void> {
|
|
118
|
+
const prunedValue = options?.schema
|
|
119
|
+
? pruneToSchema(value, options.schema)
|
|
120
|
+
: value;
|
|
121
|
+
if (prunedValue === null || prunedValue === undefined) {
|
|
122
|
+
// Nothing survived the schema; a stored draft would only resurrect a
|
|
123
|
+
// shape the caller no longer holds
|
|
124
|
+
await deleteDraftRecord(key);
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
await writeDraftRecord(
|
|
129
|
+
key,
|
|
130
|
+
prunedValue,
|
|
131
|
+
options?.ttlHours ?? DRAFT_TTL_HOURS_DEFAULT,
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Remove one persisted object.
|
|
137
|
+
*/
|
|
138
|
+
|
|
139
|
+
export function deleteDraft(key: string): Promise<void> {
|
|
140
|
+
return deleteDraftRecord(key);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Remove every persisted object (sign-out, account access revoked).
|
|
145
|
+
*/
|
|
146
|
+
|
|
147
|
+
export function clearAllDrafts() {
|
|
148
|
+
deleteAllDraftRecords();
|
|
149
|
+
}
|