Skip to content

Commit 768469e

Browse files
committed
squash: add extension interface and test
Signed-off-by: Lucas Holmquist <[email protected]>
1 parent a62221a commit 768469e

File tree

5 files changed

+17
-4
lines changed

5 files changed

+17
-4
lines changed

src/lib/cloudevent.ts

Lines changed: 2 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: object;
22+
extensions: Extensions;
2223

2324
/**
2425
* Creates a new CloudEvent instance

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: 3 additions & 1 deletion
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

@@ -120,13 +121,14 @@ describe("A 1.0 CloudEvent", () => {
120121
});
121122

122123
it("can be constructed with extensions", () => {
123-
const extensions = {
124+
const extensions: Extensions = {
124125
"extension-key": "extension-value"
125126
};
126127
const ce = new CloudEvent({
127128
extensions, ...fixture
128129
});
129130
expect(Object.keys(ce.extensions).length).to.equal(1);
131+
expect(ce.extensions["extension-key"]).to.equal(extensions["extension-key"]);
130132
});
131133

132134
it("throws ValidationError if the CloudEvent does not conform to the schema");

0 commit comments

Comments
 (0)