@plyaz/types 1.36.0 → 1.36.1
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/api/index.cjs +199 -1
- package/dist/api/index.cjs.map +1 -1
- package/dist/api/index.js +199 -1
- package/dist/api/index.js.map +1 -1
- package/dist/auth/index.cjs +1 -2
- package/dist/auth/index.cjs.map +1 -1
- package/dist/auth/index.js +1 -2
- package/dist/auth/index.js.map +1 -1
- package/dist/campaign/schemas.d.ts +1 -1
- package/dist/core/domain/files/enums.d.ts +36 -0
- package/dist/core/domain/files/index.d.ts +4 -1
- package/dist/core/domain/files/schemas.d.ts +14 -21
- package/dist/core/domain/files/streaming.d.ts +167 -0
- package/dist/core/events/index.d.ts +2 -0
- package/dist/core/events/streaming/index.d.ts +18 -0
- package/dist/core/events/streaming/responses.d.ts +164 -0
- package/dist/core/events/streaming/types.d.ts +408 -0
- package/dist/core/frontend/index.d.ts +1 -1
- package/dist/core/frontend/types.d.ts +179 -5
- package/dist/core/index.cjs +221 -0
- package/dist/core/index.cjs.map +1 -1
- package/dist/core/index.d.ts +1 -1
- package/dist/core/index.js +192 -1
- package/dist/core/index.js.map +1 -1
- package/dist/core/init/index.d.ts +1 -1
- package/dist/core/init/types.d.ts +51 -0
- package/dist/errors/codes.d.ts +47 -0
- package/dist/errors/enums.d.ts +1 -0
- package/dist/errors/index.cjs +231 -1
- package/dist/errors/index.cjs.map +1 -1
- package/dist/errors/index.js +231 -2
- package/dist/errors/index.js.map +1 -1
- package/dist/errors/types.d.ts +15 -0
- package/dist/index.cjs +450 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +420 -2
- package/dist/index.js.map +1 -1
- package/dist/store/files/index.d.ts +1 -1
- package/dist/store/files/types.d.ts +47 -1
- package/dist/store/index.d.ts +1 -0
- package/dist/store/stream/index.d.ts +9 -0
- package/dist/store/stream/types.d.ts +303 -0
- package/dist/store/types.d.ts +3 -0
- package/package.json +1 -1
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Streaming Types
|
|
3
|
+
*
|
|
4
|
+
* Global types for real-time event streaming via SSE/WebSocket.
|
|
5
|
+
*
|
|
6
|
+
* Architecture:
|
|
7
|
+
* - Domain services define their own streaming constants (e.g., FILES_STREAM_SUBTYPE)
|
|
8
|
+
* - Global constants here MERGE domain-specific constants
|
|
9
|
+
* - System constants (heartbeat, connection) are defined here as base
|
|
10
|
+
*
|
|
11
|
+
* To add a new domain:
|
|
12
|
+
* 1. Create streaming constants in the domain folder (e.g., /domain/payments/streaming.ts)
|
|
13
|
+
* 2. Import domain constants here
|
|
14
|
+
* 3. Merge into global constants (STREAM_MESSAGE_TYPE, STREAM_SUBTYPE, etc.)
|
|
15
|
+
*/
|
|
16
|
+
import type { FilesStreamMessageType, FilesStreamSubtype, FilesStreamProgressStatus, FilesUploadStreamProgress, FilesDownloadStreamProgress, FilesGenerateStreamProgress, FilesStreamProgressData, FilesStreamChannel } from '../../domain/files';
|
|
17
|
+
import type { CorePlyazStreamConfig } from '../../frontend';
|
|
18
|
+
import type { RootStoreHook } from '../../../store';
|
|
19
|
+
import type { PackageErrorLike } from '../../../errors';
|
|
20
|
+
/**
|
|
21
|
+
* Supported transport types for streaming connections
|
|
22
|
+
*/
|
|
23
|
+
export declare const STREAM_TRANSPORT: {
|
|
24
|
+
readonly SSE: "sse";
|
|
25
|
+
readonly WEBSOCKET: "websocket";
|
|
26
|
+
};
|
|
27
|
+
export type StreamTransportType = (typeof STREAM_TRANSPORT)[keyof typeof STREAM_TRANSPORT];
|
|
28
|
+
/**
|
|
29
|
+
* System-level message type categories.
|
|
30
|
+
* Used for connection lifecycle and system-wide events.
|
|
31
|
+
*/
|
|
32
|
+
export declare const SYSTEM_STREAM_MESSAGE_TYPE: {
|
|
33
|
+
readonly SYSTEM: "system";
|
|
34
|
+
readonly NOTIFICATION: "notification";
|
|
35
|
+
readonly DATA: "data";
|
|
36
|
+
};
|
|
37
|
+
export type SystemStreamMessageType = (typeof SYSTEM_STREAM_MESSAGE_TYPE)[keyof typeof SYSTEM_STREAM_MESSAGE_TYPE];
|
|
38
|
+
/**
|
|
39
|
+
* System-level message subtypes (not domain-specific).
|
|
40
|
+
* Used for connection lifecycle and system events.
|
|
41
|
+
*/
|
|
42
|
+
export declare const SYSTEM_STREAM_SUBTYPE: {
|
|
43
|
+
readonly HEARTBEAT: "heartbeat";
|
|
44
|
+
readonly CONNECTED: "connected";
|
|
45
|
+
readonly DISCONNECTED: "disconnected";
|
|
46
|
+
readonly RECONNECTING: "reconnecting";
|
|
47
|
+
};
|
|
48
|
+
export type SystemStreamSubtype = (typeof SYSTEM_STREAM_SUBTYPE)[keyof typeof SYSTEM_STREAM_SUBTYPE];
|
|
49
|
+
/**
|
|
50
|
+
* System channel for connection-level events.
|
|
51
|
+
*/
|
|
52
|
+
export declare const SYSTEM_STREAM_CHANNEL: {
|
|
53
|
+
readonly SYSTEM: "system";
|
|
54
|
+
};
|
|
55
|
+
export type SystemStreamChannel = (typeof SYSTEM_STREAM_CHANNEL)[keyof typeof SYSTEM_STREAM_CHANNEL];
|
|
56
|
+
/**
|
|
57
|
+
* All message type categories.
|
|
58
|
+
* Merged from system + all domain-specific types.
|
|
59
|
+
*
|
|
60
|
+
* To add a new domain's message types:
|
|
61
|
+
* 1. Define `{DOMAIN}_STREAM_MESSAGE_TYPE` in the domain's streaming module
|
|
62
|
+
* 2. Import and spread into this object
|
|
63
|
+
*/
|
|
64
|
+
export declare const STREAM_MESSAGE_TYPE: {
|
|
65
|
+
readonly PROGRESS: "progress";
|
|
66
|
+
readonly STATUS: "status";
|
|
67
|
+
readonly ERROR: "error";
|
|
68
|
+
readonly SYSTEM: "system";
|
|
69
|
+
readonly NOTIFICATION: "notification";
|
|
70
|
+
readonly DATA: "data";
|
|
71
|
+
};
|
|
72
|
+
export type StreamMessageType = SystemStreamMessageType | FilesStreamMessageType;
|
|
73
|
+
/**
|
|
74
|
+
* All message subtypes.
|
|
75
|
+
* Merged from system + all domain-specific subtypes.
|
|
76
|
+
*
|
|
77
|
+
* To add a new domain's subtypes:
|
|
78
|
+
* 1. Define `{DOMAIN}_STREAM_SUBTYPE` in the domain's streaming module
|
|
79
|
+
* 2. Import and spread into this object
|
|
80
|
+
*/
|
|
81
|
+
export declare const STREAM_SUBTYPE: {
|
|
82
|
+
readonly UPLOAD: "upload";
|
|
83
|
+
readonly DOWNLOAD: "download";
|
|
84
|
+
readonly GENERATE: "generate";
|
|
85
|
+
readonly HEARTBEAT: "heartbeat";
|
|
86
|
+
readonly CONNECTED: "connected";
|
|
87
|
+
readonly DISCONNECTED: "disconnected";
|
|
88
|
+
readonly RECONNECTING: "reconnecting";
|
|
89
|
+
};
|
|
90
|
+
export type StreamMessageSubtype = SystemStreamSubtype | FilesStreamSubtype;
|
|
91
|
+
/**
|
|
92
|
+
* Message scope identifies the domain that owns the message.
|
|
93
|
+
* Used for routing messages to the correct domain store.
|
|
94
|
+
*
|
|
95
|
+
* To add a new domain:
|
|
96
|
+
* 1. Add the domain name to this constant
|
|
97
|
+
* 2. Create domain streaming types in the domain's types folder
|
|
98
|
+
*/
|
|
99
|
+
export declare const STREAM_SCOPE: {
|
|
100
|
+
readonly FILES: "files";
|
|
101
|
+
readonly SYSTEM: "system";
|
|
102
|
+
};
|
|
103
|
+
export type StreamMessageScope = (typeof STREAM_SCOPE)[keyof typeof STREAM_SCOPE];
|
|
104
|
+
/**
|
|
105
|
+
* System stream events.
|
|
106
|
+
*/
|
|
107
|
+
export declare const SYSTEM_STREAM_EVENT: {
|
|
108
|
+
readonly CONNECTED: "connected";
|
|
109
|
+
readonly DISCONNECTED: "disconnected";
|
|
110
|
+
readonly HEARTBEAT: "heartbeat";
|
|
111
|
+
readonly ERROR: "error";
|
|
112
|
+
};
|
|
113
|
+
/**
|
|
114
|
+
* All stream events.
|
|
115
|
+
* Merged from system + all domain-specific events.
|
|
116
|
+
*/
|
|
117
|
+
export declare const STREAM_EVENT: {
|
|
118
|
+
readonly UPLOAD_PROGRESS: "upload:progress";
|
|
119
|
+
readonly UPLOAD_COMPLETED: "upload:completed";
|
|
120
|
+
readonly UPLOAD_FAILED: "upload:failed";
|
|
121
|
+
readonly UPLOAD_CANCELLED: "upload:cancelled";
|
|
122
|
+
readonly DOWNLOAD_PROGRESS: "download:progress";
|
|
123
|
+
readonly DOWNLOAD_COMPLETED: "download:completed";
|
|
124
|
+
readonly DOWNLOAD_FAILED: "download:failed";
|
|
125
|
+
readonly DOWNLOAD_CANCELLED: "download:cancelled";
|
|
126
|
+
readonly GENERATE_PROGRESS: "generate:progress";
|
|
127
|
+
readonly GENERATE_COMPLETED: "generate:completed";
|
|
128
|
+
readonly GENERATE_FAILED: "generate:failed";
|
|
129
|
+
readonly CONNECTED: "connected";
|
|
130
|
+
readonly DISCONNECTED: "disconnected";
|
|
131
|
+
readonly HEARTBEAT: "heartbeat";
|
|
132
|
+
readonly ERROR: "error";
|
|
133
|
+
};
|
|
134
|
+
/**
|
|
135
|
+
* All channel prefixes (for subscription matching).
|
|
136
|
+
*/
|
|
137
|
+
export declare const STREAM_CHANNEL_PREFIX: {
|
|
138
|
+
readonly UPLOAD: "upload:";
|
|
139
|
+
readonly DOWNLOAD: "download:";
|
|
140
|
+
readonly GENERATE: "generate:";
|
|
141
|
+
};
|
|
142
|
+
/**
|
|
143
|
+
* All broadcast channels (user-scoped).
|
|
144
|
+
*/
|
|
145
|
+
export declare const STREAM_BROADCAST_CHANNEL: {
|
|
146
|
+
readonly UPLOADS: "uploads";
|
|
147
|
+
readonly DOWNLOADS: "downloads";
|
|
148
|
+
readonly GENERATIONS: "generations";
|
|
149
|
+
readonly SYSTEM: "system";
|
|
150
|
+
};
|
|
151
|
+
/**
|
|
152
|
+
* All possible channel patterns.
|
|
153
|
+
* Union of system channel + all domain-specific channels.
|
|
154
|
+
*/
|
|
155
|
+
export type StreamChannel = SystemStreamChannel | FilesStreamChannel;
|
|
156
|
+
/**
|
|
157
|
+
* Represents an active streaming connection
|
|
158
|
+
*/
|
|
159
|
+
export interface StreamConnection {
|
|
160
|
+
/** Unique connection identifier */
|
|
161
|
+
id: string;
|
|
162
|
+
/** Authenticated user ID (undefined for anonymous) */
|
|
163
|
+
userId?: string;
|
|
164
|
+
/** Transport type (SSE or WebSocket) */
|
|
165
|
+
transport: StreamTransportType;
|
|
166
|
+
/** Subscribed channels */
|
|
167
|
+
channels: Set<string>;
|
|
168
|
+
/** Connection creation timestamp */
|
|
169
|
+
createdAt: Date;
|
|
170
|
+
/** Last activity timestamp (for cleanup) */
|
|
171
|
+
lastActivity: Date;
|
|
172
|
+
/** Additional connection metadata */
|
|
173
|
+
metadata?: Record<string, unknown>;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Serializable version of StreamConnection for storage/transmission
|
|
177
|
+
*/
|
|
178
|
+
export interface StreamConnectionInfo {
|
|
179
|
+
id: string;
|
|
180
|
+
userId?: string;
|
|
181
|
+
transport: StreamTransportType;
|
|
182
|
+
channels: string[];
|
|
183
|
+
createdAt: string;
|
|
184
|
+
lastActivity: string;
|
|
185
|
+
metadata?: Record<string, unknown>;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Message sent to streaming clients.
|
|
189
|
+
*
|
|
190
|
+
* @typeParam T - Payload data type (defaults to unknown)
|
|
191
|
+
*/
|
|
192
|
+
export interface StreamMessage<T = unknown> {
|
|
193
|
+
/** Event name (e.g., 'upload:progress', 'connected', 'heartbeat') */
|
|
194
|
+
event: string;
|
|
195
|
+
/** Event payload data */
|
|
196
|
+
data: T;
|
|
197
|
+
/** Optional message ID (for SSE reconnection) */
|
|
198
|
+
id?: string;
|
|
199
|
+
/** Optional retry interval in ms (for SSE) */
|
|
200
|
+
retry?: number;
|
|
201
|
+
/** Channel this message was sent to */
|
|
202
|
+
channel?: string;
|
|
203
|
+
/** Message type category */
|
|
204
|
+
type?: StreamMessageType;
|
|
205
|
+
/** Message subtype for granular filtering */
|
|
206
|
+
subtype?: StreamMessageSubtype;
|
|
207
|
+
/** Domain scope for routing to domain stores */
|
|
208
|
+
scope?: StreamMessageScope;
|
|
209
|
+
/** Transport used (sse or websocket) */
|
|
210
|
+
transport?: StreamTransportType;
|
|
211
|
+
/** Timestamp when message was created */
|
|
212
|
+
timestamp?: number;
|
|
213
|
+
/** Correlation ID for request tracing */
|
|
214
|
+
correlationId?: string;
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Progress data union type.
|
|
218
|
+
* Currently files-only, will expand as domains add streaming support.
|
|
219
|
+
*/
|
|
220
|
+
export type StreamProgressData = FilesStreamProgressData;
|
|
221
|
+
/**
|
|
222
|
+
* Progress status union type.
|
|
223
|
+
* Currently files-only, will expand as domains add streaming support.
|
|
224
|
+
*/
|
|
225
|
+
export type StreamProgressStatus = FilesStreamProgressStatus;
|
|
226
|
+
/**
|
|
227
|
+
* All progress status constants.
|
|
228
|
+
*/
|
|
229
|
+
export declare const STREAM_PROGRESS_STATUS: {
|
|
230
|
+
readonly PENDING: "pending";
|
|
231
|
+
readonly UPLOADING: "uploading";
|
|
232
|
+
readonly DOWNLOADING: "downloading";
|
|
233
|
+
readonly PROCESSING: "processing";
|
|
234
|
+
readonly GENERATING: "generating";
|
|
235
|
+
readonly COMPLETED: "completed";
|
|
236
|
+
readonly FAILED: "failed";
|
|
237
|
+
readonly CANCELLED: "cancelled";
|
|
238
|
+
};
|
|
239
|
+
export type { FilesUploadStreamProgress, FilesDownloadStreamProgress, FilesGenerateStreamProgress };
|
|
240
|
+
/**
|
|
241
|
+
* Channel subscription record
|
|
242
|
+
*/
|
|
243
|
+
export interface ChannelSubscription {
|
|
244
|
+
/** Channel name */
|
|
245
|
+
channel: string;
|
|
246
|
+
/** Connection ID */
|
|
247
|
+
connectionId: string;
|
|
248
|
+
/** Subscription timestamp */
|
|
249
|
+
subscribedAt: Date;
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Result of authentication attempt
|
|
253
|
+
*/
|
|
254
|
+
export interface StreamAuthResult {
|
|
255
|
+
/** Whether authentication succeeded */
|
|
256
|
+
authenticated: boolean;
|
|
257
|
+
/** User ID (if authenticated) */
|
|
258
|
+
userId?: string;
|
|
259
|
+
/** Allowed channel patterns (e.g., ['upload:', 'generate:']) */
|
|
260
|
+
scopes?: string[];
|
|
261
|
+
/** Additional auth metadata */
|
|
262
|
+
metadata?: Record<string, unknown>;
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Base configuration for auth adapters
|
|
266
|
+
*/
|
|
267
|
+
export interface StreamAuthAdapterConfig {
|
|
268
|
+
/** Adapter name for logging */
|
|
269
|
+
name: string;
|
|
270
|
+
/** Whether adapter is enabled */
|
|
271
|
+
enabled?: boolean;
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Configuration for token-based auth adapter
|
|
275
|
+
*/
|
|
276
|
+
export interface TokenAuthAdapterConfig extends StreamAuthAdapterConfig {
|
|
277
|
+
/** Token verification function */
|
|
278
|
+
verifyToken: (token: string) => Promise<{
|
|
279
|
+
valid: boolean;
|
|
280
|
+
userId?: string;
|
|
281
|
+
scopes?: string[];
|
|
282
|
+
}>;
|
|
283
|
+
/** Header name for token (default: 'Authorization') */
|
|
284
|
+
headerName?: string;
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Base configuration for transport adapters
|
|
288
|
+
*/
|
|
289
|
+
export interface StreamTransportAdapterConfig {
|
|
290
|
+
/** Adapter name for logging */
|
|
291
|
+
name: string;
|
|
292
|
+
/** Heartbeat interval in ms (default: 30000) */
|
|
293
|
+
heartbeatInterval?: number;
|
|
294
|
+
/** Maximum concurrent connections (default: 1000) */
|
|
295
|
+
maxConnections?: number;
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Configuration for StreamBroadcaster
|
|
299
|
+
*/
|
|
300
|
+
export interface StreamBroadcasterConfig {
|
|
301
|
+
/** Heartbeat interval in ms */
|
|
302
|
+
heartbeatInterval?: number;
|
|
303
|
+
/** Connection timeout in ms (for cleanup) */
|
|
304
|
+
connectionTimeout?: number;
|
|
305
|
+
/** Maximum stale connection age in ms before cleanup */
|
|
306
|
+
maxStaleAge?: number;
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Statistics for stream manager
|
|
310
|
+
*/
|
|
311
|
+
export interface StreamManagerStats {
|
|
312
|
+
/** Total active connections */
|
|
313
|
+
totalConnections: number;
|
|
314
|
+
/** Connections by transport type */
|
|
315
|
+
connectionsByTransport: Record<StreamTransportType, number>;
|
|
316
|
+
/** Total channel subscriptions */
|
|
317
|
+
totalSubscriptions: number;
|
|
318
|
+
/** Unique channels with subscribers */
|
|
319
|
+
activeChannels: number;
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Return type for useStreamConnection hook
|
|
323
|
+
*
|
|
324
|
+
* Note: Errors are added to the global error store automatically.
|
|
325
|
+
* Use useErrorStore or error hooks to access connection errors.
|
|
326
|
+
*/
|
|
327
|
+
export interface UseStreamConnectionResult {
|
|
328
|
+
/** Whether connection is established */
|
|
329
|
+
connected: boolean;
|
|
330
|
+
/** Reconnect function */
|
|
331
|
+
reconnect: () => void;
|
|
332
|
+
/** Disconnect function */
|
|
333
|
+
disconnect: () => void;
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Options for useStreamConnection hook
|
|
337
|
+
*
|
|
338
|
+
* @template T - The expected message data type (defaults to unknown)
|
|
339
|
+
*
|
|
340
|
+
* @example
|
|
341
|
+
* ```typescript
|
|
342
|
+
* // Without generic (message.data is unknown)
|
|
343
|
+
* useStreamConnection({
|
|
344
|
+
* channels: ['uploads'],
|
|
345
|
+
* onMessage: (message) => { ... }
|
|
346
|
+
* });
|
|
347
|
+
*
|
|
348
|
+
* // With generic (message.data is properly typed)
|
|
349
|
+
* useStreamConnection<FilesUploadStreamProgress>({
|
|
350
|
+
* channels: [`upload:${fileId}`],
|
|
351
|
+
* onMessage: (message) => {
|
|
352
|
+
* // message.data is FilesUploadStreamProgress
|
|
353
|
+
* console.log(message.data.percentage);
|
|
354
|
+
* }
|
|
355
|
+
* });
|
|
356
|
+
* ```
|
|
357
|
+
*/
|
|
358
|
+
export interface UseStreamConnectionOptions<T = unknown> {
|
|
359
|
+
/** Channels to subscribe to */
|
|
360
|
+
channels: StreamChannel[];
|
|
361
|
+
/** API endpoint URL (default: '/api/events/stream') */
|
|
362
|
+
endpoint?: string;
|
|
363
|
+
/** Whether to auto-connect on mount (default: true) */
|
|
364
|
+
autoConnect?: boolean;
|
|
365
|
+
/** Reconnect on error (default: true) */
|
|
366
|
+
autoReconnect?: boolean;
|
|
367
|
+
/** Max reconnect attempts (default: 5) */
|
|
368
|
+
maxReconnectAttempts?: number;
|
|
369
|
+
/** Reconnect delay in ms (default: 1000) */
|
|
370
|
+
reconnectDelay?: number;
|
|
371
|
+
/** Callback when connected */
|
|
372
|
+
onConnect?: () => void;
|
|
373
|
+
/** Callback when disconnected */
|
|
374
|
+
onDisconnect?: () => void;
|
|
375
|
+
/** Callback on error */
|
|
376
|
+
onError?: (error: PackageErrorLike) => void;
|
|
377
|
+
/** Callback on message received (message.data is typed as T) */
|
|
378
|
+
onMessage?: (message: StreamMessage<T>) => void;
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* Options for useStreamManager hook
|
|
382
|
+
*
|
|
383
|
+
* Used by PlyazProvider to manage SSE connection lifecycle.
|
|
384
|
+
*/
|
|
385
|
+
export interface UseStreamManagerOptions {
|
|
386
|
+
/** Streaming configuration (required when enabled) */
|
|
387
|
+
config?: CorePlyazStreamConfig;
|
|
388
|
+
/** Root store hook for accessing stream store */
|
|
389
|
+
store: RootStoreHook;
|
|
390
|
+
/** Enable verbose logging */
|
|
391
|
+
verbose?: boolean;
|
|
392
|
+
/** Whether streaming is enabled (default: false) */
|
|
393
|
+
enabled?: boolean;
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* Result type for useStreamManager hook
|
|
397
|
+
*
|
|
398
|
+
* Note: Errors are added to the global error store automatically.
|
|
399
|
+
* Use useErrorStore or error hooks to access connection errors.
|
|
400
|
+
*/
|
|
401
|
+
export interface UseStreamManagerResult {
|
|
402
|
+
/** Whether connected to SSE */
|
|
403
|
+
connected: boolean;
|
|
404
|
+
/** Subscribed channels */
|
|
405
|
+
channels: StreamChannel[];
|
|
406
|
+
/** Whether streaming is enabled */
|
|
407
|
+
enabled: boolean;
|
|
408
|
+
}
|
|
@@ -2,5 +2,5 @@
|
|
|
2
2
|
* Core Frontend Types
|
|
3
3
|
* Type definitions for @plyaz/core frontend services
|
|
4
4
|
*/
|
|
5
|
-
export type { CoreBaseFrontendStore, CoreBaseFrontendServiceConfig, CoreBaseFrontendServiceInterface, CoreStoreHandlers, CoreOptimisticUpdateConfig, OptimisticConflictResolution, CoreFetcherFunction, CoreServiceFetchers, CorePlyazApiConfig, FrontendFeatureFlagProvider, CorePlyazFeatureFlagConfig, CorePlyazStoreConfig, CorePlyazConfig, CoreFeatureFlagServiceLike, CoreFeatureFlagFetcherOptions, CoreFeatureFlagStoreInitConfig, CoreBaseFrontendServiceConstructorConfig, CoreInitializationErrorProps, CoreInitializationLoadingProps, CorePlyazServices, CorePlyazContextValue, CorePlyazProviderProps, } from './types';
|
|
5
|
+
export type { CoreBaseFrontendStore, CoreBaseFrontendServiceConfig, CoreBaseFrontendServiceInterface, CoreStoreHandlers, CoreOptimisticUpdateConfig, OptimisticConflictResolution, CoreFetcherFunction, CoreServiceFetchers, CorePlyazApiConfig, FrontendFeatureFlagProvider, CorePlyazFeatureFlagConfig, CorePlyazStoreConfig, CorePlyazStreamConfig, CorePlyazConfig, CoreFeatureFlagServiceLike, CoreFeatureFlagFetcherOptions, CoreFeatureFlagStoreInitConfig, CoreBaseFrontendServiceConstructorConfig, CoreInitializationErrorProps, CoreInitializationLoadingProps, CorePlyazServices, CorePlyazContextValue, CorePlyazProviderProps, CoreStreamStoreGetter, CoreStreamHandlerIds, CoreStreamHandlerDeclaration, } from './types';
|
|
6
6
|
export type { CoreFrontendFeatureFlagEventType, CoreFeatureFlagStore, CoreFeatureFlagServiceConfig, CoreFeatureFlagServiceInitConfig, CoreFeatureFlagServiceInterface, } from './featureFlags';
|
|
@@ -11,6 +11,7 @@ import type { RootStoreSlice, RootStoreHook } from '../../store';
|
|
|
11
11
|
import type { CoreBaseDomainServiceConfig } from '../domain';
|
|
12
12
|
import type { CoreBaseServiceConfig, CoreBaseMapperInstance, CoreBaseValidatorInstance } from '../domain';
|
|
13
13
|
import type { CoreDomainServiceInstance, CoreObservabilityConfig, CoreServiceEntry } from '../init';
|
|
14
|
+
import type { PackageErrorLike } from '../../errors';
|
|
14
15
|
/**
|
|
15
16
|
* Base store interface for frontend services.
|
|
16
17
|
*
|
|
@@ -971,6 +972,94 @@ export interface CorePlyazStoreConfig {
|
|
|
971
972
|
/** Callback when a flag value changes */
|
|
972
973
|
onFlagChange?: (key: string, prevValue: FeatureFlagValue | undefined, newValue: FeatureFlagValue) => void;
|
|
973
974
|
}
|
|
975
|
+
/**
|
|
976
|
+
* Streaming configuration for PlyazProvider
|
|
977
|
+
*
|
|
978
|
+
* When enabled, automatically establishes SSE connection and routes
|
|
979
|
+
* messages to the stream store. Domain services register handlers
|
|
980
|
+
* to process events and update their stores.
|
|
981
|
+
*
|
|
982
|
+
* @example
|
|
983
|
+
* ```typescript
|
|
984
|
+
* // Enable streaming with default settings
|
|
985
|
+
* streaming: {
|
|
986
|
+
* enabled: true,
|
|
987
|
+
* }
|
|
988
|
+
*
|
|
989
|
+
* // Custom configuration
|
|
990
|
+
* streaming: {
|
|
991
|
+
* enabled: true,
|
|
992
|
+
* endpoint: '/api/events/stream',
|
|
993
|
+
* channels: ['uploads', 'system'],
|
|
994
|
+
* autoConnect: true,
|
|
995
|
+
* reconnect: {
|
|
996
|
+
* enabled: true,
|
|
997
|
+
* maxAttempts: 10,
|
|
998
|
+
* delay: 1000,
|
|
999
|
+
* },
|
|
1000
|
+
* }
|
|
1001
|
+
* ```
|
|
1002
|
+
*/
|
|
1003
|
+
export interface CorePlyazStreamConfig {
|
|
1004
|
+
/**
|
|
1005
|
+
* Enable streaming (SSE connection).
|
|
1006
|
+
* When true, PlyazProvider establishes SSE connection on mount.
|
|
1007
|
+
* @default false
|
|
1008
|
+
*/
|
|
1009
|
+
enabled?: boolean;
|
|
1010
|
+
/**
|
|
1011
|
+
* SSE endpoint URL.
|
|
1012
|
+
* @default '/api/events/stream'
|
|
1013
|
+
*/
|
|
1014
|
+
endpoint?: string;
|
|
1015
|
+
/**
|
|
1016
|
+
* Channels to subscribe to.
|
|
1017
|
+
* @default ['uploads', 'downloads', 'generations', 'system']
|
|
1018
|
+
*/
|
|
1019
|
+
channels?: string[];
|
|
1020
|
+
/**
|
|
1021
|
+
* Auto-connect on mount.
|
|
1022
|
+
* @default true
|
|
1023
|
+
*/
|
|
1024
|
+
autoConnect?: boolean;
|
|
1025
|
+
/**
|
|
1026
|
+
* Reconnection configuration.
|
|
1027
|
+
*/
|
|
1028
|
+
reconnect?: {
|
|
1029
|
+
/**
|
|
1030
|
+
* Enable auto-reconnection on disconnect.
|
|
1031
|
+
* @default true
|
|
1032
|
+
*/
|
|
1033
|
+
enabled?: boolean;
|
|
1034
|
+
/**
|
|
1035
|
+
* Max reconnection attempts.
|
|
1036
|
+
* @default 10
|
|
1037
|
+
*/
|
|
1038
|
+
maxAttempts?: number;
|
|
1039
|
+
/**
|
|
1040
|
+
* Base delay between attempts (ms). Uses exponential backoff.
|
|
1041
|
+
* @default 1000
|
|
1042
|
+
*/
|
|
1043
|
+
delay?: number;
|
|
1044
|
+
};
|
|
1045
|
+
/**
|
|
1046
|
+
* Enable debug logging for streaming.
|
|
1047
|
+
* @default false (uses verbose from config)
|
|
1048
|
+
*/
|
|
1049
|
+
debug?: boolean;
|
|
1050
|
+
/**
|
|
1051
|
+
* Callback when connected to stream.
|
|
1052
|
+
*/
|
|
1053
|
+
onConnect?: () => void;
|
|
1054
|
+
/**
|
|
1055
|
+
* Callback when disconnected from stream.
|
|
1056
|
+
*/
|
|
1057
|
+
onDisconnect?: () => void;
|
|
1058
|
+
/**
|
|
1059
|
+
* Callback on connection error.
|
|
1060
|
+
*/
|
|
1061
|
+
onError?: (error: PackageErrorLike) => void;
|
|
1062
|
+
}
|
|
974
1063
|
/**
|
|
975
1064
|
* Full provider configuration
|
|
976
1065
|
*/
|
|
@@ -987,6 +1076,22 @@ export interface CorePlyazConfig {
|
|
|
987
1076
|
* Store integration configuration.
|
|
988
1077
|
*/
|
|
989
1078
|
store?: CorePlyazStoreConfig;
|
|
1079
|
+
/**
|
|
1080
|
+
* Streaming configuration (SSE real-time events).
|
|
1081
|
+
*
|
|
1082
|
+
* When enabled, PlyazProvider automatically establishes SSE connection
|
|
1083
|
+
* and routes messages to the stream store. Domain services can register
|
|
1084
|
+
* handlers to process events and update their stores.
|
|
1085
|
+
*
|
|
1086
|
+
* @example
|
|
1087
|
+
* ```typescript
|
|
1088
|
+
* streaming: {
|
|
1089
|
+
* enabled: true,
|
|
1090
|
+
* channels: ['uploads', 'system'],
|
|
1091
|
+
* }
|
|
1092
|
+
* ```
|
|
1093
|
+
*/
|
|
1094
|
+
streaming?: CorePlyazStreamConfig;
|
|
990
1095
|
/** App context (webapp, backoffice, mobile) */
|
|
991
1096
|
appContext?: CoreAppContext;
|
|
992
1097
|
/** Environment (production, staging, development, test) */
|
|
@@ -1046,9 +1151,9 @@ export interface CoreBaseFrontendServiceConstructorConfig<TConfig extends CoreBa
|
|
|
1046
1151
|
*/
|
|
1047
1152
|
export interface CoreInitializationErrorProps {
|
|
1048
1153
|
/** The error that occurred */
|
|
1049
|
-
error:
|
|
1154
|
+
error: PackageErrorLike;
|
|
1050
1155
|
/** Custom error component override */
|
|
1051
|
-
errorComponent?: (error:
|
|
1156
|
+
errorComponent?: (error: PackageErrorLike) => ReactNode;
|
|
1052
1157
|
}
|
|
1053
1158
|
/**
|
|
1054
1159
|
* Props for initialization loading component
|
|
@@ -1138,7 +1243,7 @@ export interface CorePlyazContextValue extends CorePlyazServices {
|
|
|
1138
1243
|
/** Whether services are initialized and ready */
|
|
1139
1244
|
isReady: boolean;
|
|
1140
1245
|
/** Initialization error if any */
|
|
1141
|
-
error:
|
|
1246
|
+
error: PackageErrorLike | null;
|
|
1142
1247
|
/** Re-initialize services */
|
|
1143
1248
|
reinitialize: () => Promise<void>;
|
|
1144
1249
|
}
|
|
@@ -1167,11 +1272,80 @@ export interface CorePlyazProviderProps {
|
|
|
1167
1272
|
/** Loading component to show while initializing */
|
|
1168
1273
|
loading?: ReactNode;
|
|
1169
1274
|
/** Error component to show on initialization failure */
|
|
1170
|
-
error?: (error:
|
|
1275
|
+
error?: (error: PackageErrorLike) => ReactNode;
|
|
1171
1276
|
/** Callback when services are ready */
|
|
1172
1277
|
onReady?: (services: CorePlyazServices) => void;
|
|
1173
1278
|
/** Callback on initialization error */
|
|
1174
|
-
onError?: (error:
|
|
1279
|
+
onError?: (error: PackageErrorLike) => void;
|
|
1175
1280
|
/** Skip showing loading state (render children immediately) */
|
|
1176
1281
|
skipLoadingState?: boolean;
|
|
1177
1282
|
}
|
|
1283
|
+
/**
|
|
1284
|
+
* Store getter function for declarative stream handlers.
|
|
1285
|
+
* Returns the root store state for accessing domain stores.
|
|
1286
|
+
*/
|
|
1287
|
+
export type CoreStreamStoreGetter = () => RootStoreSlice | undefined;
|
|
1288
|
+
/**
|
|
1289
|
+
* Stream handler IDs type.
|
|
1290
|
+
* Use this to define strongly-typed handler ID objects.
|
|
1291
|
+
*
|
|
1292
|
+
* @typeParam TKeys - Union type of handler key names
|
|
1293
|
+
*
|
|
1294
|
+
* @example
|
|
1295
|
+
* ```typescript
|
|
1296
|
+
* // Define handler IDs with type safety
|
|
1297
|
+
* protected static readonly STREAM_HANDLER_IDS: CoreStreamHandlerIds<
|
|
1298
|
+
* 'UPLOAD_PROGRESS' | 'DOWNLOAD_PROGRESS' | 'GENERATE_PROGRESS'
|
|
1299
|
+
* > = {
|
|
1300
|
+
* UPLOAD_PROGRESS: 'files:upload-progress',
|
|
1301
|
+
* DOWNLOAD_PROGRESS: 'files:download-progress',
|
|
1302
|
+
* GENERATE_PROGRESS: 'files:generate-progress',
|
|
1303
|
+
* } as const;
|
|
1304
|
+
* ```
|
|
1305
|
+
*/
|
|
1306
|
+
export type CoreStreamHandlerIds<TKeys extends string> = Readonly<Record<TKeys, string>>;
|
|
1307
|
+
/**
|
|
1308
|
+
* Declarative stream handler configuration.
|
|
1309
|
+
*
|
|
1310
|
+
* Allows defining stream handlers as configuration objects
|
|
1311
|
+
* instead of manual registration code.
|
|
1312
|
+
*
|
|
1313
|
+
* @typeParam TData - Type of message.data payload (default: unknown)
|
|
1314
|
+
*
|
|
1315
|
+
* @example
|
|
1316
|
+
* ```typescript
|
|
1317
|
+
* const handler: CoreStreamHandlerDeclaration<FilesUploadStreamProgress> = {
|
|
1318
|
+
* id: 'files:upload-progress',
|
|
1319
|
+
* channels: ['uploads', 'upload:'],
|
|
1320
|
+
* events: ['progress', 'upload:progress'],
|
|
1321
|
+
* priority: 100,
|
|
1322
|
+
* handler: (message, getStore) => {
|
|
1323
|
+
* // message.data is typed as FilesUploadStreamProgress
|
|
1324
|
+
* const filesStore = getStore()?.files;
|
|
1325
|
+
* filesStore?.setUploadProgress(message.data.fileId, { ... });
|
|
1326
|
+
* },
|
|
1327
|
+
* };
|
|
1328
|
+
* ```
|
|
1329
|
+
*/
|
|
1330
|
+
export interface CoreStreamHandlerDeclaration<TData = unknown> {
|
|
1331
|
+
/** Unique handler ID (use STREAM_HANDLER_IDS values) */
|
|
1332
|
+
id: string;
|
|
1333
|
+
/** Channels to listen to (supports prefix matching with trailing colon) */
|
|
1334
|
+
channels: string[];
|
|
1335
|
+
/** Event types to listen to */
|
|
1336
|
+
events: string[];
|
|
1337
|
+
/** Handler priority (higher = called first). Default: 100 */
|
|
1338
|
+
priority?: number;
|
|
1339
|
+
/**
|
|
1340
|
+
* Handler function.
|
|
1341
|
+
* Receives the typed message and a store getter function.
|
|
1342
|
+
*
|
|
1343
|
+
* @param message - Stream message with typed data
|
|
1344
|
+
* @param getStore - Function to get root store state
|
|
1345
|
+
*/
|
|
1346
|
+
handler: (message: {
|
|
1347
|
+
event: string;
|
|
1348
|
+
data: TData;
|
|
1349
|
+
timestamp?: number;
|
|
1350
|
+
}, getStore: CoreStreamStoreGetter) => void;
|
|
1351
|
+
}
|