Skip to content

Commit bfa8cdd

Browse files
committed
feat!: pass extension into the constructor.
* This allows someone to pass an extension/extensions into the CloudEvent contructor when creating a CloudEvent. fixes #209 Signed-off-by: Lucas Holmquist <[email protected]>
1 parent b3d9dd2 commit bfa8cdd

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/lib/cloudevent.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export type CE = CloudEventV1 | CloudEventV1Attributes | CloudEventV03 | CloudEv
1818
export 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
/**

test-ts/cloud_event_test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff 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");

0 commit comments

Comments
 (0)