Skip to content

Commit

Permalink
Merge pull request #3 from cloudevents/spec0.2
Browse files Browse the repository at this point in the history
Specification 0.2 support
  • Loading branch information
fabiojose authored Dec 9, 2018
2 parents ff3ae0a + bf28816 commit 561c339
Show file tree
Hide file tree
Showing 8 changed files with 387 additions and 118 deletions.
29 changes: 29 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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: [email protected]
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 changes: 134 additions & 106 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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.

Expand All @@ -60,9 +184,9 @@ npm test

```

# The API
## The API

## `Cloudevent` class
### `Cloudevent` class

```js

Expand All @@ -78,7 +202,7 @@ String Cloudevent.toString()

```

## `Formatter` classes
### `Formatter` classes

Every formatter class must implement these methods to work properly.

Expand Down Expand Up @@ -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)
20 changes: 20 additions & 0 deletions lib/cloudevent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
23 changes: 22 additions & 1 deletion lib/specs/spec_0_1.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Loading

0 comments on commit 561c339

Please sign in to comment.