From edc9a98b38fc75c04dbac3e62db246f9fad8ca0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Thu, 8 Nov 2018 15:54:22 -0200 Subject: [PATCH 01/45] Repository init --- .gitignore | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 29 ++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 .gitignore create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..8c8b2390 --- /dev/null +++ b/.gitignore @@ -0,0 +1,77 @@ +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# TypeScript v1 declaration files +typings/ + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env + +# parcel-bundler cache (https://parceljs.org/) +.cache + +# next.js build output +.next + +# nuxt.js build output +.nuxt + +# vuepress build output +.vuepress/dist + +# Serverless directories +.serverless + +# FuseBox cache +.fusebox/ + diff --git a/package.json b/package.json new file mode 100644 index 00000000..b6827ecf --- /dev/null +++ b/package.json @@ -0,0 +1,29 @@ +{ + "name": "cloudevents", + "version": "0.0.1", + "description": "CloudEvents SDK for JavaScript", + "main": "index.js", + "scripts": { + "test": "mocha test/test.js" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/cloudevents/sdk-javascript.git" + }, + "keywords": [ + "events", + "cloudevents", + "sdk" + ], + "author": "cloudevents.io", + "contributors": { + "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" +} 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 02/45] 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 00000000..baaacb51 --- /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 00000000..318b5ac0 --- /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 00000000..7f0ca46d --- /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 00000000..3b4e657f --- /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 00000000..de8fed89 --- /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 b6827ecf..93c56216 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 00000000..b42d71d6 --- /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 00000000..a94faa1b --- /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 03/45] Documentation --- README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/README.md b/README.md index 5ba39f8c..27c6f756 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 04/45] Documentation --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 27c6f756..a8556029 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 05/45] 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 06/45] 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 07/45] 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 318b5ac0..2419b2a9 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 08/45] Docs: how to use --- README.md | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a8556029..e2f07206 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 09/45] Docs about format dir --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index e2f07206..7731ab7e 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 10/45] 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 2419b2a9..996366b1 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 11/45] 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 996366b1..2d5bb7a0 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 12/45] Documentation --- README.md | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 7731ab7e..acaa6050 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 13/45] 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 2d5bb7a0..973049c0 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 14/45] 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 acaa6050..18da81a7 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 b42d71d6..af391bf3 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 15/45] Documentation about backward compatibility --- README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 18da81a7..523a6f99 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 16/45] Documentation --- README.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 523a6f99..9575c434 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 17/45] API Documentation --- README.md | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/README.md b/README.md index 9575c434..818d1ede 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 +# 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 18/45] API Documentation --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 818d1ede..654c4a3d 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 19/45] Document the unit test --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 654c4a3d..d8e43cde 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 20/45] Document the unit test --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index d8e43cde..18b318a5 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 21/45] Fix WPI --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 18b318a5..0431451c 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 22/45] 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 0431451c..a4656ced 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 3b4e657f..3d87770e 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 23/45] 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 973049c0..d7214d2f 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 de8fed89..01b53a87 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; From 10fef4708abd16a57105420b54acd37a9d1ef291 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 24/45] First impl. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- 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 00000000..baaacb51 --- /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 00000000..318b5ac0 --- /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 00000000..7f0ca46d --- /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 00000000..3b4e657f --- /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 00000000..de8fed89 --- /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 b6827ecf..93c56216 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 00000000..b42d71d6 --- /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 00000000..a94faa1b --- /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 1eddb696eea94f2b637ecf078f7c96d41bbdddf4 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 25/45] Documentation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/README.md b/README.md index 5ba39f8c..27c6f756 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 b6dcda74c68efe7b9c66cd7ba83f747c633ddec4 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 26/45] Documentation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 27c6f756..a8556029 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 956e7fb9d25f6f7d34ad0ec9532417ebc6352bdf 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 27/45] Refactoring MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- 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 02c9aa31991702000f476880b6aa5c811f90c91e 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 28/45] Refactoring MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- 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 b79703fd004fef7f8551ada522da35821ec3e0bc 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 29/45] Versioning the JSON Formatter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- lib/cloudevent.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/cloudevent.js b/lib/cloudevent.js index 318b5ac0..2419b2a9 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 8e2b7e159bc05b15f89af8c660864e629795484e 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 30/45] Docs: how to use MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- README.md | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a8556029..e2f07206 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 f7565c964b753a447a0b4bfe28d4ebc1732f3e10 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 31/45] Docs about format dir MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index e2f07206..7731ab7e 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 301f201e97df878747f2e2f55d972c1dfe79a126 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 32/45] Refactoring for specs directory 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_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 2419b2a9..996366b1 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 d43685e2b4d3613485b1f084fe683464578c040b 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 33/45] Refactoring for formats directory MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- 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 996366b1..2d5bb7a0 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 104b74acf5106d68a52775334a9621c8290c011b 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 34/45] Documentation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- README.md | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 7731ab7e..acaa6050 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 2ba991615334b58b96fae1cd824f22350d7d13a4 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 35/45] Docs in code and export the format MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- lib/cloudevent.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/cloudevent.js b/lib/cloudevent.js index 2d5bb7a0..973049c0 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 745e6845c67321e2a24c6a8c162c11f9db0ab71d 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 36/45] Documentation about backward compatibility MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- README.md | 22 ++++++++++++++++++++++ test/cloudevent_spec_0_1.js | 7 +++++++ 2 files changed, 29 insertions(+) diff --git a/README.md b/README.md index acaa6050..18da81a7 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 b42d71d6..af391bf3 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 8748ff0c8c81dbfd2f9721d51a0fdf3b478ac21a 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 37/45] Documentation about backward compatibility MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 18da81a7..523a6f99 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 ae70d8d4cb4fe21838543746355b880c8f772623 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 38/45] Documentation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- README.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 523a6f99..9575c434 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 ec376f528f7a5a8e8f32aaec0c695afa7f357503 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 39/45] API Documentation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- README.md | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/README.md b/README.md index 9575c434..818d1ede 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 +# 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 218684b19d74f257d8b39dfc55abeda0fe091074 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 40/45] API Documentation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 818d1ede..654c4a3d 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 de8528caace29d5e0c96fa9184b525374f274eee 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 41/45] Document the unit test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 654c4a3d..d8e43cde 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 3c70752b43e1df575189fe30d25c07d2735b9ba4 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 42/45] Document the unit test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index d8e43cde..18b318a5 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 eef6f60a9d35271b26aaafc21ebd9341ce956bf7 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 43/45] Fix WPI MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 18b318a5..0431451c 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 545441e53ea75a1da0bc0193ef786d9c89cf2f05 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 44/45] The check api MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- 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 0431451c..a4656ced 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 3b4e657f..3d87770e 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 f14b7af00c2fd6b7bec6bf217eae489648cba1ba 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 45/45] 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 973049c0..d7214d2f 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 de8fed89..01b53a87 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;