Merge pull request #3 from cloudevents/spec0.2

Specification 0.2 support
This commit is contained in:
Fabio José 2018-12-09 00:10:05 -02:00 committed by GitHub
commit 561c339413
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 387 additions and 118 deletions

29
.travis.yml Normal file
View File

@ -0,0 +1,29 @@
language: node_js
node_js:
- 8
- 7
- 6
cache: npm
jobs:
include:
- stage: github release
node_js: "6"
deploy:
provider: releases
api_key:
secure: "ls/IjZdRpFZlEQsZrsmXn7h7QTZuM8x4gZq3r75sOyTWcmKgZLN27hYCtk8SPcuNbt+ZA3otQEQJvDuGTCAwtbU4lxQjfXswP9M/om01hf3J7uTcjtol4XOsmBFqMCktW94pVH4U7Q18IHeP5JBnlfLzAY/YifRKKNWNrsI6bbnfcGgsFJYvICwKL5LEj7bxtJk156lBBJm9TscMX48BFsUBvnCNTDEBYNaaGkoCz6DPM0Da7QPF5exHcNiiF9SoR60WeGI1q+MdZyE+1AG0VpUp7sMxJafXSBbgYquPEZmG9ScZbkVyWP7ps3sgkqDkxtH4kNe9zF4PySRYfxYKJ03ECwbSmj9gxsIc8o2zhrLFinmHydLxoOEh5EygB2euekgMhoEKL0kA8bteaaqmD+3sna8MbXG4HhvIrEAspNtOFZyUm/SmgvXXxaKiDtwTw/5CvPCc7VH51NuC5QZi0UehLzatDpDNsj/ffMwDp5F4SBIZgUUmmIWfnAODNGwSwBKxdPVS3etkTeiBxerw2uzC6qjNW1QJyb9s+iU3rC59IEAuq6u4ymv3caDIHLnJbCMHRHQoIbq86oErqb8DhH6KrTlBnwlGBpUVhdpYMt0QX2Uncu9uHMBZ2CQNZ819PJa4QWvc1Ixoj3auKqvoMfPNQFOskMWnrnxkOokyPZg="
skip_cleanup: true
on:
tags: true
- stage: npm release
node_js: "6"
deploy:
provider: npm
email: fabiojose@gmail.com
api_key:
secure: "o5aF53FTPUSiJBbCZ/anBWQCgEJCctpxuTgGJbO1NpiheOM/xENiSmv+n2a5sGrhhqT0h7k15mE/ZgtL8TnM+45AHOg3EGez5JoR1XMIXnSeCno1GFK4waHDcEn3eLW9P35r1S5/RMTqbEUvqpyK/fzVZ4ecyh9t7dVvJV8MyQGo+r+oO3SLYEIt7YC6vZRh+dV3cK8jr7MHkHmQfZ6tZrALMKsj6QNTUtmk+IU52WHi4oe1iPuypS5dlaVdmanX7ZRtC7gR0Dko2/wja+DPOAKgG/S0aS794cxal7P5k/K34mvqT8iaCl4vN5uIcRsipgsprIahk2G2NgnIVCetBda9LhpMNUdn3j+v4T0lx3jiqP1eq01nk8YFpTV4Xz9VlYHK/E6NYQhUmd7N6WO9vXmOEDGBvWrDfQ1QMx+/TM9r3vzK9ps3sjkDFaAtJ2ZQ0pvFMEQTaLKak24ntWltSWZKvxdjYnS+bcfyIQQGagvqgZsnkKzeudO9N9Atp4OGcst9CAvvykDsfmLlARAiyvpuDVyUivuaOlCB9J7VBt1sbBfsiHpnJcSsMVz1OMRX5EGewbla530guoePhjTUDVit3NyUMz3ZQQTN9VSK3tA+NLyR0Ex8Oel+byHJDYyf+36GCDvoXagaPUVk5M5BINiCw2IWhxDgRGrcEp9JIUI="
skip_cleanup: true
on:
tags: true

240
README.md
View File

