Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
edc9a98
Repository init
fabiojose Nov 8, 2018
4a97553
First impl.
fabiojose Nov 9, 2018
2487db0
Documentation
fabiojose Nov 9, 2018
c278808
Documentation
fabiojose Nov 9, 2018
185f0c4
Refactoring
fabiojose Nov 9, 2018
a89494c
Refactoring
fabiojose Nov 9, 2018
16da64c
Versioning the JSON Formatter
fabiojose Nov 9, 2018
2b28def
Docs: how to use
fabiojose Nov 9, 2018
b9211cb
Docs about format dir
fabiojose Nov 9, 2018
1729405
Refactoring for specs directory
fabiojose Nov 9, 2018
f5fe463
Refactoring for formats directory
fabiojose Nov 9, 2018
17b582d
Documentation
fabiojose Nov 9, 2018
ad3902e
Docs in code and export the format
fabiojose Nov 9, 2018
f58b8ba
Documentation about backward compatibility
fabiojose Nov 9, 2018
55c4f54
Documentation about backward compatibility
fabiojose Nov 9, 2018
2bac019
Documentation
fabiojose Nov 9, 2018
c537814
API Documentation
fabiojose Nov 9, 2018
224993b
API Documentation
fabiojose Nov 9, 2018
13afaf0
Document the unit test
fabiojose Nov 9, 2018
288bc64
Document the unit test
fabiojose Nov 9, 2018
32db98c
Fix WPI
fabiojose Nov 9, 2018
1f2841d
The check api
fabiojose Nov 9, 2018
7d6a1e5
call for check
fabiojose Nov 9, 2018
10fef47
First impl.
fabiojose Nov 9, 2018
1eddb69
Documentation
fabiojose Nov 9, 2018
b6dcda7
Documentation
fabiojose Nov 9, 2018
956e7fb
Refactoring
fabiojose Nov 9, 2018
02c9aa3
Refactoring
fabiojose Nov 9, 2018
b79703f
Versioning the JSON Formatter
fabiojose Nov 9, 2018
8e2b7e1
Docs: how to use
fabiojose Nov 9, 2018
f7565c9
Docs about format dir
fabiojose Nov 9, 2018
301f201
Refactoring for specs directory
fabiojose Nov 9, 2018
d43685e
Refactoring for formats directory
fabiojose Nov 9, 2018
104b74a
Documentation
fabiojose Nov 9, 2018
2ba9916
Docs in code and export the format
fabiojose Nov 9, 2018
745e684
Documentation about backward compatibility
fabiojose Nov 9, 2018
8748ff0
Documentation about backward compatibility
fabiojose Nov 9, 2018
ae70d8d
Documentation
fabiojose Nov 9, 2018
ec376f5
API Documentation
fabiojose Nov 9, 2018
218684b
API Documentation
fabiojose Nov 9, 2018
de8528c
Document the unit test
fabiojose Nov 9, 2018
3c70752
Document the unit test
fabiojose Nov 9, 2018
eef6f60
Fix WPI
fabiojose Nov 9, 2018
545441e
The check api
fabiojose Nov 9, 2018
f14b7af
call for check
fabiojose Nov 9, 2018
60a0a5a
Merge branch 'patch-01' of https://github.com/fabiojose/sdk-javascrip…
fabiojose Nov 9, 2018
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
77 changes: 77 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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/

171 changes: 171 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,173 @@
# sdk-javascript
Javascript SDK for CloudEvents

> This is a WIP

# Repository Structure

```text
├── index.js
├── lib
│   ├── cloudevent.js
│   ├── jsonformatter.js
│   ├── format
│   │   └── json_0_1.js
│   └── specs
│   ├── 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`: library exports

* `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/specs/`: every spec implementation goes here

* `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

* `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

```js

/*
* Format the payload and return an Object.
*/
Object Cloudevent.format()

/*
* Format the payload as String.
*/
String Cloudevent.toString()

```

## `Formatter` classes

Every formatter class must implement these methods to work properly.

```js

/*
* Format the Cloudevent payload argument and return an Object.
*/
Object Formatter.format(payload)

/*
* Format the Cloudevent payload as String.
*/
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.

```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
* - JSON Format 0.1
*/
var cloudevent01 = new Cloudevent();

/*
* Implemented using Builder Design Pattern
*/
cloudevent01
.type("com.github.pull.create")
.source("urn:event:from:myapi/resourse/123");

/*
* Backward compatibility by injecting methods from spec implementation to Cloudevent
*/
cloudevent01
.eventTypeVersion("1.0");

/*
* Constructs an instance with:
* - Spec 0.2
* - JSON Format 0.1
*/
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");

```

## 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)
>
> Learn about [Builder Design Pattern](https://en.wikipedia.org/wiki/Builder_pattern)
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -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;

57 changes: 57 additions & 0 deletions lib/cloudevent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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(){
// 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.source = function(_source){
this.spec.source(_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;

14 changes: 14 additions & 0 deletions lib/formats/json_0_1.js
Original file line number Diff line number Diff line change
@@ -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;
52 changes: 52 additions & 0 deletions lib/specs/spec_0_1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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);
}
}

/*
* 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;
}

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;

Loading