@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/hooks.cjs CHANGED
@@ -999,77 +999,263 @@ var APIError = class extends Error {
999
999
  }
1000
1000
  };
1001
1001
 
1002
- // src/_api/generated/helpers/auth.ts
1003
- var ACCESS_KEY = "cfg.access_token";
1004
- var REFRESH_KEY = "cfg.refresh_token";
1005
- var API_KEY_KEY = "cfg.api_key";
1006
- var isBrowser = typeof window !== "undefined";
1007
- var localStorageBackend = {
1008
- get(key) {
1009
- if (!isBrowser) return null;
1010
- try {
1011
- return window.localStorage.getItem(key);
1012
- } catch {
1013
- return null;
1002
+ // src/_api/generated/helpers/keys.ts
1003
+ var k = /* @__PURE__ */ __name((key, store, personal) => ({ key, store, personal }), "k");
1004
+ var f = /* @__PURE__ */ __name((prefix, store, personal) => ({ prefix, store, personal }), "f");
1005
+ var CFG_KEYS = {
1006
+ accessToken: k("cfg.auth.access_token", "local", true),
1007
+ refreshToken: k("cfg.auth.refresh_token", "local", true),
1008
+ apiKey: k("cfg.auth.api_key", "local", true),
1009
+ email: k("cfg.auth.email", "local", false),
1010
+ emailTs: k("cfg.auth.email_ts", "local", false),
1011
+ redirectUrl: k("cfg.auth.redirect_url", "session", true),
1012
+ redirectUrlTs: k("cfg.auth.redirect_url_ts", "session", true),
1013
+ oauthState: k("cfg.auth.oauth_state", "session", true),
1014
+ oauthProvider: k("cfg.auth.oauth_provider", "session", true),
1015
+ profileCache: k("cfg.profile.cache", "local", true),
1016
+ monitorSessionId: k("cfg.monitor.session_id", "local", false),
1017
+ monitorSessionCookie: k("cfg.monitor.session_id", "cookie", false),
1018
+ monitorDebug: k("cfg.monitor.debug", "local", false),
1019
+ uiTabId: k("cfg.ui.tab_id", "session", false),
1020
+ uiRouterSerial: k("cfg.ui.router_serial", "session", false),
1021
+ uiState: k("cfg.ui.state", "local", false),
1022
+ chatSpeechLanguage: k("cfg.chat.speech_language", "local", false),
1023
+ chatRecentTools: k("cfg.chat.recent_tools", "local", false),
1024
+ formsSpeechDebug: k("cfg.forms.speech_debug", "local", false),
1025
+ formsSpeechPrefs: k("cfg.forms.speech_prefs", "local", false),
1026
+ agentPlaygroundApiKeyId: k("cfg.agent.playground_api_key_id", "session", true),
1027
+ agentPlaygroundBearer: k("cfg.agent.playground_bearer", "session", true),
1028
+ agentEndpointDoc: k("cfg.agent.endpoint_doc", "session", false)
1029
+ };
1030
+ var CFG_FAMILIES = {
1031
+ chatDraft: f("cfg.chat.draft.", "session", true),
1032
+ dataTree: f("cfg.data.tree.", "local", false),
1033
+ agentEndpointDraft: f("cfg.agent.endpoint_draft.", "local", false)
1034
+ };
1035
+ var OWNER_RE = /^[a-z][a-z0-9_]*$/;
1036
+ function normalizeSegment(raw) {
1037
+ return raw.trim().toLowerCase().replace(/[^a-z0-9_]+/g, "_").replace(/^_+|_+$/g, "");
1038
+ }
1039
+ __name(normalizeSegment, "normalizeSegment");
1040
+ var hasWindow = /* @__PURE__ */ __name(() => typeof window !== "undefined", "hasWindow");
1041
+ function nativeStorage(store) {
1042
+ if (!hasWindow()) return null;
1043
+ try {
1044
+ return store === "session" ? window.sessionStorage : window.localStorage;
1045
+ } catch {
1046
+ return null;
1047
+ }
1048
+ }
1049
+ __name(nativeStorage, "nativeStorage");
1050
+ function cookieNames() {
1051
+ if (!hasWindow()) return [];
1052
+ try {
1053
+ return document.cookie.split("; ").map((part) => decodeURIComponent(part.split("=")[0] ?? "")).filter(Boolean);
1054
+ } catch {
1055
+ return [];
1056
+ }
1057
+ }
1058
+ __name(cookieNames, "cookieNames");
1059
+ function createStorage(owner, options = {}) {
1060
+ if (!OWNER_RE.test(owner)) {
1061
+ throw new TypeError(`createStorage: owner "${owner}" must match ${OWNER_RE}`);
1062
+ }
1063
+ const prefix = `${owner}.`;
1064
+ const pattern = owner === "cfg" ? /^cfg(\.[a-z0-9_]+)+$/ : new RegExp(`^(cfg|${owner})(\\.[a-z0-9_]+)+$`);
1065
+ const cookieMaxAge = options.cookieMaxAgeSeconds ?? 60 * 60 * 24 * 30;
1066
+ const specs = Object.values(options.keys ?? {});
1067
+ const families = Object.values(options.families ?? {});
1068
+ const specByKey = /* @__PURE__ */ new Map();
1069
+ for (const spec of specs) specByKey.set(spec.key, [...specByKey.get(spec.key) ?? [], spec]);
1070
+ const memory = {
1071
+ local: /* @__PURE__ */ new Map(),
1072
+ session: /* @__PURE__ */ new Map(),
1073
+ cookie: /* @__PURE__ */ new Map()
1074
+ };
1075
+ const isValid = /* @__PURE__ */ __name((key) => pattern.test(key), "isValid");
1076
+ const assertValid = /* @__PURE__ */ __name((key) => {
1077
+ if (!isValid(key)) throw new TypeError(`storage key "${key}" must match ${pattern}`);
1078
+ }, "assertValid");
1079
+ const assertOwned = /* @__PURE__ */ __name((key) => {
1080
+ assertValid(key);
1081
+ if (!key.startsWith(prefix)) throw new TypeError(`storage key "${key}" is not owned by "${owner}"`);
1082
+ }, "assertOwned");
1083
+ function rawGet(store, key) {
1084
+ if (!hasWindow()) return null;
1085
+ if (store === "cookie") {
1086
+ try {
1087
+ const escaped = encodeURIComponent(key).replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
1088
+ const match = document.cookie.match(new RegExp(`(?:^|;\\s*)${escaped}=([^;]*)`));
1089
+ if (match?.[1] !== void 0) return decodeURIComponent(match[1]);
1090
+ } catch {
1091
+ }
1092
+ return memory.cookie.get(key) ?? null;
1014
1093
  }
1015
- },
1016
- set(key, value) {
1017
- if (!isBrowser) return;
1018
1094
  try {
1019
- if (value === null) window.localStorage.removeItem(key);
1020
- else window.localStorage.setItem(key, value);
1095
+ const value = nativeStorage(store)?.getItem(key) ?? null;
1096
+ if (value !== null) return value;
1021
1097
  } catch {
1022
1098
  }
1099
+ return memory[store].get(key) ?? null;
1023
1100
  }
1024
- };
1025
- var sessionStorageBackend = {
1026
- get(key) {
1027
- if (!isBrowser) return null;
1101
+ __name(rawGet, "rawGet");
1102
+ function rawSet(store, key, value) {
1103
+ if (!hasWindow()) return false;
1028
1104
  try {
1029
- return window.sessionStorage.getItem(key);
1105
+ if (store === "cookie") {
1106
+ const secure = window.location.protocol === "https:" ? "; Secure" : "";
1107
+ document.cookie = `${encodeURIComponent(key)}=${encodeURIComponent(value)}; Path=/; Max-Age=${cookieMaxAge}; SameSite=Lax${secure}`;
1108
+ if (rawGet("cookie", key) === value && !memory.cookie.has(key)) return true;
1109
+ memory.cookie.set(key, value);
1110
+ return false;
1111
+ }
1112
+ const native = nativeStorage(store);
1113
+ if (!native) throw new Error("storage unavailable");
1114
+ native.setItem(key, value);
1115
+ memory[store].delete(key);
1116
+ return true;
1030
1117
  } catch {
1031
- return null;
1118
+ memory[store].set(key, value);
1119
+ return false;
1032
1120
  }
1033
- },
1034
- set(key, value) {
1035
- if (!isBrowser) return;
1121
+ }
1122
+ __name(rawSet, "rawSet");
1123
+ function rawRemove(store, key) {
1124
+ memory[store].delete(key);
1125
+ if (!hasWindow()) return;
1036
1126
  try {
1037
- if (value === null) window.sessionStorage.removeItem(key);
1038
- else window.sessionStorage.setItem(key, value);
1127
+ if (store === "cookie") {
1128
+ const secure = window.location.protocol === "https:" ? "; Secure" : "";
1129
+ document.cookie = `${encodeURIComponent(key)}=; Path=/; Max-Age=0; SameSite=Lax${secure}`;
1130
+ } else {
1131
+ nativeStorage(store)?.removeItem(key);
1132
+ }
1039
1133
  } catch {
1040
1134
  }
1041
1135
  }
1042
- };
1043
- var COOKIE_MAX_AGE = 60 * 60 * 24 * 30;
1044
- var cookieBackend = {
1045
- get(key) {
1046
- if (!isBrowser) return null;
1136
+ __name(rawRemove, "rawRemove");
1137
+ function resolveStore(key, store) {
1138
+ return store ?? specByKey.get(key)?.[0]?.store ?? "local";
1139
+ }
1140
+ __name(resolveStore, "resolveStore");
1141
+ function listKeys(store, startsWith) {
1142
+ const out = [];
1047
1143
  try {
1048
- const re = new RegExp(`(?:^|;\\s*)${encodeURIComponent(key)}=([^;]*)`);
1049
- const m = document.cookie.match(re);
1050
- const value = m?.[1];
1051
- return value === void 0 ? null : decodeURIComponent(value);
1144
+ const native = nativeStorage(store);
1145
+ if (native) {
1146
+ for (let i = 0; i < native.length; i += 1) {
1147
+ const name = native.key(i);
1148
+ if (name?.startsWith(startsWith)) out.push(name);
1149
+ }
1150
+ }
1052
1151
  } catch {
1053
- return null;
1054
1152
  }
1055
- },
1056
- set(key, value) {
1057
- if (!isBrowser) return;
1058
- try {
1059
- const k = encodeURIComponent(key);
1060
- const secure = window.location.protocol === "https:" ? "; Secure" : "";
1061
- if (value === null) {
1062
- document.cookie = `${k}=; Path=/; Max-Age=0; SameSite=Lax${secure}`;
1063
- } else {
1064
- const v = encodeURIComponent(value);
1065
- document.cookie = `${k}=${v}; Path=/; Max-Age=${COOKIE_MAX_AGE}; SameSite=Lax${secure}`;
1153
+ for (const name of memory[store].keys()) if (name.startsWith(startsWith) && !out.includes(name)) out.push(name);
1154
+ return out;
1155
+ }
1156
+ __name(listKeys, "listKeys");
1157
+ const familyKey = /* @__PURE__ */ __name((family, id) => {
1158
+ const part = normalizeSegment(id);
1159
+ if (!part) throw new TypeError("storage family id is empty after normalisation");
1160
+ return family.prefix + part;
1161
+ }, "familyKey");
1162
+ const api = {
1163
+ owner,
1164
+ key(...parts) {
1165
+ const segments = parts.map(normalizeSegment);
1166
+ if (segments.length === 0 || segments.some((segment) => !segment)) {
1167
+ throw new TypeError(`storage key parts must be non-empty: ${JSON.stringify(parts)}`);
1066
1168
  }
1067
- } catch {
1169
+ return `${prefix}${segments.join(".")}`;
1170
+ },
1171
+ familyKey,
1172
+ isValid,
1173
+ read(key, store) {
1174
+ assertValid(key);
1175
+ const target = resolveStore(key, store);
1176
+ return rawGet(target, key);
1177
+ },
1178
+ write(key, value, store) {
1179
+ assertOwned(key);
1180
+ const target = resolveStore(key, store);
1181
+ return rawSet(target, key, value);
1182
+ },
1183
+ remove(key, store) {
1184
+ assertOwned(key);
1185
+ const target = resolveStore(key, store);
1186
+ rawRemove(target, key);
1187
+ },
1188
+ readJSON(key, store) {
1189
+ const raw = api.read(key, store);
1190
+ if (raw === null) return null;
1191
+ try {
1192
+ return JSON.parse(raw);
1193
+ } catch {
1194
+ return null;
1195
+ }
1196
+ },
1197
+ writeJSON(key, value, store) {
1198
+ return api.write(key, JSON.stringify(value), store);
1199
+ },
1200
+ keys(store = "local") {
1201
+ return listKeys(store, prefix);
1202
+ },
1203
+ clearAll({ all = false } = {}) {
1204
+ for (const spec of specs) {
1205
+ if (!all && !spec.personal) continue;
1206
+ rawRemove(spec.store, spec.key);
1207
+ }
1208
+ for (const family of families) {
1209
+ if (!all && !family.personal) continue;
1210
+ for (const name of listKeys(family.store, family.prefix)) rawRemove(family.store, name);
1211
+ }
1212
+ if (all) {
1213
+ for (const store of ["local", "session"]) {
1214
+ for (const name of listKeys(store, prefix)) rawRemove(store, name);
1215
+ }
1216
+ for (const name of cookieNames()) if (name.startsWith(prefix)) rawRemove("cookie", name);
1217
+ }
1218
+ },
1219
+ onChange(key, listener, store = "local") {
1220
+ assertValid(key);
1221
+ if (!hasWindow()) return () => {
1222
+ };
1223
+ const handler = /* @__PURE__ */ __name((event) => {
1224
+ if (event.storageArea !== nativeStorage(store)) return;
1225
+ if (event.key === null || event.key === key) listener(event.key === null ? null : event.newValue);
1226
+ }, "handler");
1227
+ window.addEventListener("storage", handler);
1228
+ return () => window.removeEventListener("storage", handler);
1068
1229
  }
1069
- }
1230
+ };
1231
+ return api;
1232
+ }
1233
+ __name(createStorage, "createStorage");
1234
+ var cfgStorage = createStorage("cfg", {
1235
+ keys: CFG_KEYS,
1236
+ families: CFG_FAMILIES
1237
+ });
1238
+
1239
+ // src/_api/generated/helpers/auth.ts
1240
+ var ACCESS_KEY = "cfg.auth.access_token";
1241
+ var REFRESH_KEY = "cfg.auth.refresh_token";
1242
+ var API_KEY_KEY = "cfg.auth.api_key";
1243
+ var storage = true ? cfgStorage : createStorage2("cfg");
1244
+ var isBrowser = typeof window !== "undefined";
1245
+ var STORE_OF = {
1246
+ localStorage: "local",
1247
+ sessionStorage: "session",
1248
+ cookie: "cookie"
1070
1249
  };
1071
1250
  function backendFor(mode) {
1072
- return mode === "cookie" ? cookieBackend : mode === "sessionStorage" ? sessionStorageBackend : localStorageBackend;
1251
+ const store = STORE_OF[mode];
1252
+ return {
1253
+ get: /* @__PURE__ */ __name((key) => storage.read(key, store), "get"),
1254
+ set(key, value) {
1255
+ if (value === null) storage.remove(key, store);
1256
+ else storage.write(key, value, store);
1257
+ }
1258
+ };
1073
1259
  }
1074
1260
  __name(backendFor, "backendFor");
1075
1261
  var _storageMode = "localStorage";
@@ -1258,10 +1444,12 @@ var auth = {
1258
1444
  notifySessionChanged();
1259
1445
  },
1260
1446
  clearTokens() {
1261
- for (const backend of [localStorageBackend, sessionStorageBackend, cookieBackend]) {
1447
+ for (const mode of ["localStorage", "sessionStorage", "cookie"]) {
1448
+ const backend = backendFor(mode);
1262
1449
  backend.set(ACCESS_KEY, null);
1263
1450
  backend.set(REFRESH_KEY, null);
1264
1451
  }
1452
+ storage.clearAll();
1265
1453
  notifySessionChanged();
1266
1454
  },
1267
1455
  /** Session-aware: token PRESENT and not past its `exp` (or a live refresh
@@ -1453,7 +1641,7 @@ function dpopEnabled() {
1453
1641
  }
1454
1642
  }
1455
1643
  __name(dpopEnabled, "dpopEnabled");
1456
- var _DPOP_DB = "cfg-auth";
1644
+ var _DPOP_DB = "cfg.auth.dpop";
1457
1645
  var _DPOP_STORE = "keys";
1458
1646
  var _DPOP_KEY_ID = "dpop-ec-p256";
1459
1647
  function _idbOpen() {