1
1
const Spec = require ( "./specs/spec_1.js" ) ;
2
2
const Formatter = require ( "./formats/json/formatter.js" ) ;
3
3
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.
8
6
*/
9
7
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 ( ) ;
13
16
14
17
// The map of extensions
15
18
this . extensions = { } ;
16
19
}
17
20
21
+ /**
22
+ * Get the formatters available to this CloudEvent
23
+ * @returns {Object } a JSON formatter
24
+ */
18
25
getFormats ( ) {
19
26
return { json : Formatter } ;
20
27
}
21
28
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
+ */
22
35
format ( ) {
23
36
// Check the constraints
24
37
this . spec . check ( ) ;
@@ -30,81 +43,174 @@ class CloudEvent {
30
43
return this . formatter . format ( this . spec . payload ) ;
31
44
}
32
45
46
+ /**
47
+ * Formats the CLoudEvent as JSON. No specification validation
48
+ * is performed.
49
+ * @returns {JSON } the CloudEvent in JSON form
50
+ */
33
51
toString ( ) {
34
52
return this . formatter . toString ( this . spec . payload ) ;
35
53
}
36
54
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
+ */
37
61
type ( type ) {
38
62
this . spec . type ( type ) ;
39
63
return this ;
40
64
}
41
65
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
+ */
42
71
getType ( ) {
43
72
return this . spec . getType ( ) ;
44
73
}
45
74
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.
46
80
specversion ( version ) {
47
81
return this . spec . specversion ( version ) ;
48
82
}
49
83
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
+ */
50
89
getSpecversion ( ) {
51
90
return this . spec . getSpecversion ( ) ;
52
91
}
53
92
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 ) ;
56
101
return this ;
57
102
}
58
103
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
+ */
59
109
getSource ( ) {
60
110
return this . spec . getSource ( ) ;
61
111
}
62
112
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 ) ;
65
121
return this ;
66
122
}
67
123
124
+ /**
125
+ * Gets the event id.
126
+ * @returns {string } the event id
127
+ */
68
128
getId ( ) {
69
129
return this . spec . getId ( ) ;
70
130
}
71
131
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 ) ;
74
142
return this ;
75
143
}
76
144
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
+ */
77
150
getTime ( ) {
151
+ // TODO: Ensure that this is represented as a Date internally,
152
+ // or update the JSDoc
78
153
return this . spec . getTime ( ) ;
79
154
}
80
155
81
- schemaurl ( _schemaurl ) {
82
- this . spec . schemaurl ( _schemaurl ) ;
156
+ // TODO: Deprecated in 1.0
157
+ schemaurl ( schemaurl ) {
158
+ this . spec . schemaurl ( schemaurl ) ;
83
159
return this ;
84
160
}
85
161
162
+ // TODO: Deprecated in 1.0
86
163
getSchemaurl ( ) {
87
164
return this . spec . getSchemaurl ( ) ;
88
165
}
89
166
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 ) ;
92
175
return this ;
93
176
}
94
177
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
+ */
95
183
getDataContenttype ( ) {
96
184
return this . spec . getDataContenttype ( ) ;
97
185
}
98
186
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 ) ;
101
195
return this ;
102
196
}
103
197
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
+ */
104
203
getData ( ) {
105
204
return this . spec . getData ( ) ;
106
205
}
107
206
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
+ */
108
214
addExtension ( key , value ) {
109
215
this . spec . addExtension ( key , value ) ;
110
216
@@ -114,6 +220,12 @@ class CloudEvent {
114
220
return this ;
115
221
}
116
222
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
+ */
117
229
getExtensions ( ) {
118
230
return this . extensions ;
119
231
}
0 commit comments