@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.
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
@@ -0,0 +1,248 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // src/_api/generated/helpers/keys.ts
5
+ var k = /* @__PURE__ */ __name((key, store, personal) => ({ key, store, personal }), "k");
6
+ var f = /* @__PURE__ */ __name((prefix, store, personal) => ({ prefix, store, personal }), "f");
7
+ var CFG_KEYS = {
8
+ accessToken: k("cfg.auth.access_token", "local", true),
9
+ refreshToken: k("cfg.auth.refresh_token", "local", true),
10
+ apiKey: k("cfg.auth.api_key", "local", true),
11
+ email: k("cfg.auth.email", "local", false),
12
+ emailTs: k("cfg.auth.email_ts", "local", false),
13
+ redirectUrl: k("cfg.auth.redirect_url", "session", true),
14
+ redirectUrlTs: k("cfg.auth.redirect_url_ts", "session", true),
15
+ oauthState: k("cfg.auth.oauth_state", "session", true),
16
+ oauthProvider: k("cfg.auth.oauth_provider", "session", true),
17
+ oauthRememberMe: k("cfg.auth.oauth_remember_me", "session", true),
18
+ profileCache: k("cfg.profile.cache", "local", true),
19
+ monitorSessionId: k("cfg.monitor.session_id", "local", false),
20
+ monitorSessionCookie: k("cfg.monitor.session_id", "cookie", false),
21
+ monitorDebug: k("cfg.monitor.debug", "local", false),
22
+ uiTabId: k("cfg.ui.tab_id", "session", false),
23
+ uiRouterSerial: k("cfg.ui.router_serial", "session", false),
24
+ uiState: k("cfg.ui.state", "local", false),
25
+ chatSpeechLanguage: k("cfg.chat.speech_language", "local", false),
26
+ chatRecentTools: k("cfg.chat.recent_tools", "local", false),
27
+ formsSpeechDebug: k("cfg.forms.speech_debug", "local", false),
28
+ formsSpeechPrefs: k("cfg.forms.speech_prefs", "local", false),
29
+ agentPlaygroundApiKeyId: k("cfg.agent.playground_api_key_id", "session", true),
30
+ agentPlaygroundBearer: k("cfg.agent.playground_bearer", "session", true),
31
+ agentEndpointDoc: k("cfg.agent.endpoint_doc", "session", false)
32
+ };
33
+ var CFG_FAMILIES = {
34
+ chatDraft: f("cfg.chat.draft.", "session", true),
35
+ dataTree: f("cfg.data.tree.", "local", false),
36
+ agentEndpointDraft: f("cfg.agent.endpoint_draft.", "local", false)
37
+ };
38
+ var OWNER_RE = /^[a-z][a-z0-9_]*$/;
39
+ function normalizeSegment(raw) {
40
+ return raw.trim().toLowerCase().replace(/[^a-z0-9_]+/g, "_").replace(/^_+|_+$/g, "");
41
+ }
42
+ __name(normalizeSegment, "normalizeSegment");
43
+ var hasWindow = /* @__PURE__ */ __name(() => typeof window !== "undefined", "hasWindow");
44
+ function nativeStorage(store) {
45
+ if (!hasWindow()) return null;
46
+ try {
47
+ return store === "session" ? window.sessionStorage : window.localStorage;
48
+ } catch {
49
+ return null;
50
+ }
51
+ }
52
+ __name(nativeStorage, "nativeStorage");
53
+ function cookieNames() {
54
+ if (!hasWindow()) return [];
55
+ try {
56
+ return document.cookie.split("; ").map((part) => decodeURIComponent(part.split("=")[0] ?? "")).filter(Boolean);
57
+ } catch {
58
+ return [];
59
+ }
60
+ }
61
+ __name(cookieNames, "cookieNames");
62
+ function createStorage(owner, options = {}) {
63
+ if (!OWNER_RE.test(owner)) {
64
+ throw new TypeError(`createStorage: owner "${owner}" must match ${OWNER_RE}`);
65
+ }
66
+ const prefix = `${owner}.`;
67
+ const pattern = owner === "cfg" ? /^cfg(\.[a-z0-9_]+)+$/ : new RegExp(`^(cfg|${owner})(\\.[a-z0-9_]+)+$`);
68
+ const cookieMaxAge = options.cookieMaxAgeSeconds ?? 60 * 60 * 24 * 30;
69
+ const specs = Object.values(options.keys ?? {});
70
+ const families = Object.values(options.families ?? {});
71
+ const specByKey = /* @__PURE__ */ new Map();
72
+ for (const spec of specs) specByKey.set(spec.key, [...specByKey.get(spec.key) ?? [], spec]);
73
+ const memory = {
74
+ local: /* @__PURE__ */ new Map(),
75
+ session: /* @__PURE__ */ new Map(),
76
+ cookie: /* @__PURE__ */ new Map()
77
+ };
78
+ const isValid = /* @__PURE__ */ __name((key) => pattern.test(key), "isValid");
79
+ const assertValid = /* @__PURE__ */ __name((key) => {
80
+ if (!isValid(key)) throw new TypeError(`storage key "${key}" must match ${pattern}`);
81
+ }, "assertValid");
82
+ const assertOwned = /* @__PURE__ */ __name((key) => {
83
+ assertValid(key);
84
+ if (!key.startsWith(prefix)) throw new TypeError(`storage key "${key}" is not owned by "${owner}"`);
85
+ }, "assertOwned");
86
+ function rawGet(store, key) {
87
+ if (!hasWindow()) return null;
88
+ if (store === "cookie") {
89
+ try {
90
+ const escaped = encodeURIComponent(key).replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
91
+ const match = document.cookie.match(new RegExp(`(?:^|;\\s*)${escaped}=([^;]*)`));
92
+ if (match?.[1] !== void 0) return decodeURIComponent(match[1]);
93
+ } catch {
94
+ }
95
+ return memory.cookie.get(key) ?? null;
96
+ }
97
+ try {
98
+ const value = nativeStorage(store)?.getItem(key) ?? null;
99
+ if (value !== null) return value;
100
+ } catch {
101
+ }
102
+ return memory[store].get(key) ?? null;
103
+ }
104
+ __name(rawGet, "rawGet");
105
+ function rawSet(store, key, value) {
106
+ if (!hasWindow()) return false;
107
+ try {
108
+ if (store === "cookie") {
109
+ const secure = window.location.protocol === "https:" ? "; Secure" : "";
110
+ document.cookie = `${encodeURIComponent(key)}=${encodeURIComponent(value)}; Path=/; Max-Age=${cookieMaxAge}; SameSite=Lax${secure}`;
111
+ if (rawGet("cookie", key) === value && !memory.cookie.has(key)) return true;
112
+ memory.cookie.set(key, value);
113
+ return false;
114
+ }
115
+ const native = nativeStorage(store);
116
+ if (!native) throw new Error("storage unavailable");
117
+ native.setItem(key, value);
118
+ memory[store].delete(key);
119
+ return true;
120
+ } catch {
121
+ memory[store].set(key, value);
122
+ return false;
123
+ }
124
+ }
125
+ __name(rawSet, "rawSet");
126
+ function rawRemove(store, key) {
127
+ memory[store].delete(key);
128
+ if (!hasWindow()) return;
129
+ try {
130
+ if (store === "cookie") {
131
+ const secure = window.location.protocol === "https:" ? "; Secure" : "";
132
+ document.cookie = `${encodeURIComponent(key)}=; Path=/; Max-Age=0; SameSite=Lax${secure}`;
133
+ } else {
134
+ nativeStorage(store)?.removeItem(key);
135
+ }
136
+ } catch {
137
+ }
138
+ }
139
+ __name(rawRemove, "rawRemove");
140
+ function resolveStore(key, store) {
141
+ return store ?? specByKey.get(key)?.[0]?.store ?? "local";
142
+ }
143
+ __name(resolveStore, "resolveStore");
144
+ function listKeys(store, startsWith) {
145
+ const out = [];
146
+ try {
147
+ const native = nativeStorage(store);
148
+ if (native) {
149
+ for (let i = 0; i < native.length; i += 1) {
150
+ const name = native.key(i);
151
+ if (name?.startsWith(startsWith)) out.push(name);
152
+ }
153
+ }
154
+ } catch {
155
+ }
156
+ for (const name of memory[store].keys()) if (name.startsWith(startsWith) && !out.includes(name)) out.push(name);
157
+ return out;
158
+ }
159
+ __name(listKeys, "listKeys");
160
+ const familyKey = /* @__PURE__ */ __name((family, id) => {
161
+ const part = normalizeSegment(id);
162
+ if (!part) throw new TypeError("storage family id is empty after normalisation");
163
+ return family.prefix + part;
164
+ }, "familyKey");
165
+ const api = {
166
+ owner,
167
+ key(...parts) {
168
+ const segments = parts.map(normalizeSegment);
169
+ if (segments.length === 0 || segments.some((segment) => !segment)) {
170
+ throw new TypeError(`storage key parts must be non-empty: ${JSON.stringify(parts)}`);
171
+ }
172
+ return `${prefix}${segments.join(".")}`;
173
+ },
174
+ familyKey,
175
+ isValid,
176
+ read(key, store) {
177
+ assertValid(key);
178
+ const target = resolveStore(key, store);
179
+ return rawGet(target, key);
180
+ },
181
+ write(key, value, store) {
182
+ assertOwned(key);
183
+ const target = resolveStore(key, store);
184
+ return rawSet(target, key, value);
185
+ },
186
+ remove(key, store) {
187
+ assertOwned(key);
188
+ const target = resolveStore(key, store);
189
+ rawRemove(target, key);
190
+ },
191
+ readJSON(key, store) {
192
+ const raw = api.read(key, store);
193
+ if (raw === null) return null;
194
+ try {
195
+ return JSON.parse(raw);
196
+ } catch {
197
+ return null;
198
+ }
199
+ },
200
+ writeJSON(key, value, store) {
201
+ return api.write(key, JSON.stringify(value), store);
202
+ },
203
+ keys(store = "local") {
204
+ return listKeys(store, prefix);
205
+ },
206
+ clearAll({ all = false } = {}) {
207
+ for (const spec of specs) {
208
+ if (!all && !spec.personal) continue;
209
+ rawRemove(spec.store, spec.key);
210
+ }
211
+ for (const family of families) {
212
+ if (!all && !family.personal) continue;
213
+ for (const name of listKeys(family.store, family.prefix)) rawRemove(family.store, name);
214
+ }
215
+ if (all) {
216
+ for (const store of ["local", "session"]) {
217
+ for (const name of listKeys(store, prefix)) rawRemove(store, name);
218
+ }
219
+ for (const name of cookieNames()) if (name.startsWith(prefix)) rawRemove("cookie", name);
220
+ }
221
+ },
222
+ onChange(key, listener, store = "local") {
223
+ assertValid(key);
224
+ if (!hasWindow()) return () => {
225
+ };
226
+ const handler = /* @__PURE__ */ __name((event) => {
227
+ if (event.storageArea !== nativeStorage(store)) return;
228
+ if (event.key === null || event.key === key) listener(event.key === null ? null : event.newValue);
229
+ }, "handler");
230
+ window.addEventListener("storage", handler);
231
+ return () => window.removeEventListener("storage", handler);
232
+ }
233
+ };
234
+ return api;
235
+ }
236
+ __name(createStorage, "createStorage");
237
+ var cfgStorage = createStorage("cfg", {
238
+ keys: CFG_KEYS,
239
+ families: CFG_FAMILIES
240
+ });
241
+ export {
242
+ CFG_FAMILIES,
243
+ CFG_KEYS,
244
+ cfgStorage,
245
+ createStorage,
246
+ normalizeSegment
247
+ };
248
+ //# sourceMappingURL=storage.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/_api/generated/helpers/keys.ts"],"sourcesContent":["// AUTO-GENERATED by django_generator / ts_extras.wrapper\n// Browser-storage key registry: one scheme, guarded access.\n// DO NOT EDIT — re-run `make gen`.\n//\n// Rules for agents and apps: @docs/STORAGE_KEYS.md in @djangocfg/api.\n//\n// Scheme: `<owner>.<area>.<name>` — lower case, dots between levels,\n// `snake_case` inside a level. The framework's owner is `cfg`; an app picks its\n// own with `createStorage('<owner>')`. Third-party keys (a library's own\n// prefix, `NEXT_LOCALE`) are not ours to rename and stay as they are.\n\nexport type KeyStore = 'local' | 'session' | 'cookie';\n\nexport interface KeySpec {\n /** The canonical key. */\n readonly key: string;\n readonly store: KeyStore;\n /** Removed by `clearAll()` (sign-out). Preferences are kept. */\n readonly personal: boolean;\n}\n\nexport interface FamilySpec {\n /** Canonical prefix ending in a dot; the id is appended in normalised form. */\n readonly prefix: string;\n readonly store: 'local' | 'session';\n readonly personal: boolean;\n}\n\nconst k = (\n key: string,\n store: KeyStore,\n personal: boolean,\n): KeySpec => ({ key, store, personal });\n\nconst f = (\n prefix: string,\n store: 'local' | 'session',\n personal: boolean,\n): FamilySpec => ({ prefix, store, personal });\n\n/** Every key the framework writes. No legacy spellings are read: old keys are simply ignored. */\nexport const CFG_KEYS = {\n accessToken: k('cfg.auth.access_token', 'local', true),\n refreshToken: k('cfg.auth.refresh_token', 'local', true),\n apiKey: k('cfg.auth.api_key', 'local', true),\n email: k('cfg.auth.email', 'local', false),\n emailTs: k('cfg.auth.email_ts', 'local', false),\n redirectUrl: k('cfg.auth.redirect_url', 'session', true),\n redirectUrlTs: k('cfg.auth.redirect_url_ts', 'session', true),\n oauthState: k('cfg.auth.oauth_state', 'session', true),\n oauthProvider: k('cfg.auth.oauth_provider', 'session', true),\n oauthRememberMe: k('cfg.auth.oauth_remember_me', 'session', true),\n profileCache: k('cfg.profile.cache', 'local', true),\n monitorSessionId: k('cfg.monitor.session_id', 'local', false),\n monitorSessionCookie: k('cfg.monitor.session_id', 'cookie', false),\n monitorDebug: k('cfg.monitor.debug', 'local', false),\n uiTabId: k('cfg.ui.tab_id', 'session', false),\n uiRouterSerial: k('cfg.ui.router_serial', 'session', false),\n uiState: k('cfg.ui.state', 'local', false),\n chatSpeechLanguage: k('cfg.chat.speech_language', 'local', false),\n chatRecentTools: k('cfg.chat.recent_tools', 'local', false),\n formsSpeechDebug: k('cfg.forms.speech_debug', 'local', false),\n formsSpeechPrefs: k('cfg.forms.speech_prefs', 'local', false),\n agentPlaygroundApiKeyId: k('cfg.agent.playground_api_key_id', 'session', true),\n agentPlaygroundBearer: k('cfg.agent.playground_bearer', 'session', true),\n agentEndpointDoc: k('cfg.agent.endpoint_doc', 'session', false),\n} as const satisfies Record<string, KeySpec>;\n\n/** Keys with a caller-supplied id part: one per room, tree, endpoint. */\nexport const CFG_FAMILIES = {\n chatDraft: f('cfg.chat.draft.', 'session', true),\n dataTree: f('cfg.data.tree.', 'local', false),\n agentEndpointDraft: f('cfg.agent.endpoint_draft.', 'local', false),\n} as const satisfies Record<string, FamilySpec>;\n\nconst OWNER_RE = /^[a-z][a-z0-9_]*$/;\n\n/** One segment of a key: lower case, `[a-z0-9_]`, anything else folded to `_`. */\nexport function normalizeSegment(raw: string): string {\n return raw\n .trim()\n .toLowerCase()\n .replace(/[^a-z0-9_]+/g, '_')\n .replace(/^_+|_+$/g, '');\n}\n\nexport interface StorageOptions {\n /** Named keys of this owner. */\n readonly keys?: Readonly<Record<string, KeySpec>>;\n readonly families?: Readonly<Record<string, FamilySpec>>;\n /** Cookie lifetime for the `cookie` store. Default 30 days. */\n readonly cookieMaxAgeSeconds?: number;\n}\n\nexport interface GuardedStorage {\n readonly owner: string;\n /** Builds `<owner>.<part>.<part>` from free-form parts; throws if a part is empty. */\n key(...parts: string[]): string;\n /** Builds a family member key: the prefix plus the normalised id. */\n familyKey(family: FamilySpec, id: string): string;\n /** `^(cfg|<owner>)(\\.[a-z0-9_]+)+$`. */\n isValid(key: string): boolean;\n read(key: string, store?: KeyStore): string | null;\n /** True when the value reached the browser's storage; false means it is held in memory for this page only. */\n write(key: string, value: string, store?: KeyStore): boolean;\n /** Removes the key. */\n remove(key: string, store?: KeyStore): void;\n readJSON<T>(key: string, store?: KeyStore): T | null;\n writeJSON(key: string, value: unknown, store?: KeyStore): boolean;\n /** Keys this owner holds in `store`. */\n keys(store?: Exclude<KeyStore, 'cookie'>): string[];\n /** Sign-out: removes the `personal` keys. `{ all: true }` removes everything under this owner. */\n clearAll(options?: { all?: boolean }): void;\n /** Cross-tab: calls `listener` with the new value when ANOTHER tab changes `key`. Cookies fire no events. */\n onChange(key: string, listener: (value: string | null) => void, store?: Exclude<KeyStore, 'cookie'>): () => void;\n}\n\nconst hasWindow = () => typeof window !== 'undefined';\n\nfunction nativeStorage(store: 'local' | 'session'): Storage | null {\n if (!hasWindow()) return null;\n try {\n return store === 'session' ? window.sessionStorage : window.localStorage;\n } catch {\n return null;\n }\n}\n\nfunction cookieNames(): string[] {\n if (!hasWindow()) return [];\n try {\n return document.cookie\n .split('; ')\n .map((part) => decodeURIComponent(part.split('=')[0] ?? ''))\n .filter(Boolean);\n } catch {\n return [];\n }\n}\n\nexport function createStorage(owner: string, options: StorageOptions = {}): GuardedStorage {\n if (!OWNER_RE.test(owner)) {\n throw new TypeError(`createStorage: owner \"${owner}\" must match ${OWNER_RE}`);\n }\n const prefix = `${owner}.`;\n const pattern = owner === 'cfg' ? /^cfg(\\.[a-z0-9_]+)+$/ : new RegExp(`^(cfg|${owner})(\\\\.[a-z0-9_]+)+$`);\n const cookieMaxAge = options.cookieMaxAgeSeconds ?? 60 * 60 * 24 * 30;\n const specs = Object.values(options.keys ?? {});\n const families = Object.values(options.families ?? {});\n const specByKey = new Map<string, KeySpec[]>();\n for (const spec of specs) specByKey.set(spec.key, [...(specByKey.get(spec.key) ?? []), spec]);\n // Held in memory when the browser refuses a write (quota, private mode), so the page still works until reload.\n const memory: Record<KeyStore, Map<string, string>> = {\n local: new Map(),\n session: new Map(),\n cookie: new Map(),\n };\n\n const isValid = (key: string) => pattern.test(key);\n const assertValid = (key: string) => {\n if (!isValid(key)) throw new TypeError(`storage key \"${key}\" must match ${pattern}`);\n };\n const assertOwned = (key: string) => {\n assertValid(key);\n if (!key.startsWith(prefix)) throw new TypeError(`storage key \"${key}\" is not owned by \"${owner}\"`);\n };\n\n function rawGet(store: KeyStore, key: string): string | null {\n if (!hasWindow()) return null;\n if (store === 'cookie') {\n try {\n const escaped = encodeURIComponent(key).replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n const match = document.cookie.match(new RegExp(`(?:^|;\\\\s*)${escaped}=([^;]*)`));\n if (match?.[1] !== undefined) return decodeURIComponent(match[1]);\n } catch {\n /* fall through to memory */\n }\n return memory.cookie.get(key) ?? null;\n }\n try {\n const value = nativeStorage(store)?.getItem(key) ?? null;\n if (value !== null) return value;\n } catch {\n /* fall through to memory */\n }\n return memory[store].get(key) ?? null;\n }\n\n function rawSet(store: KeyStore, key: string, value: string): boolean {\n if (!hasWindow()) return false;\n try {\n if (store === 'cookie') {\n const secure = window.location.protocol === 'https:' ? '; Secure' : '';\n document.cookie = `${encodeURIComponent(key)}=${encodeURIComponent(value)}; Path=/; Max-Age=${cookieMaxAge}; SameSite=Lax${secure}`;\n if (rawGet('cookie', key) === value && !memory.cookie.has(key)) return true;\n memory.cookie.set(key, value);\n return false;\n }\n const native = nativeStorage(store);\n if (!native) throw new Error('storage unavailable');\n native.setItem(key, value);\n memory[store].delete(key);\n return true;\n } catch {\n memory[store].set(key, value);\n return false;\n }\n }\n\n function rawRemove(store: KeyStore, key: string): void {\n memory[store].delete(key);\n if (!hasWindow()) return;\n try {\n if (store === 'cookie') {\n const secure = window.location.protocol === 'https:' ? '; Secure' : '';\n document.cookie = `${encodeURIComponent(key)}=; Path=/; Max-Age=0; SameSite=Lax${secure}`;\n } else {\n nativeStorage(store)?.removeItem(key);\n }\n } catch {\n /* ignore */\n }\n }\n\n function resolveStore(key: string, store?: KeyStore): KeyStore {\n return store ?? specByKey.get(key)?.[0]?.store ?? 'local';\n }\n\n function listKeys(store: 'local' | 'session', startsWith: string): string[] {\n const out: string[] = [];\n try {\n const native = nativeStorage(store);\n if (native) {\n for (let i = 0; i < native.length; i += 1) {\n const name = native.key(i);\n if (name?.startsWith(startsWith)) out.push(name);\n }\n }\n } catch {\n /* ignore */\n }\n for (const name of memory[store].keys()) if (name.startsWith(startsWith) && !out.includes(name)) out.push(name);\n return out;\n }\n\n const familyKey = (family: FamilySpec, id: string) => {\n const part = normalizeSegment(id);\n if (!part) throw new TypeError('storage family id is empty after normalisation');\n return family.prefix + part;\n };\n\n const api: GuardedStorage = {\n owner,\n key(...parts) {\n const segments = parts.map(normalizeSegment);\n if (segments.length === 0 || segments.some((segment) => !segment)) {\n throw new TypeError(`storage key parts must be non-empty: ${JSON.stringify(parts)}`);\n }\n return `${prefix}${segments.join('.')}`;\n },\n familyKey,\n isValid,\n read(key, store) {\n assertValid(key);\n const target = resolveStore(key, store);\n return rawGet(target, key);\n },\n write(key, value, store) {\n assertOwned(key);\n const target = resolveStore(key, store);\n return rawSet(target, key, value);\n },\n remove(key, store) {\n assertOwned(key);\n const target = resolveStore(key, store);\n rawRemove(target, key);\n },\n readJSON<T>(key: string, store?: KeyStore): T | null {\n const raw = api.read(key, store);\n if (raw === null) return null;\n try {\n return JSON.parse(raw) as T;\n } catch {\n return null;\n }\n },\n writeJSON(key, value, store) {\n return api.write(key, JSON.stringify(value), store);\n },\n keys(store = 'local') {\n return listKeys(store, prefix);\n },\n clearAll({ all = false } = {}) {\n for (const spec of specs) {\n if (!all && !spec.personal) continue;\n rawRemove(spec.store, spec.key);\n }\n for (const family of families) {\n if (!all && !family.personal) continue;\n for (const name of listKeys(family.store, family.prefix)) rawRemove(family.store, name);\n }\n if (all) {\n for (const store of ['local', 'session'] as const) {\n for (const name of listKeys(store, prefix)) rawRemove(store, name);\n }\n for (const name of cookieNames()) if (name.startsWith(prefix)) rawRemove('cookie', name);\n }\n },\n onChange(key, listener, store = 'local') {\n assertValid(key);\n if (!hasWindow()) return () => {};\n const handler = (event: StorageEvent) => {\n if (event.storageArea !== nativeStorage(store)) return;\n if (event.key === null || event.key === key) listener(event.key === null ? null : event.newValue);\n };\n window.addEventListener('storage', handler);\n return () => window.removeEventListener('storage', handler);\n },\n };\n return api;\n}\n\n/** The framework's own storage: every `cfg.*` key. */\nexport const cfgStorage: Guarded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AAI,MAAM,gBAAgB,cAAc,KAAK,EAAG;AAChD,YAAI,MAAM,QAAQ,QAAQ,MAAM,QAAQ,IAAK,UAAS,MAAM,QAAQ,OAAO,OAAO,MAAM,QAAQ;AAAA,MAClG,GAHgB;AAIhB,aAAO,iBAAiB,WAAW,OAAO;AAC1C,aAAO,MAAM,OAAO,oBAAoB,WAAW,OAAO;AAAA,IAC5D;AAAA,EACF;AACA,SAAO;AACT;AApLgB;AAuLT,IAAM,aAA6B,cAAc,OAAO;AAAA,EAC7D,MAAM;AAAA,EACN,UAAU;AACZ,CAAC;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@djangocfg/api",
3
- "version": "2.1.582",
3
+ "version": "2.1.583",
4
4
  "description": "Auto-generated TypeScript API client with React hooks, SWR integration, and Zod validation for Django REST Framework backends",
