JavaScript/TypeScript SDK for CloudEvents
Go to file
Michele Angioni 41710130f1 Added missing 'source' getter on Spec02
Signed-off-by: Michele Angioni <michele.angioni@takeaway.com>
2019-03-06 16:39:16 +01:00
ext External resources: JSON Schema 2019-01-19 23:44:22 -02:00
lib Added missing 'source' getter on Spec02 2019-03-06 16:39:16 +01:00
test Added missing 'source' getter on Spec02 2019-03-06 16:39:16 +01:00
.gitignore Ignore .swp vim files 2019-01-19 23:45:27 -02:00
.travis.yml Run converage 2018-12-16 11:00:44 -02:00
LICENSE Initial commit 2018-09-20 15:54:57 -04:00
README.md Coments and formatation 2019-01-18 23:05:41 -02:00
index.js double quotes 2018-12-15 11:19:48 -02:00
package.json Employ JSON Schema to validate payloads 2019-01-19 23:45:58 -02:00

README.md

Codacy Badge Codacy Badge Build Status

sdk-javascript

Official CloudEvents' SDK for JavaScript.

CloudEvents logo

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 - Structured yes yes
HTTP Transport Binding - Binary yes yes
JSON Event Format yes yes

How to use

The Cloudevent constructor arguments.


/*
 * spec  : if is null, set the spec 0.1 impl
 * format: if is null, set the JSON Format 0.1 impl
 */
Cloudevent(spec, format);

Usage

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 to spec 0.1 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");

Formatting

var Cloudevent = require("cloudevents-sdk");

/*
 * Creates an instance with default spec and format
 */
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();

Emitting

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://myserver.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

├── index.js
├── lib
│   ├── bindings
│   │   └── http
│   ├── cloudevent.js
│   ├── format
│   └── specs
├── LICENSE
├── package.json
├── README.md
  • index.js: library exports
  • lib/bindings: every binding implementation goes here
  • lib/bindings/http: every http binding implementation goes here
  • lib/cloudevent.js: implementation of Cloudevent, an interface
  • lib/format/: every format implementation goes here
  • lib/specs/: every spec implementation goes here

Unit Testing

The unit test checks the result of formatted payload and the constraints.


npm test

The API

Cloudevent class


/*
 * 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.


/*
 * 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.


/*
 * The constructor must receives the Cloudevent type.
 */
Spec(Cloudevent)

/*
 * Checks the spec constraints, throwing an error if do not pass.
 */
Spec.check()

Binding classes

Every Binding class must implement these methods to work properly.


/*
 * The constructor must receives the map of configurations.
 */
Binding(config)

/*
 * Emits the event using an instance of Cloudevent.
 */
Binding.emit(cloudevent)

See how to implement the method injection here

Learn about Builder Design Pattern

Check out the produced event payload using this tool