@aws-amplify/api-graphql 4.7.4-unstable.b8f57f2.0 → 4.7.4-websocket-event.29583bc.0

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.
@@ -255,6 +255,11 @@ export abstract class AWSWebSocketProvider {
255
255
 
256
256
  return new Promise((resolve, reject) => {
257
257
  if (this.awsRealTimeSocket) {
258
+ const timeoutId = setTimeout(() => {
259
+ cleanup();
260
+ reject(new Error('Publish operation timed out'));
261
+ }, 30000); // 30 seconds timeout
262
+
258
263
  const publishListener = (event: MessageEvent) => {
259
264
  const data = JSON.parse(event.data);
260
265
  if (data.id === subscriptionId && data.type === 'publish_success') {
@@ -263,22 +268,47 @@ export abstract class AWSWebSocketProvider {
263
268
  'message',
264
269
  publishListener,
265
270
  );
266
-
271
+ cleanup();
267
272
  resolve();
268
273
  }
269
274
 
270
275
  if (data.erroredEvents && data.erroredEvents.length > 0) {
271
276
  // TODO: handle errors
277
+ const errors = data.erroredEvents.map(
278
+ (errorEvent: any) => errorEvent.error,
279
+ );
280
+ cleanup();
281
+ reject(new Error(`Publish errors: ${errors.join(', ')}`));
272
282
  }
273
283
  };
274
- this.awsRealTimeSocket.addEventListener('message', publishListener);
275
- this.awsRealTimeSocket.addEventListener('close', () => {
284
+
285
+ const errorListener = (error: Event) => {
286
+ cleanup();
287
+ reject(new Error(`WebSocket error: ${error}`));
288
+ };
289
+
290
+ const closeListener = () => {
291
+ cleanup();
276
292
  reject(new Error('WebSocket is closed'));
277
- });
278
- //
279
- // this.awsRealTimeSocket.addEventListener('error', publishListener);
293
+ };
294
+
295
+ const cleanup = () => {
296
+ clearTimeout(timeoutId);
297
+ this.awsRealTimeSocket?.removeEventListener(
298
+ 'message',
299
+ publishListener,
300
+ );
301
+ this.awsRealTimeSocket?.removeEventListener('error', errorListener);
302
+ this.awsRealTimeSocket?.removeEventListener('close', closeListener);
303
+ };
304
+
305
+ this.awsRealTimeSocket.addEventListener('message', publishListener);
306
+ this.awsRealTimeSocket.addEventListener('error', errorListener);
307
+ this.awsRealTimeSocket.addEventListener('close', closeListener);
280
308
 
281
309
  this.awsRealTimeSocket.send(serializedSubscriptionMessage);
310
+ } else {
311
+ reject(new Error('WebSocket is not connected'));
282
312
  }
283
313
  });
284
314
  }
@@ -70,7 +70,7 @@ async function connect(
70
70
  };
71
71
 
72
72
  // WS publish is not enabled in the service yet. It will be a follow up feature
73
- const _pub = async (
73
+ const pub = async (
74
74
  event: DocumentType,
75
75
  pubOptions?: EventsOptions,
76
76
  ): Promise<any> => {
@@ -94,7 +94,7 @@ async function connect(
94
94
  return {
95
95
  subscribe: sub,
96
96
  close,
97
- // publish: pub,
97
+ publish: pub,
98
98
  };
99
99
  }
100
100
 
@@ -3,6 +3,7 @@
3
3
 
4
4
  import { Subscription } from 'rxjs';
5
5
  import type { GraphQLAuthMode } from '@aws-amplify/core/internals/utils';
6
+ import { DocumentType } from '@aws-amplify/core/internals/utils';
6
7
 
7
8
  export interface SubscriptionObserver<T> {
8
9
  next(value: T): void;
@@ -51,6 +52,15 @@ export interface EventsChannel {
51
52
  *
52
53
  */
53
54
  close(): void;
55
+ /**
56
+ *
57
+ * @param event
58
+ * @param pubOptions
59
+ *
60
+ * @example
61
+ * await channel.publish({ some: "data" });
62
+ */
63
+ publish(event: DocumentType, pubOptions?: EventsOptions): Promise<any>;
54
64
  }
55
65
 
56
66
  export type ResolvedGraphQLAuthModes = Exclude<GraphQLAuthMode, 'identityPool'>;
@@ -47,7 +47,7 @@ export const configure = () => {
47
47
  };
48
48
 
49
49
  /**
50
- * Event API expects and array of JSON strings
50
+ * Event API expects an array of JSON strings
51
51
  *
52
52
  * @param events - JSON-serializable value or an array of values
53
53
  * @returns array of JSON strings