Skip to content

Commit 2703cf6

Browse files
committed
docs: add JSDocs for top level API objects
This commit add JSDoc documentation to the CloudEvent and HTTPReceiver objects exposed by the API when using the top level imports, specifically `CloudEvent` and `HTTPReceiver`. Ref: #130 Signed-off-by: Lance Ball <[email protected]>
1 parent c1fda94 commit 2703cf6

File tree

3 files changed

+146
-20
lines changed

3 files changed

+146
-20
lines changed

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"arrow-parens": ["error", "always"],
1414
"arrow-body-style": ["error", "as-needed"],
1515
"prefer-template": "error",
16-
"max-len": ["warn", { "code": 80 }],
16+
"max-len": ["warn", { "code": 120 }],
1717
"no-unused-vars": ["warn", {
1818
"argsIgnorePattern": "^_$|^e$|^reject$|^resolve$"
1919
}],

lib/bindings/http/http_receiver.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ const V1Binary = require("./receiver_binary_1.js");
44
const V1Structured = require("./receiver_structured_1.js");
55
const constants = require("./constants");
66

7+
/**
8+
* A class to receive a CloudEvent from an HTTP POST request.
9+
*/
710
class HTTPReceiver {
11+
/**
12+
* Create an instance of an HTTPReceiver to accept incoming CloudEvents.
13+
*/
814
constructor() {
915
this.receivers = {
1016
v1: {
@@ -18,6 +24,14 @@ class HTTPReceiver {
1824
};
1925
}
2026

27+
/**
28+
* Acceptor for an incoming HTTP CloudEvent POST. Can process
29+
* binary and structured incoming CloudEvents.
30+
*
31+
* @param {Object} headers HTTP headers keyed by header name ("Content-Type")
32+
* @param {Object|JSON} body The body of the HTTP request
33+
* @return {CloudEvent} A new {CloudEvent} instance
34+
*/
2135
accept(headers, body) {
2236
const mode = getMode(headers);
2337
const version = getVersion(mode, headers, body);

lib/cloudevent.js

Lines changed: 131 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,37 @@
11
const Spec = require("./specs/spec_1.js");
22
const Formatter = require("./formats/json/formatter.js");
33

4-
/*
5-
* Class created using the Builder Design Pattern.
6-
*
7-
* https://en.wikipedia.org/wiki/Builder_pattern
4+
/**
5+
* An instance of a CloudEvent.
86
*/
97
class CloudEvent {
10-
constructor(_spec, _formatter) {
11-
this.spec = (_spec) ? new _spec(CloudEvent) : new Spec(CloudEvent);
12-
this.formatter = (_formatter) ? new _formatter() : new Formatter();
8+
/**
9+
* Creates a new CloudEvent instance
10+
* @param {Spec} [spec] A CloudEvent version specification
11+
* @param {Formatter} [formatter] Converts the event into a readable string
12+
*/
13+
constructor(spec, formatter) {
14+
this.spec = (spec) ? new spec(CloudEvent) : new Spec(CloudEvent);
15+
this.formatter = (formatter) ? new formatter() : new Formatter();
1316

1417
// The map of extensions
1518
this.extensions = {};
1619
}
1720

21+
/**
22+
* Get the formatters available to this CloudEvent
23+
* @returns {Object} a JSON formatter
24+
*/
1825
getFormats() {
1926
return { json: Formatter };
2027
}
2128

29+
/**
30+
* Format the CloudEvent as JSON. Validates the event according
31+
* to the CloudEvent specification and throws an exception if
32+
* it's invalid.
33+
* @returns {JSON} the CloudEvent in JSON form
34+
*/
2235
format() {
2336
// Check the constraints
2437
this.spec.check();
@@ -30,81 +43,174 @@ class CloudEvent {
3043
return this.formatter.format(this.spec.payload);
3144
}
3245

46+
/**
47+
* Formats the CLoudEvent as JSON. No specification validation
48+
* is performed.
49+
* @returns {JSON} the CloudEvent in JSON form
50+
*/
3351
toString() {
3452
return this.formatter.toString(this.spec.payload);
3553
}
3654

55+
/**
56+
* Sets the event type
57+
* @see https://github.com/cloudevents/spec/blob/master/spec.md#type
58+
* @param {string} type the type of event related to the originating source
59+
* @returns {CloudEvent} this CloudEvent
60+
*/
3761
type(type) {
3862
this.spec.type(type);
3963
return this;
4064
}
4165

66+
/**
67+
* Gets the event type
68+
* @see https://github.com/cloudevents/spec/blob/master/spec.md#type
69+
* @returns {String} the type of event related to the originating source
70+
*/
4271
getType() {
4372
return this.spec.getType();
4473
}
4574

75+
// TODO: The fact that this is exposed is problematic, given that it's
76+
// immutable and this method will have no effect. The specification
77+
// version is determined via the constructor - specifically the use
78+
// of cloud event creator functions in /v03 and /v1. By default this
79+
// object is created as a version 1.0 CloudEvent. Not documenting.
4680
specversion(version) {
4781
return this.spec.specversion(version);
4882
}
4983

84+
/**
85+
* Gets the CloudEvent specification version
86+
* @see https://github.com/cloudevents/spec/blob/master/spec.md#specversion
87+
* @returns {string} The CloudEvent version that this event adheres to
88+
*/
5089
getSpecversion() {
5190
return this.spec.getSpecversion();
5291
}
5392

54-
source(_source) {
55-
this.spec.source(_source);
93+
/**
94+
* Sets the origination source of this event.
95+
* @see https://github.com/cloudevents/spec/blob/master/spec.md#source-1
96+
* @param {URI} source the context in which the event happened
97+
* @returns {CloudEvent} this CloudEvent instance
98+
*/
99+
source(source) {
100+
this.spec.source(source);
56101
return this;
57102
}
58103

104+
/**
105+
* Gets the origination source of this event.
106+
* @see https://github.com/cloudevents/spec/blob/master/spec.md#source-1
107+
* @returns {string} the event source
108+
*/
59109
getSource() {
60110
return this.spec.getSource();
61111
}
62112

63-
id(_id) {
64-
this.spec.id(_id);
113+
/**
114+
* Sets the event id. Source + id must be unique for each distinct event.
115+
* @see https://github.com/cloudevents/spec/blob/master/spec.md#id
116+
* @param {string} id source+id must be unique for each distinct event
117+
* @returns {CloudEvent} this CloudEvent instance
118+
*/
119+
id(id) {
120+
this.spec.id(id);
65121
return this;
66122
}
67123

124+
/**
125+
* Gets the event id.
126+
* @returns {string} the event id
127+
*/
68128
getId() {
69129
return this.spec.getId();
70130
}
71131

72-
time(_time) {
73-
this.spec.time(_time);
132+
/**
133+
* Sets the timestamp for this event
134+
* @see https://github.com/cloudevents/spec/blob/master/spec.md#time
135+
* @param {Date} time timestamp when the event occurred
136+
* @returns {CloudEvent} this CloudEvent instance
137+
*/
138+
time(time) {
139+
// TODO: Ensure that this is represented as a Date internally,
140+
// or update the JSDoc
141+
this.spec.time(time);
74142
return this;
75143
}
76144

145+
/**
146+
* Gets the timestamp for this event
147+
* @see https://github.com/cloudevents/spec/blob/master/spec.md#time
148+
* @returns {Date} the timestamp for this event
149+
*/
77150
getTime() {
151+
// TODO: Ensure that this is represented as a Date internally,
152+
// or update the JSDoc
78153
return this.spec.getTime();
79154
}
80155

81-
schemaurl(_schemaurl) {
82-
this.spec.schemaurl(_schemaurl);
156+
// TODO: Deprecated in 1.0
157+
schemaurl(schemaurl) {
158+
this.spec.schemaurl(schemaurl);
83159
return this;
84160
}
85161

162+
// TODO: Deprecated in 1.0
86163
getSchemaurl() {
87164
return this.spec.getSchemaurl();
88165
}
89166

90-
dataContenttype(_contenttype) {
91-
this.spec.dataContenttype(_contenttype);
167+
/**
168+
* Sets the content type of the data value for this event
169+
* @see https://github.com/cloudevents/spec/blob/master/spec.md#datacontenttype
170+
* @param {string} contenttype per https://tools.ietf.org/html/rfc2046
171+
* @returns {CloudEvent} this CloudEvent instance
172+
*/
173+
dataContenttype(contenttype) {
174+
this.spec.dataContenttype(contenttype);
92175
return this;
93176
}
94177

178+
/**
179+
* Gets the content type of the data value for this event
180+
* @see https://github.com/cloudevents/spec/blob/master/spec.md#datacontenttype
181+
* @returns {string} the content type for the data in this event
182+
*/
95183
getDataContenttype() {
96184
return this.spec.getDataContenttype();
97185
}
98186

99-
data(_data) {
100-
this.spec.data(_data);
187+
/**
188+
* Sets the data for this event
189+
* @see https://github.com/cloudevents/spec/blob/master/spec.md#event-data
190+
* @param {*} data any data associated with this event
191+
* @returns {CloudEvent} this CloudEvent instance
192+
*/
193+
data(data) {
194+
this.spec.data(data);
101195
return this;
102196
}
103197

198+
/**
199+
* Gets any data that has been set for this event
200+
* @see https://github.com/cloudevents/spec/blob/master/spec.md#event-data
201+
* @returns {*} any data set for this event
202+
*/
104203
getData() {
105204
return this.spec.getData();
106205
}
107206

207+
/**
208+
* Adds an extension attribute to this CloudEvent
209+
* @see https://github.com/cloudevents/spec/blob/master/spec.md#extension-context-attributes
210+
* @param {*} key the name of the exteneion attribute
211+
* @param {*} value the value of the extension attribute
212+
* @returns {CloudEvent} this CloudEvent instance
213+
*/
108214
addExtension(key, value) {
109215
this.spec.addExtension(key, value);
110216

@@ -114,6 +220,12 @@ class CloudEvent {
114220
return this;
115221
}
116222

223+
/**
224+
* Gets the extension attributes, if any, associated with this event
225+
* @see https://github.com/cloudevents/spec/blob/master/spec.md#extension-context-attributes
226+
* @returns {Object} the extensions attributes - if none exist will will be {}
227+
* // TODO - this should return null or undefined if no extensions
228+
*/
117229
getExtensions() {
118230
return this.extensions;
119231
}

0 commit comments

Comments
 (0)