Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: pass extension into the constructor. #214

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/lib/cloudevent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Spec1 from "./bindings/http/v1/spec_1.js";
import Spec03 from "./bindings/http/v03/spec_0_3.js";
import Formatter from "./formats/json/formatter.js";
import { isBinary } from "./bindings/http/validation/fun.js";
import Extensions from "./extensions";

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

Expand All @@ -18,7 +19,7 @@ export type CE = CloudEventV1 | CloudEventV1Attributes | CloudEventV03 | CloudEv
export class CloudEvent {
spec: any;
formatter: any;
extensions: {};
extensions: Extensions;

/**
* Creates a new CloudEvent instance
Expand All @@ -33,6 +34,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 +83,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
3 changes: 3 additions & 0 deletions src/lib/extensions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default interface Extensions {
[key: string]: any
}
6 changes: 5 additions & 1 deletion src/lib/v03/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Extensions from "../extensions";

/**
* The object interface for CloudEvents 0.3.
* @see https://github.com/cloudevents/spec/blob/v0.3/spec.md
Expand Down Expand Up @@ -130,6 +132,8 @@ export interface CloudEventV03Attributes {
/**
* [OPTIONAL] CloudEvents extension attributes.
*/
extensions?: Extensions;

// tslint:disable-next-line:no-any
[key: string]: any;
}
}
5 changes: 4 additions & 1 deletion src/lib/v1/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Extensions from "../extensions";
/**
* The object interface for CloudEvents 1.0.
* @see https://github.com/cloudevents/spec/blob/v1.0/spec.md
Expand Down Expand Up @@ -124,6 +125,8 @@ export interface CloudEventV1Attributes {
/**
* [OPTIONAL] CloudEvents extension attributes.
*/
extensions?: Extensions;

// tslint:disable-next-line:no-any
[key: string]: any;
}
}
12 changes: 12 additions & 0 deletions test-ts/cloud_event_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { expect } from "chai";
import { CloudEvent } from "../";
import { CloudEventV03Attributes } from "../lib/v03";
import { CloudEventV1Attributes } from "../lib/v1";
import Extensions from "../lib/extensions";

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

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

it("can be constructed with extensions", () => {
const extensions: Extensions = {
"extension-key": "extension-value"
};
const ce = new CloudEvent({
extensions, ...fixture
});
expect(Object.keys(ce.extensions).length).to.equal(1);
lholmquist marked this conversation as resolved.
Show resolved Hide resolved
expect(ce.extensions["extension-key"]).to.equal(extensions["extension-key"]);
});

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