sdk-javascript/lib/cloudevent.js

112 lines
2.4 KiB
JavaScript

var Spec01 = require("./specs/spec_0_1.js");
var Spec02 = require("./specs/spec_0_2.js");
var JSONFormatter01 = require("./formats/json_0_1.js");
var HTTPStructured01 = require("./bindings/http/structured_0_1.js");
var HTTPBinary01 = require("./bindings/http/binary_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 Spec01(Cloudevent);
this.formatter = (_formatter) ? _formatter : new JSONFormatter01();
}
/*
* 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);
};
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.getSpecversion = function() {
return this.spec.getSpecversion();
};
Cloudevent.prototype.source = function(_source){
this.spec.source(_source);
return this;
};
Cloudevent.prototype.id = function(_id){
this.spec.id(_id);
return this;
};
Cloudevent.prototype.time = function(_time){
this.spec.time(_time);
return this;
};
Cloudevent.prototype.schemaurl = function(_schemaurl) {
this.spec.schemaurl(_schemaurl);
return this;
};
Cloudevent.prototype.contenttype = function(_contenttype){
this.spec.contenttype(_contenttype);
return this;
};
Cloudevent.prototype.getContenttype = function() {
return this.spec.getContenttype();
};
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);
return this;
};
/*
* Export the specs
*/
Cloudevent.specs = {
"0.1": Spec01,
"0.2": Spec02
};
/*
* Export the formats
*/
Cloudevent.formats = {
"json" : JSONFormatter01,
"json0.1": JSONFormatter01
};
Cloudevent.bindings = {
"http-structured" : HTTPStructured01,
"http-structured0.1" : HTTPStructured01,
"http-binary0.1" : HTTPBinary01
};
module.exports = Cloudevent;