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
1818export class CloudEvent {
1919 spec : any ;
2020 formatter : any ;
21- extensions : { } ;
21+ extensions : object ;
2222
2323 /**
2424 * Creates a new CloudEvent instance
@@ -33,6 +33,7 @@ export class CloudEvent {
3333 * @param {string } [event.schemaURL] The URI of the schema that the event data adheres to (v0.3 events)
3434 * @param {string } [event.dataContentEncoding] The content encoding for the event data (v0.3 events)
3535 * @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
3637 * @param {* } [event.data] The event payload
3738 */
3839 constructor ( event : CE ) {
@@ -81,7 +82,13 @@ export class CloudEvent {
8182 this . time = event . time ;
8283 }
8384 this . formatter = new Formatter ( ) ;
85+
8486 this . extensions = { } ;
87+ if ( event . extensions ) {
88+ for ( const key in event . extensions ) {
89+ this . addExtension ( key , event . extensions [ key ] ) ;
90+ }
91+ }
8592 }
8693
8794 /**
Original file line number Diff line number Diff line change @@ -119,6 +119,16 @@ describe("A 1.0 CloudEvent", () => {
119119 expect ( Object . keys ( ce . extensions ) . length ) . to . equal ( 0 ) ;
120120 } ) ;
121121
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+
122132 it ( "throws ValidationError if the CloudEvent does not conform to the schema" ) ;
123133 it ( "returns a JSON string even if format is invalid" ) ;
124134 it ( "correctly formats a CloudEvent as JSON" ) ;
You can’t perform that action at this time.
0 commit comments