@djangocfg/api 2.1.139 → 2.1.143
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 +2 -199
- package/dist/auth-server.cjs.map +1 -1
- package/dist/auth-server.mjs +2 -199
- package/dist/auth-server.mjs.map +1 -1
- package/dist/auth.cjs +11 -836
- package/dist/auth.cjs.map +1 -1
- package/dist/auth.mjs +11 -836
- package/dist/auth.mjs.map +1 -1
- package/dist/clients.cjs +40 -1077
- package/dist/clients.cjs.map +1 -1
- package/dist/clients.d.cts +209 -754
- package/dist/clients.d.ts +209 -754
- package/dist/clients.mjs +40 -1077
- package/dist/clients.mjs.map +1 -1
- package/dist/index.cjs +0 -1198
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +229 -1211
- package/dist/index.d.ts +229 -1211
- package/dist/index.mjs +0 -1198
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/auth/hooks/useAuthForm.ts +1 -1
- package/src/auth/hooks/useAuthGuard.ts +1 -2
- package/src/auth/hooks/useAutoAuth.ts +1 -2
- package/src/auth/hooks/useDeleteAccount.ts +1 -1
- package/src/auth/hooks/useGithubAuth.ts +1 -2
- package/src/auth/hooks/useTokenRefresh.ts +1 -0
- package/src/auth/hooks/useTwoFactor.ts +1 -2
- package/src/clients.ts +2 -12
- package/src/index.ts +0 -6
- package/src/generated/cfg_webpush/CLAUDE.md +0 -63
- package/src/generated/cfg_webpush/_utils/fetchers/index.ts +0 -29
- package/src/generated/cfg_webpush/_utils/fetchers/webpush__web_push.ts +0 -211
- package/src/generated/cfg_webpush/_utils/hooks/index.ts +0 -29
- package/src/generated/cfg_webpush/_utils/hooks/webpush__web_push.ts +0 -79
- package/src/generated/cfg_webpush/_utils/schemas/SendPushRequestRequest.schema.ts +0 -22
- package/src/generated/cfg_webpush/_utils/schemas/SendPushResponse.schema.ts +0 -20
- package/src/generated/cfg_webpush/_utils/schemas/SubscribeRequestRequest.schema.ts +0 -20
- package/src/generated/cfg_webpush/_utils/schemas/SubscribeResponse.schema.ts +0 -21
- package/src/generated/cfg_webpush/_utils/schemas/VapidPublicKeyResponse.schema.ts +0 -19
- package/src/generated/cfg_webpush/_utils/schemas/index.ts +0 -24
- package/src/generated/cfg_webpush/api-instance.ts +0 -180
- package/src/generated/cfg_webpush/client.ts +0 -322
- package/src/generated/cfg_webpush/errors.ts +0 -117
- package/src/generated/cfg_webpush/http.ts +0 -110
- package/src/generated/cfg_webpush/index.ts +0 -275
- package/src/generated/cfg_webpush/logger.ts +0 -260
- package/src/generated/cfg_webpush/retry.ts +0 -176
- package/src/generated/cfg_webpush/schema.json +0 -302
- package/src/generated/cfg_webpush/storage.ts +0 -162
- package/src/generated/cfg_webpush/validation-events.ts +0 -134
- package/src/generated/cfg_webpush/webpush__web_push/client.ts +0 -45
- package/src/generated/cfg_webpush/webpush__web_push/index.ts +0 -3
- package/src/generated/cfg_webpush/webpush__web_push/models.ts +0 -65
- package/src/hooks/webpush.ts +0 -12
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
// Auto-generated by DjangoCFG - see CLAUDE.md
|
|
2
|
-
/**
|
|
3
|
-
* Retry Configuration and Utilities
|
|
4
|
-
*
|
|
5
|
-
* Provides automatic retry logic for failed HTTP requests using p-retry.
|
|
6
|
-
* Retries only on network errors and server errors (5xx), not client errors (4xx).
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import pRetry, { AbortError } from 'p-retry';
|
|
10
|
-
import { APIError, NetworkError } from './errors';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Information about a failed retry attempt.
|
|
14
|
-
*/
|
|
15
|
-
export interface FailedAttemptInfo {
|
|
16
|
-
/** The error that caused the failure */
|
|
17
|
-
error: Error;
|
|
18
|
-
/** The attempt number (1-indexed) */
|
|
19
|
-
attemptNumber: number;
|
|
20
|
-
/** Number of retries left */
|
|
21
|
-
retriesLeft: number;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Retry configuration options.
|
|
26
|
-
*
|
|
27
|
-
* Uses exponential backoff with jitter by default to avoid thundering herd.
|
|
28
|
-
*/
|
|
29
|
-
export interface RetryConfig {
|
|
30
|
-
/**
|
|
31
|
-
* Maximum number of retry attempts.
|
|
32
|
-
* @default 3
|
|
33
|
-
*/
|
|
34
|
-
retries?: number;
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Exponential backoff factor.
|
|
38
|
-
* @default 2
|
|
39
|
-
*/
|
|
40
|
-
factor?: number;
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Minimum wait time between retries (ms).
|
|
44
|
-
* @default 1000
|
|
45
|
-
*/
|
|
46
|
-
minTimeout?: number;
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Maximum wait time between retries (ms).
|
|
50
|
-
* @default 60000
|
|
51
|
-
*/
|
|
52
|
-
maxTimeout?: number;
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Add randomness to wait times (jitter).
|
|
56
|
-
* Helps avoid thundering herd problem.
|
|
57
|
-
* @default true
|
|
58
|
-
*/
|
|
59
|
-
randomize?: boolean;
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Callback called on each failed attempt.
|
|
63
|
-
*/
|
|
64
|
-
onFailedAttempt?: (info: FailedAttemptInfo) => void;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Default retry configuration.
|
|
69
|
-
*/
|
|
70
|
-
export const DEFAULT_RETRY_CONFIG: Required<RetryConfig> = {
|
|
71
|
-
retries: 3,
|
|
72
|
-
factor: 2,
|
|
73
|
-
minTimeout: 1000,
|
|
74
|
-
maxTimeout: 60000,
|
|
75
|
-
randomize: true,
|
|
76
|
-
onFailedAttempt: () => {},
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Determine if an error should trigger a retry.
|
|
81
|
-
*
|
|
82
|
-
* Retries on:
|
|
83
|
-
* - Network errors (connection refused, timeout, etc.)
|
|
84
|
-
* - Server errors (5xx status codes)
|
|
85
|
-
* - Rate limiting (429 status code)
|
|
86
|
-
*
|
|
87
|
-
* Does NOT retry on:
|
|
88
|
-
* - Client errors (4xx except 429)
|
|
89
|
-
* - Authentication errors (401, 403)
|
|
90
|
-
* - Not found (404)
|
|
91
|
-
*
|
|
92
|
-
* @param error - The error to check
|
|
93
|
-
* @returns true if should retry, false otherwise
|
|
94
|
-
*/
|
|
95
|
-
export function shouldRetry(error: any): boolean {
|
|
96
|
-
// Always retry network errors
|
|
97
|
-
if (error instanceof NetworkError) {
|
|
98
|
-
return true;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// For API errors, check status code
|
|
102
|
-
if (error instanceof APIError) {
|
|
103
|
-
const status = error.statusCode;
|
|
104
|
-
|
|
105
|
-
// Retry on 5xx server errors
|
|
106
|
-
if (status >= 500 && status < 600) {
|
|
107
|
-
return true;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
// Retry on 429 (rate limit)
|
|
111
|
-
if (status === 429) {
|
|
112
|
-
return true;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
// Do NOT retry on 4xx client errors
|
|
116
|
-
return false;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
// Retry on unknown errors (might be network issues)
|
|
120
|
-
return true;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Wrap a function with retry logic.
|
|
125
|
-
*
|
|
126
|
-
* @param fn - Async function to retry
|
|
127
|
-
* @param config - Retry configuration
|
|
128
|
-
* @returns Result of the function
|
|
129
|
-
*
|
|
130
|
-
* @example
|
|
131
|
-
* ```typescript
|
|
132
|
-
* const result = await withRetry(
|
|
133
|
-
* async () => fetch('https://api.example.com/users'),
|
|
134
|
-
* { retries: 5, minTimeout: 2000 }
|
|
135
|
-
* );
|
|
136
|
-
* ```
|
|
137
|
-
*/
|
|
138
|
-
export async function withRetry<T>(
|
|
139
|
-
fn: () => Promise<T>,
|
|
140
|
-
config?: RetryConfig
|
|
141
|
-
): Promise<T> {
|
|
142
|
-
const finalConfig = { ...DEFAULT_RETRY_CONFIG, ...config };
|
|
143
|
-
|
|
144
|
-
return pRetry(
|
|
145
|
-
async () => {
|
|
146
|
-
try {
|
|
147
|
-
return await fn();
|
|
148
|
-
} catch (error) {
|
|
149
|
-
// Check if we should retry this error
|
|
150
|
-
if (!shouldRetry(error)) {
|
|
151
|
-
// Abort retry immediately for non-retryable errors
|
|
152
|
-
throw new AbortError(error as Error);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
// Re-throw error to trigger retry
|
|
156
|
-
throw error;
|
|
157
|
-
}
|
|
158
|
-
},
|
|
159
|
-
{
|
|
160
|
-
retries: finalConfig.retries,
|
|
161
|
-
factor: finalConfig.factor,
|
|
162
|
-
minTimeout: finalConfig.minTimeout,
|
|
163
|
-
maxTimeout: finalConfig.maxTimeout,
|
|
164
|
-
randomize: finalConfig.randomize,
|
|
165
|
-
onFailedAttempt: finalConfig.onFailedAttempt ? (error) => {
|
|
166
|
-
// Adapt p-retry's FailedAttemptError to our FailedAttemptInfo
|
|
167
|
-
const pRetryError = error as any; // p-retry's internal type
|
|
168
|
-
finalConfig.onFailedAttempt!({
|
|
169
|
-
error: pRetryError as Error,
|
|
170
|
-
attemptNumber: pRetryError.attemptNumber,
|
|
171
|
-
retriesLeft: pRetryError.retriesLeft,
|
|
172
|
-
});
|
|
173
|
-
} : undefined,
|
|
174
|
-
}
|
|
175
|
-
);
|
|
176
|
-
}
|
|
@@ -1,302 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"openapi": "3.0.3",
|
|
3
|
-
"info": {
|
|
4
|
-
"title": "Django CFG API",
|
|
5
|
-
"version": "1.0.0",
|
|
6
|
-
"description": "Complete API documentation for Django CFG Demo Project",
|
|
7
|
-
"x-django-metadata": {
|
|
8
|
-
"group": "cfg_webpush",
|
|
9
|
-
"apps": [
|
|
10
|
-
"django_cfg_webpush"
|
|
11
|
-
],
|
|
12
|
-
"generator": "django-client",
|
|
13
|
-
"generator_version": "1.0.0"
|
|
14
|
-
}
|
|
15
|
-
},
|
|
16
|
-
"paths": {
|
|
17
|
-
"/cfg/webpush/send/": {
|
|
18
|
-
"post": {
|
|
19
|
-
"operationId": "cfg_webpush_send_create",
|
|
20
|
-
"description": "Send push notification to all active subscriptions for the authenticated user.",
|
|
21
|
-
"summary": "Send push notification",
|
|
22
|
-
"tags": [
|
|
23
|
-
"Web Push"
|
|
24
|
-
],
|
|
25
|
-
"requestBody": {
|
|
26
|
-
"content": {
|
|
27
|
-
"application/json": {
|
|
28
|
-
"schema": {
|
|
29
|
-
"$ref": "#/components/schemas/SendPushRequestRequest"
|
|
30
|
-
}
|
|
31
|
-
},
|
|
32
|
-
"application/x-www-form-urlencoded": {
|
|
33
|
-
"schema": {
|
|
34
|
-
"$ref": "#/components/schemas/SendPushRequestRequest"
|
|
35
|
-
}
|
|
36
|
-
},
|
|
37
|
-
"multipart/form-data": {
|
|
38
|
-
"schema": {
|
|
39
|
-
"$ref": "#/components/schemas/SendPushRequestRequest"
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
},
|
|
43
|
-
"required": true
|
|
44
|
-
},
|
|
45
|
-
"security": [
|
|
46
|
-
{
|
|
47
|
-
"jwtAuthWithLastLogin": []
|
|
48
|
-
}
|
|
49
|
-
],
|
|
50
|
-
"responses": {
|
|
51
|
-
"200": {
|
|
52
|
-
"content": {
|
|
53
|
-
"application/json": {
|
|
54
|
-
"schema": {
|
|
55
|
-
"$ref": "#/components/schemas/SendPushResponse"
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
},
|
|
59
|
-
"description": ""
|
|
60
|
-
},
|
|
61
|
-
"400": {
|
|
62
|
-
"content": {
|
|
63
|
-
"application/json": {
|
|
64
|
-
"schema": {
|
|
65
|
-
"description": "Bad request"
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
},
|
|
69
|
-
"description": ""
|
|
70
|
-
}
|
|
71
|
-
},
|
|
72
|
-
"x-async-capable": false
|
|
73
|
-
}
|
|
74
|
-
},
|
|
75
|
-
"/cfg/webpush/subscribe/": {
|
|
76
|
-
"post": {
|
|
77
|
-
"operationId": "cfg_webpush_subscribe_create",
|
|
78
|
-
"description": "Save push subscription from browser for the authenticated user.",
|
|
79
|
-
"summary": "Subscribe to push notifications",
|
|
80
|
-
"tags": [
|
|
81
|
-
"Web Push"
|
|
82
|
-
],
|
|
83
|
-
"requestBody": {
|
|
84
|
-
"content": {
|
|
85
|
-
"application/json": {
|
|
86
|
-
"schema": {
|
|
87
|
-
"$ref": "#/components/schemas/SubscribeRequestRequest"
|
|
88
|
-
}
|
|
89
|
-
},
|
|
90
|
-
"application/x-www-form-urlencoded": {
|
|
91
|
-
"schema": {
|
|
92
|
-
"$ref": "#/components/schemas/SubscribeRequestRequest"
|
|
93
|
-
}
|
|
94
|
-
},
|
|
95
|
-
"multipart/form-data": {
|
|
96
|
-
"schema": {
|
|
97
|
-
"$ref": "#/components/schemas/SubscribeRequestRequest"
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
},
|
|
101
|
-
"required": true
|
|
102
|
-
},
|
|
103
|
-
"security": [
|
|
104
|
-
{
|
|
105
|
-
"jwtAuthWithLastLogin": []
|
|
106
|
-
}
|
|
107
|
-
],
|
|
108
|
-
"responses": {
|
|
109
|
-
"200": {
|
|
110
|
-
"content": {
|
|
111
|
-
"application/json": {
|
|
112
|
-
"schema": {
|
|
113
|
-
"$ref": "#/components/schemas/SubscribeResponse"
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
},
|
|
117
|
-
"description": ""
|
|
118
|
-
},
|
|
119
|
-
"400": {
|
|
120
|
-
"content": {
|
|
121
|
-
"application/json": {
|
|
122
|
-
"schema": {
|
|
123
|
-
"description": "Bad request"
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
},
|
|
127
|
-
"description": ""
|
|
128
|
-
}
|
|
129
|
-
},
|
|
130
|
-
"x-async-capable": false
|
|
131
|
-
}
|
|
132
|
-
},
|
|
133
|
-
"/cfg/webpush/vapid/": {
|
|
134
|
-
"get": {
|
|
135
|
-
"operationId": "cfg_webpush_vapid_retrieve",
|
|
136
|
-
"description": "Get VAPID public key for client subscription.",
|
|
137
|
-
"summary": "Get VAPID public key",
|
|
138
|
-
"tags": [
|
|
139
|
-
"Web Push"
|
|
140
|
-
],
|
|
141
|
-
"security": [
|
|
142
|
-
{
|
|
143
|
-
"jwtAuthWithLastLogin": []
|
|
144
|
-
}
|
|
145
|
-
],
|
|
146
|
-
"responses": {
|
|
147
|
-
"200": {
|
|
148
|
-
"content": {
|
|
149
|
-
"application/json": {
|
|
150
|
-
"schema": {
|
|
151
|
-
"$ref": "#/components/schemas/VapidPublicKeyResponse"
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
},
|
|
155
|
-
"description": ""
|
|
156
|
-
},
|
|
157
|
-
"503": {
|
|
158
|
-
"content": {
|
|
159
|
-
"application/json": {
|
|
160
|
-
"schema": {
|
|
161
|
-
"description": "Service unavailable"
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
},
|
|
165
|
-
"description": ""
|
|
166
|
-
}
|
|
167
|
-
},
|
|
168
|
-
"x-async-capable": false
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
},
|
|
172
|
-
"components": {
|
|
173
|
-
"schemas": {
|
|
174
|
-
"SendPushRequestRequest": {
|
|
175
|
-
"type": "object",
|
|
176
|
-
"description": "Request serializer for sending push notifications.",
|
|
177
|
-
"properties": {
|
|
178
|
-
"title": {
|
|
179
|
-
"type": "string",
|
|
180
|
-
"minLength": 1,
|
|
181
|
-
"description": "Notification title",
|
|
182
|
-
"maxLength": 255
|
|
183
|
-
},
|
|
184
|
-
"body": {
|
|
185
|
-
"type": "string",
|
|
186
|
-
"minLength": 1,
|
|
187
|
-
"description": "Notification body"
|
|
188
|
-
},
|
|
189
|
-
"icon": {
|
|
190
|
-
"type": "string",
|
|
191
|
-
"format": "uri",
|
|
192
|
-
"nullable": true,
|
|
193
|
-
"minLength": 1,
|
|
194
|
-
"description": "Notification icon URL"
|
|
195
|
-
},
|
|
196
|
-
"url": {
|
|
197
|
-
"type": "string",
|
|
198
|
-
"format": "uri",
|
|
199
|
-
"nullable": true,
|
|
200
|
-
"minLength": 1,
|
|
201
|
-
"description": "URL to open on click"
|
|
202
|
-
}
|
|
203
|
-
},
|
|
204
|
-
"required": [
|
|
205
|
-
"body",
|
|
206
|
-
"title"
|
|
207
|
-
]
|
|
208
|
-
},
|
|
209
|
-
"SendPushResponse": {
|
|
210
|
-
"type": "object",
|
|
211
|
-
"description": "Response serializer for send push endpoint.",
|
|
212
|
-
"properties": {
|
|
213
|
-
"success": {
|
|
214
|
-
"type": "boolean",
|
|
215
|
-
"description": "Whether send was successful"
|
|
216
|
-
},
|
|
217
|
-
"sent_to": {
|
|
218
|
-
"type": "integer",
|
|
219
|
-
"description": "Number of devices notification was sent to"
|
|
220
|
-
}
|
|
221
|
-
},
|
|
222
|
-
"required": [
|
|
223
|
-
"sent_to",
|
|
224
|
-
"success"
|
|
225
|
-
]
|
|
226
|
-
},
|
|
227
|
-
"SubscribeRequestRequest": {
|
|
228
|
-
"type": "object",
|
|
229
|
-
"description": "Request serializer for subscribing to push notifications.",
|
|
230
|
-
"properties": {
|
|
231
|
-
"endpoint": {
|
|
232
|
-
"type": "string",
|
|
233
|
-
"format": "uri",
|
|
234
|
-
"minLength": 1,
|
|
235
|
-
"description": "Push service endpoint URL from browser",
|
|
236
|
-
"maxLength": 500
|
|
237
|
-
},
|
|
238
|
-
"keys": {
|
|
239
|
-
"type": "object",
|
|
240
|
-
"additionalProperties": {
|
|
241
|
-
"type": "string",
|
|
242
|
-
"minLength": 1
|
|
243
|
-
},
|
|
244
|
-
"description": "Encryption keys (p256dh and auth)"
|
|
245
|
-
}
|
|
246
|
-
},
|
|
247
|
-
"required": [
|
|
248
|
-
"endpoint",
|
|
249
|
-
"keys"
|
|
250
|
-
]
|
|
251
|
-
},
|
|
252
|
-
"SubscribeResponse": {
|
|
253
|
-
"type": "object",
|
|
254
|
-
"description": "Response serializer for subscription endpoint.",
|
|
255
|
-
"properties": {
|
|
256
|
-
"success": {
|
|
257
|
-
"type": "boolean",
|
|
258
|
-
"description": "Whether subscription was successful"
|
|
259
|
-
},
|
|
260
|
-
"subscription_id": {
|
|
261
|
-
"type": "integer",
|
|
262
|
-
"description": "ID of the subscription"
|
|
263
|
-
},
|
|
264
|
-
"created": {
|
|
265
|
-
"type": "boolean",
|
|
266
|
-
"description": "Whether subscription was newly created"
|
|
267
|
-
}
|
|
268
|
-
},
|
|
269
|
-
"required": [
|
|
270
|
-
"created",
|
|
271
|
-
"subscription_id",
|
|
272
|
-
"success"
|
|
273
|
-
]
|
|
274
|
-
},
|
|
275
|
-
"VapidPublicKeyResponse": {
|
|
276
|
-
"type": "object",
|
|
277
|
-
"description": "Response serializer for VAPID public key endpoint.",
|
|
278
|
-
"properties": {
|
|
279
|
-
"publicKey": {
|
|
280
|
-
"type": "string",
|
|
281
|
-
"description": "VAPID public key for client subscription"
|
|
282
|
-
}
|
|
283
|
-
},
|
|
284
|
-
"required": [
|
|
285
|
-
"publicKey"
|
|
286
|
-
]
|
|
287
|
-
}
|
|
288
|
-
},
|
|
289
|
-
"securitySchemes": {
|
|
290
|
-
"jwtAuthWithLastLogin": {
|
|
291
|
-
"type": "http",
|
|
292
|
-
"scheme": "bearer",
|
|
293
|
-
"bearerFormat": "JWT"
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
},
|
|
297
|
-
"servers": [
|
|
298
|
-
{
|
|
299
|
-
"url": "http://localhost:8000"
|
|
300
|
-
}
|
|
301
|
-
]
|
|
302
|
-
}
|
|
@@ -1,162 +0,0 @@
|
|
|
1
|
-
// Auto-generated by DjangoCFG - see CLAUDE.md
|
|
2
|
-
/**
|
|
3
|
-
* Storage adapters for cross-platform token storage.
|
|
4
|
-
*
|
|
5
|
-
* Supports:
|
|
6
|
-
* - LocalStorage (browser)
|
|
7
|
-
* - Cookies (SSR/browser)
|
|
8
|
-
* - Memory (Node.js/Electron/testing)
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import type { APILogger } from './logger';
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Storage adapter interface for cross-platform token storage.
|
|
15
|
-
*/
|
|
16
|
-
export interface StorageAdapter {
|
|
17
|
-
getItem(key: string): string | null;
|
|
18
|
-
setItem(key: string, value: string): void;
|
|
19
|
-
removeItem(key: string): void;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* LocalStorage adapter with safe try-catch for browser environments.
|
|
24
|
-
* Works in modern browsers with localStorage support.
|
|
25
|
-
*
|
|
26
|
-
* Note: This adapter uses window.localStorage and should only be used in browser/client environments.
|
|
27
|
-
* For server-side usage, use MemoryStorageAdapter or CookieStorageAdapter instead.
|
|
28
|
-
*/
|
|
29
|
-
export class LocalStorageAdapter implements StorageAdapter {
|
|
30
|
-
private logger?: APILogger;
|
|
31
|
-
|
|
32
|
-
constructor(logger?: APILogger) {
|
|
33
|
-
this.logger = logger;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
getItem(key: string): string | null {
|
|
37
|
-
try {
|
|
38
|
-
if (typeof window !== 'undefined' && window.localStorage) {
|
|
39
|
-
const value = localStorage.getItem(key);
|
|
40
|
-
this.logger?.debug(`LocalStorage.getItem("${key}"): ${value ? 'found' : 'not found'}`);
|
|
41
|
-
return value;
|
|
42
|
-
}
|
|
43
|
-
this.logger?.warn('LocalStorage not available: window.localStorage is undefined');
|
|
44
|
-
} catch (error) {
|
|
45
|
-
this.logger?.error('LocalStorage.getItem failed:', error);
|
|
46
|
-
}
|
|
47
|
-
return null;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
setItem(key: string, value: string): void {
|
|
51
|
-
try {
|
|
52
|
-
if (typeof window !== 'undefined' && window.localStorage) {
|
|
53
|
-
localStorage.setItem(key, value);
|
|
54
|
-
this.logger?.debug(`LocalStorage.setItem("${key}"): success`);
|
|
55
|
-
} else {
|
|
56
|
-
this.logger?.warn('LocalStorage not available: window.localStorage is undefined');
|
|
57
|
-
}
|
|
58
|
-
} catch (error) {
|
|
59
|
-
this.logger?.error('LocalStorage.setItem failed:', error);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
removeItem(key: string): void {
|
|
64
|
-
try {
|
|
65
|
-
if (typeof window !== 'undefined' && window.localStorage) {
|
|
66
|
-
localStorage.removeItem(key);
|
|
67
|
-
this.logger?.debug(`LocalStorage.removeItem("${key}"): success`);
|
|
68
|
-
} else {
|
|
69
|
-
this.logger?.warn('LocalStorage not available: window.localStorage is undefined');
|
|
70
|
-
}
|
|
71
|
-
} catch (error) {
|
|
72
|
-
this.logger?.error('LocalStorage.removeItem failed:', error);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Cookie-based storage adapter for SSR and browser environments.
|
|
79
|
-
* Useful for Next.js, Nuxt.js, and other SSR frameworks.
|
|
80
|
-
*/
|
|
81
|
-
export class CookieStorageAdapter implements StorageAdapter {
|
|
82
|
-
private logger?: APILogger;
|
|
83
|
-
|
|
84
|
-
constructor(logger?: APILogger) {
|
|
85
|
-
this.logger = logger;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
getItem(key: string): string | null {
|
|
89
|
-
try {
|
|
90
|
-
if (typeof document === 'undefined') {
|
|
91
|
-
this.logger?.warn('Cookies not available: document is undefined (SSR context?)');
|
|
92
|
-
return null;
|
|
93
|
-
}
|
|
94
|
-
const value = `; ${document.cookie}`;
|
|
95
|
-
const parts = value.split(`; ${key}=`);
|
|
96
|
-
if (parts.length === 2) {
|
|
97
|
-
const result = parts.pop()?.split(';').shift() || null;
|
|
98
|
-
this.logger?.debug(`CookieStorage.getItem("${key}"): ${result ? 'found' : 'not found'}`);
|
|
99
|
-
return result;
|
|
100
|
-
}
|
|
101
|
-
this.logger?.debug(`CookieStorage.getItem("${key}"): not found`);
|
|
102
|
-
} catch (error) {
|
|
103
|
-
this.logger?.error('CookieStorage.getItem failed:', error);
|
|
104
|
-
}
|
|
105
|
-
return null;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
setItem(key: string, value: string): void {
|
|
109
|
-
try {
|
|
110
|
-
if (typeof document !== 'undefined') {
|
|
111
|
-
document.cookie = `${key}=${value}; path=/; max-age=31536000`;
|
|
112
|
-
this.logger?.debug(`CookieStorage.setItem("${key}"): success`);
|
|
113
|
-
} else {
|
|
114
|
-
this.logger?.warn('Cookies not available: document is undefined (SSR context?)');
|
|
115
|
-
}
|
|
116
|
-
} catch (error) {
|
|
117
|
-
this.logger?.error('CookieStorage.setItem failed:', error);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
removeItem(key: string): void {
|
|
122
|
-
try {
|
|
123
|
-
if (typeof document !== 'undefined') {
|
|
124
|
-
document.cookie = `${key}=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT`;
|
|
125
|
-
this.logger?.debug(`CookieStorage.removeItem("${key}"): success`);
|
|
126
|
-
} else {
|
|
127
|
-
this.logger?.warn('Cookies not available: document is undefined (SSR context?)');
|
|
128
|
-
}
|
|
129
|
-
} catch (error) {
|
|
130
|
-
this.logger?.error('CookieStorage.removeItem failed:', error);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* In-memory storage adapter for Node.js, Electron, and testing environments.
|
|
137
|
-
* Data is stored in RAM and cleared when process exits.
|
|
138
|
-
*/
|
|
139
|
-
export class MemoryStorageAdapter implements StorageAdapter {
|
|
140
|
-
private storage: Map<string, string> = new Map();
|
|
141
|
-
private logger?: APILogger;
|
|
142
|
-
|
|
143
|
-
constructor(logger?: APILogger) {
|
|
144
|
-
this.logger = logger;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
getItem(key: string): string | null {
|
|
148
|
-
const value = this.storage.get(key) || null;
|
|
149
|
-
this.logger?.debug(`MemoryStorage.getItem("${key}"): ${value ? 'found' : 'not found'}`);
|
|
150
|
-
return value;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
setItem(key: string, value: string): void {
|
|
154
|
-
this.storage.set(key, value);
|
|
155
|
-
this.logger?.debug(`MemoryStorage.setItem("${key}"): success`);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
removeItem(key: string): void {
|
|
159
|
-
this.storage.delete(key);
|
|
160
|
-
this.logger?.debug(`MemoryStorage.removeItem("${key}"): success`);
|
|
161
|
-
}
|
|
162
|
-
}
|