@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/clients.cjs CHANGED
@@ -93,77 +93,263 @@ var APIError = class extends Error {
93
93
  }
94
94
  };
95
95
 
96
- // src/_api/generated/helpers/auth.ts
97
- var ACCESS_KEY = "cfg.access_token";
98
- var REFRESH_KEY = "cfg.refresh_token";
99
- var API_KEY_KEY = "cfg.api_key";
100
- var isBrowser = typeof window !== "undefined";
101
- var localStorageBackend = {
102
- get(key) {
103
- if (!isBrowser) return null;
104
- try {
105
- return window.localStorage.getItem(key);
106
- } catch {
107
- return null;
96
+ // src/_api/generated/helpers/keys.ts
97
+ var k = /* @__PURE__ */ __name((key, store, personal) => ({ key, store, personal }), "k");
98
+ var f = /* @__PURE__ */ __name((prefix, store, personal) => ({ prefix, store, personal }), "f");
99
+ var CFG_KEYS = {
100
+ accessToken: k("cfg.auth.access_token", "local", true),
101
+ refreshToken: k("cfg.auth.refresh_token", "local", true),
102
+ apiKey: k("cfg.auth.api_key", "local", true),
103
+ email: k("cfg.auth.email", "local", false),
104
+ emailTs: k("cfg.auth.email_ts", "local", false),
105
+ redirectUrl: k("cfg.auth.redirect_url", "session", true),
106
+ redirectUrlTs: k("cfg.auth.redirect_url_ts", "session", true),
107
+ oauthState: k("cfg.auth.oauth_state", "session", true),
108
+ oauthProvider: k("cfg.auth.oauth_provider", "session", true),
109
+ profileCache: k("cfg.profile.cache", "local", true),
110
+ monitorSessionId: k("cfg.monitor.session_id", "local", false),
111
+ monitorSessionCookie: k("cfg.monitor.session_id", "cookie", false),
112
+ monitorDebug: k("cfg.monitor.debug", "local", false),
113
+ uiTabId: k("cfg.ui.tab_id", "session", false),
114
+ uiRouterSerial: k("cfg.ui.router_serial", "session", false),
115
+ uiState: k("cfg.ui.state", "local", false),
116
+ chatSpeechLanguage: k("cfg.chat.speech_language", "local", false),
117
+ chatRecentTools: k("cfg.chat.recent_tools", "local", false),
118
+ formsSpeechDebug: k("cfg.forms.speech_debug", "local", false),
119
+ formsSpeechPrefs: k("cfg.forms.speech_prefs", "local", false),
120
+ agentPlaygroundApiKeyId: k("cfg.agent.playground_api_key_id", "session", true),
121
+ agentPlaygroundBearer: k("cfg.agent.playground_bearer", "session", true),
122
+ agentEndpointDoc: k("cfg.agent.endpoint_doc", "session", false)
123
+ };
124
+ var CFG_FAMILIES = {
125
+ chatDraft: f("cfg.chat.draft.", "session", true),
126
+ dataTree: f("cfg.data.tree.", "local", false),
127
+ agentEndpointDraft: f("cfg.agent.endpoint_draft.", "local", false)
128
+ };
129
+ var OWNER_RE = /^[a-z][a-z0-9_]*$/;
130
+ function normalizeSegment(raw) {
131
+ return raw.trim().toLowerCase().replace(/[^a-z0-9_]+/g, "_").replace(/^_+|_+$/g, "");
132
+ }
133
+ __name(normalizeSegment, "normalizeSegment");
134
+ var hasWindow = /* @__PURE__ */ __name(() => typeof window !== "undefined", "hasWindow");
135
+ function nativeStorage(store) {
136
+ if (!hasWindow()) return null;
137
+ try {
138
+ return store === "session" ? window.sessionStorage : window.localStorage;
139
+ } catch {
140
+ return null;
141
+ }
142
+ }
143
+ __name(nativeStorage, "nativeStorage");
144
+ function cookieNames() {
145
+ if (!hasWindow()) return [];
146
+ try {
147
+ return document.cookie.split("; ").map((part) => decodeURIComponent(part.split("=")[0] ?? "")).filter(Boolean);
148
+ } catch {
149
+ return [];
150
+ }
151
+ }
152
+ __name(cookieNames, "cookieNames");
153
+ function createStorage(owner, options = {}) {
154
+ if (!OWNER_RE.test(owner)) {
155
+ throw new TypeError(`createStorage: owner "${owner}" must match ${OWNER_RE}`);
156
+ }
157
+ const prefix = `${owner}.`;
158
+ const pattern = owner === "cfg" ? /^cfg(\.[a-z0-9_]+)+$/ : new RegExp(`^(cfg|${owner})(\\.[a-z0-9_]+)+$`);
159
+ const cookieMaxAge = options.cookieMaxAgeSeconds ?? 60 * 60 * 24 * 30;
160
+ const specs = Object.values(options.keys ?? {});
161
+ const families = Object.values(options.families ?? {});
162
+ const specByKey = /* @__PURE__ */ new Map();
163
+ for (const spec of specs) specByKey.set(spec.key, [...specByKey.get(spec.key) ?? [], spec]);
164
+ const memory = {
165
+ local: /* @__PURE__ */ new Map(),
166
+ session: /* @__PURE__ */ new Map(),
167
+ cookie: /* @__PURE__ */ new Map()
168
+ };
169
+ const isValid = /* @__PURE__ */ __name((key) => pattern.test(key), "isValid");
170
+ const assertValid = /* @__PURE__ */ __name((key) => {
171
+ if (!isValid(key)) throw new TypeError(`storage key "${key}" must match ${pattern}`);
172
+ }, "assertValid");
173
+ const assertOwned = /* @__PURE__ */ __name((key) => {
174
+ assertValid(key);
175
+ if (!key.startsWith(prefix)) throw new TypeError(`storage key "${key}" is not owned by "${owner}"`);
176
+ }, "assertOwned");
177
+ function rawGet(store, key) {
178
+ if (!hasWindow()) return null;
179
+ if (store === "cookie") {
180
+ try {
181
+ const escaped = encodeURIComponent(key).replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
182
+ const match = document.cookie.match(new RegExp(`(?:^|;\\s*)${escaped}=([^;]*)`));
183
+ if (match?.[1] !== void 0) return decodeURIComponent(match[1]);
184
+ } catch {
185
+ }
186
+ return memory.cookie.get(key) ?? null;
108
187
  }
109
- },
110
- set(key, value) {
111
- if (!isBrowser) return;
112
188
  try {
113
- if (value === null) window.localStorage.removeItem(key);
114
- else window.localStorage.setItem(key, value);
189
+ const value = nativeStorage(store)?.getItem(key) ?? null;
190
+ if (value !== null) return value;
115
191
  } catch {
116
192
  }
193
+ return memory[store].get(key) ?? null;
117
194
  }
118
- };
119
- var sessionStorageBackend = {
120
- get(key) {
121
- if (!isBrowser) return null;
195
+ __name(rawGet, "rawGet");
196
+ function rawSet(store, key, value) {
197
+ if (!hasWindow()) return false;
122
198
  try {
123
- return window.sessionStorage.getItem(key);
199
+ if (store === "cookie") {
200
+ const secure = window.location.protocol === "https:" ? "; Secure" : "";
201
+ document.cookie = `${encodeURIComponent(key)}=${encodeURIComponent(value)}; Path=/; Max-Age=${cookieMaxAge}; SameSite=Lax${secure}`;
202
+ if (rawGet("cookie", key) === value && !memory.cookie.has(key)) return true;
203
+ memory.cookie.set(key, value);
204
+ return false;
205
+ }
206
+ const native = nativeStorage(store);
207
+ if (!native) throw new Error("storage unavailable");
208
+ native.setItem(key, value);
209
+ memory[store].delete(key);
210
+ return true;
124
211
  } catch {
125
- return null;
212
+ memory[store].set(key, value);
213
+ return false;
126
214
  }
127
- },
128
- set(key, value) {
129
- if (!isBrowser) return;
215
+ }
216
+ __name(rawSet, "rawSet");
217
+ function rawRemove(store, key) {
218
+ memory[store].delete(key);
219
+ if (!hasWindow()) return;
130
220
  try {
131
- if (value === null) window.sessionStorage.removeItem(key);
132
- else window.sessionStorage.setItem(key, value);
221
+ if (store === "cookie") {
222
+ const secure = window.location.protocol === "https:" ? "; Secure" : "";
223
+ document.cookie = `${encodeURIComponent(key)}=; Path=/; Max-Age=0; SameSite=Lax${secure}`;
224
+ } else {
225
+ nativeStorage(store)?.removeItem(key);
226
+ }
133
227
  } catch {
134
228
  }
135
229
  }
136
- };
137
- var COOKIE_MAX_AGE = 60 * 60 * 24 * 30;
138
- var cookieBackend = {
139
- get(key) {
140
- if (!isBrowser) return null;
230
+ __name(rawRemove, "rawRemove");
231
+ function resolveStore(key, store) {
232
+ return store ?? specByKey.get(key)?.[0]?.store ?? "local";
233
+ }
234
+ __name(resolveStore, "resolveStore");
235
+ function listKeys(store, startsWith) {
236
+ const out = [];
141
237
  try {
142
- const re = new RegExp(`(?:^|;\\s*)${encodeURIComponent(key)}=([^;]*)`);
143
- const m = document.cookie.match(re);
144
- const value = m?.[1];
145
- return value === void 0 ? null : decodeURIComponent(value);
238
+ const native = nativeStorage(store);
239
+ if (native) {
240
+ for (let i = 0; i < native.length; i += 1) {
241
+ const name = native.key(i);
242
+ if (name?.startsWith(startsWith)) out.push(name);
243
+ }
244
+ }
146
245
  } catch {
147
- return null;
148
246
  }
149
- },
150
- set(key, value) {
151
- if (!isBrowser) return;
152
- try {
153
- const k = encodeURIComponent(key);
154
- const secure = window.location.protocol === "https:" ? "; Secure" : "";
155
- if (value === null) {
156
- document.cookie = `${k}=; Path=/; Max-Age=0; SameSite=Lax${secure}`;
157
- } else {
158
- const v = encodeURIComponent(value);
159
- document.cookie = `${k}=${v}; Path=/; Max-Age=${COOKIE_MAX_AGE}; SameSite=Lax${secure}`;
247
+ for (const name of memory[store].keys()) if (name.startsWith(startsWith) && !out.includes(name)) out.push(name);
248
+ return out;
249
+ }
250
+ __name(listKeys, "listKeys");
251
+ const familyKey = /* @__PURE__ */ __name((family, id) => {
252
+ const part = normalizeSegment(id);
253
+ if (!part) throw new TypeError("storage family id is empty after normalisation");
254
+ return family.prefix + part;
255
+ }, "familyKey");
256
+ const api = {
257
+ owner,
258
+ key(...parts) {
259
+ const segments = parts.map(normalizeSegment);
260
+ if (segments.length === 0 || segments.some((segment) => !segment)) {
261
+ throw new TypeError(`storage key parts must be non-empty: ${JSON.stringify(parts)}`);
160
262
  }
161
- } catch {
263
+ return `${prefix}${segments.join(".")}`;
264
+ },
265
+ familyKey,
266
+ isValid,
267
+ read(key, store) {
268
+ assertValid(key);
269
+ const target = resolveStore(key, store);
270
+ return rawGet(target, key);
271
+ },
272
+ write(key, value, store) {
273
+ assertOwned(key);
274
+ const target = resolveStore(key, store);
275
+ return rawSet(target, key, value);
276
+ },
277
+ remove(key, store) {
278
+ assertOwned(key);
279
+ const target = resolveStore(key, store);
280
+ rawRemove(target, key);
281
+ },
282
+ readJSON(key, store) {
283
+ const raw = api.read(key, store);
284
+ if (raw === null) return null;
285
+ try {
286
+ return JSON.parse(raw);
287
+ } catch {
288
+ return null;
289
+ }
290
+ },
291
+ writeJSON(key, value, store) {
292
+ return api.write(key, JSON.stringify(value), store);
293
+ },
294
+ keys(store = "local") {
295
+ return listKeys(store, prefix);
296
+ },
297
+ clearAll({ all = false } = {}) {
298
+ for (const spec of specs) {
299
+ if (!all && !spec.personal) continue;
300
+ rawRemove(spec.store, spec.key);
301
+ }
302
+ for (const family of families) {
303
+ if (!all && !family.personal) continue;
304
+ for (const name of listKeys(family.store, family.prefix)) rawRemove(family.store, name);
305
+ }
306
+ if (all) {
307
+ for (const store of ["local", "session"]) {
308
+ for (const name of listKeys(store, prefix)) rawRemove(store, name);
309
+ }
310
+ for (const name of cookieNames()) if (name.startsWith(prefix)) rawRemove("cookie", name);
311
+ }
312
+ },
313
+ onChange(key, listener, store = "local") {
314
+ assertValid(key);
315
+ if (!hasWindow()) return () => {
316
+ };
317
+ const handler = /* @__PURE__ */ __name((event) => {
318
+ if (event.storageArea !== nativeStorage(store)) return;
319
+ if (event.key === null || event.key === key) listener(event.key === null ? null : event.newValue);
320
+ }, "handler");
321
+ window.addEventListener("storage", handler);
322
+ return () => window.removeEventListener("storage", handler);
162
323
  }
163
- }
324
+ };
325
+ return api;
326
+ }
327
+ __name(createStorage, "createStorage");
328
+ var cfgStorage = createStorage("cfg", {
329
+ keys: CFG_KEYS,
330
+ families: CFG_FAMILIES
331
+ });
332
+
333
+ // src/_api/generated/helpers/auth.ts
334
+ var ACCESS_KEY = "cfg.auth.access_token";
335
+ var REFRESH_KEY = "cfg.auth.refresh_token";
336
+ var API_KEY_KEY = "cfg.auth.api_key";
337
+ var storage = true ? cfgStorage : createStorage2("cfg");
338
+ var isBrowser = typeof window !== "undefined";
339
+ var STORE_OF = {
340
+ localStorage: "local",
341
+ sessionStorage: "session",
342
+ cookie: "cookie"
164
343
  };
165
344
  function backendFor(mode) {
166
- return mode === "cookie" ? cookieBackend : mode === "sessionStorage" ? sessionStorageBackend : localStorageBackend;
345
+ const store = STORE_OF[mode];
346
+ return {
347
+ get: /* @__PURE__ */ __name((key) => storage.read(key, store), "get"),
348
+ set(key, value) {
349
+ if (value === null) storage.remove(key, store);
350
+ else storage.write(key, value, store);
351
+ }
352
+ };
167
353
  }
168
354
  __name(backendFor, "backendFor");
169
355
  var _storageMode = "localStorage";
@@ -352,10 +538,12 @@ var auth = {
352
538
  notifySessionChanged();
353
539
  },
354
540
  clearTokens() {
355
- for (const backend of [localStorageBackend, sessionStorageBackend, cookieBackend]) {
541
+ for (const mode of ["localStorage", "sessionStorage", "cookie"]) {
542
+ const backend = backendFor(mode);
356
543
  backend.set(ACCESS_KEY, null);
357
544
  backend.set(REFRESH_KEY, null);
358
545
  }
546
+ storage.clearAll();
359
547
  notifySessionChanged();
360
548
  },
361
549
  /** Session-aware: token PRESENT and not past its `exp` (or a live refresh
@@ -547,7 +735,7 @@ function dpopEnabled() {
547
735
  }
548
736
  }
549
737
  __name(dpopEnabled, "dpopEnabled");
550
- var _DPOP_DB = "cfg-auth";
738
+ var _DPOP_DB = "cfg.auth.dpop";
551
739
  var _DPOP_STORE = "keys";
552
740
  var _DPOP_KEY_ID = "dpop-ec-p256";
553
741
  function _idbOpen() {