chore: use es6 for cloudevents.js (#73)

Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
This commit is contained in:
Grant Timmerman 2020-04-29 13:17:51 -07:00 committed by GitHub
parent d042ef1dbb
commit 12ac181300
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 111 additions and 116 deletions

View File

@ -6,18 +6,20 @@ const Formatter = require("./formats/json/formatter.js");
* *
* https://en.wikipedia.org/wiki/Builder_pattern * https://en.wikipedia.org/wiki/Builder_pattern
*/ */
function Cloudevent(_spec, _formatter) { class CloudEvent {
this.spec = (_spec) ? new _spec(Cloudevent) : new Spec(Cloudevent); constructor(_spec, _formatter) {
this.spec = (_spec) ? new _spec(CloudEvent) : new Spec(CloudEvent);
this.formatter = (_formatter) ? new _formatter() : new Formatter(); this.formatter = (_formatter) ? new _formatter() : new Formatter();
// The map of extensions // The map of extensions
this.extensions = {}; this.extensions = {};
} }
/* getFormats() {
* To format the payload using the formatter return { json: Formatter };
*/ }
Cloudevent.prototype.format = function() {
format() {
// Check the constraints // Check the constraints
this.spec.check(); this.spec.check();
@ -26,102 +28,95 @@ Cloudevent.prototype.format = function() {
// Then, format // Then, format
return this.formatter.format(this.spec.payload); return this.formatter.format(this.spec.payload);
}; }
Cloudevent.prototype.toString = function() { toString() {
return this.formatter.toString(this.spec.payload); return this.formatter.toString(this.spec.payload);
}; }
Cloudevent.prototype.type = function(type) { type(type) {
this.spec.type(type); this.spec.type(type);
return this; return this;
}; }
Cloudevent.prototype.getType = function() { getType() {
return this.spec.getType(); return this.spec.getType();
}; }
Cloudevent.prototype.specversion = function(version) { specversion(version) {
return this.spec.specversion(version); return this.spec.specversion(version);
}; }
Cloudevent.prototype.getSpecversion = function() { getSpecversion() {
return this.spec.getSpecversion(); return this.spec.getSpecversion();
}; }
Cloudevent.prototype.source = function(_source) { source(_source) {
this.spec.source(_source); this.spec.source(_source);
return this; return this;
}; }
Cloudevent.prototype.getSource = function() { getSource() {
return this.spec.getSource(); return this.spec.getSource();
}; }
Cloudevent.prototype.id = function(_id) { id(_id) {
this.spec.id(_id); this.spec.id(_id);
return this; return this;
}; }
Cloudevent.prototype.getId = function() { getId() {
return this.spec.getId(); return this.spec.getId();
}; }
Cloudevent.prototype.time = function(_time) { time(_time) {
this.spec.time(_time); this.spec.time(_time);
return this; return this;
}; }
Cloudevent.prototype.getTime = function() { getTime() {
return this.spec.getTime(); return this.spec.getTime();
}; }
Cloudevent.prototype.schemaurl = function(_schemaurl) { schemaurl(_schemaurl) {
this.spec.schemaurl(_schemaurl); this.spec.schemaurl(_schemaurl);
return this; return this;
}; }
Cloudevent.prototype.getSchemaurl = function() { getSchemaurl() {
return this.spec.getSchemaurl(); return this.spec.getSchemaurl();
}; }
Cloudevent.prototype.dataContenttype = function(_contenttype) { dataContenttype(_contenttype) {
this.spec.dataContenttype(_contenttype); this.spec.dataContenttype(_contenttype);
return this; return this;
}; }
Cloudevent.prototype.getDataContenttype = function() { getDataContenttype() {
return this.spec.getDataContenttype(); return this.spec.getDataContenttype();
}; }
Cloudevent.prototype.data = function(_data) { data(_data) {
this.spec.data(_data); this.spec.data(_data);
return this; return this;
}; }
Cloudevent.prototype.getData = function() { getData() {
return this.spec.getData(); return this.spec.getData();
}; }
Cloudevent.prototype.addExtension = function(key, value) { addExtension(key, value) {
this.spec.addExtension(key, value); this.spec.addExtension(key, value);
// Stores localy // Stores locally
this.extensions[key] = value; this.extensions[key] = value;
return this; return this;
}; }
Cloudevent.prototype.getExtensions = function() { getExtensions() {
return this.extensions; return this.extensions;
}; }
}
/* module.exports = CloudEvent;
* Export the formats
*/
Cloudevent.formats = {
json: Formatter,
"json0.1": Formatter
};
module.exports = Cloudevent;