feat: use CloudEvents not cloudevents everywhere (#101)

Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
This commit is contained in:
Grant Timmerman 2020-04-30 16:14:13 -07:00 committed by GitHub
parent cd6decd749
commit 05ecbdea4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 81 additions and 83 deletions

View File

@ -42,7 +42,7 @@ Checkout the new expressive additions.
> There is full example: [typescript-ex](./examples/typescript-ex)
```ts
import Cloudevent, {
import CloudEvent, {
event,
StructuredHTTPEmitter,
BinaryHTTPEmitter,
@ -51,7 +51,7 @@ import Cloudevent, {
BinaryHTTPReceiver
} from 'cloudevents-sdk/v1';
let myevent: Cloudevent = event()
let myevent: CloudEvent = event()
.source('/source')
.type('type')
.dataContentType('text/plain')
@ -231,7 +231,7 @@ app.post("/", (req, res) => {
- `ext`: external stuff, e.g, Cloud Events JSONSchema
- `lib/bindings`: every binding implementation goes here
- `lib/bindings/http`: every http binding implementation goes here
- `lib/cloudevent.js`: implementation of Cloudevent, an interface
- `lib/cloudevent.js`: implementation of CloudEvent, an interface
- `lib/formats/`: every format implementation goes here
- `lib/specs/`: every spec implementation goes here
@ -245,18 +245,18 @@ npm test
## The API
### `Cloudevent` class
### `CloudEvent` class
```js
/*
* Format the payload and return an Object.
*/
Object Cloudevent.format()
Object CloudEvent.format()
/*
* Format the payload as String.
*/
String Cloudevent.toString()
String CloudEvent.toString()
```
### `Formatter` classes
@ -265,12 +265,12 @@ Every formatter class must implement these methods to work properly.
```js
/*
* Format the Cloudevent payload argument and return an Object.
* Format the CloudEvent payload argument and return an Object.
*/
Object Formatter.format(Object)
/*
* Format the Cloudevent payload as String.
* Format the CloudEvent payload as String.
*/
String Formatter.toString(Object)
```
@ -297,9 +297,9 @@ Every Spec class must implement these methods to work properly.
```js
/*
* The constructor must receives the Cloudevent type.
* The constructor must receives the CloudEvent type.
*/
Spec(Cloudevent)
Spec(CloudEvent)
/*
* Checks the spec constraints, throwing an error if do not pass.
@ -320,7 +320,7 @@ Every Binding class must implement these methods to work properly.
#### Emitter Binding
Following we have the signature for the binding to emit Cloudevents.
Following we have the signature for the binding to emit CloudEvents.
```js
/*
@ -329,14 +329,14 @@ Following we have the signature for the binding to emit Cloudevents.
Binding(config)
/*
* Emits the event using an instance of Cloudevent.
* Emits the event using an instance of CloudEvent.
*/
Binding.emit(cloudevent)
Binding.emit(cloudEvent)
```
#### Receiver Binding
Following we have the signature for the binding to receive Cloudevents.
Following we have the signature for the binding to receive CloudEvents.
```js
/*
@ -351,9 +351,9 @@ Receiver(config)
Receiver.check(Object, Map)
/*
* Checks and parse as Cloudevent
* Checks and parse as CloudEvent
*/
Cloudevent Receiver.parse(Object, Map)
CloudEvent Receiver.parse(Object, Map)
```
> See how to implement the method injection [here](lib/specs/spec_0_1.js#L17)

View File

@ -1,4 +1,4 @@
import Cloudevent, {
import CloudEvent, {
event,
StructuredHTTPEmitter,
BinaryHTTPEmitter,
@ -8,7 +8,7 @@ import Cloudevent, {
export function doSomeStuff() {
const myevent: Cloudevent = event()
const myevent: CloudEvent = event()
.source('/source')
.type('type')
.dataContentType('text/plain')
@ -20,13 +20,13 @@ export function doSomeStuff() {
console.log(myevent.toString());
console.log(myevent.getExtensions());
let config = {
const config = {
method: "POST",
url : "https://enu90y24i64jp.x.pipedream.net/"
};
// ------ emitter structured
let structured = new StructuredHTTPEmitter(config);
const structured = new StructuredHTTPEmitter(config);
structured.emit(myevent).then(res => {
// success
console.log("Structured Mode: Success!")
@ -37,7 +37,7 @@ export function doSomeStuff() {
});
// ------ emitter binary
let binary = new BinaryHTTPEmitter(config);
const binary = new BinaryHTTPEmitter(config);
binary.emit(myevent).then(res => {
console.log("Binary Mode: Success!");
})
@ -46,20 +46,20 @@ export function doSomeStuff() {
});
// ------ receiver structured
let payload = myevent.toString();
let headers = {
const payload = myevent.toString();
const headers = {
"Content-Type":"application/cloudevents+json"
};
let receiverStructured = new StructuredHTTPReceiver();
const receiverStructured = new StructuredHTTPReceiver();
console.log(receiverStructured.parse(payload, headers).toString());
// ------ receiver binary
let extension1 = "mycuston-ext1";
let data = {
const extension1 = "mycuston-ext1";
const data = {
"data" : "dataString"
};
var attributes = {
const attributes = {
"ce-type" : "type",
"ce-specversion" : "1.0",
"ce-source" : "source",
@ -70,7 +70,7 @@ export function doSomeStuff() {
"ce-extension1" : extension1
};
let receiverBinary = new BinaryHTTPReceiver();
const receiverBinary = new BinaryHTTPReceiver();
console.log(receiverBinary.parse(data, attributes).toString());
return true;

View File

@ -1,3 +1,3 @@
const Cloudevent = require("./lib/cloudevent.js");
const CloudEvent = require("./lib/cloudevent.js");
module.exports = Cloudevent;
module.exports = CloudEvent;

View File

@ -1,6 +1,6 @@
const Constants = require("./constants.js");
const Commons = require("./commons.js");
const Cloudevent = require("../../cloudevent.js");
const CloudEvent = require("../../cloudevent.js");
const {
isDefinedOrThrow,
@ -88,7 +88,7 @@ BinaryHTTPReceiver.prototype.parse = function(payload, headers) {
const sanityHeaders = Commons.sanityAndClone(headers);
const processedHeaders = [];
const cloudevent = new Cloudevent(this.Spec);
const cloudevent = new CloudEvent(this.Spec);
// dont worry, check() have seen what was required or not
Array.from(Object.keys(this.setterByHeader))

View File

@ -1,6 +1,6 @@
const Constants = require("./constants.js");
const Commons = require("./commons.js");
const Cloudevent = require("../../cloudevent.js");
const CloudEvent = require("../../cloudevent.js");
const {
isDefinedOrThrow,
@ -61,7 +61,7 @@ StructuredHTTPReceiver.prototype.parse = function(payload, headers) {
this.spec.check(event);
const processedAttributes = [];
const cloudevent = new Cloudevent(this.Spec);
const cloudevent = new CloudEvent(this.Spec);
Array.from(Object.keys(this.setterByAttribute))
.filter((attribute) => event[attribute])

View File

@ -147,7 +147,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.3", () => {
});
describe("Parse", () => {
it("Cloudevent contains 'type'", () => {
it("CloudEvent contains 'type'", () => {
// setup
const payload = {
data: "dataString"
@ -170,7 +170,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.3", () => {
.to.equal("type");
});
it("Cloudevent contains 'specversion'", () => {
it("CloudEvent contains 'specversion'", () => {
// setup
const payload = {
data: "dataString"
@ -193,7 +193,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.3", () => {
.to.equal("0.3");
});
it("Cloudevent contains 'source'", () => {
it("CloudEvent contains 'source'", () => {
// setup
const payload = {
data: "dataString"
@ -216,7 +216,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.3", () => {
.to.equal("/source");
});
it("Cloudevent contains 'id'", () => {
it("CloudEvent contains 'id'", () => {
// setup
const payload = {
data: "dataString"
@ -239,7 +239,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.3", () => {
.to.equal("id");
});
it("Cloudevent contains 'time'", () => {
it("CloudEvent contains 'time'", () => {
// setup
const payload = {
data: "dataString"
@ -262,7 +262,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.3", () => {
.to.equal("2019-06-16T11:42:00.000Z");
});
it("Cloudevent contains 'schemaurl'", () => {
it("CloudEvent contains 'schemaurl'", () => {
// setup
const payload = {
data: "dataString"
@ -285,7 +285,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.3", () => {
.to.equal("http://schema.registry/v1");
});
it("Cloudevent contains 'datacontenttype' (application/json)", () => {
it("CloudEvent contains 'datacontenttype' (application/json)", () => {
// setup
const payload = {
data: "dataString"
@ -308,7 +308,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.3", () => {
.to.equal("application/json");
});
it("Cloudevent contains 'datacontenttype' (application/octet-stream)",
it("CloudEvent contains 'datacontenttype' (application/octet-stream)",
() => {
// setup
const payload = "The payload is binary data";
@ -330,7 +330,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.3", () => {
.to.equal("application/octet-stream");
});
it("Cloudevent contains 'data' (application/json)", () => {
it("CloudEvent contains 'data' (application/json)", () => {
// setup
const payload = {
data: "dataString"
@ -353,7 +353,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.3", () => {
.to.deep.equal(payload);
});
it("Cloudevent contains 'data' (application/octet-stream)", () => {
it("CloudEvent contains 'data' (application/octet-stream)", () => {
// setup
const payload = "The payload is binary data";
const attributes = {

View File

@ -148,7 +148,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v1.0", () => {
});
describe("Parse", () => {
it("Cloudevent contains 'type'", () => {
it("CloudEvent contains 'type'", () => {
// setup
const payload = {
data: "dataString"
@ -171,7 +171,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v1.0", () => {
.to.equal("type");
});
it("Cloudevent contains 'specversion'", () => {
it("CloudEvent contains 'specversion'", () => {
// setup
const payload = {
data: "dataString"
@ -194,7 +194,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v1.0", () => {
.to.equal("1.0");
});
it("Cloudevent contains 'source'", () => {
it("CloudEvent contains 'source'", () => {
// setup
const payload = {
data: "dataString"
@ -217,7 +217,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v1.0", () => {
.to.equal("/source");
});
it("Cloudevent contains 'id'", () => {
it("CloudEvent contains 'id'", () => {
// setup
const payload = {
data: "dataString"
@ -240,7 +240,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v1.0", () => {
.to.equal("id");
});
it("Cloudevent contains 'time'", () => {
it("CloudEvent contains 'time'", () => {
// setup
const payload = {
data: "dataString"
@ -263,7 +263,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v1.0", () => {
.to.equal("2019-06-16T11:42:00.000Z");
});
it("Cloudevent contains 'dataschema'", () => {
it("CloudEvent contains 'dataschema'", () => {
// setup
const payload = {
data: "dataString"
@ -286,7 +286,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v1.0", () => {
.to.equal("http://schema.registry/v1");
});
it("Cloudevent contains 'contenttype' (application/json)", () => {
it("CloudEvent contains 'contenttype' (application/json)", () => {
// setup
const payload = {
data: "dataString"
@ -309,7 +309,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v1.0", () => {
.to.equal("application/json");
});
it("Cloudevent contains 'contenttype' (application/octet-stream)", () => {
it("CloudEvent contains 'contenttype' (application/octet-stream)", () => {
// setup
const payload = "The payload is binary data";
const attributes = {
@ -330,7 +330,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v1.0", () => {
.to.equal("application/octet-stream");
});
it("Cloudevent contains 'data' (application/json)", () => {
it("CloudEvent contains 'data' (application/json)", () => {
// setup
const payload = {
data: "dataString"
@ -353,7 +353,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v1.0", () => {
.to.deep.equal(payload);
});
it("Cloudevent contains 'data' (application/octet-stream)", () => {
it("CloudEvent contains 'data' (application/octet-stream)", () => {
// setup
const payload = "The payload is binary data";
const attributes = {

View File

@ -1,6 +1,6 @@
const expect = require("chai").expect;
const v1 = require("../../../v1/index.js");
const Cloudevent = require("../../../index.js");
const CloudEvent = require("../../../index.js");
const { asBase64 } = require("../../../lib/utils/fun.js");
@ -82,7 +82,7 @@ describe("HTTP Transport Binding Structured Receiver for CloudEvents v1.0",
it("Throw error when the event does not follow the spec", () => {
// setup
const payload =
new Cloudevent()
new CloudEvent()
.type(type)
.source(source)
.time(now)

View File

@ -1,6 +1,6 @@
const expect = require("chai").expect;
const Unmarshaller = require("../../../lib/bindings/http/unmarshaller_0_3.js");
const Cloudevent = require("../../../index.js");
const CloudEvent = require("../../../index.js");
const v03 = require("../../../v03/index.js");
const type = "com.github.pull.create";
@ -117,7 +117,7 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
it("Should accept event that follow the spec 0.3", () => {
// setup
const payload =
new Cloudevent(v03.Spec)
new CloudEvent(v03.Spec)
.type(type)
.source(source)
.dataContentType(ceContentType)
@ -146,7 +146,7 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
it("Should parse 'data' stringfied json to json object", () => {
// setup
const payload =
new Cloudevent(v03.Spec)
new CloudEvent(v03.Spec)
.type(type)
.source(source)
.dataContentType(ceContentType)

View File

@ -2,7 +2,7 @@ const expect = require("chai").expect;
const nock = require("nock");
const BinaryHTTPEmitter =
require("../lib/bindings/http/emitter_binary_0_3.js");
const Cloudevent = require("../lib/cloudevent.js");
const CloudEvent = require("../lib/cloudevent.js");
const v03 = require("../v03/index.js");
const type = "com.github.pull.create";
@ -25,7 +25,7 @@ const ext2Name = "extension2";
const ext2Value = "acme";
const cloudevent =
new Cloudevent(v03.Spec)
new CloudEvent(v03.Spec)
.type(type)
.source(source)
.dataContentType(ceContentType)
@ -37,7 +37,7 @@ const cloudevent =
.addExtension(ext2Name, ext2Value);
const cebase64 =
new Cloudevent(v03.Spec)
new CloudEvent(v03.Spec)
.type(type)
.source(source)
.dataContentType(ceContentType)

View File

@ -7,7 +7,7 @@ const {
Spec,
BinaryHTTPEmitter,
StructuredHTTPEmitter,
Cloudevent
CloudEvent
} = require("../v1/index.js");
const type = "com.github.pull.create";
@ -28,7 +28,7 @@ const ext2Name = "extension2";
const ext2Value = "acme";
const cloudevent =
new Cloudevent(Spec)
new CloudEvent(Spec)
.type(type)
.source(source)
.dataContentType(ceContentType)
@ -94,7 +94,7 @@ describe("HTTP Transport Binding - Version 1.0", () => {
const bindata = Uint32Array.from(dataString, (c) => c.codePointAt(0));
const expected = asBase64(bindata);
const binevent =
new Cloudevent(Spec)
new CloudEvent(Spec)
.type(type)
.source(source)
.dataContentType("text/plain")
@ -111,7 +111,7 @@ describe("HTTP Transport Binding - Version 1.0", () => {
it("the payload must have 'data_base64' when data is binary", () => {
const binevent =
new Cloudevent(Spec)
new CloudEvent(Spec)
.type(type)
.source(source)
.dataContentType("text/plain")
@ -164,7 +164,7 @@ describe("HTTP Transport Binding - Version 1.0", () => {
const bindata = Uint32Array.from(dataString, (c) => c.codePointAt(0));
const expected = asBase64(bindata);
const binevent =
new Cloudevent(Spec)
new CloudEvent(Spec)
.type(type)
.source(source)
.dataContentType("text/plain")

View File

@ -1,6 +1,6 @@
const expect = require("chai").expect;
const Spec03 = require("../lib/specs/spec_0_3.js");
const Cloudevent = require("../index.js");
const CloudEvent = require("../index.js");
const { v4: uuidv4 } = require("uuid");
const id = uuidv4();
@ -16,7 +16,7 @@ const data = {
const subject = "subject-x0";
const cloudevent =
new Cloudevent(Spec03)
new CloudEvent(Spec03)
.id(id)
.source(source)
.type(type)

View File

@ -1,6 +1,6 @@
const expect = require("chai").expect;
const Spec1 = require("../lib/specs/spec_1.js");
const Cloudevent = require("../index.js");
const CloudEvent = require("../index.js");
const { v4: uuidv4 } = require("uuid");
const { asBase64 } = require("../lib/utils/fun.js");
@ -16,7 +16,7 @@ const data = {
const subject = "subject-x0";
const cloudevent =
new Cloudevent(Spec1)
new CloudEvent(Spec1)
.id(id)
.source(source)
.type(type)

View File

@ -1,4 +1,4 @@
const Cloudevent = require("../lib/cloudevent.js");
const CloudEvent = require("../lib/cloudevent.js");
const Spec = require("../lib/specs/spec_0_3.js");
const StructuredHTTPEmitter =
require("../lib/bindings/http/emitter_structured.js");
@ -12,8 +12,8 @@ const BinaryHTTPReceiver =
const HTTPUnmarshaller = require("../lib/bindings/http/unmarshaller_0_3.js");
function event() {
return new Cloudevent(Spec);
function newEvent() {
return new CloudEvent(Spec);
}
module.exports = {
@ -23,7 +23,6 @@ module.exports = {
BinaryHTTPEmitter,
BinaryHTTPReceiver,
HTTPUnmarshaller,
Cloudevent,
CloudEvent: Cloudevent,
event
CloudEvent: newEvent,
event: newEvent
};

View File

@ -1,4 +1,4 @@
const Cloudevent = require("../lib/cloudevent.js");
const CloudEvent = require("../lib/cloudevent.js");
const Spec = require("../lib/specs/spec_1.js");
const StructuredHTTPEmitter =
@ -12,8 +12,8 @@ const StructuredHTTPReceiver =
const BinaryHTTPReceiver =
require("../lib/bindings/http/receiver_binary_1.js");
function event() {
return new Cloudevent(Spec);
function newEvent() {
return new CloudEvent(Spec);
}
module.exports = {
@ -22,7 +22,6 @@ module.exports = {
BinaryHTTPEmitter,
StructuredHTTPReceiver,
BinaryHTTPReceiver,
Cloudevent: event,
CloudEvent: event,
event
CloudEvent: newEvent,
event: newEvent
};