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