@noy-db/in-pinia 0.2.0-pre.30 → 0.2.0-pre.31
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/package.json +6 -13
- package/dist/index.cjs +0 -615
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -622
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@noy-db/in-pinia",
|
|
3
|
-
"version": "0.2.0-pre.
|
|
3
|
+
"version": "0.2.0-pre.31",
|
|
4
4
|
"description": "Pinia integration for noy-db — defineNoydbStore() and the augmentation plugin for existing stores",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "vLannaAi <vicio@lanna.ai>",
|
|
@@ -17,17 +17,10 @@
|
|
|
17
17
|
"sideEffects": false,
|
|
18
18
|
"exports": {
|
|
19
19
|
".": {
|
|
20
|
-
"
|
|
21
|
-
|
|
22
|
-
"default": "./dist/index.js"
|
|
23
|
-
},
|
|
24
|
-
"require": {
|
|
25
|
-
"types": "./dist/index.d.cts",
|
|
26
|
-
"default": "./dist/index.cjs"
|
|
27
|
-
}
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"default": "./dist/index.js"
|
|
28
22
|
}
|
|
29
23
|
},
|
|
30
|
-
"main": "./dist/index.cjs",
|
|
31
24
|
"module": "./dist/index.js",
|
|
32
25
|
"types": "./dist/index.d.ts",
|
|
33
26
|
"files": [
|
|
@@ -36,19 +29,19 @@
|
|
|
36
29
|
"LICENSE"
|
|
37
30
|
],
|
|
38
31
|
"engines": {
|
|
39
|
-
"node": ">=
|
|
32
|
+
"node": ">=22.0.0"
|
|
40
33
|
},
|
|
41
34
|
"peerDependencies": {
|
|
42
35
|
"pinia": "^2.1.0 || ^3.0.0",
|
|
43
36
|
"vue": "^3.4.0",
|
|
44
|
-
"@noy-db/hub": "0.2.0-pre.
|
|
37
|
+
"@noy-db/hub": "0.2.0-pre.31"
|
|
45
38
|
},
|
|
46
39
|
"devDependencies": {
|
|
47
40
|
"@vue/test-utils": "^2.4.6",
|
|
48
41
|
"happy-dom": "^15.11.7",
|
|
49
42
|
"pinia": "^3.0.1",
|
|
50
43
|
"vue": "^3.5.32",
|
|
51
|
-
"@noy-db/hub": "0.2.0-pre.
|
|
44
|
+
"@noy-db/hub": "0.2.0-pre.31"
|
|
52
45
|
},
|
|
53
46
|
"keywords": [
|
|
54
47
|
"noy-db",
|
package/dist/index.cjs
DELETED
|
@@ -1,615 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/index.ts
|
|
21
|
-
var index_exports = {};
|
|
22
|
-
__export(index_exports, {
|
|
23
|
-
CAPABILITY_REQUESTS_COLLECTION: () => CAPABILITY_REQUESTS_COLLECTION,
|
|
24
|
-
createNoydbPiniaPlugin: () => createNoydbPiniaPlugin,
|
|
25
|
-
defineNoydbStore: () => defineNoydbStore,
|
|
26
|
-
getActiveNoydb: () => getActiveNoydb,
|
|
27
|
-
resolveNoydb: () => resolveNoydb,
|
|
28
|
-
setActiveNoydb: () => setActiveNoydb,
|
|
29
|
-
useCapabilityGrant: () => useCapabilityGrant,
|
|
30
|
-
useDictLabel: () => useDictLabel,
|
|
31
|
-
useI18nField: () => useI18nField,
|
|
32
|
-
useNoydbI18n: () => useNoydbI18n
|
|
33
|
-
});
|
|
34
|
-
module.exports = __toCommonJS(index_exports);
|
|
35
|
-
|
|
36
|
-
// src/defineNoydbStore.ts
|
|
37
|
-
var import_pinia2 = require("pinia");
|
|
38
|
-
var import_vue2 = require("vue");
|
|
39
|
-
|
|
40
|
-
// src/context.ts
|
|
41
|
-
var activeInstance = null;
|
|
42
|
-
function setActiveNoydb(instance) {
|
|
43
|
-
activeInstance = instance;
|
|
44
|
-
}
|
|
45
|
-
function getActiveNoydb() {
|
|
46
|
-
return activeInstance;
|
|
47
|
-
}
|
|
48
|
-
function resolveNoydb(explicit) {
|
|
49
|
-
if (explicit) return explicit;
|
|
50
|
-
if (activeInstance) return activeInstance;
|
|
51
|
-
throw new Error(
|
|
52
|
-
"@noy-db/pinia: no Noydb instance bound.\n Option A \u2014 pass `noydb:` directly to defineNoydbStore({...})\n Option B \u2014 call setActiveNoydb(instance) once at app startup\n Option C \u2014 install the @noy-db/nuxt module (Nuxt 4+)"
|
|
53
|
-
);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// src/useNoydbI18n.ts
|
|
57
|
-
var import_pinia = require("pinia");
|
|
58
|
-
var import_vue = require("vue");
|
|
59
|
-
var useNoydbI18n = (0, import_pinia.defineStore)("noydb-i18n", () => {
|
|
60
|
-
const locale = (0, import_vue.ref)("en");
|
|
61
|
-
const fallback = (0, import_vue.ref)(["en", "any"]);
|
|
62
|
-
function setLocale(l, opts) {
|
|
63
|
-
locale.value = l;
|
|
64
|
-
const sync = opts?.syncVault;
|
|
65
|
-
if (sync) {
|
|
66
|
-
const vaults = Array.isArray(sync) ? sync : [sync];
|
|
67
|
-
for (const v of vaults) v.setLocale(l);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
function setFallback(chain) {
|
|
71
|
-
fallback.value = chain;
|
|
72
|
-
}
|
|
73
|
-
function bindTo(source, opts) {
|
|
74
|
-
return (0, import_vue.watch)(
|
|
75
|
-
source,
|
|
76
|
-
(v) => {
|
|
77
|
-
locale.value = v;
|
|
78
|
-
},
|
|
79
|
-
// sync flush so the mirror propagates immediately (no tick lag) —
|
|
80
|
-
// a locale change should be observable synchronously by dependents.
|
|
81
|
-
{ immediate: opts?.immediate ?? true, flush: "sync" }
|
|
82
|
-
);
|
|
83
|
-
}
|
|
84
|
-
return { locale, fallback, setLocale, setFallback, bindTo };
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
// src/defineNoydbStore.ts
|
|
88
|
-
function defineNoydbStore(id, options) {
|
|
89
|
-
const collectionName = options.collection ?? id;
|
|
90
|
-
const prefetch = options.prefetch ?? true;
|
|
91
|
-
return (0, import_pinia2.defineStore)(id, () => {
|
|
92
|
-
const items = (0, import_vue2.shallowRef)([]);
|
|
93
|
-
const count = (0, import_vue2.computed)(() => items.value.length);
|
|
94
|
-
const i18nMode = options.i18n ?? "raw";
|
|
95
|
-
const i18nStore = i18nMode === "follow" ? useNoydbI18n() : null;
|
|
96
|
-
function localeOpts() {
|
|
97
|
-
if (i18nMode === "raw") return { locale: "raw" };
|
|
98
|
-
if (i18nMode === "follow") {
|
|
99
|
-
return { locale: i18nStore.locale, fallback: i18nStore.fallback };
|
|
100
|
-
}
|
|
101
|
-
const l = i18nMode.locale;
|
|
102
|
-
return {
|
|
103
|
-
locale: typeof l === "string" ? l : l.value,
|
|
104
|
-
...i18nMode.fallback !== void 0 ? { fallback: i18nMode.fallback } : {}
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
const resolveVaultName = () => typeof options.vault === "function" ? options.vault() : options.vault;
|
|
108
|
-
let cachedCompartment = null;
|
|
109
|
-
let cachedCollection = null;
|
|
110
|
-
let boundVaultName = null;
|
|
111
|
-
async function getCollection() {
|
|
112
|
-
const vaultName = resolveVaultName();
|
|
113
|
-
if (cachedCollection && boundVaultName === vaultName) return cachedCollection;
|
|
114
|
-
const noydb = resolveNoydb(options.noydb ?? null);
|
|
115
|
-
cachedCompartment = await noydb.openVault(vaultName);
|
|
116
|
-
const collOpts = {};
|
|
117
|
-
if (options.schema !== void 0) collOpts.schema = options.schema;
|
|
118
|
-
if (options.attestation !== void 0) collOpts.attestation = options.attestation;
|
|
119
|
-
if (options.persistJsonSchema !== void 0) collOpts.persistJsonSchema = options.persistJsonSchema;
|
|
120
|
-
if (options.schemaUpdate !== void 0) collOpts.schemaUpdate = options.schemaUpdate;
|
|
121
|
-
if (options.i18nFields !== void 0) collOpts.i18nFields = options.i18nFields;
|
|
122
|
-
if (options.dictKeyFields !== void 0) collOpts.dictKeyFields = options.dictKeyFields;
|
|
123
|
-
cachedCollection = cachedCompartment.collection(collectionName, collOpts);
|
|
124
|
-
boundVaultName = vaultName;
|
|
125
|
-
return cachedCollection;
|
|
126
|
-
}
|
|
127
|
-
async function refresh() {
|
|
128
|
-
const c = await getCollection();
|
|
129
|
-
const list = await c.list(localeOpts());
|
|
130
|
-
items.value = list;
|
|
131
|
-
}
|
|
132
|
-
function byId(id2) {
|
|
133
|
-
for (const item of items.value) {
|
|
134
|
-
if (item.id === id2) return item;
|
|
135
|
-
}
|
|
136
|
-
return void 0;
|
|
137
|
-
}
|
|
138
|
-
async function add(id2, record) {
|
|
139
|
-
const c = await getCollection();
|
|
140
|
-
await c.put(id2, record);
|
|
141
|
-
items.value = await c.list(localeOpts());
|
|
142
|
-
}
|
|
143
|
-
async function update(id2, record) {
|
|
144
|
-
await add(id2, record);
|
|
145
|
-
}
|
|
146
|
-
async function remove(id2) {
|
|
147
|
-
const c = await getCollection();
|
|
148
|
-
await c.delete(id2);
|
|
149
|
-
items.value = await c.list(localeOpts());
|
|
150
|
-
}
|
|
151
|
-
function query() {
|
|
152
|
-
if (!cachedCollection) {
|
|
153
|
-
throw new Error(
|
|
154
|
-
"@noy-db/pinia: query() called before the store was ready. Await store.$ready first, or set prefetch: true (default)."
|
|
155
|
-
);
|
|
156
|
-
}
|
|
157
|
-
return cachedCollection.query();
|
|
158
|
-
}
|
|
159
|
-
function liveQuery(build) {
|
|
160
|
-
if (!cachedCollection) {
|
|
161
|
-
throw new Error(
|
|
162
|
-
"@noy-db/pinia: liveQuery() called before the store was ready. Await store.$ready first, or set prefetch: true (default)."
|
|
163
|
-
);
|
|
164
|
-
}
|
|
165
|
-
const built = build(cachedCollection.query());
|
|
166
|
-
const live = built.live();
|
|
167
|
-
const items2 = (0, import_vue2.shallowRef)(live.value);
|
|
168
|
-
const error = (0, import_vue2.ref)(live.error);
|
|
169
|
-
const unsubscribe = live.subscribe(() => {
|
|
170
|
-
items2.value = live.value;
|
|
171
|
-
error.value = live.error;
|
|
172
|
-
});
|
|
173
|
-
let stopped = false;
|
|
174
|
-
const stop = () => {
|
|
175
|
-
if (stopped) return;
|
|
176
|
-
stopped = true;
|
|
177
|
-
unsubscribe();
|
|
178
|
-
live.stop();
|
|
179
|
-
};
|
|
180
|
-
if ((0, import_vue2.getCurrentScope)()) (0, import_vue2.onScopeDispose)(stop);
|
|
181
|
-
return { items: items2, error, stop };
|
|
182
|
-
}
|
|
183
|
-
const $ready = prefetch ? refresh() : Promise.resolve();
|
|
184
|
-
if (i18nMode === "follow") {
|
|
185
|
-
(0, import_vue2.watch)(
|
|
186
|
-
() => [i18nStore.locale, i18nStore.fallback],
|
|
187
|
-
() => {
|
|
188
|
-
void refresh();
|
|
189
|
-
}
|
|
190
|
-
);
|
|
191
|
-
} else if (typeof i18nMode === "object" && (0, import_vue2.isRef)(i18nMode.locale)) {
|
|
192
|
-
(0, import_vue2.watch)(i18nMode.locale, () => {
|
|
193
|
-
void refresh();
|
|
194
|
-
});
|
|
195
|
-
}
|
|
196
|
-
if (typeof options.vault === "function") {
|
|
197
|
-
(0, import_vue2.watch)(resolveVaultName, (next, prev) => {
|
|
198
|
-
if (next !== prev) void refresh();
|
|
199
|
-
});
|
|
200
|
-
}
|
|
201
|
-
return {
|
|
202
|
-
items,
|
|
203
|
-
count,
|
|
204
|
-
$ready,
|
|
205
|
-
byId,
|
|
206
|
-
add,
|
|
207
|
-
update,
|
|
208
|
-
remove,
|
|
209
|
-
refresh,
|
|
210
|
-
query,
|
|
211
|
-
liveQuery
|
|
212
|
-
};
|
|
213
|
-
});
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
// src/useI18nField.ts
|
|
217
|
-
var import_vue3 = require("vue");
|
|
218
|
-
var import_i18n = require("@noy-db/hub/i18n");
|
|
219
|
-
function useI18nField(source, opts = {}) {
|
|
220
|
-
const i18n = useNoydbI18n();
|
|
221
|
-
return (0, import_vue3.computed)(() => {
|
|
222
|
-
const map = typeof source === "function" ? source() : source;
|
|
223
|
-
if (!map || typeof map !== "object") return null;
|
|
224
|
-
const locale = opts.locale !== void 0 ? (0, import_vue3.unref)(opts.locale) : i18n.locale;
|
|
225
|
-
const fallback = opts.fallback ?? i18n.fallback;
|
|
226
|
-
const out = (0, import_i18n.resolveI18nText)(
|
|
227
|
-
map,
|
|
228
|
-
locale,
|
|
229
|
-
fallback,
|
|
230
|
-
void 0,
|
|
231
|
-
{ policy: "null" }
|
|
232
|
-
);
|
|
233
|
-
return typeof out === "string" ? out : null;
|
|
234
|
-
});
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
// src/useDictLabel.ts
|
|
238
|
-
var import_vue4 = require("vue");
|
|
239
|
-
function useDictLabel(dictionaryName, options = {}) {
|
|
240
|
-
const vault = resolveVault(options.vault);
|
|
241
|
-
const handle = vault.dictionary(dictionaryName);
|
|
242
|
-
let storeLocale = null;
|
|
243
|
-
let storeFallback = null;
|
|
244
|
-
if (options.locale === void 0 || options.fallback === void 0) {
|
|
245
|
-
try {
|
|
246
|
-
const i18n = useNoydbI18n();
|
|
247
|
-
storeLocale = (0, import_vue4.toRef)(i18n, "locale");
|
|
248
|
-
storeFallback = i18n.fallback;
|
|
249
|
-
} catch {
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
const localeRef = options.locale !== void 0 ? normaliseLocale(options.locale) : storeLocale ?? (0, import_vue4.ref)("en");
|
|
253
|
-
const fallback = options.fallback ?? storeFallback ?? ["en", "any"];
|
|
254
|
-
const onMissingMode = options.onMissing ?? "key";
|
|
255
|
-
const missing = (key) => onMissingMode === "empty" ? "" : onMissingMode === "placeholder" ? `\u27E8missing:${key}\u27E9` : key;
|
|
256
|
-
const cache = /* @__PURE__ */ new Map();
|
|
257
|
-
const db = resolveNoydb();
|
|
258
|
-
const dictCollection = `_dict_${dictionaryName}`;
|
|
259
|
-
const onChange = (event) => {
|
|
260
|
-
if (event.collection !== dictCollection) return;
|
|
261
|
-
for (const [key, r] of cache) {
|
|
262
|
-
void refresh(key, r);
|
|
263
|
-
}
|
|
264
|
-
};
|
|
265
|
-
db.on("change", onChange);
|
|
266
|
-
(0, import_vue4.watch)(localeRef, () => {
|
|
267
|
-
for (const [key, r] of cache) {
|
|
268
|
-
void refresh(key, r);
|
|
269
|
-
}
|
|
270
|
-
});
|
|
271
|
-
async function refresh(key, r) {
|
|
272
|
-
try {
|
|
273
|
-
const resolved = await handle.resolveLabel(
|
|
274
|
-
key,
|
|
275
|
-
localeRef.value,
|
|
276
|
-
fallback
|
|
277
|
-
);
|
|
278
|
-
r.value = resolved ?? missing(key);
|
|
279
|
-
} catch {
|
|
280
|
-
r.value = missing(key);
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
return (key) => {
|
|
284
|
-
let r = cache.get(key);
|
|
285
|
-
if (r) return r;
|
|
286
|
-
r = (0, import_vue4.shallowRef)(missing(key));
|
|
287
|
-
cache.set(key, r);
|
|
288
|
-
void refresh(key, r);
|
|
289
|
-
return r;
|
|
290
|
-
};
|
|
291
|
-
}
|
|
292
|
-
function resolveVault(source) {
|
|
293
|
-
const db = resolveNoydb();
|
|
294
|
-
if (source && typeof source !== "string") return source;
|
|
295
|
-
if (typeof source === "string") {
|
|
296
|
-
return db.vault(source);
|
|
297
|
-
}
|
|
298
|
-
const anyDb = db;
|
|
299
|
-
if (anyDb.vaultCache && anyDb.vaultCache.size > 0) {
|
|
300
|
-
return [...anyDb.vaultCache.values()][0];
|
|
301
|
-
}
|
|
302
|
-
throw new Error(
|
|
303
|
-
'[@noy-db/in-pinia] useDictLabel: no open vault. Pass `{ vault: "name" }` or `await db.openVault(name)` first.'
|
|
304
|
-
);
|
|
305
|
-
}
|
|
306
|
-
function normaliseLocale(locale) {
|
|
307
|
-
if (locale === void 0) return (0, import_vue4.ref)("en");
|
|
308
|
-
if (typeof locale === "string") return (0, import_vue4.ref)(locale);
|
|
309
|
-
return locale;
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
// src/plugin.ts
|
|
313
|
-
var import_hub = require("@noy-db/hub");
|
|
314
|
-
var STATE_DOC_ID = "__state__";
|
|
315
|
-
function createNoydbPiniaPlugin(opts) {
|
|
316
|
-
let dbPromise = null;
|
|
317
|
-
function getDb() {
|
|
318
|
-
if (!dbPromise) {
|
|
319
|
-
dbPromise = (async () => {
|
|
320
|
-
const secret = await opts.secret();
|
|
321
|
-
return (0, import_hub.createNoydb)({
|
|
322
|
-
store: opts.adapter,
|
|
323
|
-
user: opts.user,
|
|
324
|
-
secret,
|
|
325
|
-
...opts.noydbOptions
|
|
326
|
-
});
|
|
327
|
-
})();
|
|
328
|
-
}
|
|
329
|
-
return dbPromise;
|
|
330
|
-
}
|
|
331
|
-
const vaultCache = /* @__PURE__ */ new Map();
|
|
332
|
-
function getCompartment(name) {
|
|
333
|
-
let p = vaultCache.get(name);
|
|
334
|
-
if (!p) {
|
|
335
|
-
p = getDb().then((db) => db.openVault(name));
|
|
336
|
-
vaultCache.set(name, p);
|
|
337
|
-
}
|
|
338
|
-
return p;
|
|
339
|
-
}
|
|
340
|
-
return (context) => {
|
|
341
|
-
const noydbOption = context.options.noydb;
|
|
342
|
-
if (!noydbOption) {
|
|
343
|
-
context.store.$noydbAugmented = false;
|
|
344
|
-
return;
|
|
345
|
-
}
|
|
346
|
-
context.store.$noydbAugmented = true;
|
|
347
|
-
context.store.$noydbError = null;
|
|
348
|
-
const pending = /* @__PURE__ */ new Set();
|
|
349
|
-
const ready = (async () => {
|
|
350
|
-
try {
|
|
351
|
-
const vault = await getCompartment(noydbOption.vault);
|
|
352
|
-
const collection = vault.collection(
|
|
353
|
-
noydbOption.collection
|
|
354
|
-
);
|
|
355
|
-
const persisted = await collection.get(STATE_DOC_ID);
|
|
356
|
-
if (persisted) {
|
|
357
|
-
const validated = noydbOption.schema ? noydbOption.schema.parse(persisted) : persisted;
|
|
358
|
-
const picked = pickKeys(validated, noydbOption.persist);
|
|
359
|
-
context.store.$patch(picked);
|
|
360
|
-
}
|
|
361
|
-
context.store.$subscribe(
|
|
362
|
-
(_mutation, state) => {
|
|
363
|
-
const subset = pickKeys(state, noydbOption.persist);
|
|
364
|
-
const p = collection.put(STATE_DOC_ID, subset).catch((err) => {
|
|
365
|
-
context.store.$noydbError = err instanceof Error ? err : new Error(String(err));
|
|
366
|
-
}).finally(() => {
|
|
367
|
-
pending.delete(p);
|
|
368
|
-
});
|
|
369
|
-
pending.add(p);
|
|
370
|
-
},
|
|
371
|
-
{ detached: true }
|
|
372
|
-
// outlive the component that triggered the mutation
|
|
373
|
-
);
|
|
374
|
-
} catch (err) {
|
|
375
|
-
context.store.$noydbError = err instanceof Error ? err : new Error(String(err));
|
|
376
|
-
}
|
|
377
|
-
})();
|
|
378
|
-
context.store.$noydbReady = ready;
|
|
379
|
-
context.store.$noydbFlush = async () => {
|
|
380
|
-
await ready;
|
|
381
|
-
while (pending.size > 0) {
|
|
382
|
-
await Promise.all([...pending]);
|
|
383
|
-
}
|
|
384
|
-
};
|
|
385
|
-
};
|
|
386
|
-
}
|
|
387
|
-
function pickKeys(state, persist) {
|
|
388
|
-
if (persist === void 0 || persist === "*") {
|
|
389
|
-
return { ...state };
|
|
390
|
-
}
|
|
391
|
-
if (typeof persist === "string") {
|
|
392
|
-
return { [persist]: state[persist] };
|
|
393
|
-
}
|
|
394
|
-
if (Array.isArray(persist)) {
|
|
395
|
-
const out = {};
|
|
396
|
-
for (const key of persist) {
|
|
397
|
-
out[key] = state[key];
|
|
398
|
-
}
|
|
399
|
-
return out;
|
|
400
|
-
}
|
|
401
|
-
return { ...state };
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
// src/useCapabilityGrant.ts
|
|
405
|
-
var import_vue5 = require("vue");
|
|
406
|
-
var CAPABILITY_REQUESTS_COLLECTION = "_capability_requests";
|
|
407
|
-
function useCapabilityGrant(capability, options) {
|
|
408
|
-
const state = (0, import_vue5.ref)("idle");
|
|
409
|
-
const error = (0, import_vue5.ref)(null);
|
|
410
|
-
const recordRef = (0, import_vue5.shallowRef)(null);
|
|
411
|
-
const inBrowser = typeof window !== "undefined";
|
|
412
|
-
let expiryTimer = null;
|
|
413
|
-
let unsubscribeChangeStream = null;
|
|
414
|
-
let resolvedVault = null;
|
|
415
|
-
let stopped = false;
|
|
416
|
-
async function resolveVault2() {
|
|
417
|
-
if (resolvedVault) return resolvedVault;
|
|
418
|
-
if (typeof options.vault === "string") {
|
|
419
|
-
const noydb = resolveNoydb(null);
|
|
420
|
-
resolvedVault = await noydb.openVault(options.vault);
|
|
421
|
-
} else {
|
|
422
|
-
resolvedVault = options.vault;
|
|
423
|
-
}
|
|
424
|
-
resolvedVault.collection(
|
|
425
|
-
CAPABILITY_REQUESTS_COLLECTION
|
|
426
|
-
);
|
|
427
|
-
return resolvedVault;
|
|
428
|
-
}
|
|
429
|
-
function clearExpiryTimer() {
|
|
430
|
-
if (expiryTimer) {
|
|
431
|
-
clearTimeout(expiryTimer);
|
|
432
|
-
expiryTimer = null;
|
|
433
|
-
}
|
|
434
|
-
}
|
|
435
|
-
function scheduleExpiry(record) {
|
|
436
|
-
if (!record.expiresAt) return;
|
|
437
|
-
const remaining = new Date(record.expiresAt).getTime() - Date.now();
|
|
438
|
-
if (remaining <= 0) {
|
|
439
|
-
void handleExpiry(record);
|
|
440
|
-
return;
|
|
441
|
-
}
|
|
442
|
-
clearExpiryTimer();
|
|
443
|
-
expiryTimer = setTimeout(() => {
|
|
444
|
-
void handleExpiry(record);
|
|
445
|
-
}, remaining);
|
|
446
|
-
}
|
|
447
|
-
async function handleExpiry(record) {
|
|
448
|
-
if (stopped) return;
|
|
449
|
-
if (state.value !== "granted") return;
|
|
450
|
-
state.value = "expired";
|
|
451
|
-
try {
|
|
452
|
-
await options.onRelease?.({
|
|
453
|
-
record,
|
|
454
|
-
vault: resolvedVault,
|
|
455
|
-
cause: "expired"
|
|
456
|
-
});
|
|
457
|
-
} catch (err) {
|
|
458
|
-
error.value = err instanceof Error ? err : new Error(String(err));
|
|
459
|
-
}
|
|
460
|
-
state.value = "idle";
|
|
461
|
-
recordRef.value = null;
|
|
462
|
-
}
|
|
463
|
-
const now = (0, import_vue5.ref)(Date.now());
|
|
464
|
-
const tickTimer = inBrowser ? setInterval(() => {
|
|
465
|
-
now.value = Date.now();
|
|
466
|
-
}, 1e3) : null;
|
|
467
|
-
const timeRemaining = (0, import_vue5.computed)(() => {
|
|
468
|
-
if (state.value !== "granted" || !recordRef.value?.expiresAt) return 0;
|
|
469
|
-
void now.value;
|
|
470
|
-
const ms = new Date(recordRef.value.expiresAt).getTime() - Date.now();
|
|
471
|
-
return ms > 0 ? ms : 0;
|
|
472
|
-
});
|
|
473
|
-
(0, import_vue5.watch)(
|
|
474
|
-
() => recordRef.value?.id,
|
|
475
|
-
async (id) => {
|
|
476
|
-
if (!inBrowser || !id || unsubscribeChangeStream) return;
|
|
477
|
-
const vault = await resolveVault2();
|
|
478
|
-
const coll = vault.collection(
|
|
479
|
-
CAPABILITY_REQUESTS_COLLECTION
|
|
480
|
-
);
|
|
481
|
-
unsubscribeChangeStream = coll.subscribe(
|
|
482
|
-
(evt) => {
|
|
483
|
-
if (evt.type !== "put" || evt.id !== id) return;
|
|
484
|
-
const updated = evt.record;
|
|
485
|
-
if (!updated || stopped) return;
|
|
486
|
-
recordRef.value = updated;
|
|
487
|
-
if (updated.status === "granted") {
|
|
488
|
-
state.value = "granted";
|
|
489
|
-
scheduleExpiry(updated);
|
|
490
|
-
} else if (updated.status === "released" || updated.status === "expired") {
|
|
491
|
-
state.value = "idle";
|
|
492
|
-
clearExpiryTimer();
|
|
493
|
-
}
|
|
494
|
-
}
|
|
495
|
-
);
|
|
496
|
-
},
|
|
497
|
-
{ immediate: false }
|
|
498
|
-
);
|
|
499
|
-
async function request() {
|
|
500
|
-
if (state.value !== "idle") {
|
|
501
|
-
error.value = new Error(
|
|
502
|
-
`useCapabilityGrant: cannot request from state "${state.value}"`
|
|
503
|
-
);
|
|
504
|
-
throw error.value;
|
|
505
|
-
}
|
|
506
|
-
error.value = null;
|
|
507
|
-
if (!inBrowser) return;
|
|
508
|
-
try {
|
|
509
|
-
const vault = await resolveVault2();
|
|
510
|
-
const coll = vault.collection(
|
|
511
|
-
CAPABILITY_REQUESTS_COLLECTION
|
|
512
|
-
);
|
|
513
|
-
const id = `cap-${Date.now().toString(36)}-${Math.random().toString(16).slice(2, 10)}`;
|
|
514
|
-
const record = {
|
|
515
|
-
id,
|
|
516
|
-
capability,
|
|
517
|
-
requestedBy: vault.userId,
|
|
518
|
-
approverRole: options.approver,
|
|
519
|
-
reason: options.reason,
|
|
520
|
-
ttlMs: options.ttlMs,
|
|
521
|
-
status: "requested",
|
|
522
|
-
requestedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
523
|
-
};
|
|
524
|
-
await coll.put(id, record);
|
|
525
|
-
recordRef.value = record;
|
|
526
|
-
state.value = "requested";
|
|
527
|
-
} catch (err) {
|
|
528
|
-
error.value = err instanceof Error ? err : new Error(String(err));
|
|
529
|
-
throw error.value;
|
|
530
|
-
}
|
|
531
|
-
}
|
|
532
|
-
async function approve() {
|
|
533
|
-
const record = recordRef.value;
|
|
534
|
-
if (state.value !== "requested" || !record) {
|
|
535
|
-
error.value = new Error(
|
|
536
|
-
`useCapabilityGrant: cannot approve from state "${state.value}"`
|
|
537
|
-
);
|
|
538
|
-
throw error.value;
|
|
539
|
-
}
|
|
540
|
-
error.value = null;
|
|
541
|
-
try {
|
|
542
|
-
const vault = await resolveVault2();
|
|
543
|
-
if (vault.role !== options.approver && vault.role !== "owner") {
|
|
544
|
-
throw new Error(
|
|
545
|
-
`useCapabilityGrant: caller role "${vault.role}" cannot approve a "${options.approver}"-tier grant`
|
|
546
|
-
);
|
|
547
|
-
}
|
|
548
|
-
const approvedAt = /* @__PURE__ */ new Date();
|
|
549
|
-
const expiresAt = new Date(approvedAt.getTime() + options.ttlMs);
|
|
550
|
-
const granted = {
|
|
551
|
-
...record,
|
|
552
|
-
status: "granted",
|
|
553
|
-
approvedBy: vault.userId,
|
|
554
|
-
approvedAt: approvedAt.toISOString(),
|
|
555
|
-
expiresAt: expiresAt.toISOString()
|
|
556
|
-
};
|
|
557
|
-
const coll = vault.collection(
|
|
558
|
-
CAPABILITY_REQUESTS_COLLECTION
|
|
559
|
-
);
|
|
560
|
-
await coll.put(record.id, granted);
|
|
561
|
-
recordRef.value = granted;
|
|
562
|
-
state.value = "granted";
|
|
563
|
-
scheduleExpiry(granted);
|
|
564
|
-
await options.onGrant?.({ record: granted, vault });
|
|
565
|
-
} catch (err) {
|
|
566
|
-
error.value = err instanceof Error ? err : new Error(String(err));
|
|
567
|
-
throw error.value;
|
|
568
|
-
}
|
|
569
|
-
}
|
|
570
|
-
async function release() {
|
|
571
|
-
const record = recordRef.value;
|
|
572
|
-
if (state.value !== "granted" || !record) {
|
|
573
|
-
return;
|
|
574
|
-
}
|
|
575
|
-
error.value = null;
|
|
576
|
-
try {
|
|
577
|
-
clearExpiryTimer();
|
|
578
|
-
const vault = await resolveVault2();
|
|
579
|
-
const released = { ...record, status: "released" };
|
|
580
|
-
const coll = vault.collection(
|
|
581
|
-
CAPABILITY_REQUESTS_COLLECTION
|
|
582
|
-
);
|
|
583
|
-
await coll.put(record.id, released);
|
|
584
|
-
recordRef.value = null;
|
|
585
|
-
state.value = "idle";
|
|
586
|
-
await options.onRelease?.({ record, vault, cause: "released" });
|
|
587
|
-
} catch (err) {
|
|
588
|
-
error.value = err instanceof Error ? err : new Error(String(err));
|
|
589
|
-
throw error.value;
|
|
590
|
-
}
|
|
591
|
-
}
|
|
592
|
-
if ((0, import_vue5.getCurrentScope)()) {
|
|
593
|
-
(0, import_vue5.onScopeDispose)(() => {
|
|
594
|
-
stopped = true;
|
|
595
|
-
clearExpiryTimer();
|
|
596
|
-
if (tickTimer) clearInterval(tickTimer);
|
|
597
|
-
unsubscribeChangeStream?.();
|
|
598
|
-
});
|
|
599
|
-
}
|
|
600
|
-
return { state, timeRemaining, error, request, approve, release };
|
|
601
|
-
}
|
|
602
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
603
|
-
0 && (module.exports = {
|
|
604
|
-
CAPABILITY_REQUESTS_COLLECTION,
|
|
605
|
-
createNoydbPiniaPlugin,
|
|
606
|
-
defineNoydbStore,
|
|
607
|
-
getActiveNoydb,
|
|
608
|
-
resolveNoydb,
|
|
609
|
-
setActiveNoydb,
|
|
610
|
-
useCapabilityGrant,
|
|
611
|
-
useDictLabel,
|
|
612
|
-
useI18nField,
|
|
613
|
-
useNoydbI18n
|
|
614
|
-
});
|
|
615
|
-
//# sourceMappingURL=index.cjs.map
|