@sygnl/talon 1.0.2 → 1.0.4

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
@@ -1,242 +1,122 @@
1
- /**
2
- * Message sent TO the edge
3
- */
4
- interface OutboundMessage {
5
- /** Message type/event name */
6
- type: string;
7
- /** Message payload */
8
- data: Record<string, unknown>;
9
- /** Optional message ID for tracking */
10
- id?: string;
11
- /** Timestamp */
12
- timestamp?: number;
13
- }
14
- /**
15
- * Message received FROM the edge
16
- */
17
- interface InboundMessage {
18
- /** Message type/event name */
19
- type: string;
20
- /** Message payload */
21
- data: Record<string, unknown>;
22
- /** Optional message ID */
23
- id?: string;
24
- /** Timestamp */
25
- timestamp?: number;
26
- }
27
- /**
28
- * Transport connection state
29
- */
30
- type TransportState = 'disconnected' | 'connecting' | 'connected' | 'reconnecting' | 'error';
31
- /**
32
- * Transport interface - all transports must implement this
33
- */
34
- interface TalonTransport {
35
- /**
36
- * Current connection state
37
- */
38
- readonly state: TransportState;
39
- /**
40
- * Connect to the transport
41
- */
42
- connect(): Promise<void>;
43
- /**
44
- * Disconnect from the transport
45
- */
46
- disconnect(): void;
47
- /**
48
- * Send a message to the edge
49
- * @param message - Message to send
50
- */
51
- send(message: OutboundMessage): Promise<void>;
52
- /**
53
- * Subscribe to messages from the edge
54
- * @param handler - Function to call when message received
55
- * @returns Unsubscribe function
56
- */
57
- onMessage(handler: (message: InboundMessage) => void): () => void;
58
- /**
59
- * Subscribe to connection state changes
60
- * @param handler - Function to call when state changes
61
- * @returns Unsubscribe function
62
- */
63
- onStateChange(handler: (state: TransportState) => void): () => void;
64
- /**
65
- * Subscribe to errors
66
- * @param handler - Function to call when error occurs
67
- * @returns Unsubscribe function
68
- */
69
- onError(handler: (error: Error) => void): () => void;
70
- }
1
+ import { T as TalonClientOptions, E as EventHandler, a as TransportState } from './types-DsDnVD2W.cjs';
2
+ export { I as InboundMessage, O as OutboundMessage, b as TalonTransport } from './types-DsDnVD2W.cjs';
71
3
 
72
4
  /**
73
- * WebSocket transport options
74
- */
75
- interface WebSocketTransportOptions {
76
- /** WebSocket URL */
77
- url: string;
78
- /** Auto-reconnect on disconnect */
79
- autoReconnect?: boolean;
80
- /** Reconnect delay in ms */
81
- reconnectDelay?: number;
82
- /** Max reconnect attempts (0 = infinite) */
83
- maxReconnectAttempts?: number;
84
- /** Connection timeout in ms */
85
- connectionTimeout?: number;
86
- /** Debug mode */
87
- debug?: boolean;
88
- }
89
- /**
90
- * WebSocket transport for bidirectional communication
5
+ * TalonClient - Bidirectional event delivery client
91
6
  *
92
7
  * Features:
93
- * - Real-time bidirectional communication
94
- * - Auto-reconnect with exponential backoff
95
- * - Connection state management
96
- * - Message queuing during disconnection
8
+ * - Send events TO the edge
9
+ * - Receive updates FROM the edge
10
+ * - Transport-agnostic (WebSocket, Webhook, etc.)
11
+ * - Event emitter pattern for edge responses
97
12
  *
98
13
  * @example
99
14
  * ```typescript
100
- * const transport = new WebSocketTransport({
101
- * url: 'wss://edge.sygnl.io',
102
- * autoReconnect: true
15
+ * import { TalonClient } from '@sygnl/talon';
16
+ * import { WebSocketTransport } from '@sygnl/talon/transports';
17
+ *
18
+ * const talon = new TalonClient({
19
+ * transport: new WebSocketTransport('wss://edge.sygnl.io')
20
+ * });
21
+ *
22
+ * // Send event TO edge
23
+ * await talon.send({ event: 'add_to_cart', product_id: 'abc' });
24
+ *
25
+ * // Receive updates FROM edge
26
+ * talon.on('attribution_updated', (data) => {
27
+ * console.log('Attribution:', data);
103
28
  * });
104
29
  * ```
105
30
  */
