File tree Expand file tree Collapse file tree 2 files changed +18
-1
lines changed Expand file tree Collapse file tree 2 files changed +18
-1
lines changed Original file line number Diff line number Diff line change @@ -18,7 +18,7 @@ export type CE = CloudEventV1 | CloudEventV1Attributes | CloudEventV03 | CloudEv
18
18
export class CloudEvent {
19
19
spec : any ;
20
20
formatter : any ;
21
- extensions : { } ;
21
+ extensions : object ;
22
22
23
23
/**
24
24
* Creates a new CloudEvent instance
@@ -33,6 +33,7 @@ export class CloudEvent {
33
33
* @param {string } [event.schemaURL] The URI of the schema that the event data adheres to (v0.3 events)
34
34
* @param {string } [event.dataContentEncoding] The content encoding for the event data (v0.3 events)
35
35
* @param {string } [event.specversion] The CloudEvent specification version for this event - default: 1.0
36
+ * @param {object } [event.extensions] The CloudEvent extensions for this event
36
37
* @param {* } [event.data] The event payload
37
38
*/
38
39
constructor ( event : CE ) {
@@ -81,7 +82,13 @@ export class CloudEvent {
81
82
this . time = event . time ;
82
83
}
83
84
this . formatter = new Formatter ( ) ;
85
+
84
86
this . extensions = { } ;
87
+ if ( event . extensions ) {
88
+ for ( const key in event . extensions ) {
89
+ this . addExtension ( key , event . extensions [ key ] ) ;
90
+ }
91
+ }
85
92
}
86
93
87
94
/**
Original file line number Diff line number Diff line change @@ -119,6 +119,16 @@ describe("A 1.0 CloudEvent", () => {
119
119
expect ( Object . keys ( ce . extensions ) . length ) . to . equal ( 0 ) ;
120
120
} ) ;
121
121
122
+ it ( "can be constructed with extensions" , ( ) => {
123
+ const extensions = {
124
+ "extension-key" : "extension-value"
125
+ } ;
126
+ const ce = new CloudEvent ( {
127
+ extensions, ...fixture
128
+ } ) ;
129
+ expect ( Object . keys ( ce . extensions ) . length ) . to . equal ( 1 ) ;
130
+ } ) ;
131
+
122
132
it ( "throws ValidationError if the CloudEvent does not conform to the schema" ) ;
123
133
it ( "returns a JSON string even if format is invalid" ) ;
124
134
it ( "correctly formats a CloudEvent as JSON" ) ;
You can’t perform that action at this time.
0 commit comments