@djangocfg/api 2.1.332 → 2.1.334

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/dist/index.d.cts CHANGED
@@ -2,6 +2,16 @@ import { ConsolaInstance } from 'consola';
2
2
  import { ZodError } from 'zod';
3
3
 
4
4
  type StorageMode = 'localStorage' | 'cookie';
5
+ /**
6
+ * User-supplied refresh handler. Receives the current refresh token,
7
+ * must return a fresh access (and optional refresh) pair or null on failure.
8
+ * Set once at app bootstrap via `auth.setRefreshHandler(...)`.
9
+ */
10
+ type RefreshResult = {
11
+ access: string;
12
+ refresh?: string;
13
+ } | null;
14
+ type RefreshHandler = (refreshToken: string) => Promise<RefreshResult>;
5
15
  /**
6
16
  * Global auth/config store. All getters read live state every call —
7
17
  * the interceptor below uses these to attach headers per-request.
@@ -12,23 +22,12 @@ type StorageMode = 'localStorage' | 'cookie';
12
22
  *
13
23
  * @example
14
24
  * import { auth } from '@your/api';
15
- *
16
- * // After login
17
25
  * auth.setToken(jwt);
18
- * auth.setRefreshToken(refresh);
19
- *
20
- * // After logout
21
26
  * auth.clearTokens();
22
- *
23
- * // Switch to cookie storage (call once during app init)
24
27
  * auth.setStorageMode('cookie');
25
28
  */
