@djangocfg/api 2.1.331 → 2.1.333
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/auth-server.cjs +1130 -1067
- package/dist/auth-server.cjs.map +1 -1
- package/dist/auth-server.mjs +1130 -1067
- package/dist/auth-server.mjs.map +1 -1
- package/dist/auth.cjs +1229 -1166
- package/dist/auth.cjs.map +1 -1
- package/dist/auth.mjs +1229 -1166
- package/dist/auth.mjs.map +1 -1
- package/dist/clients.cjs +210 -974
- package/dist/clients.cjs.map +1 -1
- package/dist/clients.d.cts +24 -49
- package/dist/clients.d.ts +24 -49
- package/dist/clients.mjs +210 -974
- package/dist/clients.mjs.map +1 -1
- package/dist/index.cjs +1199 -1099
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +673 -657
- package/dist/index.d.ts +673 -657
- package/dist/index.mjs +1199 -1099
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/_api/generated/_cfg_accounts/api.ts +29 -82
- package/src/_api/generated/_cfg_accounts/index.ts +4 -4
- package/src/_api/generated/_cfg_centrifugo/api.ts +29 -82
- package/src/_api/generated/_cfg_centrifugo/index.ts +4 -4
- package/src/_api/generated/_cfg_totp/api.ts +29 -82
- package/src/_api/generated/_cfg_totp/index.ts +4 -4
- package/src/_api/generated/client.gen.ts +4 -0
- package/src/_api/generated/helpers/auth.ts +240 -0
- package/src/_api/generated/helpers/index.ts +1 -0
- package/src/_api/generated/index.ts +17 -13
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@djangocfg/api",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.333",
|
|
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",
|
|
@@ -79,7 +79,7 @@
|
|
|
79
79
|
"devDependencies": {
|
|
80
80
|
"@types/node": "^24.7.2",
|
|
81
81
|
"@types/react": "^19.1.0",
|
|
82
|
-
"@djangocfg/typescript-config": "^2.1.
|
|
82
|
+
"@djangocfg/typescript-config": "^2.1.333",
|
|
83
83
|
"next": "^16.2.2",
|
|
84
84
|
"react": "^19.1.0",
|
|
85
85
|
"tsup": "^8.5.0",
|
|
@@ -1,36 +1,16 @@
|
|
|
1
1
|
// AUTO-GENERATED by django_generator / ts_extras.wrapper
|
|
2
|
-
//
|
|
2
|
+
// Thin per-group proxy over the global `auth` store. All actual auth
|
|
3
|
+
// wiring lives in `helpers/auth.ts` (one interceptor, one source of
|
|
4
|
+
// truth). DO NOT EDIT — re-run `make gen`.
|
|
3
5
|
|
|
4
|
-
import {
|
|
5
|
-
import type { StorageAdapter } from '../helpers/storage';
|
|
6
|
-
import { LocalStorageAdapter } from '../helpers/storage';
|
|
6
|
+
import { auth } from '../helpers/auth';
|
|
7
7
|
import { APILogger, type LoggerConfig } from '../helpers/logger';
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
|
|
12
|
-
const ACCESS_KEY = 'cfg.access_token';
|
|
13
|
-
const REFRESH_KEY = 'cfg.refresh_token';
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
/** Auto-detect locale from cookie NEXT_LOCALE or navigator.language. */
|
|
18
|
-
function detectLocale(): string | null {
|
|
19
|
-
try {
|
|
20
|
-
if (typeof document !== 'undefined') {
|
|
21
|
-
const m = document.cookie.match(/(?:^|;\s*)NEXT_LOCALE=([^;]*)/);
|
|
22
|
-
if (m) return decodeURIComponent(m[1]);
|
|
23
|
-
}
|
|
24
|
-
if (typeof navigator !== 'undefined' && navigator.language) {
|
|
25
|
-
return navigator.language;
|
|
26
|
-
}
|
|
27
|
-
} catch {}
|
|
28
|
-
return null;
|
|
29
|
-
}
|
|
30
12
|
|
|
31
13
|
export interface APIOptions {
|
|
32
|
-
/** Override storage backend (LocalStorage by default; Memory for SSR/tests). */
|
|
33
|
-
storage?: StorageAdapter;
|
|
34
14
|
/** Logger config (defaults to dev-only). */
|
|
35
15
|
logger?: Partial<LoggerConfig>;
|
|
36
16
|
/** Locale for `Accept-Language`. If omitted, auto-detected from cookie/navigator. */
|
|
@@ -42,79 +22,46 @@ export interface APIOptions {
|
|
|
42
22
|
}
|
|
43
23
|
|
|
44
24
|
/**
|
|
45
|
-
*
|
|
25
|
+
* Per-group ergonomic facade.
|
|
46
26
|
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
27
|
+
* Calling `setToken/setApiKey/setLocale/setBaseUrl` proxies to the
|
|
28
|
+
* global `auth` store — the change applies to **every** group's API
|
|
29
|
+
* instance because they share the same HTTP client and interceptor.
|
|
30
|
+
*
|
|
31
|
+
* Use the global `auth` object directly when you don't need a group
|
|
32
|
+
* facade: `import { auth } from '@your/api'; auth.setToken(jwt);`
|
|
53
33
|
*/
|
|
54
34
|
export class API {
|
|
55
|
-
private baseUrl: string;
|
|
56
|
-
private storage: StorageAdapter;
|
|
57
|
-
private locale: string | null;
|
|
58
|
-
private apiKey: string | null;
|
|
59
35
|
readonly logger: APILogger;
|
|
60
36
|
|
|
61
37
|
|
|
62
38
|
|
|
63
|
-
constructor(
|
|
64
|
-
this.baseUrl = baseUrl.replace(/\/$/, '');
|
|
65
|
-
this.storage = opts.storage ?? new LocalStorageAdapter();
|
|
39
|
+
constructor(_baseUrl?: string, opts: APIOptions = {}) {
|
|
66
40
|
this.logger = new APILogger(opts.logger);
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
client.setConfig({ baseUrl: this.baseUrl, credentials });
|
|
72
|
-
|
|
73
|
-
client.interceptors.request.use((request) => {
|
|
74
|
-
const access = this.getToken();
|
|
75
|
-
if (access) request.headers.set('Authorization', `Bearer ${access}`);
|
|
76
|
-
|
|
77
|
-
const locale = this.locale ?? detectLocale();
|
|
78
|
-
if (locale) request.headers.set('Accept-Language', locale);
|
|
79
|
-
|
|
80
|
-
if (this.apiKey) request.headers.set('X-API-Key', this.apiKey);
|
|
81
|
-
|
|
82
|
-
return request;
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
|
|
41
|
+
if (_baseUrl) auth.setBaseUrl(_baseUrl);
|
|
42
|
+
if (opts.locale !== undefined) auth.setLocale(opts.locale);
|
|
43
|
+
if (opts.apiKey !== undefined) auth.setApiKey(opts.apiKey);
|
|
44
|
+
if (opts.withCredentials !== undefined) auth.setWithCredentials(opts.withCredentials);
|
|
86
45
|
}
|
|
87
46
|
|
|
88
47
|
// ── Base URL ────────────────────────────────────────────────────────────
|
|
89
|
-
getBaseUrl(): string { return
|
|
90
|
-
setBaseUrl(url: string): void {
|
|
91
|
-
this.baseUrl = url.replace(/\/$/, '');
|
|
92
|
-
client.setConfig({ baseUrl: this.baseUrl });
|
|
93
|
-
}
|
|
48
|
+
getBaseUrl(): string { return auth.getBaseUrl(); }
|
|
49
|
+
setBaseUrl(url: string): void { auth.setBaseUrl(url); }
|
|
94
50
|
|
|
95
51
|
// ── Tokens ──────────────────────────────────────────────────────────────
|
|
96
|
-
getToken(): string | null { return
|
|
97
|
-
setToken(token: string | null): void {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
setRefreshToken(token: string | null): void {
|
|
103
|
-
if (token) this.storage.setItem(REFRESH_KEY, token);
|
|
104
|
-
else this.storage.removeItem(REFRESH_KEY);
|
|
105
|
-
}
|
|
106
|
-
clearToken(): void {
|
|
107
|
-
this.storage.removeItem(ACCESS_KEY);
|
|
108
|
-
this.storage.removeItem(REFRESH_KEY);
|
|
109
|
-
}
|
|
110
|
-
isAuthenticated(): boolean { return this.getToken() !== null; }
|
|
52
|
+
getToken(): string | null { return auth.getToken(); }
|
|
53
|
+
setToken(token: string | null): void { auth.setToken(token); }
|
|
54
|
+
getRefreshToken(): string | null { return auth.getRefreshToken(); }
|
|
55
|
+
setRefreshToken(token: string | null): void { auth.setRefreshToken(token); }
|
|
56
|
+
clearToken(): void { auth.clearTokens(); }
|
|
57
|
+
isAuthenticated(): boolean { return auth.isAuthenticated(); }
|
|
111
58
|
|
|
112
59
|
// ── Locale / API key ────────────────────────────────────────────────────
|
|
113
|
-
getLocale(): string | null { return
|
|
114
|
-
setLocale(locale: string | null): void {
|
|
115
|
-
getApiKey(): string | null { return
|
|
116
|
-
setApiKey(key: string | null): void {
|
|
60
|
+
getLocale(): string | null { return auth.getLocale(); }
|
|
61
|
+
setLocale(locale: string | null): void { auth.setLocale(locale); }
|
|
62
|
+
getApiKey(): string | null { return auth.getApiKey(); }
|
|
63
|
+
setApiKey(key: string | null): void { auth.setApiKey(key); }
|
|
117
64
|
}
|
|
118
65
|
|
|
119
66
|
export { };
|
|
120
|
-
export {
|
|
67
|
+
export { auth };
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
// AUTO-GENERATED by django_generator / ts_extras.wrapper
|
|
2
2
|
// Group barrel. DO NOT EDIT — re-run `make gen`.
|
|
3
3
|
|
|
4
|
-
// Wrapper class + per-group SDK re-exports
|
|
5
|
-
export { API, type APIOptions, } from './api';
|
|
4
|
+
// Wrapper class + per-group SDK re-exports + global auth.
|
|
5
|
+
export { API, type APIOptions, auth, } from './api';
|
|
6
6
|
|
|
7
|
-
// Shared utilities
|
|
7
|
+
// Shared utilities.
|
|
8
8
|
export {
|
|
9
9
|
type StorageAdapter,
|
|
10
10
|
LocalStorageAdapter,
|
|
@@ -25,5 +25,5 @@ export {
|
|
|
25
25
|
type ValidationErrorEvent,
|
|
26
26
|
} from '../helpers';
|
|
27
27
|
|
|
28
|
-
// Generated artifacts (Hey API)
|
|
28
|
+
// Generated artifacts (Hey API).
|
|
29
29
|
export type * from '../types.gen';
|
|
@@ -1,36 +1,16 @@
|
|
|
1
1
|
// AUTO-GENERATED by django_generator / ts_extras.wrapper
|
|
2
|
-
//
|
|
2
|
+
// Thin per-group proxy over the global `auth` store. All actual auth
|
|
3
|
+
// wiring lives in `helpers/auth.ts` (one interceptor, one source of
|
|
4
|
+
// truth). DO NOT EDIT — re-run `make gen`.
|
|
3
5
|
|
|
4
|
-
import {
|
|
5
|
-
import type { StorageAdapter } from '../helpers/storage';
|
|
6
|
-
import { LocalStorageAdapter } from '../helpers/storage';
|
|
6
|
+
import { auth } from '../helpers/auth';
|
|
7
7
|
import { APILogger, type LoggerConfig } from '../helpers/logger';
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
|
|
12
|
-
const ACCESS_KEY = 'cfg.access_token';
|
|
13
|
-
const REFRESH_KEY = 'cfg.refresh_token';
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
/** Auto-detect locale from cookie NEXT_LOCALE or navigator.language. */
|
|
18
|
-
function detectLocale(): string | null {
|
|
19
|
-
try {
|
|
20
|
-
if (typeof document !== 'undefined') {
|
|
21
|
-
const m = document.cookie.match(/(?:^|;\s*)NEXT_LOCALE=([^;]*)/);
|
|
22
|
-
if (m) return decodeURIComponent(m[1]);
|
|
23
|
-
}
|
|
24
|
-
if (typeof navigator !== 'undefined' && navigator.language) {
|
|
25
|
-
return navigator.language;
|
|
26
|
-
}
|
|
27
|
-
} catch {}
|
|
28
|
-
return null;
|
|
29
|
-
}
|
|
30
12
|
|
|
31
13
|
export interface APIOptions {
|
|
32
|
-
/** Override storage backend (LocalStorage by default; Memory for SSR/tests). */
|
|
33
|
-
storage?: StorageAdapter;
|
|
34
14
|
/** Logger config (defaults to dev-only). */
|
|
35
15
|
logger?: Partial<LoggerConfig>;
|
|
36
16
|
/** Locale for `Accept-Language`. If omitted, auto-detected from cookie/navigator. */
|
|
@@ -42,79 +22,46 @@ export interface APIOptions {
|
|
|
42
22
|
}
|
|
43
23
|
|
|
44
24
|
/**
|
|
45
|
-
*
|
|
25
|
+
* Per-group ergonomic facade.
|
|
46
26
|
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
27
|
+
* Calling `setToken/setApiKey/setLocale/setBaseUrl` proxies to the
|
|
28
|
+
* global `auth` store — the change applies to **every** group's API
|
|
29
|
+
* instance because they share the same HTTP client and interceptor.
|
|
30
|
+
*
|
|
31
|
+
* Use the global `auth` object directly when you don't need a group
|
|
32
|
+
* facade: `import { auth } from '@your/api'; auth.setToken(jwt);`
|
|
53
33
|
*/
|
|
54
34
|
export class API {
|
|
55
|
-
private baseUrl: string;
|
|
56
|
-
private storage: StorageAdapter;
|
|
57
|
-
private locale: string | null;
|
|
58
|
-
private apiKey: string | null;
|
|
59
35
|
readonly logger: APILogger;
|
|
60
36
|
|
|
61
37
|
|
|
62
38
|
|
|
63
|
-
constructor(
|
|
64
|
-
this.baseUrl = baseUrl.replace(/\/$/, '');
|
|
65
|
-
this.storage = opts.storage ?? new LocalStorageAdapter();
|
|
39
|
+
constructor(_baseUrl?: string, opts: APIOptions = {}) {
|
|
66
40
|
this.logger = new APILogger(opts.logger);
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
client.setConfig({ baseUrl: this.baseUrl, credentials });
|
|
72
|
-
|
|
73
|
-
client.interceptors.request.use((request) => {
|
|
74
|
-
const access = this.getToken();
|
|
75
|
-
if (access) request.headers.set('Authorization', `Bearer ${access}`);
|
|
76
|
-
|
|
77
|
-
const locale = this.locale ?? detectLocale();
|
|
78
|
-
if (locale) request.headers.set('Accept-Language', locale);
|
|
79
|
-
|
|
80
|
-
if (this.apiKey) request.headers.set('X-API-Key', this.apiKey);
|
|
81
|
-
|
|
82
|
-
return request;
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
|
|
41
|
+
if (_baseUrl) auth.setBaseUrl(_baseUrl);
|
|
42
|
+
if (opts.locale !== undefined) auth.setLocale(opts.locale);
|
|
43
|
+
if (opts.apiKey !== undefined) auth.setApiKey(opts.apiKey);
|
|
44
|
+
if (opts.withCredentials !== undefined) auth.setWithCredentials(opts.withCredentials);
|
|
86
45
|
}
|
|
87
46
|
|
|
88
47
|
// ── Base URL ────────────────────────────────────────────────────────────
|
|
89
|
-
getBaseUrl(): string { return
|
|
90
|
-
setBaseUrl(url: string): void {
|
|
91
|
-
this.baseUrl = url.replace(/\/$/, '');
|
|
92
|
-
client.setConfig({ baseUrl: this.baseUrl });
|
|
93
|
-
}
|
|
48
|
+
getBaseUrl(): string { return auth.getBaseUrl(); }
|
|
49
|
+
setBaseUrl(url: string): void { auth.setBaseUrl(url); }
|
|
94
50
|
|
|
95
51
|
// ── Tokens ──────────────────────────────────────────────────────────────
|
|
96
|
-
getToken(): string | null { return
|
|
97
|
-
setToken(token: string | null): void {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
setRefreshToken(token: string | null): void {
|
|
103
|
-
if (token) this.storage.setItem(REFRESH_KEY, token);
|
|
104
|
-
else this.storage.removeItem(REFRESH_KEY);
|
|
105
|
-
}
|
|
106
|
-
clearToken(): void {
|
|
107
|
-
this.storage.removeItem(ACCESS_KEY);
|
|
108
|
-
this.storage.removeItem(REFRESH_KEY);
|
|
109
|
-
}
|
|
110
|
-
isAuthenticated(): boolean { return this.getToken() !== null; }
|
|
52
|
+
getToken(): string | null { return auth.getToken(); }
|
|
53
|
+
setToken(token: string | null): void { auth.setToken(token); }
|
|
54
|
+
getRefreshToken(): string | null { return auth.getRefreshToken(); }
|
|
55
|
+
setRefreshToken(token: string | null): void { auth.setRefreshToken(token); }
|
|
56
|
+
clearToken(): void { auth.clearTokens(); }
|
|
57
|
+
isAuthenticated(): boolean { return auth.isAuthenticated(); }
|
|
111
58
|
|
|
112
59
|
// ── Locale / API key ────────────────────────────────────────────────────
|
|
113
|
-
getLocale(): string | null { return
|
|
114
|
-
setLocale(locale: string | null): void {
|
|
115
|
-
getApiKey(): string | null { return
|
|
116
|
-
setApiKey(key: string | null): void {
|
|
60
|
+
getLocale(): string | null { return auth.getLocale(); }
|
|
61
|
+
setLocale(locale: string | null): void { auth.setLocale(locale); }
|
|
62
|
+
getApiKey(): string | null { return auth.getApiKey(); }
|
|
63
|
+
setApiKey(key: string | null): void { auth.setApiKey(key); }
|
|
117
64
|
}
|
|
118
65
|
|
|
119
66
|
export { };
|
|
120
|
-
export {
|
|
67
|
+
export { auth };
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
// AUTO-GENERATED by django_generator / ts_extras.wrapper
|
|
2
2
|
// Group barrel. DO NOT EDIT — re-run `make gen`.
|
|
3
3
|
|
|
4
|
-
// Wrapper class + per-group SDK re-exports
|
|
5
|
-
export { API, type APIOptions, } from './api';
|
|
4
|
+
// Wrapper class + per-group SDK re-exports + global auth.
|
|
5
|
+
export { API, type APIOptions, auth, } from './api';
|
|
6
6
|
|
|
7
|
-
// Shared utilities
|
|
7
|
+
// Shared utilities.
|
|
8
8
|
export {
|
|
9
9
|
type StorageAdapter,
|
|
10
10
|
LocalStorageAdapter,
|
|
@@ -25,5 +25,5 @@ export {
|
|
|
25
25
|
type ValidationErrorEvent,
|
|
26
26
|
} from '../helpers';
|
|
27
27
|
|
|
28
|
-
// Generated artifacts (Hey API)
|
|
28
|
+
// Generated artifacts (Hey API).
|
|
29
29
|
export type * from '../types.gen';
|
|
@@ -1,36 +1,16 @@
|
|
|
1
1
|
// AUTO-GENERATED by django_generator / ts_extras.wrapper
|
|
2
|
-
//
|
|
2
|
+
// Thin per-group proxy over the global `auth` store. All actual auth
|
|
3
|
+
// wiring lives in `helpers/auth.ts` (one interceptor, one source of
|
|
4
|
+
// truth). DO NOT EDIT — re-run `make gen`.
|
|
3
5
|
|
|
4
|
-
import {
|
|
5
|
-
import type { StorageAdapter } from '../helpers/storage';
|
|
6
|
-
import { LocalStorageAdapter } from '../helpers/storage';
|
|
6
|
+
import { auth } from '../helpers/auth';
|
|
7
7
|
import { APILogger, type LoggerConfig } from '../helpers/logger';
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
|
|
12
|
-
const ACCESS_KEY = 'cfg.access_token';
|
|
13
|
-
const REFRESH_KEY = 'cfg.refresh_token';
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
/** Auto-detect locale from cookie NEXT_LOCALE or navigator.language. */
|
|
18
|
-
function detectLocale(): string | null {
|
|
19
|
-
try {
|
|
20
|
-
if (typeof document !== 'undefined') {
|
|
21
|
-
const m = document.cookie.match(/(?:^|;\s*)NEXT_LOCALE=([^;]*)/);
|
|
22
|
-
if (m) return decodeURIComponent(m[1]);
|
|
23
|
-
}
|
|
24
|
-
if (typeof navigator !== 'undefined' && navigator.language) {
|
|
25
|
-
return navigator.language;
|
|
26
|
-
}
|
|
27
|
-
} catch {}
|
|
28
|
-
return null;
|
|
29
|
-
}
|
|
30
12
|
|
|
31
13
|
export interface APIOptions {
|
|
32
|
-
/** Override storage backend (LocalStorage by default; Memory for SSR/tests). */
|
|
33
|
-
storage?: StorageAdapter;
|
|
34
14
|
/** Logger config (defaults to dev-only). */
|
|
35
15
|
logger?: Partial<LoggerConfig>;
|
|
36
16
|
/** Locale for `Accept-Language`. If omitted, auto-detected from cookie/navigator. */
|
|
@@ -42,79 +22,46 @@ export interface APIOptions {
|
|
|
42
22
|
}
|
|
43
23
|
|
|
44
24
|
/**
|
|
45
|
-
*
|
|
25
|
+
* Per-group ergonomic facade.
|
|
46
26
|
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
27
|
+
* Calling `setToken/setApiKey/setLocale/setBaseUrl` proxies to the
|
|
28
|
+
* global `auth` store — the change applies to **every** group's API
|
|
29
|
+
* instance because they share the same HTTP client and interceptor.
|
|
30
|
+
*
|
|
31
|
+
* Use the global `auth` object directly when you don't need a group
|
|
32
|
+
* facade: `import { auth } from '@your/api'; auth.setToken(jwt);`
|
|
53
33
|
*/
|
|
54
34
|
export class API {
|
|
55
|
-
private baseUrl: string;
|
|
56
|
-
private storage: StorageAdapter;
|
|
57
|
-
private locale: string | null;
|
|
58
|
-
private apiKey: string | null;
|
|
59
35
|
readonly logger: APILogger;
|
|
60
36
|
|
|
61
37
|
|
|
62
38
|
|
|
63
|
-
constructor(
|
|
64
|
-
this.baseUrl = baseUrl.replace(/\/$/, '');
|
|
65
|
-
this.storage = opts.storage ?? new LocalStorageAdapter();
|
|
39
|
+
constructor(_baseUrl?: string, opts: APIOptions = {}) {
|
|
66
40
|
this.logger = new APILogger(opts.logger);
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
client.setConfig({ baseUrl: this.baseUrl, credentials });
|
|
72
|
-
|
|
73
|
-
client.interceptors.request.use((request) => {
|
|
74
|
-
const access = this.getToken();
|
|
75
|
-
if (access) request.headers.set('Authorization', `Bearer ${access}`);
|
|
76
|
-
|
|
77
|
-
const locale = this.locale ?? detectLocale();
|
|
78
|
-
if (locale) request.headers.set('Accept-Language', locale);
|
|
79
|
-
|
|
80
|
-
if (this.apiKey) request.headers.set('X-API-Key', this.apiKey);
|
|
81
|
-
|
|
82
|
-
return request;
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
|
|
41
|
+
if (_baseUrl) auth.setBaseUrl(_baseUrl);
|
|
42
|
+
if (opts.locale !== undefined) auth.setLocale(opts.locale);
|
|
43
|
+
if (opts.apiKey !== undefined) auth.setApiKey(opts.apiKey);
|
|
44
|
+
if (opts.withCredentials !== undefined) auth.setWithCredentials(opts.withCredentials);
|
|
86
45
|
}
|
|
87
46
|
|
|
88
47
|
// ── Base URL ────────────────────────────────────────────────────────────
|
|
89
|
-
getBaseUrl(): string { return
|
|
90
|
-
setBaseUrl(url: string): void {
|
|
91
|
-
this.baseUrl = url.replace(/\/$/, '');
|
|
92
|
-
client.setConfig({ baseUrl: this.baseUrl });
|
|
93
|
-
}
|
|
48
|
+
getBaseUrl(): string { return auth.getBaseUrl(); }
|
|
49
|
+
setBaseUrl(url: string): void { auth.setBaseUrl(url); }
|
|
94
50
|
|
|
95
51
|
// ── Tokens ──────────────────────────────────────────────────────────────
|
|
96
|
-
getToken(): string | null { return
|
|
97
|
-
setToken(token: string | null): void {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
setRefreshToken(token: string | null): void {
|
|
103
|
-
if (token) this.storage.setItem(REFRESH_KEY, token);
|
|
104
|
-
else this.storage.removeItem(REFRESH_KEY);
|
|
105
|
-
}
|
|
106
|
-
clearToken(): void {
|
|
107
|
-
this.storage.removeItem(ACCESS_KEY);
|
|
108
|
-
this.storage.removeItem(REFRESH_KEY);
|
|
109
|
-
}
|
|
110
|
-
isAuthenticated(): boolean { return this.getToken() !== null; }
|
|
52
|
+
getToken(): string | null { return auth.getToken(); }
|
|
53
|
+
setToken(token: string | null): void { auth.setToken(token); }
|
|
54
|
+
getRefreshToken(): string | null { return auth.getRefreshToken(); }
|
|
55
|
+
setRefreshToken(token: string | null): void { auth.setRefreshToken(token); }
|
|
56
|
+
clearToken(): void { auth.clearTokens(); }
|
|
57
|
+
isAuthenticated(): boolean { return auth.isAuthenticated(); }
|
|
111
58
|
|
|
112
59
|
// ── Locale / API key ────────────────────────────────────────────────────
|
|
113
|
-
getLocale(): string | null { return
|
|
114
|
-
setLocale(locale: string | null): void {
|
|
115
|
-
getApiKey(): string | null { return
|
|
116
|
-
setApiKey(key: string | null): void {
|
|
60
|
+
getLocale(): string | null { return auth.getLocale(); }
|
|
61
|
+
setLocale(locale: string | null): void { auth.setLocale(locale); }
|
|
62
|
+
getApiKey(): string | null { return auth.getApiKey(); }
|
|
63
|
+
setApiKey(key: string | null): void { auth.setApiKey(key); }
|
|
117
64
|
}
|
|
118
65
|
|
|
119
66
|
export { };
|
|
120
|
-
export {
|
|
67
|
+
export { auth };
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
// AUTO-GENERATED by django_generator / ts_extras.wrapper
|
|
2
2
|
// Group barrel. DO NOT EDIT — re-run `make gen`.
|
|
3
3
|
|
|
4
|
-
// Wrapper class + per-group SDK re-exports
|
|
5
|
-
export { API, type APIOptions, } from './api';
|
|
4
|
+
// Wrapper class + per-group SDK re-exports + global auth.
|
|
5
|
+
export { API, type APIOptions, auth, } from './api';
|
|
6
6
|
|
|
7
|
-
// Shared utilities
|
|
7
|
+
// Shared utilities.
|
|
8
8
|
export {
|
|
9
9
|
type StorageAdapter,
|
|
10
10
|
LocalStorageAdapter,
|
|
@@ -25,5 +25,5 @@ export {
|
|
|
25
25
|
type ValidationErrorEvent,
|
|
26
26
|
} from '../helpers';
|
|
27
27
|
|
|
28
|
-
// Generated artifacts (Hey API)
|
|
28
|
+
// Generated artifacts (Hey API).
|
|
29
29
|
export type * from '../types.gen';
|
|
@@ -14,3 +14,7 @@ import type { ClientOptions as ClientOptions2 } from './types.gen';
|
|
|
14
14
|
export type CreateClientConfig<T extends ClientOptions = ClientOptions2> = (override?: Config<ClientOptions & T>) => Config<Required<ClientOptions> & T>;
|
|
15
15
|
|
|
16
16
|
export const client = createClient(createConfig<ClientOptions2>({ baseUrl: 'http://localhost:8000' }));
|
|
17
|
+
|
|
18
|
+
// auto-init: install auth on client
|
|
19
|
+
import { installAuthOnClient } from './helpers/auth';
|
|
20
|
+
installAuthOnClient(client);
|