diff --git a/src/lib/cloudevent.ts b/src/lib/cloudevent.ts index 3559d929..e650fdb9 100644 --- a/src/lib/cloudevent.ts +++ b/src/lib/cloudevent.ts @@ -1,5 +1,5 @@ -import { CloudEventV1 } from "./v1"; -import { CloudEventV03 } from "./v03"; +import { CloudEventV1, CloudEventV1Attributes } from "./v1"; +import { CloudEventV03, CloudEventV03Attributes } from "./v03"; import Spec1 from "./bindings/http/v1/spec_1.js"; import Spec03 from "./bindings/http/v03/spec_0_3.js"; @@ -8,6 +8,8 @@ import { isBinary } from "./bindings/http/validation/fun.js"; const { SPEC_V1, SPEC_V03 } = require("./bindings/http/constants.js"); +export type Event = CloudEventV1 | CloudEventV1Attributes | CloudEventV03 | CloudEventV03Attributes + /** * A CloudEvent describes event data in common formats to provide * interoperability across services, platforms and systems. @@ -33,7 +35,7 @@ export class CloudEvent { * @param {string} [event.specversion] The CloudEvent specification version for this event - default: 1.0 * @param {*} [event.data] The event payload */ - constructor(event: CloudEventV1 | CloudEventV03 | any) { + constructor(event: Event) { if (!event || !event.type || !event.source) { throw new TypeError("event type and source are required"); } diff --git a/src/lib/v03/index.ts b/src/lib/v03/index.ts index b3f8657f..b2c46718 100644 --- a/src/lib/v03/index.ts +++ b/src/lib/v03/index.ts @@ -2,7 +2,8 @@ * The object interface for CloudEvents 0.3. * @see https://github.com/cloudevents/spec/blob/v0.3/spec.md */ -export interface CloudEventV03 { + +export interface CloudEventV03 extends CloudEventV03Attributes { // REQUIRED Attributes /** * [REQUIRED] Identifies the event. Producers MUST ensure that `source` + `id` @@ -14,6 +15,17 @@ export interface CloudEventV03 { * @example A UUID */ id: string; + /** + * [REQUIRED] The version of the CloudEvents specification which the event + * uses. This enables the interpretation of the context. Compliant event + * producers MUST use a value of `1.0` when referring to this version of the + * specification. + * @required MUST be a non-empty string. + */ + specversion: string; +} + +export interface CloudEventV03Attributes { /** * [REQUIRED] Identifies the context in which an event happened. Often this * will include information such as the type of the event source, the @@ -30,14 +42,6 @@ export interface CloudEventV03 { * @required Non-empty URI-reference */ source: string; - /** - * [REQUIRED] The version of the CloudEvents specification which the event - * uses. This enables the interpretation of the context. Compliant event - * producers MUST use a value of `1.0` when referring to this version of the - * specification. - * @required MUST be a non-empty string. - */ - specversion: string; /** * [REQUIRED] This attribute contains a value describing the type of event * related to the originating occurrence. Often this attribute is used for diff --git a/src/lib/v1/index.ts b/src/lib/v1/index.ts index 2bd52089..51f93399 100644 --- a/src/lib/v1/index.ts +++ b/src/lib/v1/index.ts @@ -2,7 +2,7 @@ * The object interface for CloudEvents 1.0. * @see https://github.com/cloudevents/spec/blob/v1.0/spec.md */ -export interface CloudEventV1 { +export interface CloudEventV1 extends CloudEventV1Attributes { // REQUIRED Attributes /** * [REQUIRED] Identifies the event. Producers MUST ensure that `source` + `id` @@ -14,6 +14,18 @@ export interface CloudEventV1 { * @example A UUID */ id: string; + + /** + * [REQUIRED] The version of the CloudEvents specification which the event + * uses. This enables the interpretation of the context. Compliant event + * producers MUST use a value of `1.0` when referring to this version of the + * specification. + * @required MUST be a non-empty string. + */ + specversion: string; +} + +export interface CloudEventV1Attributes { /** * [REQUIRED] Identifies the context in which an event happened. Often this * will include information such as the type of the event source, the @@ -30,14 +42,7 @@ export interface CloudEventV1 { * @required Non-empty URI-reference */ source: string; - /** - * [REQUIRED] The version of the CloudEvents specification which the event - * uses. This enables the interpretation of the context. Compliant event - * producers MUST use a value of `1.0` when referring to this version of the - * specification. - * @required MUST be a non-empty string. - */ - specversion: string; + /** * [REQUIRED] This attribute contains a value describing the type of event * related to the originating occurrence. Often this attribute is used for diff --git a/test/bindings/http/promiscuous_receiver_test.js b/test/bindings/http/promiscuous_receiver_test.js index 0dfb7cf0..202d1959 100644 --- a/test/bindings/http/promiscuous_receiver_test.js +++ b/test/bindings/http/promiscuous_receiver_test.js @@ -1,5 +1,5 @@ const { expect } = require("chai"); -const { CloudEvent, HTTPReceiver } = require("../../..//index.js"); +const { CloudEvent, HTTPReceiver } = require("../../../index.js"); const { HEADER_CONTENT_TYPE, DEFAULT_CONTENT_TYPE, diff --git a/test/bindings/http/receiver_structured_1_test.js b/test/bindings/http/receiver_structured_1_test.js index 4aadbdc1..9b5b7387 100644 --- a/test/bindings/http/receiver_structured_1_test.js +++ b/test/bindings/http/receiver_structured_1_test.js @@ -1,6 +1,6 @@ const expect = require("chai").expect; const { Spec } = require("../../../lib/bindings/http/v1/index.js"); -const { CloudEvent } = require("../../..//index.js"); +const { CloudEvent } = require("../../../index.js"); const { asBase64 } = require("../../../lib/bindings/http/validation/fun.js"); const { SPEC_V1 } = require("../../../lib/bindings/http/constants.js"); const ValidationError = require("../../../lib/bindings/http/validation/validation_error.js");