From 4a97553dfba2105ef63fa72449a791bbd7ccbde5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Fri, 9 Nov 2018 13:45:59 -0200 Subject: [PATCH 01/22] First impl. --- index.js | 8 +++++++ lib/cloudevent.js | 34 +++++++++++++++++++++++++++++ lib/jsonformatter.js | 14 ++++++++++++ lib/spec_0_1.js | 43 +++++++++++++++++++++++++++++++++++++ lib/spec_0_2.js | 26 ++++++++++++++++++++++ package.json | 17 ++++++++++----- test/cloudevent_spec_0_1.js | 32 +++++++++++++++++++++++++++ test/cloudevent_spec_0_2.js | 32 +++++++++++++++++++++++++++ 8 files changed, 201 insertions(+), 5 deletions(-) create mode 100644 index.js create mode 100644 lib/cloudevent.js create mode 100644 lib/jsonformatter.js create mode 100644 lib/spec_0_1.js create mode 100644 lib/spec_0_2.js create mode 100644 test/cloudevent_spec_0_1.js create mode 100644 test/cloudevent_spec_0_2.js diff --git a/index.js b/index.js new file mode 100644 index 0000000..baaacb5 --- /dev/null +++ b/index.js @@ -0,0 +1,8 @@ +var Cloudevent = require('./lib/cloudevent.js'); +//var Spec_0_1 = require('./lib/spec_0_1.js'); +//var Spec_0_2 = require('./lib/spec_0_2.js'); + +module.exports = Cloudevent; +//module.exports.Spec_0_1 = Spec_0_1; +//module.exports.Spec_0_2 = Spec_0_2; + diff --git a/lib/cloudevent.js b/lib/cloudevent.js new file mode 100644 index 0000000..318b5ac --- /dev/null +++ b/lib/cloudevent.js @@ -0,0 +1,34 @@ +var Spec_0_1 = require('./spec_0_1.js'); +var Spec_0_2 = require('./spec_0_2.js'); +var JSONFormatter = require('./jsonformatter.js'); + +function Cloudevent(_spec, _formatter){ + this.spec = (_spec) ? new _spec(Cloudevent) : new Spec_0_1(Cloudevent); + this.formatter = (_formatter) ? _formatter : new JSONFormatter(); +} + +Cloudevent.prototype.format = function(){ + return this.formatter.format(this.spec.payload); +} + +Cloudevent.prototype.toString = function(){ + return this.formatter.toString(this.spec.payload); +} + +Cloudevent.prototype.type = function(type){ + this.spec.type(type); + return this; +} + +Cloudevent.prototype.source = function(_source){ + this.spec.source(_source); + return this; +} + +Cloudevent.specs = { + '0.1': Spec_0_1, + '0.2': Spec_0_2 +}; + +module.exports = Cloudevent; + diff --git a/lib/jsonformatter.js b/lib/jsonformatter.js new file mode 100644 index 0000000..7f0ca46 --- /dev/null +++ b/lib/jsonformatter.js @@ -0,0 +1,14 @@ + +function JSONFormatter(){ + +} + +JSONFormatter.prototype.format = function(payload){ + return payload; +} + +JSONFormatter.prototype.toString = function(payload){ + return JSON.stringify(payload); +} + +module.exports = JSONFormatter; diff --git a/lib/spec_0_1.js b/lib/spec_0_1.js new file mode 100644 index 0000000..3b4e657 --- /dev/null +++ b/lib/spec_0_1.js @@ -0,0 +1,43 @@ +var uuid = require('uuid/v4'); + +function Spec_0_1(_caller){ + this.payload = { + cloudEventsVersion: '0.1', + eventID: uuid() + }; + + /* + * Used to inject backward compatibility functions or attributes. + */ + this.caller = _caller; + + /* + * Inject the method to set the version related to data attribute. + */ + this.caller.prototype.eventTypeVersion = function(_version){ + this.spec.eventTypeVersion(_version); + } +} + +Spec_0_1.prototype.type = function(_type){ + this.payload['eventType'] = _type; + return this; +} + +Spec_0_1.prototype.eventTypeVersion = function(version){ + this.payload['eventTypeVersion'] = version; + return this; +} + +Spec_0_1.prototype.source = function(_source){ + this.payload['source'] = _source; + return this; +} + +Spec_0_1.prototype.id = function(_id){ + this.payload['eventID'] = _id; + return this; +} + +module.exports = Spec_0_1; + diff --git a/lib/spec_0_2.js b/lib/spec_0_2.js new file mode 100644 index 0000000..de8fed8 --- /dev/null +++ b/lib/spec_0_2.js @@ -0,0 +1,26 @@ +var uuid = require('uuid/v4'); + +function Spec_0_2(){ + this.payload = { + specversion: '0.2', + id: uuid() + }; +} + +Spec_0_2.prototype.type = function(_type){ + this.payload['type'] = _type; + return this; +} + +Spec_0_2.prototype.source = function(_source){ + this.payload['source'] = _source; + return this; +} + +Spec_0_2.prototype.id = function(_id){ + this.payload['id'] = _id; + return this; +} + +module.exports = Spec_0_2; + diff --git a/package.json b/package.json index b6827ec..93c5621 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "CloudEvents SDK for JavaScript", "main": "index.js", "scripts": { - "test": "mocha test/test.js" + "test": "./node_modules/.bin/mocha -C test/*.js" }, "repository": { "type": "git", @@ -17,13 +17,20 @@ ], "author": "cloudevents.io", "contributors": { - "name" : "Fábio José de Moraes", - "email" : "fabiojose@gmail.com", - "url" : "https://github.com/fabiojose" + "name": "Fábio José de Moraes", + "email": "fabiojose@gmail.com", + "url": "https://github.com/fabiojose" }, "license": "Apache-2.0", "bugs": { "url": "https://github.com/cloudevents/sdk-javascript/issues" }, - "homepage": "https://github.com/cloudevents/sdk-javascript#readme" + "homepage": "https://github.com/cloudevents/sdk-javascript#readme", + "dependencies": { + "uuid": "3.3.2" + }, + "devDependencies": { + "chai": "4.2.0", + "mocha": "5.2.0" + } } diff --git a/test/cloudevent_spec_0_1.js b/test/cloudevent_spec_0_1.js new file mode 100644 index 0000000..b42d71d --- /dev/null +++ b/test/cloudevent_spec_0_1.js @@ -0,0 +1,32 @@ +var expect = require("chai").expect; +var Cloudevent = require("../index.js"); + +var cloudevent = new Cloudevent() + .type("com.github.pull.create") + .source("urn:event:from:myapi/resourse/123"); + +describe("CloudEvents Spec 0.1 - JavaScript SDK", () => { + + describe("JSON Format", () => { + + describe("Required context attributes", () => { + it("requires 'eventType'", () => { + expect(cloudevent.format()).to.have.property('eventType'); + }); + + it("requires 'cloudEventsVersion'", () => { + expect(cloudevent.format()).to.have.property('cloudEventsVersion'); + }); + + it("requires 'source'", () => { + expect(cloudevent.format()).to.have.property('source'); + }); + + it("requires 'eventID'", () => { + expect(cloudevent.format()).to.have.property('eventID'); + }); + }); + + }); + +}); diff --git a/test/cloudevent_spec_0_2.js b/test/cloudevent_spec_0_2.js new file mode 100644 index 0000000..a94faa1 --- /dev/null +++ b/test/cloudevent_spec_0_2.js @@ -0,0 +1,32 @@ +var expect = require("chai").expect; +var Cloudevent = require("../index.js"); + +var cloudevent = new Cloudevent(Cloudevent.specs['0.2']) + .type("com.github.pull.create") + .source("urn:event:from:myapi/resourse/123"); + +describe("CloudEvents Spec 0.2 - JavaScript SDK", () => { + + describe("JSON Format", () => { + + describe("Required context attributes", () => { + it("requires 'type'", () => { + expect(cloudevent.format()).to.have.property('type'); + }); + + it("requires 'specversion'", () => { + expect(cloudevent.format()).to.have.property('specversion'); + }); + + it("requires 'source'", () => { + expect(cloudevent.format()).to.have.property('source'); + }); + + it("requires 'id'", () => { + expect(cloudevent.format()).to.have.property('id'); + }); + }); + + }); + +}); From 2487db05a9d1b1d68241b77dfdaf6579c5fa9146 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Fri, 9 Nov 2018 13:58:07 -0200 Subject: [PATCH 02/22] Documentation --- README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/README.md b/README.md index 5ba39f8..27c6f75 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,31 @@ # sdk-javascript Javascript SDK for CloudEvents + +# Repository Structure + +```text +├── index.js +├── lib +│   ├── cloudevent.js +│   ├── jsonformatter.js +│   ├── spec_0_1.js +│   └── spec_0_2.js +├── LICENSE +├── package.json +├── README.md +└── test + ├── cloudevent_spec_0_1.js + └── cloudevent_spec_0_2.js + +``` + +* `index.js`: exports the Cloudevent class + +* `lib/cloudevent.js`: implementation of Cloudevent class + +* `lib/jsonformatter.js`: implementation for JSON Format + +* `lib/spec_0_1.js`: implementation for spec version 0.1 + +* `lib/spec_0_2.js`: implementation for spec version 0.2 + From c2788085ab37a792355c94c1202d0954640abb60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Fri, 9 Nov 2018 14:02:36 -0200 Subject: [PATCH 03/22] Documentation --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 27c6f75..a855602 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ Javascript SDK for CloudEvents * `lib/cloudevent.js`: implementation of Cloudevent class -* `lib/jsonformatter.js`: implementation for JSON Format +* `lib/jsonformatter.js`: implementation for JSON formatting [json-format](https://github.com/cloudevents/spec/blob/v0.1/json-format.md) * `lib/spec_0_1.js`: implementation for spec version 0.1 From 185f0c43a0f6dda13c64e7804990b45fa039787a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Fri, 9 Nov 2018 14:03:34 -0200 Subject: [PATCH 04/22] Refactoring --- lib/{ => format}/jsonformatter.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename lib/{ => format}/jsonformatter.js (100%) diff --git a/lib/jsonformatter.js b/lib/format/jsonformatter.js similarity index 100% rename from lib/jsonformatter.js rename to lib/format/jsonformatter.js From a89494c4f72ff95af37d9ba809044666d1551038 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Fri, 9 Nov 2018 14:04:08 -0200 Subject: [PATCH 05/22] Refactoring --- lib/format/{jsonformatter.js => json_0_1.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename lib/format/{jsonformatter.js => json_0_1.js} (100%) diff --git a/lib/format/jsonformatter.js b/lib/format/json_0_1.js similarity index 100% rename from lib/format/jsonformatter.js rename to lib/format/json_0_1.js From 16da64c814bde6705273190a4d7cf3cb6673cb2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Fri, 9 Nov 2018 14:06:00 -0200 Subject: [PATCH 06/22] Versioning the JSON Formatter --- lib/cloudevent.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/cloudevent.js b/lib/cloudevent.js index 318b5ac..2419b2a 100644 --- a/lib/cloudevent.js +++ b/lib/cloudevent.js @@ -1,10 +1,10 @@ -var Spec_0_1 = require('./spec_0_1.js'); -var Spec_0_2 = require('./spec_0_2.js'); -var JSONFormatter = require('./jsonformatter.js'); +var Spec_0_1 = require('./spec_0_1.js'); +var Spec_0_2 = require('./spec_0_2.js'); +var JSONFormatter_0_1 = require('./format/json_0_1.js'); function Cloudevent(_spec, _formatter){ this.spec = (_spec) ? new _spec(Cloudevent) : new Spec_0_1(Cloudevent); - this.formatter = (_formatter) ? _formatter : new JSONFormatter(); + this.formatter = (_formatter) ? _formatter : new JSONFormatter_0_1(); } Cloudevent.prototype.format = function(){ From 2b28def2621c55e74298faee6b55e0ceda3fe89a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Fri, 9 Nov 2018 14:18:29 -0200 Subject: [PATCH 07/22] Docs: how to use --- README.md | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a855602..e2f0720 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,8 @@ Javascript SDK for CloudEvents ├── lib │   ├── cloudevent.js │   ├── jsonformatter.js +│   ├── format +│   │   └── json_0_1.js │   ├── spec_0_1.js │   └── spec_0_2.js ├── LICENSE @@ -19,13 +21,36 @@ Javascript SDK for CloudEvents ``` -* `index.js`: exports the Cloudevent class +* `index.js`: library exports -* `lib/cloudevent.js`: implementation of Cloudevent class +* `lib/cloudevent.js`: implementation of Cloudevent, an interface -* `lib/jsonformatter.js`: implementation for JSON formatting [json-format](https://github.com/cloudevents/spec/blob/v0.1/json-format.md) +* `lib/format/json_0_1.js`: implementation for JSON formatting [version 0.1](https://github.com/cloudevents/spec/blob/v0.1/json-format.md) -* `lib/spec_0_1.js`: implementation for spec version 0.1 +* `lib/spec_0_1.js`: implementation for spec [version 0.1](https://github.com/cloudevents/spec/blob/v0.1/spec.md) -* `lib/spec_0_2.js`: implementation for spec version 0.2 +* `lib/spec_0_2.js`: implementation for spec [version 0.2](https://github.com/cloudevents/spec/blob/master/spec.md) +* `test/cloudevent_spec_0_1.js`: unit testing for spec 0.1 + +* `test/cloudevent_spec_0_2.js`: unit testing for spec 0.2 + +# How to use + +```js + +/* + * Constructs a default instance with: + * - Spec 0.1 + * - JSON Format 0.1 + */ +var cloudevent01 = new Cloudevent(); + +/* + * Constructs an instance with: + * - Spec 0.2 + * - JSON Format 0.1 + */ +var cloudevent02 = new Cloudevent(Cloudevent.specs['0.2']); + +``` From b9211cbc7269f110b5eedad0d322ac9d6ca8be5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Fri, 9 Nov 2018 14:20:14 -0200 Subject: [PATCH 08/22] Docs about format dir --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index e2f0720..7731ab7 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,8 @@ Javascript SDK for CloudEvents * `lib/cloudevent.js`: implementation of Cloudevent, an interface +* `lib/format`: every format implementation goes here + * `lib/format/json_0_1.js`: implementation for JSON formatting [version 0.1](https://github.com/cloudevents/spec/blob/v0.1/json-format.md) * `lib/spec_0_1.js`: implementation for spec [version 0.1](https://github.com/cloudevents/spec/blob/v0.1/spec.md) From 1729405735ae693625dddfe3e51fdbd918695b0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Fri, 9 Nov 2018 14:21:31 -0200 Subject: [PATCH 09/22] Refactoring for specs directory --- lib/cloudevent.js | 4 ++-- lib/{ => specs}/spec_0_1.js | 0 lib/{ => specs}/spec_0_2.js | 0 3 files changed, 2 insertions(+), 2 deletions(-) rename lib/{ => specs}/spec_0_1.js (100%) rename lib/{ => specs}/spec_0_2.js (100%) diff --git a/lib/cloudevent.js b/lib/cloudevent.js index 2419b2a..996366b 100644 --- a/lib/cloudevent.js +++ b/lib/cloudevent.js @@ -1,5 +1,5 @@ -var Spec_0_1 = require('./spec_0_1.js'); -var Spec_0_2 = require('./spec_0_2.js'); +var Spec_0_1 = require('./specs/spec_0_1.js'); +var Spec_0_2 = require('./specs/spec_0_2.js'); var JSONFormatter_0_1 = require('./format/json_0_1.js'); function Cloudevent(_spec, _formatter){ diff --git a/lib/spec_0_1.js b/lib/specs/spec_0_1.js similarity index 100% rename from lib/spec_0_1.js rename to lib/specs/spec_0_1.js diff --git a/lib/spec_0_2.js b/lib/specs/spec_0_2.js similarity index 100% rename from lib/spec_0_2.js rename to lib/specs/spec_0_2.js From f5fe4633a61c4828603fb4dec58f64d579681ac7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Fri, 9 Nov 2018 14:22:05 -0200 Subject: [PATCH 10/22] Refactoring for formats directory --- lib/cloudevent.js | 2 +- lib/{format => formats}/json_0_1.js | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename lib/{format => formats}/json_0_1.js (100%) diff --git a/lib/cloudevent.js b/lib/cloudevent.js index 996366b..2d5bb7a 100644 --- a/lib/cloudevent.js +++ b/lib/cloudevent.js @@ -1,6 +1,6 @@ var Spec_0_1 = require('./specs/spec_0_1.js'); var Spec_0_2 = require('./specs/spec_0_2.js'); -var JSONFormatter_0_1 = require('./format/json_0_1.js'); +var JSONFormatter_0_1 = require('./formats/json_0_1.js'); function Cloudevent(_spec, _formatter){ this.spec = (_spec) ? new _spec(Cloudevent) : new Spec_0_1(Cloudevent); diff --git a/lib/format/json_0_1.js b/lib/formats/json_0_1.js similarity index 100% rename from lib/format/json_0_1.js rename to lib/formats/json_0_1.js From 17b582d6826abe233415ce8c4d81f50a905a6276 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Fri, 9 Nov 2018 14:23:44 -0200 Subject: [PATCH 11/22] Documentation --- README.md | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 7731ab7..acaa605 100644 --- a/README.md +++ b/README.md @@ -10,8 +10,9 @@ Javascript SDK for CloudEvents │   ├── jsonformatter.js │   ├── format │   │   └── json_0_1.js -│   ├── spec_0_1.js -│   └── spec_0_2.js +│   └── specs +│   ├── spec_0_1.js +│   └── spec_0_2.js ├── LICENSE ├── package.json ├── README.md @@ -25,13 +26,15 @@ Javascript SDK for CloudEvents * `lib/cloudevent.js`: implementation of Cloudevent, an interface -* `lib/format`: every format implementation goes here +* `lib/format/`: every format implementation goes here * `lib/format/json_0_1.js`: implementation for JSON formatting [version 0.1](https://github.com/cloudevents/spec/blob/v0.1/json-format.md) -* `lib/spec_0_1.js`: implementation for spec [version 0.1](https://github.com/cloudevents/spec/blob/v0.1/spec.md) +* `lib/specs/`: every spec implementation goes here -* `lib/spec_0_2.js`: implementation for spec [version 0.2](https://github.com/cloudevents/spec/blob/master/spec.md) +* `lib/specs/spec_0_1.js`: implementation for spec [version 0.1](https://github.com/cloudevents/spec/blob/v0.1/spec.md) + +* `lib/specs/spec_0_2.js`: implementation for spec [version 0.2](https://github.com/cloudevents/spec/blob/master/spec.md) * `test/cloudevent_spec_0_1.js`: unit testing for spec 0.1 From ad3902efafec3773c491ef53666d252ed6cf539f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Fri, 9 Nov 2018 14:29:18 -0200 Subject: [PATCH 12/22] Docs in code and export the format --- lib/cloudevent.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/cloudevent.js b/lib/cloudevent.js index 2d5bb7a..973049c 100644 --- a/lib/cloudevent.js +++ b/lib/cloudevent.js @@ -2,11 +2,19 @@ var Spec_0_1 = require('./specs/spec_0_1.js'); var Spec_0_2 = require('./specs/spec_0_2.js'); var JSONFormatter_0_1 = require('./formats/json_0_1.js'); +/* + * Class created using the Builder Design Pattern. + * + * https://en.wikipedia.org/wiki/Builder_pattern + */ function Cloudevent(_spec, _formatter){ this.spec = (_spec) ? new _spec(Cloudevent) : new Spec_0_1(Cloudevent); this.formatter = (_formatter) ? _formatter : new JSONFormatter_0_1(); } +/* + * To format the payload using the formatter + */ Cloudevent.prototype.format = function(){ return this.formatter.format(this.spec.payload); } @@ -25,10 +33,21 @@ Cloudevent.prototype.source = function(_source){ return this; } +/* + * Export the specs + */ Cloudevent.specs = { '0.1': Spec_0_1, '0.2': Spec_0_2 }; +/* + * Export the formats + */ +Cloudevent.formats = { + 'json' : JSONFormatter_0_1, + 'json0.1': JSONFormatter_0_1 +}; + module.exports = Cloudevent; From f58b8baa3b68a119bc63e844aeb2c99cdfcd97f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Fri, 9 Nov 2018 14:43:07 -0200 Subject: [PATCH 13/22] Documentation about backward compatibility --- README.md | 22 ++++++++++++++++++++++ test/cloudevent_spec_0_1.js | 7 +++++++ 2 files changed, 29 insertions(+) diff --git a/README.md b/README.md index acaa605..18da81a 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,20 @@ Javascript SDK for CloudEvents */ var cloudevent01 = new Cloudevent(); +/* + * Implemented using [Builder Design Pattern](https://en.wikipedia.org/wiki/Builder_pattern) + */ +cloudevent01 + .type("com.github.pull.create") + .source("urn:event:from:myapi/resourse/123"); + +/* + * Backward compatibility by injecting methods from spec implementation to `Cloudevent` + * See how [here](lib/specs/spec_0_1.js#L17) + */ +cloudevent01 + .eventTypeVersion("1.0"); + /* * Constructs an instance with: * - Spec 0.2 @@ -58,4 +72,12 @@ var cloudevent01 = new Cloudevent(); */ var cloudevent02 = new Cloudevent(Cloudevent.specs['0.2']); +/* + * Different specs, but the same API. + */ +cloudevent02 + .type("com.github.pull.create") + .source("urn:event:from:myapi/resourse/123"); + + ``` diff --git a/test/cloudevent_spec_0_1.js b/test/cloudevent_spec_0_1.js index b42d71d..af391bf 100644 --- a/test/cloudevent_spec_0_1.js +++ b/test/cloudevent_spec_0_1.js @@ -27,6 +27,13 @@ describe("CloudEvents Spec 0.1 - JavaScript SDK", () => { }); }); + describe("Backward compatibility", () => { + it("should have 'eventTypeVersion'", () => { + cloudevent.eventTypeVersion("1.0"); + expect(cloudevent.format()).to.have.property('eventTypeVersion'); + }); + }); + }); }); From 55c4f54b215640568889c15a852eafcc7adefd7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Fri, 9 Nov 2018 14:46:28 -0200 Subject: [PATCH 14/22] Documentation about backward compatibility --- README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 18da81a..523a6f9 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ Javascript SDK for CloudEvents var cloudevent01 = new Cloudevent(); /* - * Implemented using [Builder Design Pattern](https://en.wikipedia.org/wiki/Builder_pattern) + * Implemented using Builder Design Pattern */ cloudevent01 .type("com.github.pull.create") @@ -60,7 +60,6 @@ cloudevent01 /* * Backward compatibility by injecting methods from spec implementation to `Cloudevent` - * See how [here](lib/specs/spec_0_1.js#L17) */ cloudevent01 .eventTypeVersion("1.0"); @@ -81,3 +80,7 @@ cloudevent02 ``` +> See how to implement the method injection [here](lib/specs/spec_0_1.js#L17) +> +> [Builder Design Pattern](https://en.wikipedia.org/wiki/Builder_pattern) +> From 2bac01939ba27de4f787a2496cdc7b9ab2da99c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Fri, 9 Nov 2018 15:03:13 -0200 Subject: [PATCH 15/22] Documentation --- README.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 523a6f9..9575c43 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ cloudevent01 .source("urn:event:from:myapi/resourse/123"); /* - * Backward compatibility by injecting methods from spec implementation to `Cloudevent` + * Backward compatibility by injecting methods from spec implementation to Cloudevent */ cloudevent01 .eventTypeVersion("1.0"); @@ -78,9 +78,8 @@ cloudevent02 .type("com.github.pull.create") .source("urn:event:from:myapi/resourse/123"); - ``` + > See how to implement the method injection [here](lib/specs/spec_0_1.js#L17) > -> [Builder Design Pattern](https://en.wikipedia.org/wiki/Builder_pattern) -> +> Learn about [Builder Design Pattern](https://en.wikipedia.org/wiki/Builder_pattern) From c53781489c07baea5e1bc228171d17b6e8f4beeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Fri, 9 Nov 2018 15:25:54 -0200 Subject: [PATCH 16/22] API Documentation --- README.md | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9575c43..818d1ed 100644 --- a/README.md +++ b/README.md @@ -40,10 +40,57 @@ Javascript SDK for CloudEvents * `test/cloudevent_spec_0_2.js`: unit testing for spec 0.2 -# How to use +# The API + +## `Cloudevent` class ```js +/* + * Format the payload and return it. + */ +Cloudevent.format() + +/* + * Format the payload as String. + */ +Cloudevent.toString() + +``` + +## `Formatter` class + +```js + +/* + * Format the Cloudevent payload argument and return an Object. + */ +Object Formatter.format(payload) + +/* + * Format the Cloudevent payload as String. + */ +String Formatter.toString(payload) + +``` + +# How to use + +The `Cloudevent` constructor arguments. + +```js + +/* + * spec : if is null, set the spec 0.1 impl + * format: if is null, set the JSON Format 0.1 impl + */ +Cloudevent(spec, format); + +``` + +## How to construct instances? + +```js /* * Constructs a default instance with: * - Spec 0.1 @@ -78,6 +125,22 @@ cloudevent02 .type("com.github.pull.create") .source("urn:event:from:myapi/resourse/123"); +var cloudevent = new Cloudevent + +``` + +## How to get the formatted payload? + +```js +var cloudevent = new Cloudevent() + .type("com.github.pull.create") + .source("urn:event:from:myapi/resourse/123"); + +/* + * Format the payload and return it. + */ +var formatted = cloudevent.format(); + ``` > See how to implement the method injection [here](lib/specs/spec_0_1.js#L17) From 224993b8245f736e3e1e65e00e6f9bc48b6e4ae6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Fri, 9 Nov 2018 15:27:00 -0200 Subject: [PATCH 17/22] API Documentation --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 818d1ed..654c4a3 100644 --- a/README.md +++ b/README.md @@ -47,14 +47,14 @@ Javascript SDK for CloudEvents ```js /* - * Format the payload and return it. + * Format the payload and return an Object. */ -Cloudevent.format() +Object Cloudevent.format() /* * Format the payload as String. */ -Cloudevent.toString() +String Cloudevent.toString() ``` From 13afaf0f63b08e6a7c5d786dfcc932096c2f98f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Fri, 9 Nov 2018 15:29:18 -0200 Subject: [PATCH 18/22] Document the unit test --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 654c4a3..d8e43cd 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # sdk-javascript Javascript SDK for CloudEvents +> This is a WPI + # Repository Structure ```text @@ -40,6 +42,16 @@ Javascript SDK for CloudEvents * `test/cloudevent_spec_0_2.js`: unit testing for spec 0.2 +# Unit Testing + +The unit test checks the result of formatted payload and the constraints. + +```bash + +npm test + +``` + # The API ## `Cloudevent` class From 288bc640210bf4e678bb181d5ff025a82e79625d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Fri, 9 Nov 2018 15:30:38 -0200 Subject: [PATCH 19/22] Document the unit test --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index d8e43cd..18b318a 100644 --- a/README.md +++ b/README.md @@ -137,8 +137,6 @@ cloudevent02 .type("com.github.pull.create") .source("urn:event:from:myapi/resourse/123"); -var cloudevent = new Cloudevent - ``` ## How to get the formatted payload? From 32db98ced104d2a46d161ce9841669b48fa715e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Fri, 9 Nov 2018 15:31:26 -0200 Subject: [PATCH 20/22] Fix WPI --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 18b318a..0431451 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # sdk-javascript Javascript SDK for CloudEvents -> This is a WPI +> This is a WIP # Repository Structure From 1f2841d5bd449263dd7eaf9b400ddcff163413ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Fri, 9 Nov 2018 15:51:44 -0200 Subject: [PATCH 21/22] The check api --- README.md | 17 ++++++++++++++++- lib/specs/spec_0_1.js | 9 +++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0431451..a4656ce 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,9 @@ String Cloudevent.toString() ``` -## `Formatter` class +## `Formatter` classes + +Every formatter class must implement these methods to work properly. ```js @@ -86,6 +88,19 @@ String Formatter.toString(payload) ``` +## `Spec` classes + +Every Spec class must implement these methods to work properly. + +```js + +/* + * Check the spec constraints, throwing an error if do not pass. + */ +Spec.check() + +``` + # How to use The `Cloudevent` constructor arguments. diff --git a/lib/specs/spec_0_1.js b/lib/specs/spec_0_1.js index 3b4e657..3d87770 100644 --- a/lib/specs/spec_0_1.js +++ b/lib/specs/spec_0_1.js @@ -19,6 +19,15 @@ function Spec_0_1(_caller){ } } +/* + * Check the constraints. + * + * throw an error if do not pass. + */ +Spec_0_1.prototype.check = function() { + +} + Spec_0_1.prototype.type = function(_type){ this.payload['eventType'] = _type; return this; From 7d6a1e51f934a01eff5a2f415835467b5e82f8d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Fri, 9 Nov 2018 15:55:05 -0200 Subject: [PATCH 22/22] call for check MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- lib/cloudevent.js | 4 ++++ lib/specs/spec_0_2.js | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/lib/cloudevent.js b/lib/cloudevent.js index 973049c..d7214d2 100644 --- a/lib/cloudevent.js +++ b/lib/cloudevent.js @@ -16,6 +16,10 @@ function Cloudevent(_spec, _formatter){ * To format the payload using the formatter */ Cloudevent.prototype.format = function(){ + // Check the constraints + this.spec.check(); + + // Then, format return this.formatter.format(this.spec.payload); } diff --git a/lib/specs/spec_0_2.js b/lib/specs/spec_0_2.js index de8fed8..01b53a8 100644 --- a/lib/specs/spec_0_2.js +++ b/lib/specs/spec_0_2.js @@ -7,6 +7,13 @@ function Spec_0_2(){ }; } +/* + * Check the spec constraints. + */ +Spec_0_2.prototype.check = function(){ + +} + Spec_0_2.prototype.type = function(_type){ this.payload['type'] = _type; return this;