@@ -18,11 +18,27 @@ import type {
18
18
} from './types' ;
19
19
20
20
/**
21
+ * @experimental API may change in future versions
22
+ *
21
23
* Establish a WebSocket connection to an Events channel
22
24
*
25
+ * @example
26
+ * const channel = await events.connect("default/channel")
27
+ *
28
+ * channel.subscribe({
29
+ * next: (data) => { console.log(data) },
30
+ * error: (err) => { console.error(err) },
31
+ * })
32
+ *
33
+ * @example // authMode override
34
+ * const channel = await events.connect("default/channel", { authMode: "userPool" })
35
+ *
36
+ * @param channel - channel path; `<namespace>/<channel>`
37
+ * @param options - request overrides: `authMode`, `authToken`
38
+ *
23
39
*/
24
40
async function connect (
25
- channelName : string ,
41
+ channel : string ,
26
42
options ?: EventsOptions ,
27
43
) : Promise < EventsChannel > {
28
44
const providerOptions = configure ( ) ;
@@ -40,7 +56,7 @@ async function connect(
40
56
observer : SubscriptionObserver < any > ,
41
57
subOptions ?: EventsOptions ,
42
58
) : Subscription => {
43
- const subscribeOptions = { ...providerOptions , query : channelName } ;
59
+ const subscribeOptions = { ...providerOptions , query : channel } ;
44
60
subscribeOptions . authenticationType = normalizeAuth (
45
61
subOptions ?. authMode ,
46
62
subscribeOptions . authenticationType ,
@@ -60,7 +76,7 @@ async function connect(
60
76
) : Promise < any > => {
61
77
const publishOptions = {
62
78
...providerOptions ,
63
- query : channelName ,
79
+ query : channel ,
64
80
variables : event ,
65
81
} ;
66
82
publishOptions . authenticationType = normalizeAuth (
@@ -76,27 +92,35 @@ async function connect(
76
92
} ;
77
93
78
94
return {
79
- /**
80
- * Subscribe to incoming events
81
- */
82
95
subscribe : sub ,
83
- /**
84
- * Close channel and any active subscriptions
85
- */
86
96
close,
87
97
// publish: pub,
88
98
} ;
89
99
}
90
100
91
101
/**
92
- * Publish events to a channel via REST request
102
+ * @experimental API may change in future versions
93
103
*
94
- * @param channelName - publish destination
104
+ * Publish events to a channel via HTTP request
105
+ *
106
+ * @example
107
+ * await events.post("default/channel", { some: "event" })
108
+ *
109
+ * @example // event batching
110
+ * await events.post("default/channel", [{ some: "event" }, { some: "event2" }])
111
+ *
112
+ * @example // authMode override
113
+ * await events.post("default/channel", { some: "event" }, { authMode: "userPool" })
114
+ *
115
+ * @param channel - channel path; `<namespace>/<channel>`
95
116
* @param event - JSON-serializable value or an array of values
96
- * @param options
117
+ * @param options - request overrides: `authMode`, `authToken`
118
+ *
119
+ * @returns void on success
120
+ * @throws on error
97
121
*/
98
122
async function post (
99
- channelName : string ,
123
+ channel : string ,
100
124
event : DocumentType | DocumentType [ ] ,
101
125
options ?: EventsOptions ,
102
126
) : Promise < void | PublishedEvent [ ] > {
@@ -107,8 +131,7 @@ async function post(
107
131
) ;
108
132
109
133
// trailing slash required in publish
110
- const normalizedChannelName =
111
- channelName [ 0 ] === '/' ? channelName : `/${ channelName } ` ;
134
+ const normalizedChannelName = channel [ 0 ] === '/' ? channel : `/${ channel } ` ;
112
135
113
136
const publishOptions = {
114
137
...providerOptions ,
0 commit comments