26
29
  declare const auth: {
27
30
  getStorageMode(): StorageMode;
28
- /**
29
- * Switch the storage backend. Existing values in the *previous*
30
- * backend are NOT migrated — set fresh values after switching.
31
- */
32
31
  setStorageMode(mode: StorageMode): void;
33
32
  getToken(): string | null;
34
33
  setToken(token: string | null): void;
@@ -36,14 +35,10 @@ declare const auth: {
36
35
  setRefreshToken(token: string | null): void;
37
36
  clearTokens(): void;
38
37
  isAuthenticated(): boolean;
39
- /** In-memory API key. Falls back to storage, then NEXT_PUBLIC_API_KEY. */
40
38
  getApiKey(): string | null;
41
- /** In-memory only (cleared on reload). */
42
39
  setApiKey(key: string | null): void;
43
- /** Persist to active storage backend (localStorage or cookie). */
44
40
  setApiKeyPersist(key: string | null): void;
45
41
  clearApiKey(): void;
46
- /** Override locale → falls back to NEXT_LOCALE cookie / navigator.language. */
47
42
  getLocale(): string | null;
48
43
  setLocale(locale: string | null): void;
49
44
  getBaseUrl(): string;
@@ -51,11 +46,26 @@ declare const auth: {
51
46
  getWithCredentials(): boolean;
52
47
  setWithCredentials(value: boolean): void;
53
48
  /**
54
- * Register a callback fired on every 401 response. Use this to wire
55
- * a token-refresh flow or a forced logout. Setting `null` removes
56
- * the handler.
49
+ * Fired when the server returns 401 AND no refresh path recovers it
50
+ * (no refresh token, no refresh handler, refresh failed, or retry
51
+ * still 401). The app should clear local state and redirect to login.
52
+ *
53
+ * NOT fired for 401 that gets transparently recovered by the refresh
54
+ * handler — those are invisible to callers.
57
55
  */
58
56
  onUnauthorized(cb: ((response: Response) => void) | null): void;
57
+ /**
58
+ * Register the refresh strategy. The handler receives the current
59
+ * refresh token and must call your refresh endpoint, returning
60
+ * `{ access, refresh? }` on success or `null` on failure.
61
+ *
62
+ * @example
63
+ * auth.setRefreshHandler(async (refresh) => {
64
+ * const { data } = await Auth.tokenRefreshCreate({ body: { refresh } });
65
+ * return data ? { access: data.access, refresh: data.refresh } : null;
66
+ * });
67
+ */
68
+ setRefreshHandler(fn: RefreshHandler | null): void;
59
69
  };
60
70
 
61
71
  interface RequestLog {
@@ -143,6 +153,13 @@ declare class API$2 {
143
153
  setLocale(locale: string | null): void;
144
154
  getApiKey(): string | null;
145
155
  setApiKey(key: string | null): void;
156
+ /** Fired only on terminal 401 (after refresh+retry path is exhausted). */
157
+ onUnauthorized(cb: ((response: Response) => void) | null): void;
158
+ /** Provide a refresh strategy. See `auth.setRefreshHandler` for the contract. */
159
+ setRefreshHandler(fn: ((refreshToken: string) => Promise<{
160
+ access: string;
161
+ refresh?: string;
162
+ } | null>) | null): void;
146
163
  }
147
164
 
148
165
  interface StorageAdapter {
@@ -1406,6 +1423,13 @@ declare class API$1 {
1406
1423
  setLocale(locale: string | null): void;
1407
1424
  getApiKey(): string | null;
1408
1425
  setApiKey(key: string | null): void;
1426
+ /** Fired only on terminal 401 (after refresh+retry path is exhausted). */
1427
+ onUnauthorized(cb: ((response: Response) => void) | null): void;
1428
+ /** Provide a refresh strategy. See `auth.setRefreshHandler` for the contract. */
1429
+ setRefreshHandler(fn: ((refreshToken: string) => Promise<{
1430
+ access: string;
1431
+ refresh?: string;
1432
+ } | null>) | null): void;
1409
1433
  }
1410
1434
 
1411
1435
  interface APIOptions {
@@ -1443,6 +1467,13 @@ declare class API {
1443
1467
  setLocale(locale: string | null): void;
1444
1468
  getApiKey(): string | null;
1445
1469
  setApiKey(key: string | null): void;
1470
+ /** Fired only on terminal 401 (after refresh+retry path is exhausted). */
1471
+ onUnauthorized(cb: ((response: Response) => void) | null): void;
1472
+ /** Provide a refresh strategy. See `auth.setRefreshHandler` for the contract. */
1473
+ setRefreshHandler(fn: ((refreshToken: string) => Promise<{
1474
+ access: string;
1475
+ refresh?: string;
1476
+ } | null>) | null): void;
1446
1477
  }
1447
1478
 
1448
1479
  declare const CfgAccountsApi: API$2;
package/dist/index.d.ts CHANGED
@@ -2,6 +2,16 @@ import { ConsolaInstance } from 'consola';
2
2
  import { ZodError } from 'zod';
3
3
 
4
4
  type StorageMode = 'localStorage' | 'cookie';
5
+ /**
6
+ * User-supplied refresh handler. Receives the current refresh token,
7
+ * must return a fresh access (and optional refresh) pair or null on failure.
8
+ * Set once at app bootstrap via `auth.setRefreshHandler(...)`.
9
+ */
10
+ type RefreshResult = {
11
+ access: string;
12
+ refresh?: string;
13
+ } | null;
14
+ type RefreshHandler = (refreshToken: string) => Promise<RefreshResult>;
5
15
  /**
6
16
  * Global auth/config store. All getters read live state every call —
7
17
  * the interceptor below uses these to attach headers per-request.
@@ -12,23 +22,12 @@ type StorageMode = 'localStorage' | 'cookie';
12
22
  *
13
23
  * @example
14
24
  * import { auth } from '@your/api';
15
- *
16
- * // After login
17
25
  * auth.setToken(jwt);
18
- * auth.setRefreshToken(refresh);
19
- *
20
- * // After logout
21
26
  * auth.clearTokens();
22
- *
23
- * // Switch to cookie storage (call once during app init)
24
27
  * auth.setStorageMode('cookie');
25
28
  */
26
29
  declare const auth: {
27
30
  getStorageMode(): StorageMode;
28
- /**
29
- * Switch the storage backend. Existing values in the *previous*
30
- * backend are NOT migrated — set fresh values after switching.
31
- */
32
31
  setStorageMode(mode: StorageMode): void;
33
32
  getToken(): string | null;
34
33
  setToken(token: string | null): void;
@@ -36,14 +35,10 @@ declare const auth: {
36
35
  setRefreshToken(token: string | null): void;
37
36
  clearTokens(): void;
38
37
  isAuthenticated(): boolean;
39
- /** In-memory API key. Falls back to storage, then NEXT_PUBLIC_API_KEY. */
40
38
  getApiKey(): string | null;
41
- /** In-memory only (cleared on reload). */
42
39
  setApiKey(key: string | null): void;
43
- /** Persist to active storage backend (localStorage or cookie). */
44
40
  setApiKeyPersist(key: string | null): void;
45
41
  clearApiKey(): void;
46
- /** Override locale → falls back to NEXT_LOCALE cookie / navigator.language. */
47
42
  getLocale(): string | null;
48
43
  setLocale(locale: string | null): void;
49
44
  getBaseUrl(): string;
@@ -51,11 +46,26 @@ declare const auth: {
51
46
  getWithCredentials(): boolean;
52
47
  setWithCredentials(value: boolean): void;
53
48
  /**
54
- * Register a callback fired on every 401 response. Use this to wire
55
- * a token-refresh flow or a forced logout. Setting `null` removes
56
- * the handler.
49
+ * Fired when the server returns 401 AND no refresh path recovers it
50
+ * (no refresh token, no refresh handler, refresh failed, or retry
51
+ * still 401). The app should clear local state and redirect to login.
52
+ *
53
+ * NOT fired for 401 that gets transparently recovered by the refresh
54
+ * handler — those are invisible to callers.
57
55
  */
58
56
  onUnauthorized(cb: ((response: Response) => void) | null): void;
57
+ /**
58
+ * Register the refresh strategy. The handler receives the current
59
+ * refresh token and must call your refresh endpoint, returning
60
+ * `{ access, refresh? }` on success or `null` on failure.
61
+ *
62
+ * @example
63
+ * auth.setRefreshHandler(async (refresh) => {
64
+ * const { data } = await Auth.tokenRefreshCreate({ body: { refresh } });
65
+ * return data ? { access: data.access, refresh: data.refresh } : null;
66
+ * });
67
+ */
68
+ setRefreshHandler(fn: RefreshHandler | null): void;
59
69
  };
60
70
 
61
71
  interface RequestLog {
@@ -143,6 +153,13 @@ declare class API$2 {
143
153
  setLocale(locale: string | null): void;
144
154
  getApiKey(): string | null;
145
155
  setApiKey(key: string | null): void;
156
+ /** Fired only on terminal 401 (after refresh+retry path is exhausted). */
157
+ onUnauthorized(cb: ((response: Response) => void) | null): void;
158
+ /** Provide a refresh strategy. See `auth.setRefreshHandler` for the contract. */
159
+ setRefreshHandler(fn: ((refreshToken: string) => Promise<{
160
+ access: string;
161
+ refresh?: string;
162
+ } | null>) | null): void;
146
163
  }
147
164
 
148
165
  interface StorageAdapter {
@@ -1406,6 +1423,13 @@ declare class API$1 {
1406
1423
  setLocale(locale: string | null): void;
1407
1424
  getApiKey(): string | null;
1408
1425
  setApiKey(key: string | null): void;
1426
+ /** Fired only on terminal 401 (after refresh+retry path is exhausted). */
1427
+ onUnauthorized(cb: ((response: Response) => void) | null): void;
1428
+ /** Provide a refresh strategy. See `auth.setRefreshHandler` for the contract. */
1429
+ setRefreshHandler(fn: ((refreshToken: string) => Promise<{
1430
+ access: string;
1431
+ refresh?: string;
1432
+ } | null>) | null): void;
1409
1433
  }
1410
1434
 
1411
1435
  interface APIOptions {
@@ -1443,6 +1467,13 @@ declare class API {
1443
1467
  setLocale(locale: string | null): void;
1444
1468
  getApiKey(): string | null;
1445
1469
  setApiKey(key: string | null): void;
1470
+ /** Fired only on terminal 401 (after refresh+retry path is exhausted). */
1471
+ onUnauthorized(cb: ((response: Response) => void) | null): void;
1472
+ /** Provide a refresh strategy. See `auth.setRefreshHandler` for the contract. */
1473
+ setRefreshHandler(fn: ((refreshToken: string) => Promise<{
1474
+ access: string;
1475
+ refresh?: string;
1476
+ } | null>) | null): void;
1446
1477
  }
1447
1478
 
1448
1479
  declare const CfgAccountsApi: API$2;