Skip to content

Commit

Permalink
featpass extension into the contructor.
Browse files Browse the repository at this point in the history
* This allows someone to pass an extension/extensions into the CloudEvent contructor when creating a CloudEvent.

fixes cloudevents#209

Signed-off-by: Lucas Holmquist <lholmqui@redhat.com>
  • Loading branch information
lholmquist committed Jun 8, 2020
1 parent b3d9dd2 commit bb807a2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/lib/cloudevent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type CE = CloudEventV1 | CloudEventV1Attributes | CloudEventV03 | CloudEv
export class CloudEvent {
spec: any;
formatter: any;
extensions: {};
extensions: object;

/**
* Creates a new CloudEvent instance
Expand All @@ -33,6 +33,7 @@ export class CloudEvent {
* @param {string} [event.schemaURL] The URI of the schema that the event data adheres to (v0.3 events)
* @param {string} [event.dataContentEncoding] The content encoding for the event data (v0.3 events)
* @param {string} [event.specversion] The CloudEvent specification version for this event - default: 1.0
* @param {object} [event.extensions] The CloudEvent extensions for this event
* @param {*} [event.data] The event payload
*/
constructor(event: CE) {
Expand Down Expand Up @@ -81,7 +82,13 @@ export class CloudEvent {
this.time = event.time;
}
this.formatter = new Formatter();

this.extensions = {};
if (event.extensions) {
for (const key in event.extensions) {
this.addExtension(key, event.extensions[key]);
}
}
}

/**
Expand Down
10 changes: 10 additions & 0 deletions test-ts/cloud_event_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ describe("A 1.0 CloudEvent", () => {
expect(Object.keys(ce.extensions).length).to.equal(0);
});

it("can be constructed with extensions", () => {
const extensions = {
"extension-key": "extension-value"
};
const ce = new CloudEvent({
extensions, ...fixture
});
expect(Object.keys(ce.extensions).length).to.equal(1);
});

it("throws ValidationError if the CloudEvent does not conform to the schema");
it("returns a JSON string even if format is invalid");
it("correctly formats a CloudEvent as JSON");
Expand Down

0 comments on commit bb807a2

Please sign in to comment.