@djangocfg/api 2.1.582 → 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/hooks.mjs
CHANGED
|
@@ -864,77 +864,264 @@ var APIError = class extends Error {
|
|
|
864
864
|
}
|
|
865
865
|
};
|
|
866
866
|
|
|
867
|
-
// src/_api/generated/helpers/
|
|
868
|
-
var
|
|
869
|
-
var
|
|
870
|
-
var
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
867
|
+
// src/_api/generated/helpers/keys.ts
|
|
868
|
+
var k = /* @__PURE__ */ __name((key, store, personal) => ({ key, store, personal }), "k");
|
|
869
|
+
var f = /* @__PURE__ */ __name((prefix, store, personal) => ({ prefix, store, personal }), "f");
|
|
870
|
+
var CFG_KEYS = {
|
|
871
|
+
accessToken: k("cfg.auth.access_token", "local", true),
|
|
872
|
+
refreshToken: k("cfg.auth.refresh_token", "local", true),
|
|
873
|
+
apiKey: k("cfg.auth.api_key", "local", true),
|
|
874
|
+
email: k("cfg.auth.email", "local", false),
|
|
875
|
+
emailTs: k("cfg.auth.email_ts", "local", false),
|
|
876
|
+
redirectUrl: k("cfg.auth.redirect_url", "session", true),
|
|
877
|
+
redirectUrlTs: k("cfg.auth.redirect_url_ts", "session", true),
|
|
878
|
+
oauthState: k("cfg.auth.oauth_state", "session", true),
|
|
879
|
+
oauthProvider: k("cfg.auth.oauth_provider", "session", true),
|
|
880
|
+
oauthRememberMe: k("cfg.auth.oauth_remember_me", "session", true),
|
|
881
|
+
profileCache: k("cfg.profile.cache", "local", true),
|
|
882
|
+
monitorSessionId: k("cfg.monitor.session_id", "local", false),
|
|
883
|
+
monitorSessionCookie: k("cfg.monitor.session_id", "cookie", false),
|
|
884
|
+
monitorDebug: k("cfg.monitor.debug", "local", false),
|
|
885
|
+
uiTabId: k("cfg.ui.tab_id", "session", false),
|
|
886
|
+
uiRouterSerial: k("cfg.ui.router_serial", "session", false),
|
|
887
|
+
uiState: k("cfg.ui.state", "local", false),
|
|
888
|
+
chatSpeechLanguage: k("cfg.chat.speech_language", "local", false),
|
|
889
|
+
chatRecentTools: k("cfg.chat.recent_tools", "local", false),
|
|
890
|
+
formsSpeechDebug: k("cfg.forms.speech_debug", "local", false),
|
|
891
|
+
formsSpeechPrefs: k("cfg.forms.speech_prefs", "local", false),
|
|
892
|
+
agentPlaygroundApiKeyId: k("cfg.agent.playground_api_key_id", "session", true),
|
|
893
|
+
agentPlaygroundBearer: k("cfg.agent.playground_bearer", "session", true),
|
|
894
|
+
agentEndpointDoc: k("cfg.agent.endpoint_doc", "session", false)
|
|
895
|
+
};
|
|
896
|
+
var CFG_FAMILIES = {
|
|
897
|
+
chatDraft: f("cfg.chat.draft.", "session", true),
|
|
898
|
+
dataTree: f("cfg.data.tree.", "local", false),
|
|
899
|
+
agentEndpointDraft: f("cfg.agent.endpoint_draft.", "local", false)
|
|
900
|
+
};
|
|
901
|
+
var OWNER_RE = /^[a-z][a-z0-9_]*$/;
|
|
902
|
+
function normalizeSegment(raw) {
|
|
903
|
+
return raw.trim().toLowerCase().replace(/[^a-z0-9_]+/g, "_").replace(/^_+|_+$/g, "");
|
|
904
|
+
}
|
|
905
|
+
__name(normalizeSegment, "normalizeSegment");
|
|
906
|
+
var hasWindow = /* @__PURE__ */ __name(() => typeof window !== "undefined", "hasWindow");
|
|
907
|
+
function nativeStorage(store) {
|
|
908
|
+
if (!hasWindow()) return null;
|
|
909
|
+
try {
|
|
910
|
+
return store === "session" ? window.sessionStorage : window.localStorage;
|
|
911
|
+
} catch {
|
|
912
|
+
return null;
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
__name(nativeStorage, "nativeStorage");
|
|
916
|
+
function cookieNames() {
|
|
917
|
+
if (!hasWindow()) return [];
|
|
918
|
+
try {
|
|
919
|
+
return document.cookie.split("; ").map((part) => decodeURIComponent(part.split("=")[0] ?? "")).filter(Boolean);
|
|
920
|
+
} catch {
|
|
921
|
+
return [];
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
__name(cookieNames, "cookieNames");
|
|
925
|
+
function createStorage(owner, options = {}) {
|
|
926
|
+
if (!OWNER_RE.test(owner)) {
|
|
927
|
+
throw new TypeError(`createStorage: owner "${owner}" must match ${OWNER_RE}`);
|
|
928
|
+
}
|
|
929
|
+
const prefix = `${owner}.`;
|
|
930
|
+
const pattern = owner === "cfg" ? /^cfg(\.[a-z0-9_]+)+$/ : new RegExp(`^(cfg|${owner})(\\.[a-z0-9_]+)+$`);
|
|
931
|
+
const cookieMaxAge = options.cookieMaxAgeSeconds ?? 60 * 60 * 24 * 30;
|
|
932
|
+
const specs = Object.values(options.keys ?? {});
|
|
933
|
+
const families = Object.values(options.families ?? {});
|
|
934
|
+
const specByKey = /* @__PURE__ */ new Map();
|
|
935
|
+
for (const spec of specs) specByKey.set(spec.key, [...specByKey.get(spec.key) ?? [], spec]);
|
|
936
|
+
const memory = {
|
|
937
|
+
local: /* @__PURE__ */ new Map(),
|
|
938
|
+
session: /* @__PURE__ */ new Map(),
|
|
939
|
+
cookie: /* @__PURE__ */ new Map()
|
|
940
|
+
};
|
|
941
|
+
const isValid = /* @__PURE__ */ __name((key) => pattern.test(key), "isValid");
|
|
942
|
+
const assertValid = /* @__PURE__ */ __name((key) => {
|
|
943
|
+
if (!isValid(key)) throw new TypeError(`storage key "${key}" must match ${pattern}`);
|
|
944
|
+
}, "assertValid");
|
|
945
|
+
const assertOwned = /* @__PURE__ */ __name((key) => {
|
|
946
|
+
assertValid(key);
|
|
947
|
+
if (!key.startsWith(prefix)) throw new TypeError(`storage key "${key}" is not owned by "${owner}"`);
|
|
948
|
+
}, "assertOwned");
|
|
949
|
+
function rawGet(store, key) {
|
|
950
|
+
if (!hasWindow()) return null;
|
|
951
|
+
if (store === "cookie") {
|
|
952
|
+
try {
|
|
953
|
+
const escaped = encodeURIComponent(key).replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
954
|
+
const match = document.cookie.match(new RegExp(`(?:^|;\\s*)${escaped}=([^;]*)`));
|
|
955
|
+
if (match?.[1] !== void 0) return decodeURIComponent(match[1]);
|
|
956
|
+
} catch {
|
|
957
|
+
}
|
|
958
|
+
return memory.cookie.get(key) ?? null;
|
|
879
959
|
}
|
|
880
|
-
},
|
|
881
|
-
set(key, value) {
|
|
882
|
-
if (!isBrowser) return;
|
|
883
960
|
try {
|
|
884
|
-
|
|
885
|
-
|
|
961
|
+
const value = nativeStorage(store)?.getItem(key) ?? null;
|
|
962
|
+
if (value !== null) return value;
|
|
886
963
|
} catch {
|
|
887
964
|
}
|
|
965
|
+
return memory[store].get(key) ?? null;
|
|
888
966
|
}
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
if (!isBrowser) return null;
|
|
967
|
+
__name(rawGet, "rawGet");
|
|
968
|
+
function rawSet(store, key, value) {
|
|
969
|
+
if (!hasWindow()) return false;
|
|
893
970
|
try {
|
|
894
|
-
|
|
971
|
+
if (store === "cookie") {
|
|
972
|
+
const secure = window.location.protocol === "https:" ? "; Secure" : "";
|
|
973
|
+
document.cookie = `${encodeURIComponent(key)}=${encodeURIComponent(value)}; Path=/; Max-Age=${cookieMaxAge}; SameSite=Lax${secure}`;
|
|
974
|
+
if (rawGet("cookie", key) === value && !memory.cookie.has(key)) return true;
|
|
975
|
+
memory.cookie.set(key, value);
|
|
976
|
+
return false;
|
|
977
|
+
}
|
|
978
|
+
const native = nativeStorage(store);
|
|
979
|
+
if (!native) throw new Error("storage unavailable");
|
|
980
|
+
native.setItem(key, value);
|
|
981
|
+
memory[store].delete(key);
|
|
982
|
+
return true;
|
|
895
983
|
} catch {
|
|
896
|
-
|
|
984
|
+
memory[store].set(key, value);
|
|
985
|
+
return false;
|
|
897
986
|
}
|
|
898
|
-
}
|
|
899
|
-
|
|
900
|
-
|
|
987
|
+
}
|
|
988
|
+
__name(rawSet, "rawSet");
|
|
989
|
+
function rawRemove(store, key) {
|
|
990
|
+
memory[store].delete(key);
|
|
991
|
+
if (!hasWindow()) return;
|
|
901
992
|
try {
|
|
902
|
-
if (
|
|
903
|
-
|
|
993
|
+
if (store === "cookie") {
|
|
994
|
+
const secure = window.location.protocol === "https:" ? "; Secure" : "";
|
|
995
|
+
document.cookie = `${encodeURIComponent(key)}=; Path=/; Max-Age=0; SameSite=Lax${secure}`;
|
|
996
|
+
} else {
|
|
997
|
+
nativeStorage(store)?.removeItem(key);
|
|
998
|
+
}
|
|
904
999
|
} catch {
|
|
905
1000
|
}
|
|
906
1001
|
}
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
1002
|
+
__name(rawRemove, "rawRemove");
|
|
1003
|
+
function resolveStore(key, store) {
|
|
1004
|
+
return store ?? specByKey.get(key)?.[0]?.store ?? "local";
|
|
1005
|
+
}
|
|
1006
|
+
__name(resolveStore, "resolveStore");
|
|
1007
|
+
function listKeys(store, startsWith) {
|
|
1008
|
+
const out = [];
|
|
912
1009
|
try {
|
|
913
|
-
const
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
1010
|
+
const native = nativeStorage(store);
|
|
1011
|
+
if (native) {
|
|
1012
|
+
for (let i = 0; i < native.length; i += 1) {
|
|
1013
|
+
const name = native.key(i);
|
|
1014
|
+
if (name?.startsWith(startsWith)) out.push(name);
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
917
1017
|
} catch {
|
|
918
|
-
return null;
|
|
919
1018
|
}
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
1019
|
+
for (const name of memory[store].keys()) if (name.startsWith(startsWith) && !out.includes(name)) out.push(name);
|
|
1020
|
+
return out;
|
|
1021
|
+
}
|
|
1022
|
+
__name(listKeys, "listKeys");
|
|
1023
|
+
const familyKey = /* @__PURE__ */ __name((family, id) => {
|
|
1024
|
+
const part = normalizeSegment(id);
|
|
1025
|
+
if (!part) throw new TypeError("storage family id is empty after normalisation");
|
|
1026
|
+
return family.prefix + part;
|
|
1027
|
+
}, "familyKey");
|
|
1028
|
+
const api = {
|
|
1029
|
+
owner,
|
|
1030
|
+
key(...parts) {
|
|
1031
|
+
const segments = parts.map(normalizeSegment);
|
|
1032
|
+
if (segments.length === 0 || segments.some((segment) => !segment)) {
|
|
1033
|
+
throw new TypeError(`storage key parts must be non-empty: ${JSON.stringify(parts)}`);
|
|
931
1034
|
}
|
|
932
|
-
|
|
1035
|
+
return `${prefix}${segments.join(".")}`;
|
|
1036
|
+
},
|
|
1037
|
+
familyKey,
|
|
1038
|
+
isValid,
|
|
1039
|
+
read(key, store) {
|
|
1040
|
+
assertValid(key);
|
|
1041
|
+
const target = resolveStore(key, store);
|
|
1042
|
+
return rawGet(target, key);
|
|
1043
|
+
},
|
|
1044
|
+
write(key, value, store) {
|
|
1045
|
+
assertOwned(key);
|
|
1046
|
+
const target = resolveStore(key, store);
|
|
1047
|
+
return rawSet(target, key, value);
|
|
1048
|
+
},
|
|
1049
|
+
remove(key, store) {
|
|
1050
|
+
assertOwned(key);
|
|
1051
|
+
const target = resolveStore(key, store);
|
|
1052
|
+
rawRemove(target, key);
|
|
1053
|
+
},
|
|
1054
|
+
readJSON(key, store) {
|
|
1055
|
+
const raw = api.read(key, store);
|
|
1056
|
+
if (raw === null) return null;
|
|
1057
|
+
try {
|
|
1058
|
+
return JSON.parse(raw);
|
|
1059
|
+
} catch {
|
|
1060
|
+
return null;
|
|
1061
|
+
}
|
|
1062
|
+
},
|
|
1063
|
+
writeJSON(key, value, store) {
|
|
1064
|
+
return api.write(key, JSON.stringify(value), store);
|
|
1065
|
+
},
|
|
1066
|
+
keys(store = "local") {
|
|
1067
|
+
return listKeys(store, prefix);
|
|
1068
|
+
},
|
|
1069
|
+
clearAll({ all = false } = {}) {
|
|
1070
|
+
for (const spec of specs) {
|
|
1071
|
+
if (!all && !spec.personal) continue;
|
|
1072
|
+
rawRemove(spec.store, spec.key);
|
|
1073
|
+
}
|
|
1074
|
+
for (const family of families) {
|
|
1075
|
+
if (!all && !family.personal) continue;
|
|
1076
|
+
for (const name of listKeys(family.store, family.prefix)) rawRemove(family.store, name);
|
|
1077
|
+
}
|
|
1078
|
+
if (all) {
|
|
1079
|
+
for (const store of ["local", "session"]) {
|
|
1080
|
+
for (const name of listKeys(store, prefix)) rawRemove(store, name);
|
|
1081
|
+
}
|
|
1082
|
+
for (const name of cookieNames()) if (name.startsWith(prefix)) rawRemove("cookie", name);
|
|
1083
|
+
}
|
|
1084
|
+
},
|
|
1085
|
+
onChange(key, listener, store = "local") {
|
|
1086
|
+
assertValid(key);
|
|
1087
|
+
if (!hasWindow()) return () => {
|
|
1088
|
+
};
|
|
1089
|
+
const handler = /* @__PURE__ */ __name((event) => {
|
|
1090
|
+
if (event.storageArea !== nativeStorage(store)) return;
|
|
1091
|
+
if (event.key === null || event.key === key) listener(event.key === null ? null : event.newValue);
|
|
1092
|
+
}, "handler");
|
|
1093
|
+
window.addEventListener("storage", handler);
|
|
1094
|
+
return () => window.removeEventListener("storage", handler);
|
|
933
1095
|
}
|
|
934
|
-
}
|
|
1096
|
+
};
|
|
1097
|
+
return api;
|
|
1098
|
+
}
|
|
1099
|
+
__name(createStorage, "createStorage");
|
|
1100
|
+
var cfgStorage = createStorage("cfg", {
|
|
1101
|
+
keys: CFG_KEYS,
|
|
1102
|
+
families: CFG_FAMILIES
|
|
1103
|
+
});
|
|
1104
|
+
|
|
1105
|
+
// src/_api/generated/helpers/auth.ts
|
|
1106
|
+
var ACCESS_KEY = "cfg.auth.access_token";
|
|
1107
|
+
var REFRESH_KEY = "cfg.auth.refresh_token";
|
|
1108
|
+
var API_KEY_KEY = "cfg.auth.api_key";
|
|
1109
|
+
var storage = true ? cfgStorage : createStorage2("cfg");
|
|
1110
|
+
var isBrowser = typeof window !== "undefined";
|
|
1111
|
+
var STORE_OF = {
|
|
1112
|
+
localStorage: "local",
|
|
1113
|
+
sessionStorage: "session",
|
|
1114
|
+
cookie: "cookie"
|
|
935
1115
|
};
|
|
936
1116
|
function backendFor(mode) {
|
|
937
|
-
|
|
1117
|
+
const store = STORE_OF[mode];
|
|
1118
|
+
return {
|
|
1119
|
+
get: /* @__PURE__ */ __name((key) => storage.read(key, store), "get"),
|
|
1120
|
+
set(key, value) {
|
|
1121
|
+
if (value === null) storage.remove(key, store);
|
|
1122
|
+
else storage.write(key, value, store);
|
|
1123
|
+
}
|
|
1124
|
+
};
|
|
938
1125
|
}
|
|
939
1126
|
__name(backendFor, "backendFor");
|
|
940
1127
|
var _storageMode = "localStorage";
|
|
@@ -1123,10 +1310,12 @@ var auth = {
|
|
|
1123
1310
|
notifySessionChanged();
|
|
1124
1311
|
},
|
|
1125
1312
|
clearTokens() {
|
|
1126
|
-
for (const
|
|
1313
|
+
for (const mode of ["localStorage", "sessionStorage", "cookie"]) {
|
|
1314
|
+
const backend = backendFor(mode);
|
|
1127
1315
|
backend.set(ACCESS_KEY, null);
|
|
1128
1316
|
backend.set(REFRESH_KEY, null);
|
|
1129
1317
|
}
|
|
1318
|
+
storage.clearAll();
|
|
1130
1319
|
notifySessionChanged();
|
|
1131
1320
|
},
|
|
1132
1321
|
/** Session-aware: token PRESENT and not past its `exp` (or a live refresh
|
|
@@ -1318,7 +1507,7 @@ function dpopEnabled() {
|
|
|
1318
1507
|
}
|
|
1319
1508
|
}
|
|
1320
1509
|
__name(dpopEnabled, "dpopEnabled");
|
|
1321
|
-
var _DPOP_DB = "cfg
|
|
1510
|
+
var _DPOP_DB = "cfg.auth.dpop";
|
|
1322
1511
|
var _DPOP_STORE = "keys";
|
|
1323
1512
|
var _DPOP_KEY_ID = "dpop-ec-p256";
|
|
1324
1513
|
function _idbOpen() {
|