First impl.
Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
parent
edc9a98b38
commit
10fef4708a
|
|
@ -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;
|
||||
|
||||
|
|
@ -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;
|
||||
|
||||
|
|
@ -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;
|
||||
|
|
@ -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;
|
||||
|
||||
|
|
@ -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;
|
||||
|
||||
17
package.json
17
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"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
|
@ -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');
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
Loading…
Reference in New Issue