Skip to content

Commit c19b2f3

Browse files
authored
chore(api-graphql): events client docstrings (#13967)
1 parent 847fb51 commit c19b2f3

File tree

2 files changed

+74
-15
lines changed

2 files changed

+74
-15
lines changed

packages/api-graphql/src/internals/events/index.ts

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,27 @@ import type {
1818
} from './types';
1919

2020
/**
21+
* @experimental API may change in future versions
22+
*
2123
* Establish a WebSocket connection to an Events channel
2224
*
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+
*
2339
*/
2440
async function connect(
25-
channelName: string,
41+
channel: string,
2642
options?: EventsOptions,
2743
): Promise<EventsChannel> {
2844
const providerOptions = configure();
@@ -40,7 +56,7 @@ async function connect(
4056
observer: SubscriptionObserver<any>,
4157
subOptions?: EventsOptions,
4258
): Subscription => {
43-
const subscribeOptions = { ...providerOptions, query: channelName };
59+
const subscribeOptions = { ...providerOptions, query: channel };
4460
subscribeOptions.authenticationType = normalizeAuth(
4561
subOptions?.authMode,
4662
subscribeOptions.authenticationType,
@@ -60,7 +76,7 @@ async function connect(
6076
): Promise<any> => {
6177
const publishOptions = {
6278
...providerOptions,
63-
query: channelName,
79+
query: channel,
6480
variables: event,
6581
};
6682
publishOptions.authenticationType = normalizeAuth(
@@ -76,27 +92,35 @@ async function connect(
7692
};
7793

7894
return {
79-
/**
80-
* Subscribe to incoming events
81-
*/
8295
subscribe: sub,
83-
/**
84-
* Close channel and any active subscriptions
85-
*/
8696
close,
8797
// publish: pub,
8898
};
8999
}
90100

91101
/**
92-
* Publish events to a channel via REST request
102+
* @experimental API may change in future versions
93103
*
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>`
95116
* @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
97121
*/
98122
async function post(
99-
channelName: string,
123+
channel: string,
100124
event: DocumentType | DocumentType[],
101125
options?: EventsOptions,
102126
): Promise<void | PublishedEvent[]> {
@@ -107,8 +131,7 @@ async function post(
107131
);
108132

109133
// trailing slash required in publish
110-
const normalizedChannelName =
111-
channelName[0] === '/' ? channelName : `/${channelName}`;
134+
const normalizedChannelName = channel[0] === '/' ? channel : `/${channel}`;
112135

113136
const publishOptions = {
114137
...providerOptions,

packages/api-graphql/src/internals/events/types.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,46 @@ export interface SubscriptionObserver<T> {
1010
}
1111

1212
export interface EventsChannel {
13+
/**
14+
* @experimental API may change in future versions
15+
*
16+
* Subscribe to Events
17+
*
18+
* @example
19+
* const channel = await events.connect("default/channel")
20+
*
21+
* channel.subscribe({
22+
* next: (data) => { console.log(data) },
23+
* error: (err) => { console.error(err) },
24+
* })
25+
*
26+
* @example // authMode override
27+
* channel.subscribe({
28+
* next: (data) => { console.log(data) },
29+
* error: (err) => { console.error(err) },
30+
* }, { authMode: 'userPool' })
31+
*
32+
* @param observer - observer callback handlers
33+
* `{ next: () => {}, error: () => {}}`
34+
*
35+
* @param options - subscribe overrides: `authMode`, `authToken`
36+
*
37+
*/
1338
subscribe(
1439
observer: SubscriptionObserver<any>,
1540
subOptions?: EventsOptions,
1641
): Subscription;
42+
/**
43+
* @experimental API may change in future versions
44+
*
45+
* Close channel and any active subscriptions
46+
*
47+
* @example
48+
* const channel = await events.connect("default/channel")
49+
*
50+
* channel.close()
51+
*
52+
*/
1753
close(): void;
1854
}
1955

0 commit comments

Comments
 (0)