@ -1,9 +1,133 @@
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/bd66e7c52002481993cd6d610534b0f7)](https://www.codacy.com/app/fabiojose/sdk-javascript?utm_source=github.com&utm_medium=referral&utm_content=cloudevents/sdk-javascript&utm_campaign=Badge_Grade)
[![Build Status](https://travis-ci.org/cloudevents/sdk-javascript.svg?branch=master)](https://travis-ci.org/cloudevents/sdk-javascript)
# sdk-javascript
Javascript SDK for CloudEvents
> This is a WIP
# Repository Structure
## Installation
This CloudEvents SDK requires nodejs 6.11+
### Nodejs
```
npm install cloudevents-sdk
```
## Specification Support
These are the supported specifications by this version.
| **Specifications** | **v0.1** | **v0.2** |
|----------------------------|----------|----------|
| CloudEvents | yes | yes |
| HTTP Transport Binding | yes | yes |
| JSON Event Format | yes | yes |
## 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
var Cloudevent = require("cloudevents-sdk");
/*
* 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 = require("cloudevents-sdk");
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();
```
### How to emit an event?
```js
var Cloudevent = require("cloudevents-sdk");
// The event
var cloudevent = new Cloudevent()
.type("com.github.pull.create")
.source("urn:event:from:myapi/resourse/123");
// The binding configuration using POST
var config = {
method: 'POST',
url : 'https://mywebhook.com'
};
// The binding instance
var binding = Cloudevent.bindings['http-structured0.1'](config);
// Emit the event using Promise
binding.emit(cloudevent)
.then(response => {
// Treat the response
console.log(response.data);
}).catch(err => {
// Treat the error
console.error(err);
});
```
## Repository Structure
```text
├── index.js
@ -44,13 +168,13 @@ Javascript SDK for CloudEvents
* `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)
* `lib/specs/spec_0_2.js`: implementation for spec [version 0.2](https://github.com/cloudevents/spec/blob/v0.2/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
## Unit Testing
The unit test checks the result of formatted payload and the constraints.
@ -60,9 +184,9 @@ npm test
```
# The API
## The API
## `Cloudevent` class
### `Cloudevent` class
```js
@ -78,7 +202,7 @@ String Cloudevent.toString()
```
## `Formatter` classes
### `Formatter` classes
Every formatter class must implement these methods to work properly.
@ -113,122 +237,26 @@ Spec(Cloudevent)
Spec.check()
```
## `Binding` classes
### `Binding` classes
Every Binding class must implement these methods to work properly.
```js
/*
/*
* The constructor must receives the map of configurations.
*/
Binding(config)
/*
/*
* Emits the event using an instance of Cloudevent.
*/
Binding.emit(cloudevent)
```
# 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();
```
## How to emit an event?
```js
// The event
var cloudevent = new Cloudevent()
.type("com.github.pull.create")
.source("urn:event:from:myapi/resourse/123");
// The binding configuration using POST
var config = {
method: 'POST',
url : 'https://mywebhook.com'
};
// The binding instance
var binding = Cloudevent.bindings['http-structured0.1'](config);
// Emit the event using Promise
binding.emit(cloudevent)
.then(response => {
// Treat the response
console.log(response.data);
}).catch(err => {
// Treat the error
console.error(err);
});
```
> 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)
>
>
> Check out the produced event payload using this [tool](https://webhook.site)

View File

@ -48,6 +48,26 @@ Cloudevent.prototype.time = function(_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.data = function(_data) {
this.spec.data(_data);
return this;
}
Cloudevent.prototype.addExtension = function(key, value){
this.spec.addExtension(key, value);
return this;
}
/*
* Export the specs
*/

View File

@ -57,7 +57,28 @@ Spec_0_1.prototype.time = function(_time){
return this;
}
//TODO another attributes . . .
Spec_0_1.prototype.schemaurl = function(_schemaurl){
this.payload['schemaURL'] = _schemaurl;
return this;
}
Spec_0_1.prototype.contenttype = function(_contenttype){
this.payload['contentType'] = _contenttype;
return this;
}
Spec_0_1.prototype.data = function(_data){
this.payload['data'] = _data;
return this;
}
Spec_0_1.prototype.addExtension = function(key, value){
if(!this.payload['extensions']){
this.payload['extensions'] = {};
}
this.payload['extensions'][key] = value;
return this;
}
module.exports = Spec_0_1;

View File

@ -1,4 +1,5 @@
var uuid = require('uuid/v4');
var uuid = require('uuid/v4');
var empty = require('is-empty')
function Spec_0_2(){
this.payload = {
@ -12,6 +13,22 @@ function Spec_0_2(){
*/
Spec_0_2.prototype.check = function(){
if(empty(this.payload['type'])) {
throw {message: "'type' is invalid"};
}
if(empty(this.payload['specversion'])) {
throw {message: "'specversion' is invalid"};
}
if(this.payload['specversion'] !== '0.2') {
throw {message: "'specversion' value is invalid: '"
+ this.payload['specversion'] + "'"};
}
if(empty(this.payload['id'])) {
throw {message: "'id' is invalid"};
}
}
Spec_0_2.prototype.type = function(_type){
@ -34,7 +51,25 @@ Spec_0_2.prototype.time = function(_time){
return this;
}
//TODO another attributes . . .
Spec_0_2.prototype.schemaurl = function(_schemaurl){
this.payload['schemaurl'] = _schemaurl;
return this;
}
Spec_0_2.prototype.contenttype = function(_contenttype){
this.payload['contenttype'] = _contenttype;
return this;
}
Spec_0_2.prototype.data = function(_data){
this.payload['data'] = _data;
return this;
}
Spec_0_2.prototype.addExtension = function(key, value){
this.payload[key] = value;
return this;
}
module.exports = Spec_0_2;

View File

@ -1,6 +1,6 @@
{
"name": "cloudevents",
"version": "0.0.1",
"version": "0.1.0",
"description": "CloudEvents SDK for JavaScript",
"main": "index.js",
"scripts": {
@ -13,7 +13,9 @@
"keywords": [
"events",
"cloudevents",
"sdk"
"sdk",
"javascript",
"cncf"
],
"author": "cloudevents.io",
"contributors": {
@ -28,6 +30,8 @@
"homepage": "https://github.com/cloudevents/sdk-javascript#readme",
"dependencies": {
"axios": "0.18.0",
"is-empty": "1.2.0",
"uri-js": "4.2.2",
"uuid": "3.3.2"
},
"devDependencies": {
@ -35,5 +39,8 @@
"mocha": "5.2.0",
"chai-http": "4.2.0",
"nock": "10.0.2"
},
"publishConfig": {
"access": "public"
}
}

View File

@ -4,6 +4,10 @@ var Cloudevent = require("../index.js");
const type = "com.github.pull.create";
const source = "urn:event:from:myapi/resourse/123";
const time = new Date();
const schemaurl = "http://example.com/registry/myschema.json";
const contenttype = "application/json";
const data = {};
const extensions = {};
var cloudevent = new Cloudevent()
.type(type)
@ -18,6 +22,11 @@ describe("CloudEvents Spec 0.1 - JavaScript SDK", () => {
expect(cloudevent.format()).to.have.property('eventType');
});
it("requires 'eventTypeVersion'", () => {
cloudevent.eventTypeVersion("1.0");
expect(cloudevent.format()).to.have.property('eventTypeVersion');
});
it("requires 'cloudEventsVersion'", () => {
expect(cloudevent.format()).to.have.property('cloudEventsVersion');
});
@ -31,14 +40,41 @@ describe("CloudEvents Spec 0.1 - JavaScript SDK", () => {
});
});
describe("Backward compatibility", () => {
it("should have 'eventTypeVersion'", () => {
cloudevent.eventTypeVersion("1.0");
expect(cloudevent.format()).to.have.property('eventTypeVersion');
describe("Optional context attributes", () => {
it("contains 'eventTime'", () => {
cloudevent.time(time);
expect(cloudevent.format()).to.have.property('eventTime');
});
it("contains 'schemaURL'", () => {
cloudevent.schemaurl(schemaurl);
expect(cloudevent.format()).to.have.property('schemaURL');
});
it("contains 'contentType'", () => {
cloudevent.contenttype(contenttype);
expect(cloudevent.format()).to.have.property('contentType');
});
it("contains 'data'", () => {
cloudevent.data(data);
expect(cloudevent.format()).to.have.property('data');
});
it("contains 'extensions'", () => {
cloudevent.addExtension('foo', 'value');
expect(cloudevent.format()).to.have.property('extensions');
});
it("'extensions' should have 'bar' extension", () => {
cloudevent.addExtension('bar', 'value');
expect(cloudevent.format().extensions)
.to.have.property('foo');
});
});
describe("The Constraint check", () => {
describe("The Constraints check", () => {
describe("'eventType'", () => {
it("should throw an error when is an empty string", () => {
cloudevent.type("");

View File

@ -1,9 +1,17 @@
var expect = require("chai").expect;
var Cloudevent = require("../index.js");
const type = "com.github.pull.create";
const source = "urn:event:from:myapi/resourse/123";
const time = new Date();
const schemaurl = "http://example.com/registry/myschema.json";
const contenttype = "application/json";
const data = {};
const extensions = {};
var cloudevent = new Cloudevent(Cloudevent.specs['0.2'])
.type("com.github.pull.create")
.source("urn:event:from:myapi/resourse/123");
.type(type)
.source(source);
describe("CloudEvents Spec 0.2 - JavaScript SDK", () => {
@ -27,6 +35,91 @@ describe("CloudEvents Spec 0.2 - JavaScript SDK", () => {
});
});
describe("Optional context attributes", () => {
it("contains 'time'", () => {
cloudevent.time(time);
expect(cloudevent.format()).to.have.property('time');
});
it("contains 'schemaurl'", () => {
cloudevent.schemaurl(schemaurl);
expect(cloudevent.format()).to.have.property('schemaurl');
});
it("contains 'contenttype'", () => {
cloudevent.contenttype(contenttype);
expect(cloudevent.format()).to.have.property('contenttype');
});
it("contains 'data'", () => {
cloudevent.data(data);
expect(cloudevent.format()).to.have.property('data');
});
it("contains 'extension1'", () => {
cloudevent.addExtension("extension1", "value1");
expect(cloudevent.format()).to.have.property('extension1');
});
it("'extension2' should have value equals to 'value1'", () => {
cloudevent.addExtension("extension2", "value2");
expect(cloudevent.format()['extension2']).to.equal('value2');
});
});
describe("The Constraints check", () => {
describe("'type'", () => {
it("should throw an error when is an empty string", () => {
cloudevent.type("");
expect(cloudevent.format.bind(cloudevent))
.to
.throw("'type' is invalid");
});
it("must be a non-empty string", () => {
cloudevent.type(type);
cloudevent.format();
});
it("should be prefixed with a reverse-DNS name", () => {
//TODO how to assert it?
});
});
describe("'specversion'", () => {
it("compliant event producers must use a value of '0.2'", () => {
expect(cloudevent.format()['specversion']).to.equal("0.2");
});
it("should throw an error when is an empty string", () => {
cloudevent.spec.payload.specversion = "";
expect(cloudevent.format.bind(cloudevent))
.to
.throw("'specversion' is invalid");
cloudevent.spec.payload.specversion = "0.2";
});
});
describe("'id'", () => {
it("should throw an error when is an empty string", () => {
cloudevent.id("");
expect(cloudevent.format.bind(cloudevent))
.to
.throw("'id' is invalid");
});
it("must be a non-empty string", () => {
cloudevent.id("my.id-0x0090");
cloudevent.format();
});
});
describe("'time'", () => {
it("must adhere to the format specified in RFC 3339", () => {
cloudevent.time(time);
expect(cloudevent.format()['time']).to.equal(time.toISOString());
});
});
});
});
});