106
- declare class WebSocketTransport implements TalonTransport {
107
- private url;
108
- private ws;
109
- private _state;
110
- private autoReconnect;
111
- private reconnectDelay;
112
- private maxReconnectAttempts;
113
- private reconnectAttempts;
114
- private reconnectTimeout;
115
- private connectionTimeout;
31
+ declare class TalonClient {
32
+ private transport;
33
+ private eventHandlers;
34
+ private messageUnsubscribe?;
35
+ private stateUnsubscribe?;
36
+ private errorUnsubscribe?;
116
37
  private debug;
117
- private messageHandlers;
118
- private stateHandlers;
119
- private errorHandlers;
120
- private messageQueue;
121
- constructor(options: WebSocketTransportOptions);
122
- get state(): TransportState;
38
+ constructor(options: TalonClientOptions);
39
+ /**
40
+ * Connect to the edge
41
+ */
123
42
  connect(): Promise<void>;
43
+ /**
44
+ * Disconnect from the edge
45
+ */
124
46
  disconnect(): void;
125
- send(message: OutboundMessage): Promise<void>;
126
- onMessage(handler: (message: InboundMessage) => void): () => void;
127
- onStateChange(handler: (state: TransportState) => void): () => void;
128
- onError(handler: (error: Error) => void): () => void;
129
47
  /**
130
- * Handle incoming WebSocket message
48
+ * Send event TO the edge
49
+ *
50
+ * @param event - Event data to send
51
+ * @returns Promise that resolves when sent
52
+ *
53
+ * @example
54
+ * ```typescript
55
+ * await talon.send({
56
+ * event: 'add_to_cart',
57
+ * product_id: 'prod_123',
58
+ * price: 99.99
59
+ * });
60
+ * ```
61
+ */
62
+ send(event: Record<string, unknown>): Promise<void>;
63
+ /**
64
+ * Subscribe to events FROM the edge
65
+ *
66
+ * @param eventType - Event type to listen for
67
+ * @param handler - Function to call when event received
68
+ * @returns Unsubscribe function
69
+ *
70
+ * @example
71
+ * ```typescript
72
+ * const unsubscribe = talon.on('attribution_updated', (data) => {
73
+ * console.log('Attribution:', data);
74
+ * });
75
+ *
76
+ * // Later: unsubscribe()
77
+ * ```
131
78
  */
132
- private handleMessage;
79
+ on<T = any>(eventType: string, handler: EventHandler<T>): () => void;
133
80
  /**
134
- * Handle WebSocket disconnect
81
+ * Subscribe to event once (auto-unsubscribe after first call)
135
82
  */
136
- private handleDisconnect;
83
+ once<T = any>(eventType: string, handler: EventHandler<T>): () => void;
137
84
  /**
138
- * Schedule reconnection attempt with exponential backoff
85
+ * Remove all handlers for an event type
139
86
  */
140
- private scheduleReconnect;
87
+ off(eventType: string): void;
141
88
  /**
142
- * Flush queued messages
89
+ * Get current connection state
143
90
  */
144
- private flushMessageQueue;
91
+ get state(): TransportState;
145
92
  /**
146
- * Set connection state
93
+ * Check if connected
147
94
  */
148
- private setState;
95
+ get connected(): boolean;
149
96
  /**
150
- * Emit error to handlers
97
+ * Handle incoming message from transport
151
98
  */
152
- private emitError;
99
+ private handleMessage;
153
100
  /**
154
- * Debug logging
101
+ * Handle transport state change
155
102
  */
156
- private log;
157
- }
158
-
159
- /**
160
- * Webhook transport options
161
- */
162
- interface WebhookTransportOptions {
163
- /** Webhook URL */
164
- url: string;
165
- /** HTTP method */
166
- method?: 'POST' | 'PUT';
167
- /** Custom headers */
168
- headers?: Record<string, string>;
169
- /** Request timeout in ms */
170
- timeout?: number;
171
- /** Retry failed requests */
172
- retry?: boolean;
173
- /** Max retry attempts */
174
- maxRetries?: number;
175
- /** Retry delay in ms */
176
- retryDelay?: number;
177
- /** Debug mode */
178
- debug?: boolean;
179
- }
180
- /**
181
- * Webhook transport for fire-and-forget HTTP requests
182
- *
183
- * Features:
184
- * - One-way communication (no responses)
185
- * - HTTP POST/PUT requests
186
- * - Optional retry logic
187
- * - Custom headers support
188
- *
189
- * Use this for:
190
- * - Server-side event delivery
191
- * - Fire-and-forget scenarios
192
- * - When WebSocket not available/needed
193
- *
194
- * @example
195
- * ```typescript
196
- * const transport = new WebhookTransport({
197
- * url: 'https://edge.sygnl.io/webhook',
198
- * method: 'POST',
199
- * retry: true
200
- * });
201
- * ```
202
- */
203
- declare class WebhookTransport implements TalonTransport {
204
- private url;
205
- private method;
206
- private headers;
207
- private timeout;
208
- private retry;
209
- private maxRetries;
210
- private retryDelay;
211
- private debug;
212
- private _state;
213
- private messageHandlers;
214
- private stateHandlers;
215
- private errorHandlers;
216
- constructor(options: WebhookTransportOptions);
217
- get state(): TransportState;
218
- connect(): Promise<void>;
219
- disconnect(): void;
220
- send(message: OutboundMessage): Promise<void>;
221
- onMessage(handler: (message: InboundMessage) => void): () => void;
222
- onStateChange(handler: (state: TransportState) => void): () => void;
223
- onError(handler: (error: Error) => void): () => void;
103
+ private handleStateChange;
224
104
  /**
225
- * Send HTTP request
105
+ * Handle transport error
226
106
  */
227
- private sendRequest;
107
+ private handleError;
228
108
  /**
229
- * Set connection state
109
+ * Cleanup subscriptions
230
110
  */
231
- private setState;
111
+ private cleanup;
232
112
  /**
233
- * Emit error to handlers
113
+ * Generate unique message ID
234
114
  */
235
- private emitError;
115
+ private generateId;
236
116
  /**
237
117
  * Debug logging
238
118
  */
239
119
  private log;
240
120
  }
241
121
 
242
- export { WebSocketTransport, type WebSocketTransportOptions, WebhookTransport, type WebhookTransportOptions };
122
+ export { EventHandler, TalonClient, TalonClientOptions, TransportState };
package/dist/index.d.ts CHANGED
@@ -1,242 +1,122 @@
1
- /**
2
- * Message sent TO the edge
3
- */
4
- interface OutboundMessage {
5
- /** Message type/event name */
6
- type: string;
7
- /** Message payload */
8
- data: Record<string, unknown>;
9
- /** Optional message ID for tracking */
10
- id?: string;
11
- /** Timestamp */
12
- timestamp?: number;
13
- }
14
- /**
15
- * Message received FROM the edge
16
- */
17
- interface InboundMessage {
18
- /** Message type/event name */
19
- type: string;
20
- /** Message payload */
21
- data: Record<string, unknown>;
22
- /** Optional message ID */
23
- id?: string;
24
- /** Timestamp */
25
- timestamp?: number;
26
- }
27
- /**
28
- * Transport connection state
29
- */
30
- type TransportState = 'disconnected' | 'connecting' | 'connected' | 'reconnecting' | 'error';
31
- /**
32
- * Transport interface - all transports must implement this
33
- */
34
- interface TalonTransport {
35
- /**
36
- * Current connection state
37
- */
38
- readonly state: TransportState;
39
- /**
40
- * Connect to the transport
41
- */
42
- connect(): Promise<void>;
43
- /**
44
- * Disconnect from the transport
45
- */
46
- disconnect(): void;
47
- /**
48
- * Send a message to the edge
49
- * @param message - Message to send
50
- */
51
- send(message: OutboundMessage): Promise<void>;
52
- /**
53
- * Subscribe to messages from the edge
54
- * @param handler - Function to call when message received
55
- * @returns Unsubscribe function
56
- */
57
- onMessage(handler: (message: InboundMessage) => void): () => void;
58
- /**
59
- * Subscribe to connection state changes
60
- * @param handler - Function to call when state changes
61
- * @returns Unsubscribe function
62
- */
63
- onStateChange(handler: (state: TransportState) => void): () => void;
64
- /**
65
- * Subscribe to errors
66
- * @param handler - Function to call when error occurs
67
- * @returns Unsubscribe function
68
- */
69
- onError(handler: (error: Error) => void): () => void;
70
- }
1
+ import { T as TalonClientOptions, E as EventHandler, a as TransportState } from './types-DsDnVD2W.js';
2
+ export { I as InboundMessage, O as OutboundMessage, b as TalonTransport } from './types-DsDnVD2W.js';
71
3
 
72
4
  /**
73
- * WebSocket transport options
74
- */
75
- interface WebSocketTransportOptions {
76
- /** WebSocket URL */
77
- url: string;
78
- /** Auto-reconnect on disconnect */
79
- autoReconnect?: boolean;
80
- /** Reconnect delay in ms */
81
- reconnectDelay?: number;
82
- /** Max reconnect attempts (0 = infinite) */
83
- maxReconnectAttempts?: number;
84
- /** Connection timeout in ms */
85
- connectionTimeout?: number;
86
- /** Debug mode */
87
- debug?: boolean;
88
- }
89
- /**
90
- * WebSocket transport for bidirectional communication
5
+ * TalonClient - Bidirectional event delivery client
91
6
  *
92
7
  * Features:
93
- * - Real-time bidirectional communication
94
- * - Auto-reconnect with exponential backoff
95
- * - Connection state management
96
- * - Message queuing during disconnection
8
+ * - Send events TO the edge
9
+ * - Receive updates FROM the edge
10
+ * - Transport-agnostic (WebSocket, Webhook, etc.)
11
+ * - Event emitter pattern for edge responses
97
12
  *
98
13
  * @example
99
14
  * ```typescript
100
- * const transport = new WebSocketTransport({
101
- * url: 'wss://edge.sygnl.io',
102
- * autoReconnect: true
15
+ * import { TalonClient } from '@sygnl/talon';
16
+ * import { WebSocketTransport } from '@sygnl/talon/transports';
17
+ *
18
+ * const talon = new TalonClient({
19
+ * transport: new WebSocketTransport('wss://edge.sygnl.io')
20
+ * });
21
+ *
22
+ * // Send event TO edge
23
+ * await talon.send({ event: 'add_to_cart', product_id: 'abc' });
24
+ *
25
+ * // Receive updates FROM edge
26
+ * talon.on('attribution_updated', (data) => {
27
+ * console.log('Attribution:', data);
103
28
  * });
104
29
  * ```
105
30
  */
106
- declare class WebSocketTransport implements TalonTransport {
107
- private url;
108
- private ws;
109
- private _state;
110
- private autoReconnect;
111
- private reconnectDelay;
112
- private maxReconnectAttempts;
113
- private reconnectAttempts;
114
- private reconnectTimeout;
115
- private connectionTimeout;
31
+ declare class TalonClient {
32
+ private transport;
33
+ private eventHandlers;
34
+ private messageUnsubscribe?;
35
+ private stateUnsubscribe?;
36
+ private errorUnsubscribe?;
116
37
  private debug;
117
- private messageHandlers;
118
- private stateHandlers;
119
- private errorHandlers;
120
- private messageQueue;
121
- constructor(options: WebSocketTransportOptions);
122
- get state(): TransportState;
38
+ constructor(options: TalonClientOptions);
39
+ /**
40
+ * Connect to the edge
41
+ */
123
42
  connect(): Promise<void>;
43
+ /**
44
+ * Disconnect from the edge
45
+ */
124
46
  disconnect(): void;
125
- send(message: OutboundMessage): Promise<void>;
126
- onMessage(handler: (message: InboundMessage) => void): () => void;
127
- onStateChange(handler: (state: TransportState) => void): () => void;
128
- onError(handler: (error: Error) => void): () => void;
129
47
  /**
130
- * Handle incoming WebSocket message
48
+ * Send event TO the edge
49
+ *
50
+ * @param event - Event data to send
51
+ * @returns Promise that resolves when sent
52
+ *
53
+ * @example
54
+ * ```typescript
55
+ * await talon.send({
56
+ * event: 'add_to_cart',
57
+ * product_id: 'prod_123',
58
+ * price: 99.99
59
+ * });
60
+ * ```
61
+ */
62
+ send(event: Record<string, unknown>): Promise<void>;
63
+ /**
64
+ * Subscribe to events FROM the edge
65
+ *
66
+ * @param eventType - Event type to listen for
67
+ * @param handler - Function to call when event received
68
+ * @returns Unsubscribe function
69
+ *
70
+ * @example
71
+ * ```typescript
72
+ * const unsubscribe = talon.on('attribution_updated', (data) => {
73
+ * console.log('Attribution:', data);
74
+ * });
75
+ *
76
+ * // Later: unsubscribe()
77
+ * ```
131
78
  */
132
- private handleMessage;
79
+ on<T = any>(eventType: string, handler: EventHandler<T>): () => void;
133
80
  /**
134
- * Handle WebSocket disconnect
81
+ * Subscribe to event once (auto-unsubscribe after first call)
135
82
  */
136
- private handleDisconnect;
83
+ once<T = any>(eventType: string, handler: EventHandler<T>): () => void;
137
84
  /**
138
- * Schedule reconnection attempt with exponential backoff
85
+ * Remove all handlers for an event type
139
86
  */
140
- private scheduleReconnect;
87
+ off(eventType: string): void;
141
88
  /**
142
- * Flush queued messages
89
+ * Get current connection state
143
90
  */
144
- private flushMessageQueue;
91
+ get state(): TransportState;
145
92
  /**
146
- * Set connection state
93
+ * Check if connected
147
94
  */
148
- private setState;
95
+ get connected(): boolean;
149
96
  /**
150
- * Emit error to handlers
97
+ * Handle incoming message from transport
151
98
  */
152
- private emitError;
99
+ private handleMessage;
153
100
  /**
154
- * Debug logging
101
+ * Handle transport state change
155
102
  */
156
- private log;
157
- }
158
-
159
- /**
160
- * Webhook transport options
161
- */
162
- interface WebhookTransportOptions {
163
- /** Webhook URL */
164
- url: string;
165
- /** HTTP method */
166
- method?: 'POST' | 'PUT';
167
- /** Custom headers */
168
- headers?: Record<string, string>;
169
- /** Request timeout in ms */
170
- timeout?: number;
171
- /** Retry failed requests */
172
- retry?: boolean;
173
- /** Max retry attempts */
174
- maxRetries?: number;
175
- /** Retry delay in ms */
176
- retryDelay?: number;
177
- /** Debug mode */
178
- debug?: boolean;
179
- }
180
- /**
181
- * Webhook transport for fire-and-forget HTTP requests
182
- *
183
- * Features:
184
- * - One-way communication (no responses)
185
- * - HTTP POST/PUT requests
186
- * - Optional retry logic
187
- * - Custom headers support
188
- *
189
- * Use this for:
190
- * - Server-side event delivery
191
- * - Fire-and-forget scenarios
192
- * - When WebSocket not available/needed
193
- *
194
- * @example
195
- * ```typescript
196
- * const transport = new WebhookTransport({
197
- * url: 'https://edge.sygnl.io/webhook',
198
- * method: 'POST',
199
- * retry: true
200
- * });
201
- * ```
202
- */
203
- declare class WebhookTransport implements TalonTransport {
204
- private url;
205
- private method;
206
- private headers;
207
- private timeout;
208
- private retry;
209
- private maxRetries;
210
- private retryDelay;
211
- private debug;
212
- private _state;
213
- private messageHandlers;
214
- private stateHandlers;
215
- private errorHandlers;
216
- constructor(options: WebhookTransportOptions);
217
- get state(): TransportState;
218
- connect(): Promise<void>;
219
- disconnect(): void;
220
- send(message: OutboundMessage): Promise<void>;
221
- onMessage(handler: (message: InboundMessage) => void): () => void;
222
- onStateChange(handler: (state: TransportState) => void): () => void;
223
- onError(handler: (error: Error) => void): () => void;
103
+ private handleStateChange;
224
104
  /**
225
- * Send HTTP request
105
+ * Handle transport error
226
106
  */
227
- private sendRequest;
107
+ private handleError;
228
108
  /**
229
- * Set connection state
109
+ * Cleanup subscriptions
230
110
  */
231
- private setState;
111
+ private cleanup;
232
112
  /**
233
- * Emit error to handlers
113
+ * Generate unique message ID
234
114
  */
235
- private emitError;
115
+ private generateId;
236
116
  /**
237
117
  * Debug logging
238
118
  */
239
119
  private log;
240
120
  }
241
121
 
242
- export { WebSocketTransport, type WebSocketTransportOptions, WebhookTransport, type WebhookTransportOptions };
122
+ export { EventHandler, TalonClient, TalonClientOptions, TransportState };