@djangocfg/api 2.1.582 → 2.1.584

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