5
5
  "keywords": [
6
6
  "django",
@@ -50,6 +50,11 @@
50
50
  "import": "./dist/auth-server.mjs",
51
51
  "require": "./dist/auth-server.cjs"
52
52
  },
53
+ "./storage": {
54
+ "types": "./dist/storage.d.ts",
55
+ "import": "./dist/storage.mjs",
56
+ "require": "./dist/storage.cjs"
57
+ },
53
58
  "./clients": {
54
59
  "types": "./dist/clients.d.ts",
55
60
  "import": "./dist/clients.mjs",
@@ -64,6 +69,7 @@
64
69
  "files": [
65
70
  "dist",
66
71
  "src",
72
+ "@docs",
67
73
  "README.md",
68
74
  "LICENSE"
69
75
  ],
@@ -85,7 +91,7 @@
85
91
  "zod": "^4.3.6"
86
92
  },
87
93
  "devDependencies": {
88
- "@djangocfg/typescript-config": "^2.1.582",
94
+ "@djangocfg/typescript-config": "^2.1.583",
89
95
  "@testing-library/react": "^16.3.2",
90
96
  "@types/node": "^25.9.5",
91
97
  "@types/react": "19.2.15",
@@ -0,0 +1,27 @@
1
+ import { readdirSync, readFileSync, statSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import { describe, expect, it } from 'vitest';
4
+
5
+ // Storage keys are defined once, in the registry. A literal anywhere else is a bug.
6
+ const FORBIDDEN = [
7
+ 'user_profile_cache', 'auth_email', 'auth_redirect_url', 'fm_session_id', 'oauth_state',
8
+ 'oauth_provider', 'cfg.access_token', 'cfg.refresh_token', 'cfg.api_key',
9
+ ];
10
+
11
+ function walk(dir: string, out: string[] = []): string[] {
12
+ for (const name of readdirSync(dir)) {
13
+ const path = join(dir, name);
14
+ if (statSync(path).isDirectory()) {
15
+ if (name !== '__tests__' && name !== 'generated') walk(path, out);
16
+ } else if (/\.tsx?$/.test(name)) out.push(path);
17
+ }
18
+ return out;
19
+ }
20
+
21
+ describe('no key literals outside the registry', () => {
22
+ const files = walk(join(__dirname, '..'));
23
+ it.each(FORBIDDEN)('"%s" is not spelled in source', (literal) => {
24
+ const hits = files.filter((f) => readFileSync(f, 'utf8').includes(`'${literal}'`) || readFileSync(f, 'utf8').includes(`"${literal}"`));
25
+ expect(hits).toEqual([]);
26
+ });
27
+ });
@@ -0,0 +1,134 @@
1
+ // @vitest-environment jsdom
2
+ import { beforeEach, describe, expect, it, vi } from 'vitest';
3
+
4
+ import { CFG_FAMILIES, CFG_KEYS, cfgStorage, createStorage, normalizeSegment } from '../storage';
5
+
6
+ const SCHEME = /^cfg(\.[a-z0-9_]+)+$/;
7
+
8
+ beforeEach(() => {
9
+ localStorage.clear();
10
+ sessionStorage.clear();
11
+ document.cookie.split('; ').forEach((c) => {
12
+ document.cookie = `${c.split('=')[0]}=; Max-Age=0; Path=/`;
13
+ });
14
+ cfgStorage.clearAll({ all: true });
15
+ });
16
+
17
+ describe('registry', () => {
18
+ it.each(Object.entries(CFG_KEYS))('%s follows the scheme and round-trips', (_name, spec) => {
19
+ expect(spec.key).toMatch(SCHEME);
20
+ expect(cfgStorage.write(spec.key, 'v', spec.store)).toBe(true);
21
+ expect(cfgStorage.read(spec.key, spec.store)).toBe('v');
22
+ cfgStorage.remove(spec.key, spec.store);
23
+ expect(cfgStorage.read(spec.key, spec.store)).toBeNull();
24
+ });
25
+
26
+ it('has no duplicate key within one store', () => {
27
+ const seen = new Set<string>();
28
+ for (const spec of Object.values(CFG_KEYS)) {
29
+ const id = `${spec.store}:${spec.key}`;
30
+ expect(seen.has(id)).toBe(false);
31
+ seen.add(id);
32
+ }
33
+ });
34
+
35
+ it.each(Object.entries(CFG_FAMILIES))('family %s prefix follows the scheme', (_name, family) => {
36
+ expect(family.prefix).toMatch(/^cfg(\.[a-z0-9_]+)+\.$/);
37
+ expect(cfgStorage.familyKey(family, 'Room 1/A')).toBe(`${family.prefix}room_1_a`);
38
+ });
39
+
40
+ it('does not read old spellings', () => {
41
+ localStorage.setItem('cfg.access_token', 'old');
42
+ expect(cfgStorage.read(CFG_KEYS.accessToken.key)).toBeNull();
43
+ });
44
+ });
45
+
46
+ describe('validation', () => {
47
+ it('rejects keys outside the scheme', () => {
48
+ for (const bad of ['auth_email', 'cfg', 'cfg.', 'cfg.Auth.x', 'cfg.auth-x', 'other.x']) {
49
+ expect(() => cfgStorage.read(bad)).toThrow(TypeError);
50
+ }
51
+ });
52
+
53
+ it('app storage reads cfg and own keys, writes only its own', () => {
54
+ const app = createStorage('kino');
55
+ expect(app.isValid('kino.player.volume')).toBe(true);
56
+ expect(app.isValid('cfg.auth.email')).toBe(true);
57
+ expect(app.isValid('other.x')).toBe(false);
58
+ expect(() => app.write('cfg.auth.email', 'x')).toThrow(TypeError);
59
+ expect(app.write('kino.player.volume', '3')).toBe(true);
60
+ expect(app.read('kino.player.volume')).toBe('3');
61
+ });
62
+
63
+ it('builds and normalises keys from parts', () => {
64
+ const app = createStorage('kino');
65
+ expect(app.key('Player', 'Last Volume')).toBe('kino.player.last_volume');
66
+ expect(() => app.key('player', ' ')).toThrow(TypeError);
67
+ expect(() => createStorage('Bad-Owner')).toThrow(TypeError);
68
+ expect(normalizeSegment(' A-b c ')).toBe('a_b_c');
69
+ });
70
+ });
71
+
72
+ describe('guarded I/O', () => {
73
+ it('keeps the value in memory when the browser refuses a write', () => {
74
+ const spy = vi.spyOn(Storage.prototype, 'setItem').mockImplementation(() => {
75
+ throw new DOMException('full', 'QuotaExceededError');
76
+ });
77
+ expect(cfgStorage.write(CFG_KEYS.email.key, 'a@b.c')).toBe(false);
78
+ expect(cfgStorage.read(CFG_KEYS.email.key)).toBe('a@b.c');
79
+ spy.mockRestore();
80
+ });
81
+
82
+ it('survives a throwing storage getter', () => {
83
+ const spy = vi.spyOn(Storage.prototype, 'getItem').mockImplementation(() => {
84
+ throw new Error('denied');
85
+ });
86
+ expect(cfgStorage.read(CFG_KEYS.email.key)).toBeNull();
87
+ spy.mockRestore();
88
+ });
89
+
90
+ it('readJSON returns null on garbage', () => {
91
+ localStorage.setItem(CFG_KEYS.uiState.key, '{oops');
92
+ expect(cfgStorage.readJSON(CFG_KEYS.uiState.key)).toBeNull();
93
+ cfgStorage.writeJSON(CFG_KEYS.uiState.key, { a: 1 });
94
+ expect(cfgStorage.readJSON(CFG_KEYS.uiState.key)).toEqual({ a: 1 });
95
+ });
96
+ });
97
+
98
+ describe('clearAll', () => {
99
+ it('removes personal keys and family members, keeps preferences', () => {
100
+ cfgStorage.write(CFG_KEYS.accessToken.key, 't');
101
+ cfgStorage.write(CFG_KEYS.chatSpeechLanguage.key, 'ru');
102
+ cfgStorage.write(cfgStorage.familyKey(CFG_FAMILIES.chatDraft, 'r1'), 'draft', 'session');
103
+ cfgStorage.write(cfgStorage.familyKey(CFG_FAMILIES.dataTree, 't1'), 'tree');
104
+ cfgStorage.clearAll();
105
+ expect(cfgStorage.read(CFG_KEYS.accessToken.key)).toBeNull();
106
+ expect(cfgStorage.read(cfgStorage.familyKey(CFG_FAMILIES.chatDraft, 'r1'), 'session')).toBeNull();
107
+ expect(cfgStorage.read(CFG_KEYS.chatSpeechLanguage.key)).toBe('ru');
108
+ expect(cfgStorage.read(cfgStorage.familyKey(CFG_FAMILIES.dataTree, 't1'))).toBe('tree');
109
+ });
110
+
111
+ it('{ all: true } wipes the owner namespace but not foreign keys', () => {
112
+ localStorage.setItem('NEXT_LOCALE', 'en');
113
+ cfgStorage.write(CFG_KEYS.chatSpeechLanguage.key, 'ru');
114
+ cfgStorage.write('cfg.custom.thing', '1');
115
+ cfgStorage.clearAll({ all: true });
116
+ expect(localStorage.getItem('cfg.custom.thing')).toBeNull();
117
+ expect(localStorage.getItem('NEXT_LOCALE')).toBe('en');
118
+ });
119
+ });
120
+
121
+ describe('cross-tab', () => {
122
+ it('onChange fires for the key from another tab only', () => {
123
+ const listener = vi.fn();
124
+ const off = cfgStorage.onChange(CFG_KEYS.accessToken.key, listener);
125
+ window.dispatchEvent(new StorageEvent('storage', { key: 'cfg.other.x', newValue: '1', storageArea: localStorage }));
126
+ window.dispatchEvent(new StorageEvent('storage', { key: CFG_KEYS.accessToken.key, newValue: 'n', storageArea: sessionStorage }));
127
+ expect(listener).not.toHaveBeenCalled();
128
+ window.dispatchEvent(new StorageEvent('storage', { key: CFG_KEYS.accessToken.key, newValue: 'n', storageArea: localStorage }));
129
+ expect(listener).toHaveBeenCalledWith('n');
130
+ off();
131
+ window.dispatchEvent(new StorageEvent('storage', { key: null, storageArea: localStorage }));
132
+ expect(listener).toHaveBeenCalledTimes(1);
133
+ });
134
+ });
@@ -0,0 +1,14 @@
1
+ import { describe, expect, it } from 'vitest';
2
+
3
+ import { CFG_KEYS, cfgStorage } from '../storage';
4
+
5
+ describe('storage on the server', () => {
6
+ it('never throws and never holds state', () => {
7
+ expect(typeof window).toBe('undefined');
8
+ expect(cfgStorage.read(CFG_KEYS.accessToken.key)).toBeNull();
9
+ expect(cfgStorage.write(CFG_KEYS.accessToken.key, 'x')).toBe(false);
10
+ expect(cfgStorage.read(CFG_KEYS.accessToken.key)).toBeNull();
11
+ expect(() => cfgStorage.clearAll({ all: true })).not.toThrow();
12
+ expect(cfgStorage.onChange(CFG_KEYS.accessToken.key, () => {})).toBeTypeOf('function');
13
+ });
14
+ });
@@ -5,10 +5,14 @@
5
5
  // DO NOT EDIT — re-run `make gen`.
6
6
 
7
7
  import { APIError } from './errors';
8
+ import { cfgStorage, createStorage, type KeyStore } from './keys';
8
9
 
9
- const ACCESS_KEY = 'cfg.access_token';
10
- const REFRESH_KEY = 'cfg.refresh_token';
11
- const API_KEY_KEY = 'cfg.api_key';
10
+ const ACCESS_KEY = 'cfg.auth.access_token';
11
+ const REFRESH_KEY = 'cfg.auth.refresh_token';
12
+ const API_KEY_KEY = 'cfg.auth.api_key';
13
+
14
+ // The framework's own keys use the shared registry; a custom owner gets the same guarded helpers.
15
+ const storage = 'cfg' === 'cfg' ? cfgStorage : createStorage('cfg');
12
16
 
13
17
  const isBrowser = typeof window !== 'undefined';
14
18
 
@@ -21,68 +25,24 @@ interface KVStore {
21
25
  set(key: string, value: string | null): void;
22
26
  }
23
27
 
24
- const localStorageBackend: KVStore = {
25
- get(key) {
26
- if (!isBrowser) return null;
27
- try { return window.localStorage.getItem(key); } catch { return null; }
28
- },
29
- set(key, value) {
30
- if (!isBrowser) return;
31
- try {
32
- if (value === null) window.localStorage.removeItem(key);
33
- else window.localStorage.setItem(key, value);
34
- } catch {}
35
- },
36
- };
37
-
38
- const sessionStorageBackend: KVStore = {
39
- get(key) {
40
- if (!isBrowser) return null;
41
- try { return window.sessionStorage.getItem(key); } catch { return null; }
42
- },
43
- set(key, value) {
44
- if (!isBrowser) return;
45
- try {
46
- if (value === null) window.sessionStorage.removeItem(key);
47
- else window.sessionStorage.setItem(key, value);
48
- } catch {}
49
- },
50
- };
51
-
52
- /** 30 days, matches typical refresh-token lifetime. */
53
- const COOKIE_MAX_AGE = 60 * 60 * 24 * 30;
54
-
55
- const cookieBackend: KVStore = {
56
- get(key) {
57
- if (!isBrowser) return null;
58
- try {
59
- const re = new RegExp(`(?:^|;\\s*)${encodeURIComponent(key)}=([^;]*)`);
60
- const m = document.cookie.match(re);
61
- const value = m?.[1];
62
- return value === undefined ? null : decodeURIComponent(value);
63
- } catch { return null; }
64
- },
65
- set(key, value) {
66
- if (!isBrowser) return;
67
- try {
68
- const k = encodeURIComponent(key);
69
- const secure = window.location.protocol === 'https:' ? '; Secure' : '';
70
- if (value === null) {
71
- document.cookie = `${k}=; Path=/; Max-Age=0; SameSite=Lax${secure}`;
72
- } else {
73
- const v = encodeURIComponent(value);
74
- document.cookie = `${k}=${v}; Path=/; Max-Age=${COOKIE_MAX_AGE}; SameSite=Lax${secure}`;
75
- }
76
- } catch {}
77
- },
28
+ // Every read and write goes through the registry's guarded storage: SSR-safe,
29
+ // private-mode and quota safe, and a key written under an older spelling is
30
+ // moved to its `cfg.auth.*` name on first read.
31
+ const STORE_OF: Record<StorageMode, KeyStore> = {
32
+ localStorage: 'local',
33
+ sessionStorage: 'session',
34
+ cookie: 'cookie',
78
35
  };
79
36
 
80
37
  function backendFor(mode: StorageMode): KVStore {
81
- return mode === 'cookie'
82
- ? cookieBackend
83
- : mode === 'sessionStorage'
84
- ? sessionStorageBackend
85
- : localStorageBackend;
38
+ const store = STORE_OF[mode];
39
+ return {
40
+ get: (key) => storage.read(key, store),
41
+ set(key, value) {
42
+ if (value === null) storage.remove(key, store);
43
+ else storage.write(key, value, store);
44
+ },
45
+ };
86
46
  }
87
47
 
88
48
  /**
@@ -411,10 +371,13 @@ export const auth = {
411
371
  // Clear every backend, not just the active one. Sign-out must leave nothing
412
372
  // behind anywhere — including tokens an older build stranded in
413
373
  // sessionStorage — or the next page load could resurrect a dead session.
414
- for (const backend of [localStorageBackend, sessionStorageBackend, cookieBackend]) {
374
+ for (const mode of ['localStorage', 'sessionStorage', 'cookie'] as const) {
375
+ const backend = backendFor(mode);
415
376
  backend.set(ACCESS_KEY, null);
416
377
  backend.set(REFRESH_KEY, null);
417
378
  }
379
+ // The rest of the signed-in user's keys (profile cache, OAuth leftovers) go with the tokens.
380
+ storage.clearAll();
418
381
  notifySessionChanged();
419
382
  },
420
383
  /** Session-aware: token PRESENT and not past its `exp` (or a live refresh
@@ -679,7 +642,7 @@ function dpopEnabled(): boolean {
679
642
  } catch { return false; }
680
643
  }
681
644
 
682
- const _DPOP_DB = 'cfg-auth';
645
+ const _DPOP_DB = 'cfg.auth.dpop';
683
646
  const _DPOP_STORE = 'keys';
684
647
  const _DPOP_KEY_ID = 'dpop-ec-p256';
685
648
 
@@ -9,6 +9,18 @@ export {
9
9
  CookieStorageAdapter,
10
10
  } from './storage';
11
11
  export { APIError, NetworkError } from './errors';
12
+ export {
13
+ CFG_KEYS,
14
+ CFG_FAMILIES,
15
+ cfgStorage,
16
+ createStorage,
17
+ normalizeSegment,
18
+ type KeySpec,
19
+ type FamilySpec,
20
+ type KeyStore,
21
+ type GuardedStorage,
22
+ type StorageOptions,
23
+ } from './keys';
12
24
  export {
13
25
  APILogger,
14
26
  defaultLogger,