Skip to content

Commit

Permalink
lib: extract CloudEventVXAttributes and extend the interface
Browse files Browse the repository at this point in the history
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 <lball@redhat.com>
  • Loading branch information
lance committed May 29, 2020
1 parent a3ce6c8 commit 26095e7
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 23 deletions.
8 changes: 5 additions & 3 deletions src/lib/cloudevent.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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.
Expand All @@ -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");
}
Expand Down
22 changes: 13 additions & 9 deletions src/lib/v03/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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
Expand All @@ -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
Expand Down
23 changes: 14 additions & 9 deletions src/lib/v1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/bindings/http/promiscuous_receiver_test.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion test/bindings/http/receiver_structured_1_test.js
Original file line number Diff line number Diff line change
@@ -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");
Expand Down

0 comments on commit 26095e7

Please sign in to comment.