@djangocfg/api 2.1.581 → 2.1.583
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 +18 -0
- package/dist/auth.cjs +272 -80
- package/dist/auth.cjs.map +1 -1
- package/dist/auth.mjs +272 -80
- package/dist/auth.mjs.map +1 -1
- package/dist/clients.cjs +243 -54
- package/dist/clients.cjs.map +1 -1
- package/dist/clients.mjs +243 -54
- package/dist/clients.mjs.map +1 -1
- package/dist/hooks.cjs +243 -54
- package/dist/hooks.cjs.map +1 -1
- package/dist/hooks.mjs +243 -54
- package/dist/hooks.mjs.map +1 -1
- package/dist/index.cjs +248 -54
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +85 -1
- package/dist/index.d.ts +85 -1
- package/dist/index.mjs +248 -54
- package/dist/index.mjs.map +1 -1
- package/dist/storage.cjs +269 -0
- package/dist/storage.cjs.map +1 -0
- package/dist/storage.d.cts +85 -0
- package/dist/storage.d.ts +85 -0
- package/dist/storage.mjs +248 -0
- package/dist/storage.mjs.map +1 -0
- package/package.json +8 -2
- package/src/__tests__/noStorageLiterals.test.ts +27 -0
- package/src/__tests__/storage.test.ts +134 -0
- package/src/__tests__/storageSsr.test.ts +14 -0
- package/src/_api/generated/helpers/auth.ts +27 -64
- package/src/_api/generated/helpers/index.ts +12 -0
- package/src/_api/generated/helpers/keys.ts +328 -0
- package/src/auth/__tests__/authRedirect.test.ts +1 -1
- package/src/auth/__tests__/profileCache.test.ts +1 -1
- package/src/auth/context/AuthContext.tsx +2 -1
- package/src/auth/hooks/useAuthRedirect.ts +3 -2
- package/src/auth/hooks/useGithubAuth.ts +13 -8
- package/src/auth/hooks/useLocalStorage.ts +7 -7
- package/src/auth/hooks/useProfileCache.ts +2 -1
- package/src/auth/hooks/useSessionStorage.ts +7 -7
- package/src/storage.ts +21 -0
package/dist/index.cjs
CHANGED
|
@@ -23,6 +23,8 @@ var index_exports = {};
|
|
|
23
23
|
__export(index_exports, {
|
|
24
24
|
APIError: () => APIError,
|
|
25
25
|
APILogger: () => APILogger,
|
|
26
|
+
CFG_FAMILIES: () => CFG_FAMILIES,
|
|
27
|
+
CFG_KEYS: () => CFG_KEYS,
|
|
26
28
|
CfgAccounts: () => CfgAccountsOtp,
|
|
27
29
|
CfgAccountsAPI: () => API,
|
|
28
30
|
CfgAccountsApi: () => CfgAccountsApi,
|
|
@@ -44,6 +46,8 @@ __export(index_exports, {
|
|
|
44
46
|
api: () => CfgAccountsApi,
|
|
45
47
|
applyRoleLogPolicy: () => applyRoleLogPolicy,
|
|
46
48
|
auth: () => auth,
|
|
49
|
+
cfgStorage: () => cfgStorage,
|
|
50
|
+
createStorage: () => createStorage,
|
|
47
51
|
defaultLogger: () => defaultLogger,
|
|
48
52
|
dispatchValidationError: () => dispatchValidationError,
|
|
49
53
|
dpopEnabled: () => dpopEnabled2,
|
|
@@ -53,6 +57,7 @@ __export(index_exports, {
|
|
|
53
57
|
isDev: () => isDev,
|
|
54
58
|
isProd: () => isProd,
|
|
55
59
|
isStaticBuild: () => isStaticBuild,
|
|
60
|
+
normalizeSegment: () => normalizeSegment,
|
|
56
61
|
onLogLevelChange: () => onLogLevelChange,
|
|
57
62
|
onValidationError: () => onValidationError,
|
|
58
63
|
setLogLevel: () => setLogLevel
|
|
@@ -136,77 +141,264 @@ var NetworkError = class extends Error {
|
|
|
136
141
|
}
|
|
137
142
|
};
|
|
138
143
|
|
|
139
|
-
// src/_api/generated/helpers/
|
|
140
|
-
var
|
|
141
|
-
var
|
|
142
|
-
var
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
144
|
+
// src/_api/generated/helpers/keys.ts
|
|
145
|
+
var k = /* @__PURE__ */ __name((key, store, personal) => ({ key, store, personal }), "k");
|
|
146
|
+
var f = /* @__PURE__ */ __name((prefix, store, personal) => ({ prefix, store, personal }), "f");
|
|
147
|
+
var CFG_KEYS = {
|
|
148
|
+
accessToken: k("cfg.auth.access_token", "local", true),
|
|
149
|
+
refreshToken: k("cfg.auth.refresh_token", "local", true),
|
|
150
|
+
apiKey: k("cfg.auth.api_key", "local", true),
|
|
151
|
+
email: k("cfg.auth.email", "local", false),
|
|
152
|
+
emailTs: k("cfg.auth.email_ts", "local", false),
|
|
153
|
+
redirectUrl: k("cfg.auth.redirect_url", "session", true),
|
|
154
|
+
redirectUrlTs: k("cfg.auth.redirect_url_ts", "session", true),
|
|
155
|
+
oauthState: k("cfg.auth.oauth_state", "session", true),
|
|
156
|
+
oauthProvider: k("cfg.auth.oauth_provider", "session", true),
|
|
157
|
+
oauthRememberMe: k("cfg.auth.oauth_remember_me", "session", true),
|
|
158
|
+
profileCache: k("cfg.profile.cache", "local", true),
|
|
159
|
+
monitorSessionId: k("cfg.monitor.session_id", "local", false),
|
|
160
|
+
monitorSessionCookie: k("cfg.monitor.session_id", "cookie", false),
|
|
161
|
+
monitorDebug: k("cfg.monitor.debug", "local", false),
|
|
162
|
+
uiTabId: k("cfg.ui.tab_id", "session", false),
|
|
163
|
+
uiRouterSerial: k("cfg.ui.router_serial", "session", false),
|
|
164
|
+
uiState: k("cfg.ui.state", "local", false),
|
|
165
|
+
chatSpeechLanguage: k("cfg.chat.speech_language", "local", false),
|
|
166
|
+
chatRecentTools: k("cfg.chat.recent_tools", "local", false),
|
|
167
|
+
formsSpeechDebug: k("cfg.forms.speech_debug", "local", false),
|
|
168
|
+
formsSpeechPrefs: k("cfg.forms.speech_prefs", "local", false),
|
|
169
|
+
agentPlaygroundApiKeyId: k("cfg.agent.playground_api_key_id", "session", true),
|
|
170
|
+
agentPlaygroundBearer: k("cfg.agent.playground_bearer", "session", true),
|
|
171
|
+
agentEndpointDoc: k("cfg.agent.endpoint_doc", "session", false)
|
|
172
|
+
};
|
|
173
|
+
var CFG_FAMILIES = {
|
|
174
|
+
chatDraft: f("cfg.chat.draft.", "session", true),
|
|
175
|
+
dataTree: f("cfg.data.tree.", "local", false),
|
|
176
|
+
agentEndpointDraft: f("cfg.agent.endpoint_draft.", "local", false)
|
|
177
|
+
};
|
|
178
|
+
var OWNER_RE = /^[a-z][a-z0-9_]*$/;
|
|
179
|
+
function normalizeSegment(raw) {
|
|
180
|
+
return raw.trim().toLowerCase().replace(/[^a-z0-9_]+/g, "_").replace(/^_+|_+$/g, "");
|
|
181
|
+
}
|
|
182
|
+
__name(normalizeSegment, "normalizeSegment");
|
|
183
|
+
var hasWindow = /* @__PURE__ */ __name(() => typeof window !== "undefined", "hasWindow");
|
|
184
|
+
function nativeStorage(store) {
|
|
185
|
+
if (!hasWindow()) return null;
|
|
186
|
+
try {
|
|
187
|
+
return store === "session" ? window.sessionStorage : window.localStorage;
|
|
188
|
+
} catch {
|
|
189
|
+
return null;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
__name(nativeStorage, "nativeStorage");
|
|
193
|
+
function cookieNames() {
|
|
194
|
+
if (!hasWindow()) return [];
|
|
195
|
+
try {
|
|
196
|
+
return document.cookie.split("; ").map((part) => decodeURIComponent(part.split("=")[0] ?? "")).filter(Boolean);
|
|
197
|
+
} catch {
|
|
198
|
+
return [];
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
__name(cookieNames, "cookieNames");
|
|
202
|
+
function createStorage(owner, options = {}) {
|
|
203
|
+
if (!OWNER_RE.test(owner)) {
|
|
204
|
+
throw new TypeError(`createStorage: owner "${owner}" must match ${OWNER_RE}`);
|
|
205
|
+
}
|
|
206
|
+
const prefix = `${owner}.`;
|
|
207
|
+
const pattern = owner === "cfg" ? /^cfg(\.[a-z0-9_]+)+$/ : new RegExp(`^(cfg|${owner})(\\.[a-z0-9_]+)+$`);
|
|
208
|
+
const cookieMaxAge = options.cookieMaxAgeSeconds ?? 60 * 60 * 24 * 30;
|
|
209
|
+
const specs = Object.values(options.keys ?? {});
|
|
210
|
+
const families = Object.values(options.families ?? {});
|
|
211
|
+
const specByKey = /* @__PURE__ */ new Map();
|
|
212
|
+
for (const spec of specs) specByKey.set(spec.key, [...specByKey.get(spec.key) ?? [], spec]);
|
|
213
|
+
const memory = {
|
|
214
|
+
local: /* @__PURE__ */ new Map(),
|
|
215
|
+
session: /* @__PURE__ */ new Map(),
|
|
216
|
+
cookie: /* @__PURE__ */ new Map()
|
|
217
|
+
};
|
|
218
|
+
const isValid = /* @__PURE__ */ __name((key) => pattern.test(key), "isValid");
|
|
219
|
+
const assertValid = /* @__PURE__ */ __name((key) => {
|
|
220
|
+
if (!isValid(key)) throw new TypeError(`storage key "${key}" must match ${pattern}`);
|
|
221
|
+
}, "assertValid");
|
|
222
|
+
const assertOwned = /* @__PURE__ */ __name((key) => {
|
|
223
|
+
assertValid(key);
|
|
224
|
+
if (!key.startsWith(prefix)) throw new TypeError(`storage key "${key}" is not owned by "${owner}"`);
|
|
225
|
+
}, "assertOwned");
|
|
226
|
+
function rawGet(store, key) {
|
|
227
|
+
if (!hasWindow()) return null;
|
|
228
|
+
if (store === "cookie") {
|
|
229
|
+
try {
|
|
230
|
+
const escaped = encodeURIComponent(key).replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
231
|
+
const match = document.cookie.match(new RegExp(`(?:^|;\\s*)${escaped}=([^;]*)`));
|
|
232
|
+
if (match?.[1] !== void 0) return decodeURIComponent(match[1]);
|
|
233
|
+
} catch {
|
|
234
|
+
}
|
|
235
|
+
return memory.cookie.get(key) ?? null;
|
|
151
236
|
}
|
|
152
|
-
},
|
|
153
|
-
set(key, value) {
|
|
154
|
-
if (!isBrowser) return;
|
|
155
237
|
try {
|
|
156
|
-
|
|
157
|
-
|
|
238
|
+
const value = nativeStorage(store)?.getItem(key) ?? null;
|
|
239
|
+
if (value !== null) return value;
|
|
158
240
|
} catch {
|
|
159
241
|
}
|
|
242
|
+
return memory[store].get(key) ?? null;
|
|
160
243
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
if (!isBrowser) return null;
|
|
244
|
+
__name(rawGet, "rawGet");
|
|
245
|
+
function rawSet(store, key, value) {
|
|
246
|
+
if (!hasWindow()) return false;
|
|
165
247
|
try {
|
|
166
|
-
|
|
248
|
+
if (store === "cookie") {
|
|
249
|
+
const secure = window.location.protocol === "https:" ? "; Secure" : "";
|
|
250
|
+
document.cookie = `${encodeURIComponent(key)}=${encodeURIComponent(value)}; Path=/; Max-Age=${cookieMaxAge}; SameSite=Lax${secure}`;
|
|
251
|
+
if (rawGet("cookie", key) === value && !memory.cookie.has(key)) return true;
|
|
252
|
+
memory.cookie.set(key, value);
|
|
253
|
+
return false;
|
|
254
|
+
}
|
|
255
|
+
const native = nativeStorage(store);
|
|
256
|
+
if (!native) throw new Error("storage unavailable");
|
|
257
|
+
native.setItem(key, value);
|
|
258
|
+
memory[store].delete(key);
|
|
259
|
+
return true;
|
|
167
260
|
} catch {
|
|
168
|
-
|
|
261
|
+
memory[store].set(key, value);
|
|
262
|
+
return false;
|
|
169
263
|
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
|
|
264
|
+
}
|
|
265
|
+
__name(rawSet, "rawSet");
|
|
266
|
+
function rawRemove(store, key) {
|
|
267
|
+
memory[store].delete(key);
|
|
268
|
+
if (!hasWindow()) return;
|
|
173
269
|
try {
|
|
174
|
-
if (
|
|
175
|
-
|
|
270
|
+
if (store === "cookie") {
|
|
271
|
+
const secure = window.location.protocol === "https:" ? "; Secure" : "";
|
|
272
|
+
document.cookie = `${encodeURIComponent(key)}=; Path=/; Max-Age=0; SameSite=Lax${secure}`;
|
|
273
|
+
} else {
|
|
274
|
+
nativeStorage(store)?.removeItem(key);
|
|
275
|
+
}
|
|
176
276
|
} catch {
|
|
177
277
|
}
|
|
178
278
|
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
279
|
+
__name(rawRemove, "rawRemove");
|
|
280
|
+
function resolveStore(key, store) {
|
|
281
|
+
return store ?? specByKey.get(key)?.[0]?.store ?? "local";
|
|
282
|
+
}
|
|
283
|
+
__name(resolveStore, "resolveStore");
|
|
284
|
+
function listKeys(store, startsWith) {
|
|
285
|
+
const out = [];
|
|
184
286
|
try {
|
|
185
|
-
const
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
287
|
+
const native = nativeStorage(store);
|
|
288
|
+
if (native) {
|
|
289
|
+
for (let i = 0; i < native.length; i += 1) {
|
|
290
|
+
const name = native.key(i);
|
|
291
|
+
if (name?.startsWith(startsWith)) out.push(name);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
189
294
|
} catch {
|
|
190
|
-
return null;
|
|
191
295
|
}
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
296
|
+
for (const name of memory[store].keys()) if (name.startsWith(startsWith) && !out.includes(name)) out.push(name);
|
|
297
|
+
return out;
|
|
298
|
+
}
|
|
299
|
+
__name(listKeys, "listKeys");
|
|
300
|
+
const familyKey = /* @__PURE__ */ __name((family, id) => {
|
|
301
|
+
const part = normalizeSegment(id);
|
|
302
|
+
if (!part) throw new TypeError("storage family id is empty after normalisation");
|
|
303
|
+
return family.prefix + part;
|
|
304
|
+
}, "familyKey");
|
|
305
|
+
const api = {
|
|
306
|
+
owner,
|
|
307
|
+
key(...parts) {
|
|
308
|
+
const segments = parts.map(normalizeSegment);
|
|
309
|
+
if (segments.length === 0 || segments.some((segment) => !segment)) {
|
|
310
|
+
throw new TypeError(`storage key parts must be non-empty: ${JSON.stringify(parts)}`);
|
|
203
311
|
}
|
|
204
|
-
|
|
312
|
+
return `${prefix}${segments.join(".")}`;
|
|
313
|
+
},
|
|
314
|
+
familyKey,
|
|
315
|
+
isValid,
|
|
316
|
+
read(key, store) {
|
|
317
|
+
assertValid(key);
|
|
318
|
+
const target = resolveStore(key, store);
|
|
319
|
+
return rawGet(target, key);
|
|
320
|
+
},
|
|
321
|
+
write(key, value, store) {
|
|
322
|
+
assertOwned(key);
|
|
323
|
+
const target = resolveStore(key, store);
|
|
324
|
+
return rawSet(target, key, value);
|
|
325
|
+
},
|
|
326
|
+
remove(key, store) {
|
|
327
|
+
assertOwned(key);
|
|
328
|
+
const target = resolveStore(key, store);
|
|
329
|
+
rawRemove(target, key);
|
|
330
|
+
},
|
|
331
|
+
readJSON(key, store) {
|
|
332
|
+
const raw = api.read(key, store);
|
|
333
|
+
if (raw === null) return null;
|
|
334
|
+
try {
|
|
335
|
+
return JSON.parse(raw);
|
|
336
|
+
} catch {
|
|
337
|
+
return null;
|
|
338
|
+
}
|
|
339
|
+
},
|
|
340
|
+
writeJSON(key, value, store) {
|
|
341
|
+
return api.write(key, JSON.stringify(value), store);
|
|
342
|
+
},
|
|
343
|
+
keys(store = "local") {
|
|
344
|
+
return listKeys(store, prefix);
|
|
345
|
+
},
|
|
346
|
+
clearAll({ all = false } = {}) {
|
|
347
|
+
for (const spec of specs) {
|
|
348
|
+
if (!all && !spec.personal) continue;
|
|
349
|
+
rawRemove(spec.store, spec.key);
|
|
350
|
+
}
|
|
351
|
+
for (const family of families) {
|
|
352
|
+
if (!all && !family.personal) continue;
|
|
353
|
+
for (const name of listKeys(family.store, family.prefix)) rawRemove(family.store, name);
|
|
354
|
+
}
|
|
355
|
+
if (all) {
|
|
356
|
+
for (const store of ["local", "session"]) {
|
|
357
|
+
for (const name of listKeys(store, prefix)) rawRemove(store, name);
|
|
358
|
+
}
|
|
359
|
+
for (const name of cookieNames()) if (name.startsWith(prefix)) rawRemove("cookie", name);
|
|
360
|
+
}
|
|
361
|
+
},
|
|
362
|
+
onChange(key, listener, store = "local") {
|
|
363
|
+
assertValid(key);
|
|
364
|
+
if (!hasWindow()) return () => {
|
|
365
|
+
};
|
|
366
|
+
const handler = /* @__PURE__ */ __name((event) => {
|
|
367
|
+
if (event.storageArea !== nativeStorage(store)) return;
|
|
368
|
+
if (event.key === null || event.key === key) listener(event.key === null ? null : event.newValue);
|
|
369
|
+
}, "handler");
|
|
370
|
+
window.addEventListener("storage", handler);
|
|
371
|
+
return () => window.removeEventListener("storage", handler);
|
|
205
372
|
}
|
|
206
|
-
}
|
|
373
|
+
};
|
|
374
|
+
return api;
|
|
375
|
+
}
|
|
376
|
+
__name(createStorage, "createStorage");
|
|
377
|
+
var cfgStorage = createStorage("cfg", {
|
|
378
|
+
keys: CFG_KEYS,
|
|
379
|
+
families: CFG_FAMILIES
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
// src/_api/generated/helpers/auth.ts
|
|
383
|
+
var ACCESS_KEY = "cfg.auth.access_token";
|
|
384
|
+
var REFRESH_KEY = "cfg.auth.refresh_token";
|
|
385
|
+
var API_KEY_KEY = "cfg.auth.api_key";
|
|
386
|
+
var storage = true ? cfgStorage : createStorage2("cfg");
|
|
387
|
+
var isBrowser = typeof window !== "undefined";
|
|
388
|
+
var STORE_OF = {
|
|
389
|
+
localStorage: "local",
|
|
390
|
+
sessionStorage: "session",
|
|
391
|
+
cookie: "cookie"
|
|
207
392
|
};
|
|
208
393
|
function backendFor(mode) {
|
|
209
|
-
|
|
394
|
+
const store = STORE_OF[mode];
|
|
395
|
+
return {
|
|
396
|
+
get: /* @__PURE__ */ __name((key) => storage.read(key, store), "get"),
|
|
397
|
+
set(key, value) {
|
|
398
|
+
if (value === null) storage.remove(key, store);
|
|
399
|
+
else storage.write(key, value, store);
|
|
400
|
+
}
|
|
401
|
+
};
|
|
210
402
|
}
|
|
211
403
|
__name(backendFor, "backendFor");
|
|
212
404
|
var _storageMode = "localStorage";
|
|
@@ -395,10 +587,12 @@ var auth = {
|
|
|
395
587
|
notifySessionChanged();
|
|
396
588
|
},
|
|
397
589
|
clearTokens() {
|
|
398
|
-
for (const
|
|
590
|
+
for (const mode of ["localStorage", "sessionStorage", "cookie"]) {
|
|
591
|
+
const backend = backendFor(mode);
|
|
399
592
|
backend.set(ACCESS_KEY, null);
|
|
400
593
|
backend.set(REFRESH_KEY, null);
|
|
401
594
|
}
|
|
595
|
+
storage.clearAll();
|
|
402
596
|
notifySessionChanged();
|
|
403
597
|
},
|
|
404
598
|
/** Session-aware: token PRESENT and not past its `exp` (or a live refresh
|
|
@@ -590,7 +784,7 @@ function dpopEnabled() {
|
|
|
590
784
|
}
|
|
591
785
|
}
|
|
592
786
|
__name(dpopEnabled, "dpopEnabled");
|
|
593
|
-
var _DPOP_DB = "cfg
|
|
787
|
+
var _DPOP_DB = "cfg.auth.dpop";
|
|
594
788
|
var _DPOP_STORE = "keys";
|
|
595
789
|
var _DPOP_KEY_ID = "dpop-ec-p256";
|
|
596
790
|
function _idbOpen() {
|