Skip to content

fix: use es6 for cloudevents.js #73

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 2 commits into from
Apr 29, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
227 changes: 111 additions & 116 deletions lib/cloudevent.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,122 +6,117 @@ const Formatter = require("./formats/json/formatter.js");
*
* https://en.wikipedia.org/wiki/Builder_pattern
*/
function Cloudevent(_spec, _formatter) {
this.spec = (_spec) ? new _spec(Cloudevent) : new Spec(Cloudevent);
this.formatter = (_formatter) ? new _formatter() : new Formatter();
class CloudEvent {
constructor(_spec, _formatter) {
this.spec = (_spec) ? new _spec(CloudEvent) : new Spec(CloudEvent);
this.formatter = (_formatter) ? new _formatter() : new Formatter();

// The map of extensions
this.extensions = {};
}

getFormats() {
return { json: Formatter };
}

format() {
// Check the constraints
this.spec.check();

// To run asData()
this.getData();

// Then, format
return this.formatter.format(this.spec.payload);
}

toString() {
return this.formatter.toString(this.spec.payload);
}

type(type) {
this.spec.type(type);
return this;
}

getType() {
return this.spec.getType();
}

specversion(version) {
return this.spec.specversion(version);
}

getSpecversion() {
return this.spec.getSpecversion();
}

source(_source) {
this.spec.source(_source);
return this;
}

getSource() {
return this.spec.getSource();
}

id(_id) {
this.spec.id(_id);
return this;
}

getId() {
return this.spec.getId();
}

time(_time) {
this.spec.time(_time);
return this;
}

getTime() {
return this.spec.getTime();
}

schemaurl(_schemaurl) {
this.spec.schemaurl(_schemaurl);
return this;
}

getSchemaurl() {
return this.spec.getSchemaurl();
}

dataContenttype(_contenttype) {
this.spec.dataContenttype(_contenttype);
return this;
}

getDataContenttype() {
return this.spec.getDataContenttype();
}

data(_data) {
this.spec.data(_data);
return this;
}

getData() {
return this.spec.getData();
}

addExtension(key, value) {
this.spec.addExtension(key, value);

// Stores locally
this.extensions[key] = value;

// The map of extensions
this.extensions = {};
return this;
}

getExtensions() {
return this.extensions;
}
}

/*
* To format the payload using the formatter
*/
Cloudevent.prototype.format = function() {
// Check the constraints
this.spec.check();

// To run asData()
this.getData();

// Then, format
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.getType = function() {
return this.spec.getType();
};

Cloudevent.prototype.specversion = function(version) {
return this.spec.specversion(version);
};

Cloudevent.prototype.getSpecversion = function() {
return this.spec.getSpecversion();
};

Cloudevent.prototype.source = function(_source) {
this.spec.source(_source);
return this;
};

Cloudevent.prototype.getSource = function() {
return this.spec.getSource();
};

Cloudevent.prototype.id = function(_id) {
this.spec.id(_id);
return this;
};

Cloudevent.prototype.getId = function() {
return this.spec.getId();
};

Cloudevent.prototype.time = function(_time) {
this.spec.time(_time);
return this;
};

Cloudevent.prototype.getTime = function() {
return this.spec.getTime();
};

Cloudevent.prototype.schemaurl = function(_schemaurl) {
this.spec.schemaurl(_schemaurl);
return this;
};

Cloudevent.prototype.getSchemaurl = function() {
return this.spec.getSchemaurl();
};

Cloudevent.prototype.dataContenttype = function(_contenttype) {
this.spec.dataContenttype(_contenttype);
return this;
};

Cloudevent.prototype.getDataContenttype = function() {
return this.spec.getDataContenttype();
};

Cloudevent.prototype.data = function(_data) {
this.spec.data(_data);
return this;
};

Cloudevent.prototype.getData = function() {
return this.spec.getData();
};

Cloudevent.prototype.addExtension = function(key, value) {
this.spec.addExtension(key, value);

// Stores localy
this.extensions[key] = value;

return this;
};

Cloudevent.prototype.getExtensions = function() {
return this.extensions;
};

/*
* Export the formats
*/
Cloudevent.formats = {
json: Formatter,
"json0.1": Formatter
};

module.exports = Cloudevent;
module.exports = CloudEvent;