@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.
Files changed (41) hide show
  1. package/README.md +18 -0
  2. package/dist/auth.cjs +272 -80
  3. package/dist/auth.cjs.map +1 -1
  4. package/dist/auth.mjs +272 -80
  5. package/dist/auth.mjs.map +1 -1
  6. package/dist/clients.cjs +243 -54
  7. package/dist/clients.cjs.map +1 -1
  8. package/dist/clients.mjs +243 -54
  9. package/dist/clients.mjs.map +1 -1
  10. package/dist/hooks.cjs +243 -54
  11. package/dist/hooks.cjs.map +1 -1
  12. package/dist/hooks.mjs +243 -54
  13. package/dist/hooks.mjs.map +1 -1
  14. package/dist/index.cjs +248 -54
  15. package/dist/index.cjs.map +1 -1
  16. package/dist/index.d.cts +85 -1
  17. package/dist/index.d.ts +85 -1
  18. package/dist/index.mjs +248 -54
  19. package/dist/index.mjs.map +1 -1
  20. package/dist/storage.cjs +269 -0
  21. package/dist/storage.cjs.map +1 -0
  22. package/dist/storage.d.cts +85 -0
  23. package/dist/storage.d.ts +85 -0
  24. package/dist/storage.mjs +248 -0
  25. package/dist/storage.mjs.map +1 -0
  26. package/package.json +8 -2
  27. package/src/__tests__/noStorageLiterals.test.ts +27 -0
  28. package/src/__tests__/storage.test.ts +134 -0
  29. package/src/__tests__/storageSsr.test.ts +14 -0
  30. package/src/_api/generated/helpers/auth.ts +27 -64
  31. package/src/_api/generated/helpers/index.ts +12 -0
  32. package/src/_api/generated/helpers/keys.ts +328 -0
  33. package/src/auth/__tests__/authRedirect.test.ts +1 -1
  34. package/src/auth/__tests__/profileCache.test.ts +1 -1
  35. package/src/auth/context/AuthContext.tsx +2 -1
  36. package/src/auth/hooks/useAuthRedirect.ts +3 -2
  37. package/src/auth/hooks/useGithubAuth.ts +13 -8
  38. package/src/auth/hooks/useLocalStorage.ts +7 -7
  39. package/src/auth/hooks/useProfileCache.ts +2 -1
  40. package/src/auth/hooks/useSessionStorage.ts +7 -7
  41. 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/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
+ 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
- if (value === null) window.localStorage.removeItem(key);
157
- else window.localStorage.setItem(key, value);
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
- var sessionStorageBackend = {
163
- get(key) {
164
- if (!isBrowser) return null;
244
+ __name(rawGet, "rawGet");
245
+ function rawSet(store, key, value) {
246
+ if (!hasWindow()) return false;
165
247
  try {
166
- return window.sessionStorage.getItem(key);
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
- return null;
261
+ memory[store].set(key, value);
262
+ return false;
169
263
  }
170
- },
171
- set(key, value) {
172
- if (!isBrowser) return;
264
+ }
265
+ __name(rawSet, "rawSet");
266
+ function rawRemove(store, key) {
267
+ memory[store].delete(key);
268
+ if (!hasWindow()) return;
173
269
  try {
174
- if (value === null) window.sessionStorage.removeItem(key);
175
- else window.sessionStorage.setItem(key, value);
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
- var COOKIE_MAX_AGE = 60 * 60 * 24 * 30;
181
- var cookieBackend = {
182
- get(key) {
183
- if (!isBrowser) return null;
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 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);
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
- 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}`;
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
- } catch {
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
- return mode === "cookie" ? cookieBackend : mode === "sessionStorage" ? sessionStorageBackend : localStorageBackend;
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 backend of [localStorageBackend, sessionStorageBackend, cookieBackend]) {
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-auth";
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() {