Skip to content

Commit

Permalink
chore(api-graphql): events client docstrings (#13967)
Browse files Browse the repository at this point in the history
  • Loading branch information
iartemiev authored Oct 28, 2024
1 parent 847fb51 commit c19b2f3
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 15 deletions.
53 changes: 38 additions & 15 deletions packages/api-graphql/src/internals/events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,27 @@ import type {
} from './types';

/**
* @experimental API may change in future versions
*
* Establish a WebSocket connection to an Events channel
*
* @example
* const channel = await events.connect("default/channel")
*
* channel.subscribe({
* next: (data) => { console.log(data) },
* error: (err) => { console.error(err) },
* })
*
* @example // authMode override
* const channel = await events.connect("default/channel", { authMode: "userPool" })
*
* @param channel - channel path; `<namespace>/<channel>`
* @param options - request overrides: `authMode`, `authToken`
*
*/
async function connect(
channelName: string,
channel: string,
options?: EventsOptions,
): Promise<EventsChannel> {
const providerOptions = configure();
Expand All @@ -40,7 +56,7 @@ async function connect(
observer: SubscriptionObserver<any>,
subOptions?: EventsOptions,
): Subscription => {
const subscribeOptions = { ...providerOptions, query: channelName };
const subscribeOptions = { ...providerOptions, query: channel };
subscribeOptions.authenticationType = normalizeAuth(
subOptions?.authMode,
subscribeOptions.authenticationType,
Expand All @@ -60,7 +76,7 @@ async function connect(
): Promise<any> => {
const publishOptions = {
...providerOptions,
query: channelName,
query: channel,
variables: event,
};
publishOptions.authenticationType = normalizeAuth(
Expand All @@ -76,27 +92,35 @@ async function connect(
};

return {
/**
* Subscribe to incoming events
*/
subscribe: sub,
/**
* Close channel and any active subscriptions
*/
close,
// publish: pub,
};
}

/**
* Publish events to a channel via REST request
* @experimental API may change in future versions
*
* @param channelName - publish destination
* Publish events to a channel via HTTP request
*
* @example
* await events.post("default/channel", { some: "event" })
*
* @example // event batching
* await events.post("default/channel", [{ some: "event" }, { some: "event2" }])
*
* @example // authMode override
* await events.post("default/channel", { some: "event" }, { authMode: "userPool" })
*
* @param channel - channel path; `<namespace>/<channel>`
* @param event - JSON-serializable value or an array of values
* @param options
* @param options - request overrides: `authMode`, `authToken`
*
* @returns void on success
* @throws on error
*/
async function post(
channelName: string,
channel: string,
event: DocumentType | DocumentType[],
options?: EventsOptions,
): Promise<void | PublishedEvent[]> {
Expand All @@ -107,8 +131,7 @@ async function post(
);

// trailing slash required in publish
const normalizedChannelName =
channelName[0] === '/' ? channelName : `/${channelName}`;
const normalizedChannelName = channel[0] === '/' ? channel : `/${channel}`;

const publishOptions = {
...providerOptions,
Expand Down
36 changes: 36 additions & 0 deletions packages/api-graphql/src/internals/events/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,46 @@ export interface SubscriptionObserver<T> {
}

export interface EventsChannel {
/**
* @experimental API may change in future versions
*
* Subscribe to Events
*
* @example
* const channel = await events.connect("default/channel")
*
* channel.subscribe({
* next: (data) => { console.log(data) },
* error: (err) => { console.error(err) },
* })
*
* @example // authMode override
* channel.subscribe({
* next: (data) => { console.log(data) },
* error: (err) => { console.error(err) },
* }, { authMode: 'userPool' })
*
* @param observer - observer callback handlers
* `{ next: () => {}, error: () => {}}`
*
* @param options - subscribe overrides: `authMode`, `authToken`
*
*/
subscribe(
observer: SubscriptionObserver<any>,
subOptions?: EventsOptions,
): Subscription;
/**
* @experimental API may change in future versions
*
* Close channel and any active subscriptions
*
* @example
* const channel = await events.connect("default/channel")
*
* channel.close()
*
*/
close(): void;
}

Expand Down

0 comments on commit c19b2f3

Please sign in to comment.