Skip to content

Commit 0c17ff3

Browse files
authored
doc: improve generated documentation (#410)
Adds some additional JSDoc properties so the docs are clearer. Signed-off-by: Lance Ball <[email protected]>
1 parent d68b85a commit 0c17ff3

File tree

3 files changed

+56
-14
lines changed

3 files changed

+56
-14
lines changed

src/message/http/index.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ import {
1212
import { isStringOrObjectOrThrow, ValidationError } from "../../event/validation";
1313
import { JSONParser, MappedParser, Parser, parserByContentType } from "../../parsers";
1414

15-
// implements Serializer
15+
/**
16+
* Serialize a CloudEvent for HTTP transport in binary mode
17+
* @implements {Serializer}
18+
* @see https://github.com/cloudevents/spec/blob/v1.0.1/http-protocol-binding.md#31-binary-content-mode
19+
*
20+
* @param {CloudEvent} event The event to serialize
21+
* @returns {Message} a Message object with headers and body
22+
*/
1623
export function binary(event: CloudEvent): Message {
1724
const contentType: Headers = { [CONSTANTS.HEADER_CONTENT_TYPE]: CONSTANTS.DEFAULT_CONTENT_TYPE };
1825
const headers: Headers = { ...contentType, ...headersFor(event) };
@@ -27,7 +34,14 @@ export function binary(event: CloudEvent): Message {
2734
};
2835
}
2936

30-
// implements Serializer
37+
/**
38+
* Serialize a CloudEvent for HTTP transport in structured mode
39+
* @implements {Serializer}
40+
* @see https://github.com/cloudevents/spec/blob/v1.0.1/http-protocol-binding.md#32-structured-content-mode
41+
*
42+
* @param {CloudEvent} event the CloudEvent to be serialized
43+
* @returns {Message} a Message object with headers and body
44+
*/
3145
export function structured(event: CloudEvent): Message {
3246
if (event.data_base64) {
3347
// The event's data is binary - delete it
@@ -41,9 +55,15 @@ export function structured(event: CloudEvent): Message {
4155
};
4256
}
4357

44-
// implements Detector
45-
// TODO: this could probably be optimized
58+
/**
59+
* Determine if a Message is a CloudEvent
60+
* @implements {Detector}
61+
*
62+
* @param {Message} message an incoming Message object
63+
* @returns {boolean} true if this Message is a CloudEvent
64+
*/
4665
export function isEvent(message: Message): boolean {
66+
// TODO: this could probably be optimized
4767
try {
4868
deserialize(message);
4969
return true;
@@ -54,6 +74,7 @@ export function isEvent(message: Message): boolean {
5474

5575
/**
5676
* Converts a Message to a CloudEvent
77+
* @implements {Deserializer}
5778
*
5879
* @param {Message} message the incoming message
5980
* @return {CloudEvent} A new {CloudEvent} instance

src/message/index.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ import { binary, deserialize, structured, isEvent } from "./http";
66
* Binding is an interface for transport protocols to implement,
77
* which provides functions for sending CloudEvent Messages over
88
* the wire.
9+
* @interface
10+
*
11+
* @property {@link Serializer} `binary` - converts a CloudEvent into a Message in binary mode
12+
* @property {@link Serializer} `structured` - converts a CloudEvent into a Message in structured mode
13+
* @property {@link Deserializer} `toEvent` - converts a Message into a CloudEvent
14+
* @property {@link Detector} `isEvent` - determines if a Message can be converted to a CloudEvent
915
*/
1016
export interface Binding {
1117
binary: Serializer;
@@ -17,6 +23,7 @@ export interface Binding {
1723
/**
1824
* Headers is an interface representing transport-agnostic headers as
1925
* key/value string pairs
26+
* @interface
2027
*/
2128
export interface Headers extends IncomingHttpHeaders {
2229
[key: string]: string | string[] | undefined;
@@ -25,6 +32,9 @@ export interface Headers extends IncomingHttpHeaders {
2532
/**
2633
* Message is an interface representing a CloudEvent as a
2734
* transport-agnostic message
35+
* @interface
36+
* @property {@linkcode Headers} `headers` - the headers for the event Message
37+
* @property string `body` - the body of the event Message
2838
*/
2939
export interface Message {
3040
headers: Headers;
@@ -33,6 +43,7 @@ export interface Message {
3343

3444
/**
3545
* An enum representing the two transport modes, binary and structured
46+
* @interface
3647
*/
3748
export enum Mode {
3849
BINARY = "binary",
@@ -42,6 +53,7 @@ export enum Mode {
4253
/**
4354
* Serializer is an interface for functions that can convert a
4455
* CloudEvent into a Message.
56+
* @interface
4557
*/
4658
export interface Serializer {
4759
(event: CloudEvent): Message;
@@ -50,6 +62,7 @@ export interface Serializer {
5062
/**
5163
* Deserializer is a function interface that converts a
5264
* Message to a CloudEvent
65+
* @interface
5366
*/
5467
export interface Deserializer {
5568
(message: Message): CloudEvent;
@@ -58,12 +71,16 @@ export interface Deserializer {
5871
/**
5972
* Detector is a function interface that detects whether
6073
* a message contains a valid CloudEvent
74+
* @interface
6175
*/
6276
export interface Detector {
6377
(message: Message): boolean;
6478
}
6579

66-
// HTTP Message capabilities
80+
/**
81+
* Bindings for HTTP transport support
82+
* @implements {@linkcode Binding}
83+
*/
6784
export const HTTP: Binding = {
6885
binary: binary as Serializer,
6986
structured: structured as Serializer,

src/transport/emitter.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@ import { EventEmitter } from "events";
55
/**
66
* Options is an additional, optional dictionary of options that may
77
* be passed to an EmitterFunction and TransportFunction
8+
* @interface
89
*/
910
export interface Options {
1011
[key: string]: string | Record<string, unknown> | unknown;
1112
}
1213

1314
/**
14-
* EmitterFunction is an invokable interface returned by the emitterFactory
15-
* function. Invoke an EmitterFunction with a CloudEvent and optional transport
15+
* EmitterFunction is an invokable interface returned by {@linkcode emitterFor}.
16+
* Invoke an EmitterFunction with a CloudEvent and optional transport
1617
* options to send the event as a Message across supported transports.
18+
* @interface
1719
*/
1820
export interface EmitterFunction {
1921
(event: CloudEvent, options?: Options): Promise<unknown>;
@@ -23,18 +25,20 @@ export interface EmitterFunction {
2325
* TransportFunction is an invokable interface provided to the emitterFactory.
2426
* A TransportFunction's responsiblity is to send a JSON encoded event Message
2527
* across the wire.
28+
* @interface
2629
*/
2730
export interface TransportFunction {
2831
(message: Message, options?: Options): Promise<unknown>;
2932
}
3033

3134
const emitterDefaults = { binding: HTTP, mode: Mode.BINARY };
3235
/**
33-
* emitterFactory creates and returns an EmitterFunction using the supplied
34-
* TransportFunction. The returned EmitterFunction will invoke the Binding's
35-
* `binary` or `structured` function to convert a CloudEvent into a JSON
36-
* Message based on the Mode provided, and invoke the TransportFunction with
37-
* the Message and any supplied options.
36+
* Creates and returns an {@linkcode EmitterFunction} using the supplied
37+
* {@linkcode TransportFunction}. The returned {@linkcode EmitterFunction}
38+
* will invoke the {@linkcode Binding}'s `binary` or `structured` function
39+
* to convert a {@linkcode CloudEvent} into a JSON
40+
* {@linkcode Message} based on the {@linkcode Mode} provided, and invoke the
41+
* TransportFunction with the Message and any supplied options.
3842
*
3943
* @param {TransportFunction} fn a TransportFunction that can accept an event Message
4044
* @param { {Binding, Mode} } options network binding and message serialization options
@@ -62,7 +66,7 @@ export function emitterFor(fn: TransportFunction, options = emitterDefaults): Em
6266
}
6367

6468
/**
65-
* A static class to emit CloudEvents within an application
69+
* A helper class to emit CloudEvents within an application
6670
*/
6771
export class Emitter {
6872
/**
@@ -97,7 +101,7 @@ export class Emitter {
97101
* Emit an event inside this application
98102
*
99103
* @param {CloudEvent} event to emit
100-
* @param {boolean} ensureDelivery fail the promise if one listener fail
104+
* @param {boolean} ensureDelivery fail the promise if one listener fails
101105
* @return {void}
102106
*/
103107
static async emitEvent(event: CloudEvent, ensureDelivery = true): Promise<void> {

0 commit comments

Comments
 (0)