Skip to content

Commit 26095e7

Browse files
committed
lib: extract CloudEventVXAttributes and extend the interface
This commit extracts all of the attributes from a `CloudEventVX` that are not generated by the constructor (id and specversion) into their own `CloudEventVXAttributes` interface which the `CloudEventVX` interface now extends. This allows TS devs to optionally provide `id` and `specversion` with proper autocompletion. Additionally, I have added a union type, `Event` in `cloudevent.ts` which represents any of `CloudEventV1`, `CloudEventv03`, `CloudEventV1Attributes` and `CloudEventV03Attributes`. Signed-off-by: Lance Ball <[email protected]>
1 parent a3ce6c8 commit 26095e7

File tree

5 files changed

+34
-23
lines changed

5 files changed

+34
-23
lines changed

src/lib/cloudevent.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { CloudEventV1 } from "./v1";
2-
import { CloudEventV03 } from "./v03";
1+
import { CloudEventV1, CloudEventV1Attributes } from "./v1";
2+
import { CloudEventV03, CloudEventV03Attributes } from "./v03";
33

44
import Spec1 from "./bindings/http/v1/spec_1.js";
55
import Spec03 from "./bindings/http/v03/spec_0_3.js";
@@ -8,6 +8,8 @@ import { isBinary } from "./bindings/http/validation/fun.js";
88

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

11+
export type Event = CloudEventV1 | CloudEventV1Attributes | CloudEventV03 | CloudEventV03Attributes
12+
1113
/**
1214
* A CloudEvent describes event data in common formats to provide
1315
* interoperability across services, platforms and systems.
@@ -33,7 +35,7 @@ export class CloudEvent {
3335
* @param {string} [event.specversion] The CloudEvent specification version for this event - default: 1.0
3436
* @param {*} [event.data] The event payload
3537
*/
36-
constructor(event: CloudEventV1 | CloudEventV03 | any) {
38+
constructor(event: Event) {
3739
if (!event || !event.type || !event.source) {
3840
throw new TypeError("event type and source are required");
3941
}

src/lib/v03/index.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
* The object interface for CloudEvents 0.3.
33
* @see https://github.com/cloudevents/spec/blob/v0.3/spec.md
44
*/
5-
export interface CloudEventV03 {
5+
6+
export interface CloudEventV03 extends CloudEventV03Attributes {
67
// REQUIRED Attributes
78
/**
89
* [REQUIRED] Identifies the event. Producers MUST ensure that `source` + `id`
@@ -14,6 +15,17 @@ export interface CloudEventV03 {
1415
* @example A UUID
1516
*/
1617
id: string;
18+
/**
19+
* [REQUIRED] The version of the CloudEvents specification which the event
20+
* uses. This enables the interpretation of the context. Compliant event
21+
* producers MUST use a value of `1.0` when referring to this version of the
22+
* specification.
23+
* @required MUST be a non-empty string.
24+
*/
25+
specversion: string;
26+
}
27+
28+
export interface CloudEventV03Attributes {
1729
/**
1830
* [REQUIRED] Identifies the context in which an event happened. Often this
1931
* will include information such as the type of the event source, the
@@ -30,14 +42,6 @@ export interface CloudEventV03 {
3042
* @required Non-empty URI-reference
3143
*/
3244
source: string;
33-
/**
34-
* [REQUIRED] The version of the CloudEvents specification which the event
35-
* uses. This enables the interpretation of the context. Compliant event
36-
* producers MUST use a value of `1.0` when referring to this version of the
37-
* specification.
38-
* @required MUST be a non-empty string.
39-
*/
40-
specversion: string;
4145
/**
4246
* [REQUIRED] This attribute contains a value describing the type of event
4347
* related to the originating occurrence. Often this attribute is used for

src/lib/v1/index.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* The object interface for CloudEvents 1.0.
33
* @see https://github.com/cloudevents/spec/blob/v1.0/spec.md
44
*/
5-
export interface CloudEventV1 {
5+
export interface CloudEventV1 extends CloudEventV1Attributes {
66
// REQUIRED Attributes
77
/**
88
* [REQUIRED] Identifies the event. Producers MUST ensure that `source` + `id`
@@ -14,6 +14,18 @@ export interface CloudEventV1 {
1414
* @example A UUID
1515
*/
1616
id: string;
17+
18+
/**
19+
* [REQUIRED] The version of the CloudEvents specification which the event
20+
* uses. This enables the interpretation of the context. Compliant event
21+
* producers MUST use a value of `1.0` when referring to this version of the
22+
* specification.
23+
* @required MUST be a non-empty string.
24+
*/
25+
specversion: string;
26+
}
27+
28+
export interface CloudEventV1Attributes {
1729
/**
1830
* [REQUIRED] Identifies the context in which an event happened. Often this
1931
* will include information such as the type of the event source, the
@@ -30,14 +42,7 @@ export interface CloudEventV1 {
3042
* @required Non-empty URI-reference
3143
*/
3244
source: string;
33-
/**
34-
* [REQUIRED] The version of the CloudEvents specification which the event
35-
* uses. This enables the interpretation of the context. Compliant event
36-
* producers MUST use a value of `1.0` when referring to this version of the
37-
* specification.
38-
* @required MUST be a non-empty string.
39-
*/
40-
specversion: string;
45+
4146
/**
4247
* [REQUIRED] This attribute contains a value describing the type of event
4348
* related to the originating occurrence. Often this attribute is used for

test/bindings/http/promiscuous_receiver_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { expect } = require("chai");
2-
const { CloudEvent, HTTPReceiver } = require("../../..//index.js");
2+
const { CloudEvent, HTTPReceiver } = require("../../../index.js");
33
const {
44
HEADER_CONTENT_TYPE,
55
DEFAULT_CONTENT_TYPE,

test/bindings/http/receiver_structured_1_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const expect = require("chai").expect;
22
const { Spec } = require("../../../lib/bindings/http/v1/index.js");
3-
const { CloudEvent } = require("../../..//index.js");
3+
const { CloudEvent } = require("../../../index.js");
44
const { asBase64 } = require("../../../lib/bindings/http/validation/fun.js");
55
const { SPEC_V1 } = require("../../../lib/bindings/http/constants.js");
66
const ValidationError = require("../../../lib/bindings/http/validation/validation_error.js");

0 commit comments

Comments
 (0)