11const Spec = require ( "./specs/spec_1.js" ) ;
22const 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 */
97class 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 } [UserSpec] A CloudEvent version specification
11+ * @param {Formatter } [UserFormatter] Converts the event into a readable string
12+ */
13+ constructor ( UserSpec , UserFormatter ) {
14+ this . spec = ( UserSpec ) ? new UserSpec ( CloudEvent ) : new Spec ( CloudEvent ) ;
15+ this . formatter = ( UserFormatter ) ? new UserFormatter ( ) : 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