Skip to content

Commit 0378f4c

Browse files
authored
feat: pass extension into the constructor. (#214)
* 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 <lholmqui@redhat.com>
1 parent dcb3c4e commit 0378f4c

File tree

5 files changed

+33
-3
lines changed

5 files changed

+33
-3
lines changed

src/lib/cloudevent.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Spec1 from "./bindings/http/v1/spec_1.js";
55
import Spec03 from "./bindings/http/v03/spec_0_3.js";
66
import Formatter from "./formats/json/formatter.js";
77
import { isBinary } from "./bindings/http/validation/fun.js";
8+
import Extensions from "./extensions";
89

910
const { SPEC_V1, SPEC_V03 } = require("./bindings/http/constants");
1011

@@ -18,7 +19,7 @@ export type CE = CloudEventV1 | CloudEventV1Attributes | CloudEventV03 | CloudEv
1819
export class CloudEvent {
1920
spec: any;
2021
formatter: any;
21-
extensions: {};
22+
extensions: Extensions;
2223

2324
/**
2425
* Creates a new CloudEvent instance
@@ -33,6 +34,7 @@ export class CloudEvent {
3334
* @param {string} [event.schemaURL] The URI of the schema that the event data adheres to (v0.3 events)
3435
* @param {string} [event.dataContentEncoding] The content encoding for the event data (v0.3 events)
3536
* @param {string} [event.specversion] The CloudEvent specification version for this event - default: 1.0
37+
* @param {object} [event.extensions] The CloudEvent extensions for this event
3638
* @param {*} [event.data] The event payload
3739
*/
3840
constructor(event: CE) {
@@ -81,7 +83,13 @@ export class CloudEvent {
8183
this.time = event.time;
8284
}
8385
this.formatter = new Formatter();
86+
8487
this.extensions = {};
88+
if (event.extensions) {
89+
for (const key in event.extensions) {
90+
this.addExtension(key, event.extensions[key]);
91+
}
92+
}
8593
}
8694

8795
/**

src/lib/extensions.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default interface Extensions {
2+
[key: string]: any
3+
}

src/lib/v03/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import Extensions from "../extensions";
2+
13
/**
24
* The object interface for CloudEvents 0.3.
35
* @see https://github.com/cloudevents/spec/blob/v0.3/spec.md
@@ -130,6 +132,8 @@ export interface CloudEventV03Attributes {
130132
/**
131133
* [OPTIONAL] CloudEvents extension attributes.
132134
*/
135+
extensions?: Extensions;
136+
133137
// tslint:disable-next-line:no-any
134138
[key: string]: any;
135-
}
139+
}

src/lib/v1/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Extensions from "../extensions";
12
/**
23
* The object interface for CloudEvents 1.0.
34
* @see https://github.com/cloudevents/spec/blob/v1.0/spec.md
@@ -124,6 +125,8 @@ export interface CloudEventV1Attributes {
124125
/**
125126
* [OPTIONAL] CloudEvents extension attributes.
126127
*/
128+
extensions?: Extensions;
129+
127130
// tslint:disable-next-line:no-any
128131
[key: string]: any;
129-
}
132+
}

test-ts/cloud_event_test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { expect } from "chai";
22
import { CloudEvent } from "../";
33
import { CloudEventV03Attributes } from "../lib/v03";
44
import { CloudEventV1Attributes } from "../lib/v1";
5+
import Extensions from "../lib/extensions";
56

67
const { SPEC_V1, SPEC_V03 } = require("../lib/bindings/http/constants");
78

@@ -119,6 +120,17 @@ describe("A 1.0 CloudEvent", () => {
119120
expect(Object.keys(ce.extensions).length).to.equal(0);
120121
});
121122

123+
it("can be constructed with extensions", () => {
124+
const extensions: Extensions = {
125+
"extension-key": "extension-value"
126+
};
127+
const ce = new CloudEvent({
128+
extensions, ...fixture
129+
});
130+
expect(Object.keys(ce.extensions).length).to.equal(1);
131+
expect(ce.extensions["extension-key"]).to.equal(extensions["extension-key"]);
132+
});
133+
122134
it("throws ValidationError if the CloudEvent does not conform to the schema");
123135
it("returns a JSON string even if format is invalid");
124136
it("correctly formats a CloudEvent as JSON");

0 commit comments

Comments
 (0)